🔨 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))
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)
}
}

View File

@ -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)!
}
}