macintosh.js/src/main/windows.js

107 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-03-13 23:06:00 +00:00
const { BrowserWindow, shell } = require("electron");
2020-07-26 16:06:09 +00:00
const path = require("path");
2020-07-27 06:07:08 +00:00
const { getIsDevMode } = require("./devmode");
2020-07-26 16:06:09 +00:00
const windowList = {};
let mainWindow;
2020-07-27 17:33:55 +00:00
function getMainWindow() {
return mainWindow;
}
2020-07-26 16:06:09 +00:00
function handleNewWindow(event, url, frameName, disposition, options) {
// open window as modal
event.preventDefault();
// Don't open the same window multiple times
if (windowList[url]) {
windowList[url].focus();
return;
}
Object.assign(options, {
parent: mainWindow,
2020-07-27 02:25:48 +00:00
width: 350,
height: 630,
2020-07-26 16:06:09 +00:00
frame: true,
transparent: false,
resizable: true,
webPreferences: {
2020-07-27 02:25:48 +00:00
nodeIntegration: true,
2020-07-27 02:01:57 +00:00
navigateOnDragDrop: false,
2020-07-26 16:06:09 +00:00
},
});
2020-07-27 02:01:57 +00:00
let newWindow = new BrowserWindow(options);
2020-07-26 16:06:09 +00:00
2020-07-27 02:01:57 +00:00
newWindow.webContents.on("will-navigate", (event, url) => {
if (url.startsWith("http")) {
2020-07-26 16:06:09 +00:00
event.preventDefault();
shell.openExternal(url);
}
});
event.newGuest = newWindow;
newWindow.setMenu(null);
windowList[url] = newWindow;
2020-07-27 06:07:08 +00:00
if (getIsDevMode()) {
2020-07-26 16:06:09 +00:00
newWindow.webContents.toggleDevTools();
}
2020-07-27 02:01:57 +00:00
newWindow.on("closed", () => {
2020-07-26 16:06:09 +00:00
delete windowList[url];
});
}
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 900,
height: 730,
useContentSize: true,
frame: true,
2020-07-26 16:06:09 +00:00
transparent: true,
resizable: true,
2020-07-26 16:06:09 +00:00
webPreferences: {
nodeIntegration: true,
2023-03-13 23:06:00 +00:00
nodeIntegrationInWorker: true,
2020-07-26 16:06:09 +00:00
nativeWindowOpen: true,
2023-03-13 23:06:00 +00:00
contextIsolation: false,
2020-07-26 16:06:09 +00:00
navigateOnDragDrop: false,
nodeIntegrationInWorker: true,
2020-07-27 02:01:57 +00:00
sandbox: false,
2020-07-26 16:06:09 +00:00
},
});
2023-03-13 23:06:00 +00:00
// Ensure that we have access to SharedArrayBuffer
mainWindow.webContents.session.webRequest.onHeadersReceived(
(details, callback) => {
details.responseHeaders["Cross-Origin-Opener-Policy"] = ["same-origin"];
details.responseHeaders["Cross-Origin-Embedder-Policy"] = [
"require-corp",
];
callback({ responseHeaders: details.responseHeaders });
}
);
2020-07-26 16:06:09 +00:00
// and load the index.html of the app.
mainWindow.loadFile(path.join(__dirname, "../renderer/index.html"));
// Disable menu
mainWindow.setMenu(null);
// Ensure we create child windows with the correct settings
mainWindow.webContents.on("new-window", handleNewWindow);
2020-07-27 06:07:08 +00:00
if (getIsDevMode()) {
2020-07-26 16:06:09 +00:00
mainWindow.webContents.toggleDevTools();
}
2020-07-27 02:01:57 +00:00
}
2020-07-26 16:06:09 +00:00
module.exports = {
2020-07-27 02:01:57 +00:00
createWindow,
2023-03-13 23:06:00 +00:00
getMainWindow,
2020-07-27 02:01:57 +00:00
};