bitburner-src/electron/gameWindow.js

81 lines
2.3 KiB
JavaScript
Raw Normal View History

/* eslint-disable @typescript-eslint/no-var-requires */
2023-02-25 23:42:26 +01:00
const { app, shell, BrowserWindow } = require("electron");
const log = require("electron-log");
const utils = require("./utils");
const achievements = require("./achievements");
const menu = require("./menu");
const api = require("./api-server");
const path = require("path");
const { windowTracker } = require("./windowTracker");
const debug = process.argv.includes("--debug");
async function createWindow(killall) {
2022-01-09 20:02:11 +01:00
const setStopProcessHandler = global.app_handlers.stopProcess;
2022-04-07 01:30:08 +02:00
app.setAppUserModelId("Bitburner");
2022-01-23 18:38:47 +01:00
let icon;
2022-04-07 01:30:08 +02:00
if (process.platform == "linux") {
icon = path.join(__dirname, "icon.png");
2022-01-23 18:38:47 +01:00
}
2022-04-07 01:30:08 +02:00
const tracker = windowTracker("main");
const window = new BrowserWindow({
2022-01-23 18:38:47 +01:00
icon,
show: false,
backgroundThrottling: false,
backgroundColor: "#000000",
2022-04-07 01:30:08 +02:00
title: "Bitburner",
x: tracker.state.x,
y: tracker.state.y,
width: tracker.state.width,
height: tracker.state.height,
minWidth: 600,
minHeight: 400,
webPreferences: {
nativeWindowOpen: true,
2022-04-07 01:30:08 +02:00
preload: path.join(__dirname, "preload.js"),
},
});
setTimeout(() => tracker.track(window), 1000);
if (tracker.state.isMaximized) window.maximize();
window.removeMenu();
noScripts = killall ? { query: { noScripts: killall } } : {};
window.loadFile("index.html", noScripts);
window.show();
if (debug) window.webContents.openDevTools();
2023-02-25 23:42:26 +01:00
window.webContents.setWindowOpenHandler(({ url }) => {
// File protocol is allowed because it will use the file protocol intercept from main.js
if (url.startsWith("file://")) return { action: "allow" };
// Only http and https requests will be forwarded to browser.
// By using shell.openExternal and returning action: "deny"
if (url.startsWith("http://") || url.startsWith("https://")) shell.openExternal(url);
return { action: "deny" };
});
2023-02-25 23:42:26 +01:00
window.webContents.backgroundThrottling = false;
achievements.enableAchievementsInterval(window);
utils.attachUnresponsiveAppHandler(window);
2022-01-05 03:08:06 +01:00
utils.setZoomFactor(window);
try {
await api.initialize(window);
} catch (error) {
log.error(error);
2022-01-09 20:02:11 +01:00
utils.showErrorBox("Error starting http server", error);
}
menu.refreshMenu(window);
2023-01-11 01:12:23 +01:00
setStopProcessHandler(app, window);
return window;
}
module.exports = {
createWindow,
2022-01-09 20:02:11 +01:00
};