From dc2f00057aa298e30cd864db65ace15f7fad3eb0 Mon Sep 17 00:00:00 2001 From: Martin Fournier Date: Sun, 23 Jan 2022 12:29:32 -0500 Subject: [PATCH 1/2] Include lodash in Electron Will be used to debounce function calls. Is a prerequisite for a few other fixes. --- electron/package-lock.json | 13 ++++++++++++- electron/package.json | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/electron/package-lock.json b/electron/package-lock.json index 00b2d1d7e..b512a9036 100644 --- a/electron/package-lock.json +++ b/electron/package-lock.json @@ -9,7 +9,8 @@ "version": "1.0.0", "dependencies": { "electron-config": "^2.0.0", - "electron-log": "^4.4.4" + "electron-log": "^4.4.4", + "lodash": "^4.17.21" } }, "node_modules/conf": { @@ -104,6 +105,11 @@ "node": ">=4" } }, + "node_modules/lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" + }, "node_modules/make-dir": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-1.3.0.tgz", @@ -259,6 +265,11 @@ "path-exists": "^3.0.0" } }, + "lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" + }, "make-dir": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-1.3.0.tgz", diff --git a/electron/package.json b/electron/package.json index f3697e2c1..ddf366f92 100755 --- a/electron/package.json +++ b/electron/package.json @@ -25,6 +25,7 @@ }, "dependencies": { "electron-config": "^2.0.0", - "electron-log": "^4.4.4" + "electron-log": "^4.4.4", + "lodash": "^4.17.21" } } From ca5f51b7f0bda0447415af7fa6e617e5f0d7b6ef Mon Sep 17 00:00:00 2001 From: Martin Fournier Date: Sun, 23 Jan 2022 08:41:09 -0500 Subject: [PATCH 2/2] Keep Electron window position in settings This allows the app to be kept at the same location it was previously. --- electron/gameWindow.js | 13 +++++++- electron/windowTracker.js | 63 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 electron/windowTracker.js diff --git a/electron/gameWindow.js b/electron/gameWindow.js index fb14326d4..6a60b330d 100644 --- a/electron/gameWindow.js +++ b/electron/gameWindow.js @@ -8,6 +8,7 @@ const api = require("./api-server"); const cp = require("child_process"); const path = require("path"); const fs = require("fs"); +const { windowTracker } = require("./windowTracker"); const { fileURLToPath } = require("url"); const debug = process.argv.includes("--debug"); @@ -20,18 +21,28 @@ async function createWindow(killall) { icon = path.join(__dirname, 'icon.png'); } + const tracker = windowTracker('main'); const window = new BrowserWindow({ icon, show: false, backgroundThrottling: false, 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: { nativeWindowOpen: true, }, }); + setTimeout(() => tracker.track(window), 1000); + if (tracker.state.isMaximized) window.maximize(); + window.removeMenu(); - window.maximize(); noScripts = killall ? { query: { noScripts: killall } } : {}; window.loadFile("index.html", noScripts); window.show(); diff --git a/electron/windowTracker.js b/electron/windowTracker.js new file mode 100644 index 000000000..563822177 --- /dev/null +++ b/electron/windowTracker.js @@ -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 };