Merge pull request #2780 from MartinFournier/feat/electron-store-winposition

Store Electron window position in user settings
This commit is contained in:
hydroflame 2022-01-26 12:57:09 -05:00 committed by GitHub
commit e392e6d71f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 75 additions and 1 deletions

@ -8,6 +8,7 @@ const api = require("./api-server");
const cp = require("child_process"); const cp = require("child_process");
const path = require("path"); const path = require("path");
const fs = require("fs"); const fs = require("fs");
const { windowTracker } = require("./windowTracker");
const { fileURLToPath } = require("url"); const { fileURLToPath } = require("url");
const debug = process.argv.includes("--debug"); const debug = process.argv.includes("--debug");
@ -20,19 +21,29 @@ async function createWindow(killall) {
icon = path.join(__dirname, 'icon.png'); icon = path.join(__dirname, 'icon.png');
} }
const tracker = windowTracker('main');
const window = new BrowserWindow({ const window = new BrowserWindow({
icon, icon,
show: false, show: false,
backgroundThrottling: false, backgroundThrottling: false,
backgroundColor: "#000000", backgroundColor: "#000000",
title: 'Bitburner',
x: tracker.state.x,
y: tracker.state.y,
width: tracker.state.width,
height: tracker.state.height,
minWidth: 600,
minHeight: 400,
webPreferences: { webPreferences: {
nativeWindowOpen: true, nativeWindowOpen: true,
preload: path.join(__dirname, 'preload.js'), preload: path.join(__dirname, 'preload.js'),
}, },
}); });
setTimeout(() => tracker.track(window), 1000);
if (tracker.state.isMaximized) window.maximize();
window.removeMenu(); window.removeMenu();
window.maximize();
noScripts = killall ? { query: { noScripts: killall } } : {}; noScripts = killall ? { query: { noScripts: killall } } : {};
window.loadFile("index.html", noScripts); window.loadFile("index.html", noScripts);
window.show(); window.show();

63
electron/windowTracker.js Normal file

@ -0,0 +1,63 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const { screen } = require("electron");
const log = require("electron-log");
const debounce = require("lodash/debounce");
const Config = require("electron-config");
const config = new Config();
// https://stackoverflow.com/a/68627253
const windowTracker = (windowName) => {
let window, windowState;
const setBounds = () => {
// Restore from appConfig
if (config.has(`window.${windowName}`)) {
windowState = config.get(`window.${windowName}`);
return;
}
const size = screen.getPrimaryDisplay().workAreaSize;
// Default
windowState = {
x: undefined,
y: undefined,
width: size.width,
height: size.height,
isMaximized: true,
};
};
const saveState = debounce(() => {
if (!windowState.isMaximized) {
windowState = window.getBounds();
}
windowState.isMaximized = window.isMaximized();
log.silly(`Saving window.${windowName} to configs`);
config.set(`window.${windowName}`, windowState);
log.silly(windowState);
}, 1000);
const track = (win) => {
window = win;
['resize', 'move', 'close'].forEach((event) => {
win.on(event, saveState);
});
};
setBounds();
return {
state: {
x: windowState.x,
y: windowState.y,
width: windowState.width,
height: windowState.height,
isMaximized: windowState.isMaximized,
},
track,
};
};
module.exports = { windowTracker };