From c8f62f6d19e5d6035bf3c7cf9b15152dd540a730 Mon Sep 17 00:00:00 2001 From: Araxeus <78568641+Araxeus@users.noreply.github.com> Date: Thu, 20 Jan 2022 23:45:57 +0200 Subject: [PATCH] fix window position save spam (and also window position being forgotten on maximizing) --- index.js | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 07801d58..999b7e8a 100644 --- a/index.js +++ b/index.js @@ -142,22 +142,44 @@ function createMainWindow() { win.on("closed", onClosed); win.on("move", () => { + if (win.isMaximized()) return; let position = win.getPosition(); - config.set("window-position", { x: position[0], y: position[1] }); + lateSave("window-position", { x: position[0], y: position[1] }); }); + let winWasMaximized; + win.on("resize", () => { const windowSize = win.getSize(); - config.set("window-maximized", win.isMaximized()); - if (!win.isMaximized()) { - config.set("window-size", { + const isMaximized = win.isMaximized(); + if (winWasMaximized !== isMaximized) { + winWasMaximized = isMaximized; + config.set("window-maximized", isMaximized); + console.log('set window-maximized to ', isMaximized) // DELETE + } + if (!isMaximized) { + lateSave("window-size", { width: windowSize[0], height: windowSize[1], }); } }); + let savedTimeout = { + "window-position": null, + "window-siza": null + } + function lateSave(key, value) { + if (savedTimeout[key]) clearTimeout(savedTimeout[key]); + + savedTimeout[key] = setTimeout(() => { + config.set(key, value); + console.log('saving ', key, value); // DELETE + savedTimeout[key] = null; + }, 1000) + } + win.webContents.on("render-process-gone", (event, webContents, details) => { showUnresponsiveDialog(win, details); });