partey_workadventure/desktop/electron/src/window.ts

151 lines
4.0 KiB
TypeScript
Raw Normal View History

import { BrowserView, BrowserWindow, app } from "electron";
2022-02-20 20:30:53 +01:00
import electronIsDev from "electron-is-dev";
2022-02-17 18:09:57 +01:00
import windowStateKeeper from "electron-window-state";
2022-02-17 19:48:08 +01:00
import path from "path";
2022-02-20 20:30:53 +01:00
import { loadCustomScheme } from "./serve";
2022-02-17 18:09:57 +01:00
let mainWindow: BrowserWindow | undefined;
2022-02-18 01:51:35 +01:00
let appView: BrowserView | undefined;
let appViewUrl = "";
2022-02-17 18:09:57 +01:00
const sidebarWidth = 80;
2022-02-17 18:09:57 +01:00
export function getWindow() {
2022-02-18 23:03:05 +01:00
return mainWindow;
2022-02-17 18:09:57 +01:00
}
2022-02-18 01:51:35 +01:00
export function getAppView() {
2022-02-18 23:03:05 +01:00
return appView;
2022-02-18 01:51:35 +01:00
}
2022-02-22 16:57:56 +01:00
function resizeAppView() {
2022-02-22 19:58:51 +01:00
// TODO: workaround: set timeout is needed as mainWindow.getBounds() needs some time to update
setTimeout(() => {
if (!mainWindow || !appView) {
return;
}
const { width, height } = mainWindow.getBounds();
appView.setBounds({
x: sidebarWidth,
y: 0,
width: width - sidebarWidth,
height: height,
});
2022-02-22 16:57:56 +01:00
});
}
2022-02-20 20:30:53 +01:00
export async function createWindow() {
2022-02-18 23:03:05 +01:00
// do not re-create window if still existing
if (mainWindow) {
return;
}
2022-02-17 18:09:57 +01:00
2022-02-18 23:03:05 +01:00
// Load the previous state with fallback to defaults
const windowState = windowStateKeeper({
defaultWidth: 1000,
defaultHeight: 800,
maximize: true,
});
2022-02-17 18:09:57 +01:00
2022-02-18 23:03:05 +01:00
mainWindow = new BrowserWindow({
x: windowState.x,
y: windowState.y,
width: windowState.width,
height: windowState.height,
autoHideMenuBar: true,
show: false,
webPreferences: {
2022-02-19 01:48:56 +01:00
preload: path.resolve(__dirname, "..", "dist", "preload-local-app", "preload.js"),
2022-02-18 23:03:05 +01:00
},
});
2022-02-22 16:57:56 +01:00
mainWindow.setMenu(null);
2022-02-17 18:09:57 +01:00
2022-02-18 23:03:05 +01:00
// Let us register listeners on the window, so we can update the state
// automatically (the listeners will be removed when the window is closed)
// and restore the maximized or full screen state
windowState.manage(mainWindow);
2022-02-17 18:09:57 +01:00
2022-02-18 23:03:05 +01:00
mainWindow.on("closed", () => {
mainWindow = undefined;
});
2022-02-17 18:09:57 +01:00
2022-02-18 23:03:05 +01:00
// mainWindow.on('close', async (event) => {
// if (!app.confirmedExitPrompt) {
// event.preventDefault(); // Prevents the window from closing
// const choice = await dialog.showMessageBox(getMainWindow(), {
// type: 'question',
// buttons: ['Yes', 'Abort'],
// title: 'Confirm',
// message: 'Are you sure you want to quit?',
// });
// if (choice.response === 0) {
// app.confirmedExitPrompt = true;
// mainWindow.close();
// }
// } else {
// app.confirmedExitPrompt = false;
// }
// });
2022-02-17 18:09:57 +01:00
2022-02-18 23:03:05 +01:00
appView = new BrowserView({
webPreferences: {
2022-02-19 01:48:56 +01:00
preload: path.resolve(__dirname, "..", "dist", "preload-app", "preload.js"),
2022-02-18 23:03:05 +01:00
},
});
2022-02-22 16:57:56 +01:00
resizeAppView();
appView.setAutoResize({ width: true, height: true });
2022-02-22 16:57:56 +01:00
mainWindow.on("resize", resizeAppView);
2022-02-18 01:51:35 +01:00
2022-02-18 23:03:05 +01:00
mainWindow.once("ready-to-show", () => {
2022-02-19 01:55:05 +01:00
mainWindow?.show();
2022-02-18 23:03:05 +01:00
});
2022-02-18 01:51:35 +01:00
2022-02-18 23:03:05 +01:00
mainWindow.webContents.on("did-finish-load", () => {
2022-03-07 18:10:26 +01:00
mainWindow?.setTitle("WorkAdventure Desktop (alpha release)");
2022-02-18 23:03:05 +01:00
});
2022-02-18 01:51:35 +01:00
2022-02-20 20:30:53 +01:00
if (electronIsDev && process.env.LOCAL_APP_URL) {
await mainWindow.loadURL(process.env.LOCAL_APP_URL);
} else {
// load custom url scheme app://
await loadCustomScheme(mainWindow);
await mainWindow.loadURL("app://-");
}
2022-02-19 01:08:33 +01:00
}
export async function showAppView(url?: string) {
2022-02-19 01:08:33 +01:00
if (!appView) {
throw new Error("App view not found");
}
if (!mainWindow) {
throw new Error("Main window not found");
}
if (mainWindow.getBrowserView()) {
mainWindow.removeBrowserView(appView);
}
mainWindow.addBrowserView(appView);
if (url && url !== appViewUrl) {
await appView.webContents.loadURL(url);
appViewUrl = url;
2022-02-19 01:08:33 +01:00
}
2022-02-24 12:11:48 +01:00
appView.webContents.focus();
2022-02-19 01:08:33 +01:00
}
export function hideAppView() {
if (!appView) {
throw new Error("App view not found");
}
if (!mainWindow) {
throw new Error("Main window not found");
}
mainWindow.removeBrowserView(appView);
2022-02-17 18:09:57 +01:00
}