Created
May 25, 2022 06:52
-
-
Save alefoll/f7b4423542e889e9f3b1aa050fb5ad3f to your computer and use it in GitHub Desktop.
Webview Sizing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<link href="./styles.css" rel="stylesheet"> | |
<title>Hello World!</title> | |
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'"> | |
</head> | |
<body class="control"> | |
<button data-url="https://www.electronjs.org/">Electron</button> | |
<button data-url="https://github.com/electron/electron">Github</button> | |
<button data-url="https://en.wikipedia.org/wiki/Electron_(software_framework)">Wikipedia</button> | |
<script> | |
const buttons = [...document.querySelectorAll(".control button")]; | |
buttons.map((button) => { | |
button.addEventListener("click", () => { | |
window.electronAPI.openView(button.dataset.url); | |
}, false); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Modules to control application life and create native browser window | |
const {app, BrowserView, BrowserWindow, ipcMain} = require('electron'); | |
const path = require('path'); | |
function createWindow () { | |
// Create the browser window. | |
const mainWindow = new BrowserWindow({ | |
width: 800, | |
height: 600, | |
titleBarStyle: "hidden", | |
titleBarOverlay: { | |
height: 40, | |
color: "#000000", | |
symbolColor: "#ffffff", | |
}, | |
}); | |
const controlView = new BrowserView({ | |
webPreferences: { | |
preload: path.join(__dirname, 'preload.js'), | |
devTools: true, | |
} | |
}); | |
controlView.setBounds({ x: 0, y: 0, width: 800, height: 40 }); | |
controlView.setAutoResize({ | |
width: true, | |
height: false, | |
horizontal: false, | |
vertical: false, | |
}); | |
controlView.webContents.loadFile("control.html"); | |
mainWindow.setBrowserView(controlView); | |
// controlView.webContents.openDevTools({ mode: "detach" }); | |
const browserViews = new Map(); | |
let currentView; | |
ipcMain.on("openView", (_, url) => { | |
if (!browserViews.has(url)) { | |
const view = new BrowserView(); | |
view.setAutoResize({ | |
width: true, | |
height: true, | |
horizontal: false, | |
vertical: false, | |
}); | |
view.webContents.loadURL(url); | |
browserViews.set(url, view); | |
} | |
if (currentView) { | |
mainWindow.removeBrowserView(currentView); | |
} | |
const view = browserViews.get(url); | |
const { width, height } = mainWindow.getContentBounds(); | |
view.setBounds({ x: 0, y: 40, width, height: height - 40 }); | |
mainWindow.addBrowserView(view); | |
currentView = view; | |
}); | |
} | |
// This method will be called when Electron has finished | |
// initialization and is ready to create browser windows. | |
// Some APIs can only be used after this event occurs. | |
app.whenReady().then(() => { | |
createWindow() | |
app.on('activate', function () { | |
// On macOS it's common to re-create a window in the app when the | |
// dock icon is clicked and there are no other windows open. | |
if (BrowserWindow.getAllWindows().length === 0) createWindow() | |
}) | |
}) | |
// Quit when all windows are closed, except on macOS. There, it's common | |
// for applications and their menu bar to stay active until the user quits | |
// explicitly with Cmd + Q. | |
app.on('window-all-closed', function () { | |
if (process.platform !== 'darwin') app.quit() | |
}) | |
// In this file you can include the rest of your app's specific main process | |
// code. You can also put them in separate files and require them here. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Webview size", | |
"productName": "Webview size", | |
"description": "My Electron application description", | |
"keywords": [], | |
"main": "./main.js", | |
"version": "1.0.0", | |
"author": "Alexandre Le Foll", | |
"scripts": { | |
"start": "electron ." | |
}, | |
"dependencies": {}, | |
"devDependencies": { | |
"electron": "19.0.0" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { contextBridge, ipcRenderer } = require('electron'); | |
contextBridge.exposeInMainWorld('electronAPI', { | |
openView: (viewId) => ipcRenderer.send('openView', viewId), | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html, body { | |
margin: 0; | |
padding: 0; | |
max-width: 100%; | |
} | |
.control { | |
background: linear-gradient(270deg, rgba(0,0,0,1) 20%, rgba(36,36,255,1) 55%, rgba(0,212,255,1) 95%); | |
-webkit-user-select: none; | |
-webkit-app-region: drag; | |
} | |
.control button { | |
-webkit-app-region: no-drag; | |
margin: 10px 5px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment