🔨 Refactor(custom): refactored window manager

This commit is contained in:
Kuingsmile 2024-08-01 17:30:59 +08:00
parent eae84cb096
commit dfbbadea46
2 changed files with 32 additions and 50 deletions

View File

@ -233,19 +233,9 @@ windowList.set(IWindowList.RENAME_WINDOW, {
window.loadURL(handleWindowParams(RENAME_WINDOW_URL)) window.loadURL(handleWindowParams(RENAME_WINDOW_URL))
const currentWindow = windowManager.getAvailableWindow(true) const currentWindow = windowManager.getAvailableWindow(true)
if (currentWindow && currentWindow.isVisible()) { if (currentWindow && currentWindow.isVisible()) {
// bounds: { x: 821, y: 75, width: 800, height: 450 } const { x, y, width, height } = currentWindow.getBounds()
const bounds = currentWindow.getBounds() const positionX = Math.floor(x + width / 2 - 150)
let positionX = bounds.x + bounds.width / 2 - 150 const positionY = Math.floor(y + height / 2 - (height > 400 ? 88 : 0))
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)
window.setPosition(positionX, positionY, false) window.setPosition(positionX, positionY, false)
} }
} }
@ -259,16 +249,9 @@ windowList.set(IWindowList.TOOLBOX_WINDOW, {
window.loadURL(TOOLBOX_WINDOW_URL) window.loadURL(TOOLBOX_WINDOW_URL)
const currentWindow = windowManager.getAvailableWindow(true) const currentWindow = windowManager.getAvailableWindow(true)
if (currentWindow && currentWindow.isVisible()) { if (currentWindow && currentWindow.isVisible()) {
const bounds = currentWindow.getBounds() const { x, y, width, height } = currentWindow.getBounds()
let positionX = bounds.x + bounds.width / 2 - 400 const positionX = Math.floor(x + width / 2 - 400)
let positionY const positionY = Math.floor(y + height / 2 - (height > 400 ? 225 : 0))
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)
window.setPosition(positionX, positionY, false) window.setPosition(positionX, positionY, false)
} }
} }

View File

@ -9,36 +9,31 @@ class WindowManager implements IWindowManager {
create(name: IWindowList) { create(name: IWindowList) {
const windowConfig: IWindowListItem = windowList.get(name)! const windowConfig: IWindowListItem = windowList.get(name)!
if (windowConfig.isValid) { if (!windowConfig.isValid) return null
if (!windowConfig.multiple) { if (!windowConfig.multiple) {
if (this.has(name)) return this.#windowMap.get(name)! if (this.has(name)) return this.#windowMap.get(name)!
} }
const window = new BrowserWindow(windowConfig.options()) const window = new BrowserWindow(windowConfig.options())
const id = window.id const id = window.id
if (windowConfig.multiple) { const windowName = windowConfig.multiple ? `${name}_${id}` : name
this.#windowMap.set(`${name}_${window.id}`, window)
this.#windowIdMap.set(window.id, `${name}_${window.id}`) this.#windowMap.set(windowName, window)
} else { this.#windowIdMap.set(id, windowName)
this.#windowMap.set(name, window)
this.#windowIdMap.set(window.id, name)
}
windowConfig.callback(window, this) windowConfig.callback(window, this)
window.on('close', () => { window.on('close', () => {
this.deleteById(id) this.deleteById(id)
}) })
return window return window
} else {
return null
}
} }
get(name: IWindowList) { get(name: IWindowList) {
if (this.has(name)) { if (this.has(name)) {
return this.#windowMap.get(name)! return this.#windowMap.get(name)!
} else {
const window = this.create(name)
return window
} }
return this.create(name)
} }
has(name: IWindowList) { has(name: IWindowList) {
@ -57,11 +52,15 @@ class WindowManager implements IWindowManager {
const miniWindow = this.#windowMap.get(IWindowList.MINI_WINDOW) const miniWindow = this.#windowMap.get(IWindowList.MINI_WINDOW)
if (miniWindow && miniWindow.isVisible() && !isSkipMiniWindow) { if (miniWindow && miniWindow.isVisible() && !isSkipMiniWindow) {
return miniWindow 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)!
} }
} }