From dfbbadea4621e17578dbfb48fd5da2a12e6a555b Mon Sep 17 00:00:00 2001 From: Kuingsmile Date: Thu, 1 Aug 2024 17:30:59 +0800 Subject: [PATCH] :hammer: Refactor(custom): refactored window manager --- src/main/apis/app/window/windowList.ts | 29 +++---------- src/main/apis/app/window/windowManager.ts | 53 +++++++++++------------ 2 files changed, 32 insertions(+), 50 deletions(-) diff --git a/src/main/apis/app/window/windowList.ts b/src/main/apis/app/window/windowList.ts index 6c7688c..9f3febc 100644 --- a/src/main/apis/app/window/windowList.ts +++ b/src/main/apis/app/window/windowList.ts @@ -233,19 +233,9 @@ windowList.set(IWindowList.RENAME_WINDOW, { window.loadURL(handleWindowParams(RENAME_WINDOW_URL)) const currentWindow = windowManager.getAvailableWindow(true) if (currentWindow && currentWindow.isVisible()) { - // bounds: { x: 821, y: 75, width: 800, height: 450 } - const bounds = currentWindow.getBounds() - let positionX = bounds.x + bounds.width / 2 - 150 - let positionY - // if is the settingWindow - if (bounds.height > 400) { - positionY = bounds.y + bounds.height / 2 - 88 - } else { - // if is the miniWindow - positionY = bounds.y + bounds.height / 2 - } - positionX = Math.floor(positionX) - positionY = Math.floor(positionY) + const { x, y, width, height } = currentWindow.getBounds() + const positionX = Math.floor(x + width / 2 - 150) + const positionY = Math.floor(y + height / 2 - (height > 400 ? 88 : 0)) window.setPosition(positionX, positionY, false) } } @@ -259,16 +249,9 @@ windowList.set(IWindowList.TOOLBOX_WINDOW, { window.loadURL(TOOLBOX_WINDOW_URL) const currentWindow = windowManager.getAvailableWindow(true) if (currentWindow && currentWindow.isVisible()) { - const bounds = currentWindow.getBounds() - let positionX = bounds.x + bounds.width / 2 - 400 - let positionY - if (bounds.height > 400) { - positionY = bounds.y + bounds.height / 2 - 225 - } else { - positionY = bounds.y + bounds.height / 2 - } - positionX = Math.floor(positionX) - positionY = Math.floor(positionY) + const { x, y, width, height } = currentWindow.getBounds() + const positionX = Math.floor(x + width / 2 - 400) + const positionY = Math.floor(y + height / 2 - (height > 400 ? 225 : 0)) window.setPosition(positionX, positionY, false) } } diff --git a/src/main/apis/app/window/windowManager.ts b/src/main/apis/app/window/windowManager.ts index 0ec2cc0..960eae6 100644 --- a/src/main/apis/app/window/windowManager.ts +++ b/src/main/apis/app/window/windowManager.ts @@ -9,36 +9,31 @@ class WindowManager implements IWindowManager { create(name: IWindowList) { const windowConfig: IWindowListItem = windowList.get(name)! - if (windowConfig.isValid) { - if (!windowConfig.multiple) { - if (this.has(name)) return this.#windowMap.get(name)! - } - const window = new BrowserWindow(windowConfig.options()) - const id = window.id - if (windowConfig.multiple) { - this.#windowMap.set(`${name}_${window.id}`, window) - this.#windowIdMap.set(window.id, `${name}_${window.id}`) - } else { - this.#windowMap.set(name, window) - this.#windowIdMap.set(window.id, name) - } - windowConfig.callback(window, this) - window.on('close', () => { - this.deleteById(id) - }) - return window - } else { - return null + if (!windowConfig.isValid) return null + + if (!windowConfig.multiple) { + if (this.has(name)) return this.#windowMap.get(name)! } + + const window = new BrowserWindow(windowConfig.options()) + const id = window.id + const windowName = windowConfig.multiple ? `${name}_${id}` : name + + this.#windowMap.set(windowName, window) + this.#windowIdMap.set(id, windowName) + + windowConfig.callback(window, this) + window.on('close', () => { + this.deleteById(id) + }) + return window } get(name: IWindowList) { if (this.has(name)) { return this.#windowMap.get(name)! - } else { - const window = this.create(name) - return window } + return this.create(name) } has(name: IWindowList) { @@ -57,11 +52,15 @@ class WindowManager implements IWindowManager { const miniWindow = this.#windowMap.get(IWindowList.MINI_WINDOW) if (miniWindow && miniWindow.isVisible() && !isSkipMiniWindow) { return miniWindow - } else { - const settingWindow = this.#windowMap.get(IWindowList.SETTING_WINDOW) - const trayWindow = this.#windowMap.get(IWindowList.TRAY_WINDOW) - return settingWindow || trayWindow || this.create(IWindowList.SETTING_WINDOW)! } + + const settingWindow = this.#windowMap.get(IWindowList.SETTING_WINDOW) + if (settingWindow) return settingWindow + + const trayWindow = this.#windowMap.get(IWindowList.TRAY_WINDOW) + if (trayWindow) return trayWindow + + return this.create(IWindowList.SETTING_WINDOW)! } }