Skip to content

Instantly share code, notes, and snippets.

@jasonsturges
Last active November 30, 2020 05:10
Show Gist options
  • Save jasonsturges/f0ecd1e82dabbca4355acf180ff06802 to your computer and use it in GitHub Desktop.
Save jasonsturges/f0ecd1e82dabbca4355acf180ff06802 to your computer and use it in GitHub Desktop.
Electron Multiple Windows
const windows = new Set();
export const createWindow = async () => {
if (
process.env.NODE_ENV === 'development' ||
process.env.DEBUG_PROD === 'true'
) {
await installExtensions();
}
let x, y;
const currentWindow = BrowserWindow.getFocusedWindow();
if (currentWindow) {
const [currentWindowX, currentWindowY] = currentWindow.getPosition();
x = currentWindowX + 24;
y = currentWindowY + 24;
}
let newWindow = new BrowserWindow({
show: false,
width: 1200,
height: 812,
x,
y,
webPreferences: {
nodeIntegration: true
}
});
newWindow.loadURL(`file://${__dirname}/app.html`);
newWindow.webContents.on('did-finish-load', () => {
if (!newWindow) {
throw new Error('"newWindow" is not defined');
}
if (process.env.START_MINIMIZED) {
newWindow.minimize();
} else {
newWindow.show();
newWindow.focus();
}
});
newWindow.on('closed', () => {
windows.delete(newWindow);
newWindow = null;
});
newWindow.on('focus', () => {
const menuBuilder = new MenuBuilder(newWindow);
menuBuilder.buildMenu();
});
windows.add(newWindow);
return newWindow;
};
app.on('activate', () => {
// 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 (windows.size === 0) createWindow();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment