From cda9b8fb426c0ee93075afc5308fbfd51f7d15d3 Mon Sep 17 00:00:00 2001 From: Kelvin Date: Tue, 4 Feb 2025 00:37:50 -0300 Subject: [PATCH 1/5] feat: screen state --- src/main/services/window-manager.ts | 115 +++++++++++++++++++++++----- 1 file changed, 94 insertions(+), 21 deletions(-) diff --git a/src/main/services/window-manager.ts b/src/main/services/window-manager.ts index 2d0bf24d..702c59b8 100644 --- a/src/main/services/window-manager.ts +++ b/src/main/services/window-manager.ts @@ -20,31 +20,18 @@ import { slice, sortBy } from "lodash-es"; import type { UserPreferences } from "@types"; import { AuthPage } from "@shared"; import { isStaging } from "@main/constants"; +import { writeFile, readFile } from "fs/promises"; export class WindowManager { public static mainWindow: Electron.BrowserWindow | null = null; - private static loadMainWindowURL(hash = "") { - // HMR for renderer base on electron-vite cli. - // Load the remote URL for development or the local html file for production. - if (is.dev && process.env["ELECTRON_RENDERER_URL"]) { - this.mainWindow?.loadURL( - `${process.env["ELECTRON_RENDERER_URL"]}#/${hash}` - ); - } else { - this.mainWindow?.loadFile( - path.join(__dirname, "../renderer/index.html"), - { - hash, - } - ); - } - } + private static configPath = path.join( + app.getPath("userData"), + "HydraScreenState.json" + ); - public static createMainWindow() { - if (this.mainWindow) return; - - this.mainWindow = new BrowserWindow({ + private static initialConfigInitializationMainWindow: Electron.BrowserWindowConstructorOptions = + { width: 1200, height: 720, minWidth: 1024, @@ -63,7 +50,78 @@ export class WindowManager { sandbox: false, }, show: false, - }); + }; + + private static async saveScreenConfig({ + ...configScreenWhenClosed + }: { + x: number | undefined; + y: number | undefined; + width: number; + height: number; + isMaximized: boolean; + }) { + try { + await writeFile( + this.configPath, + JSON.stringify(configScreenWhenClosed, null, 0), + "utf-8" + ); + } catch (error) { + console.error("failed to save screenConfig", error); + } + } + + private static async loadScreenConfig() { + try { + const screenConfigData = await readFile(this.configPath, "utf-8"); + return JSON.parse(screenConfigData); + } catch (error) { + console.error("failed to load screenConfig:", error); + return {}; + } + } + private static updateInitialConfig( + newConfig: Partial + ) { + this.initialConfigInitializationMainWindow = { + ...this.initialConfigInitializationMainWindow, + ...newConfig, + }; + } + + private static loadMainWindowURL(hash = "") { + // HMR for renderer base on electron-vite cli. + // Load the remote URL for development or the local html file for production. + if (is.dev && process.env["ELECTRON_RENDERER_URL"]) { + this.mainWindow?.loadURL( + `${process.env["ELECTRON_RENDERER_URL"]}#/${hash}` + ); + } else { + this.mainWindow?.loadFile( + path.join(__dirname, "../renderer/index.html"), + { + hash, + } + ); + } + } + + public static async createMainWindow() { + if (this.mainWindow) return; + + const { isMaximized, ...configWithoutMaximized } = + await this.loadScreenConfig(); + + this.updateInitialConfig(configWithoutMaximized); + + this.mainWindow = new BrowserWindow( + this.initialConfigInitializationMainWindow + ); + + if (isMaximized) { + this.mainWindow.maximize(); + } this.mainWindow.webContents.session.webRequest.onBeforeSendHeaders( (details, callback) => { @@ -134,6 +192,7 @@ export class WindowManager { }); this.mainWindow.on("close", async () => { + const lastBounds = this.mainWindow?.getBounds(); const userPreferences = await db.get( levelKeys.userPreferences, { @@ -144,6 +203,20 @@ export class WindowManager { if (userPreferences?.preferQuitInsteadOfHiding) { app.quit(); } + if (lastBounds) { + const isMaximized = this.mainWindow?.isMaximized() ?? false; + const screenConfig = isMaximized + ? { + x: undefined, + y: undefined, + height: this.initialConfigInitializationMainWindow.height!, + width: this.initialConfigInitializationMainWindow.width!, + isMaximized: true, + } + : { ...lastBounds, isMaximized }; + + await this.saveScreenConfig(screenConfig); + } WindowManager.mainWindow?.setProgressBar(-1); WindowManager.mainWindow = null; }); From afa34c74d7d6d4740096ceb44ff1dc04270cc7ac Mon Sep 17 00:00:00 2001 From: Kelvin Date: Tue, 4 Feb 2025 00:48:55 -0300 Subject: [PATCH 2/5] fix --- src/main/services/window-manager.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/services/window-manager.ts b/src/main/services/window-manager.ts index 702c59b8..567bcdec 100644 --- a/src/main/services/window-manager.ts +++ b/src/main/services/window-manager.ts @@ -82,7 +82,7 @@ export class WindowManager { } } private static updateInitialConfig( - newConfig: Partial + newConfig: Partial ) { this.initialConfigInitializationMainWindow = { ...this.initialConfigInitializationMainWindow, @@ -203,8 +203,10 @@ export class WindowManager { if (userPreferences?.preferQuitInsteadOfHiding) { app.quit(); } - if (lastBounds) { - const isMaximized = this.mainWindow?.isMaximized() ?? false; + + if (this.mainWindow) { + const lastBounds = this.mainWindow.getBounds(); + const isMaximized = this.mainWindow.isMaximized() ?? false; const screenConfig = isMaximized ? { x: undefined, From c33aa3c874af736b02a2205ec6bcb9d126f7f7ff Mon Sep 17 00:00:00 2001 From: Kelvin Date: Tue, 4 Feb 2025 00:50:18 -0300 Subject: [PATCH 3/5] fix --- src/main/services/window-manager.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/services/window-manager.ts b/src/main/services/window-manager.ts index 567bcdec..8893a0e7 100644 --- a/src/main/services/window-manager.ts +++ b/src/main/services/window-manager.ts @@ -192,7 +192,6 @@ export class WindowManager { }); this.mainWindow.on("close", async () => { - const lastBounds = this.mainWindow?.getBounds(); const userPreferences = await db.get( levelKeys.userPreferences, { From 38dc8bdea5194635bcb1494b9050d5f503c35dad Mon Sep 17 00:00:00 2001 From: Kelvin Date: Tue, 4 Feb 2025 22:26:01 -0300 Subject: [PATCH 4/5] fix --- src/main/services/window-manager.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/services/window-manager.ts b/src/main/services/window-manager.ts index 8893a0e7..5412a548 100644 --- a/src/main/services/window-manager.ts +++ b/src/main/services/window-manager.ts @@ -199,10 +199,6 @@ export class WindowManager { } ); - if (userPreferences?.preferQuitInsteadOfHiding) { - app.quit(); - } - if (this.mainWindow) { const lastBounds = this.mainWindow.getBounds(); const isMaximized = this.mainWindow.isMaximized() ?? false; @@ -218,6 +214,11 @@ export class WindowManager { await this.saveScreenConfig(screenConfig); } + + if (userPreferences?.preferQuitInsteadOfHiding) { + app.quit(); + } + WindowManager.mainWindow?.setProgressBar(-1); WindowManager.mainWindow = null; }); From 08935ceabd77d1fca0f19821d27480f2f763b4fb Mon Sep 17 00:00:00 2001 From: Kelvin Date: Wed, 5 Feb 2025 22:50:02 -0300 Subject: [PATCH 5/5] refactor to levelDB --- src/main/level/sublevels/keys.ts | 1 + src/main/services/window-manager.ts | 33 +++++-------------- .../game-details/hero/hero-panel-actions.tsx | 1 - src/types/level.types.ts | 8 +++++ 4 files changed, 18 insertions(+), 25 deletions(-) diff --git a/src/main/level/sublevels/keys.ts b/src/main/level/sublevels/keys.ts index 53eae44b..78969c8b 100644 --- a/src/main/level/sublevels/keys.ts +++ b/src/main/level/sublevels/keys.ts @@ -13,4 +13,5 @@ export const levelKeys = { userPreferences: "userPreferences", language: "language", sqliteMigrationDone: "sqliteMigrationDone", + screenState: "screenState", }; diff --git a/src/main/services/window-manager.ts b/src/main/services/window-manager.ts index 5412a548..37874d60 100644 --- a/src/main/services/window-manager.ts +++ b/src/main/services/window-manager.ts @@ -17,19 +17,13 @@ import { HydraApi } from "./hydra-api"; import UserAgent from "user-agents"; import { db, gamesSublevel, levelKeys } from "@main/level"; import { slice, sortBy } from "lodash-es"; -import type { UserPreferences } from "@types"; +import type { ScreenState, UserPreferences } from "@types"; import { AuthPage } from "@shared"; import { isStaging } from "@main/constants"; -import { writeFile, readFile } from "fs/promises"; export class WindowManager { public static mainWindow: Electron.BrowserWindow | null = null; - private static configPath = path.join( - app.getPath("userData"), - "HydraScreenState.json" - ); - private static initialConfigInitializationMainWindow: Electron.BrowserWindowConstructorOptions = { width: 1200, @@ -61,25 +55,16 @@ export class WindowManager { height: number; isMaximized: boolean; }) { - try { - await writeFile( - this.configPath, - JSON.stringify(configScreenWhenClosed, null, 0), - "utf-8" - ); - } catch (error) { - console.error("failed to save screenConfig", error); - } + await db.put(levelKeys.screenState, configScreenWhenClosed, { + valueEncoding: "json", + }); } private static async loadScreenConfig() { - try { - const screenConfigData = await readFile(this.configPath, "utf-8"); - return JSON.parse(screenConfigData); - } catch (error) { - console.error("failed to load screenConfig:", error); - return {}; - } + const data = await db.get(levelKeys.screenState, { + valueEncoding: "json", + }); + return data ?? {}; } private static updateInitialConfig( newConfig: Partial @@ -110,7 +95,7 @@ export class WindowManager { public static async createMainWindow() { if (this.mainWindow) return; - const { isMaximized, ...configWithoutMaximized } = + const { isMaximized = false, ...configWithoutMaximized } = await this.loadScreenConfig(); this.updateInitialConfig(configWithoutMaximized); diff --git a/src/renderer/src/pages/game-details/hero/hero-panel-actions.tsx b/src/renderer/src/pages/game-details/hero/hero-panel-actions.tsx index 37e0ff1f..9dc531f4 100644 --- a/src/renderer/src/pages/game-details/hero/hero-panel-actions.tsx +++ b/src/renderer/src/pages/game-details/hero/hero-panel-actions.tsx @@ -196,7 +196,6 @@ export function HeroPanelActions() { {game.favorite ? : } -