Last active
January 6, 2022 17:47
-
-
Save matt-allan/f2ba61de30cfde2aa1f90d44177d68cf to your computer and use it in GitHub Desktop.
Electron Fiddle Gist
This file contains 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"> | |
<title>Context Bridge Example</title> | |
<link rel="stylesheet" type="text/css" href="./styles.css"> | |
</head> | |
<body> | |
<main class="app"> | |
<button onclick="window.app.setFullscreen(true)">Enter fullscreen</button> | |
<button onclick="window.app.setFullscreen(false)">Exit fullscreen</button> | |
</main> | |
</body> | |
</html> |
This file contains 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, BrowserWindow, ipcMain} = require('electron') | |
const path = require('path') | |
function createWindow () { | |
// Create the browser window. | |
const mainWindow = new BrowserWindow({ | |
width: 800, | |
height: 600, | |
webPreferences: { | |
allowRunningInsecureContent: false, | |
contextIsolation: true, | |
enableRemoteModule: false, | |
nodeIntegration: false, | |
sandbox: true, | |
preload: path.join(app.getAppPath(), 'preload.js'), | |
} | |
}) | |
// and load the index.html of the app. | |
mainWindow.loadFile('index.html') | |
// Open the DevTools. | |
// mainWindow.webContents.openDevTools() | |
ipcMain.handle('setFullscreen', (event, flag) => { | |
if (mainWindow) { | |
mainWindow.setFullScreen(flag) | |
} | |
}) | |
} | |
// 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.on('ready', createWindow) | |
// Quit when all windows are closed. | |
app.on('window-all-closed', function () { | |
// On OS X it is common for applications and their menu bar | |
// to stay active until the user quits explicitly with Cmd + Q | |
if (process.platform !== 'darwin') { | |
app.quit() | |
} | |
}) | |
app.on('activate', function () { | |
// On OS X 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() | |
} | |
}) |
This file contains 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 { ipcRenderer, contextBridge } = require('electron') | |
contextBridge.exposeInMainWorld( | |
'app', | |
{ | |
setFullscreen: (flag) => ipcRenderer.invoke('setFullscreen', flag), | |
} | |
) |
This file contains 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
// Empty |
This file contains 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
.app { | |
text-align: center; | |
margin: 10rem; | |
} |
@matt-allan
tried to follow your example and transpose it into an Electron-Typescript-React-Webpack app, but I'm encountering this problem: Cannot read property 'setFullscreen' of undefined
:
preload.js :
const {
contextBridge,
ipcRenderer
} = require("electron")
contextBridge.exposeInMainWorld(
"api", {
{
setFullscreen: (flag) => ipcRenderer.invoke('setFullscreen', flag),
}
}
)
global.ts :
export {}
declare global {
interface Window {
"api": {
setFullscreen: (flag) => void;
}
}
}
main.ts :
let mainWindow: BrowserWindow;
const createWindow = (): void => {
// Create the browser window.
mainWindow = new BrowserWindow({
height: 600,
width: 800,
backgroundColor: '#242424',
webPreferences: {
nodeIntegration: false,
enableRemoteModule: false,
contextIsolation: true,
nodeIntegrationInWorker: false,
nodeIntegrationInSubFrames: false,
webSecurity: true,
webviewTag: false,
preload: './preload/preload',
devTools: process.env.NODE_ENV !== 'production',
},
});
mainWindow.webContents.openDevTools();
// and load the index.html of the app.
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, './index.html'),
protocol: 'file:',
slashes: true,
}),
).finally(() => { /* no action */ });
ipcMain.handle('setFullscreen', (event, flag) => {
if (mainWindow) {
mainWindow.setFullScreen(flag);
}
});
mainWindow.on('close', () => {
console.log('Closing WindowTypeA...');
});
}
in the renderer process App.tsx :
import * as React from 'react';
function App() {
const toggleFullScreen = (flag: boolean) => {
window.api.setFullscreen(flag);
}
return (
<div className='container'>
<h2 className='heading'>
Multiple Selective Windows Communication
</h2>
<p>
<button onClick={() => {
toggleFullScreen(true);
}}>Enter fullscreen</button>
<button onClick={() => {
toggleFullScreen(false);
}}>Exit fullscreen</button>
</p>
</div>
);
If I set in App.tsx :
const sendProxy = window.api.send;
I get the same error and the window is not rendered :
What am I doing wrongly? How to solve the problem?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well now that does make sense indeed, otherwise, why disabling nodeIntegration if it is to be able to require node extensions ^^'
Anyway, thank you for coming back to me on this!
I wanted to make sure this was a typo and not something I didn't understand!