From b85f9102ce9b992e37e6ff5130e03d373ffae673 Mon Sep 17 00:00:00 2001 From: Michael Clark <5285928+MikesGlitch@users.noreply.github.com> Date: Sat, 13 Jul 2024 13:05:42 +0100 Subject: [PATCH] =?UTF-8?q?:electron:=20Convert=20window-state.js=20?= =?UTF-8?q?=E2=9E=A1=EF=B8=8F=20window-state.ts=20(#3027)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/desktop-electron/index.ts | 3 +- packages/desktop-electron/modules.d.ts | 1 + .../{window-state.js => window-state.ts} | 60 +++++++++++-------- upcoming-release-notes/3027.md | 6 ++ 4 files changed, 45 insertions(+), 25 deletions(-) rename packages/desktop-electron/{window-state.js => window-state.ts} (67%) create mode 100644 upcoming-release-notes/3027.md diff --git a/packages/desktop-electron/index.ts b/packages/desktop-electron/index.ts index bb75cbf69..049e79c35 100644 --- a/packages/desktop-electron/index.ts +++ b/packages/desktop-electron/index.ts @@ -9,6 +9,7 @@ import { Menu, dialog, shell, + powerMonitor, protocol, utilityProcess, UtilityProcess, @@ -241,7 +242,7 @@ app.on('ready', async () => { // This is mainly to aid debugging Sentry errors - it will add a // breadcrumb - require('electron').powerMonitor.on('suspend', () => { + powerMonitor.on('suspend', () => { console.log('Suspending', new Date()); }); diff --git a/packages/desktop-electron/modules.d.ts b/packages/desktop-electron/modules.d.ts index 509b52346..7341f7749 100644 --- a/packages/desktop-electron/modules.d.ts +++ b/packages/desktop-electron/modules.d.ts @@ -5,4 +5,5 @@ declare module 'module' { // bundles not available until we build them declare module 'loot-core/lib-dist/bundle.desktop.js' { const initApp: (isDev: boolean) => void; + const lib: { getDataDir: () => string }; } diff --git a/packages/desktop-electron/window-state.js b/packages/desktop-electron/window-state.ts similarity index 67% rename from packages/desktop-electron/window-state.js rename to packages/desktop-electron/window-state.ts index 205c88162..46b1a0771 100644 --- a/packages/desktop-electron/window-state.js +++ b/packages/desktop-electron/window-state.ts @@ -1,13 +1,22 @@ -const fs = require('fs'); -const path = require('path'); +import fs from 'fs'; +import path from 'path'; -const electron = require('electron'); +import electron, { BrowserWindow } from 'electron'; -// eslint-disable-next-line import/extensions -const backend = require('loot-core/lib-dist/bundle.desktop.js'); +const backend = undefined; +const getBackend = async () => + // eslint-disable-next-line import/extensions + backend || (await import('loot-core/lib-dist/bundle.desktop.js')); -function loadState() { - let state = {}; +type WindowState = Electron.Rectangle & { + isMaximized?: boolean; + isFullScreen?: boolean; + displayBounds?: Electron.Rectangle; +}; + +async function loadState() { + let state: WindowState | undefined = undefined; + const backend = await getBackend(); try { state = JSON.parse( fs.readFileSync( @@ -18,24 +27,27 @@ function loadState() { } catch (e) { console.log('Could not load window state'); } + return validateState(state); } -function updateState(win, state) { - const screen = electron.screen || electron.remote.screen; +function updateState(win: BrowserWindow, state: WindowState) { + const screen = electron.screen; const bounds = win.getBounds(); if (!win.isMaximized() && !win.isMinimized() && !win.isFullScreen()) { - state.x = bounds.x; - state.y = bounds.y; state.width = bounds.width; state.height = bounds.height; } + + state.x = bounds.x; + state.y = bounds.y; state.isMaximized = win.isMaximized(); state.isFullScreen = win.isFullScreen(); state.displayBounds = screen.getDisplayMatching(bounds).bounds; } -function saveState(win, state) { +async function saveState(win: BrowserWindow, state: WindowState) { + const backend = await getBackend(); updateState(win, state); fs.writeFileSync( path.join(backend.lib.getDataDir(), 'window.json'), @@ -44,7 +56,7 @@ function saveState(win, state) { ); } -function listen(win, state) { +export function listen(win: BrowserWindow, state: WindowState) { if (state.isMaximized) { win.maximize(); } @@ -61,7 +73,7 @@ function listen(win, state) { }; } -function hasBounds(state) { +function hasBounds(state: WindowState) { return ( Number.isInteger(state.x) && Number.isInteger(state.y) && @@ -72,15 +84,18 @@ function hasBounds(state) { ); } -function validateState(state) { - if (!(hasBounds(state) || state.isMaximized || state.isFullScreen)) { +function validateState(state?: WindowState): Partial<WindowState> { + if ( + !state || + !(hasBounds(state) || state.isMaximized || state.isFullScreen) + ) { return {}; } const newState = Object.assign({}, state); if (hasBounds(state) && state.displayBounds) { - const screen = electron.screen || electron.remote.screen; + const screen = electron.screen; // Check if the display where the window was last open is still available const displayBounds = screen.getDisplayMatching(state).bounds; @@ -116,22 +131,19 @@ function validateState(state) { return newState; } -async function get() { - const screen = electron.screen || electron.remote.screen; +export async function get() { + const screen = electron.screen; const displayBounds = screen.getPrimaryDisplay().bounds; - let state = loadState(); - state = Object.assign( + const state: WindowState = Object.assign( { x: 100, y: 50, width: Math.min(1000, displayBounds.width - 100), height: Math.min(700, displayBounds.width - 50), }, - state, + await loadState(), ); return state; } - -module.exports = { get, listen }; diff --git a/upcoming-release-notes/3027.md b/upcoming-release-notes/3027.md new file mode 100644 index 000000000..c32c14123 --- /dev/null +++ b/upcoming-release-notes/3027.md @@ -0,0 +1,6 @@ +--- +category: Maintenance +authors: [MikesGlitch] +--- + +Updated Electron window-state file to use typescript -- GitLab