🔨 Refactor: windowManager

This commit is contained in:
Molunerfinn 2020-04-12 22:34:45 +08:00
parent 7e866185e9
commit b5ceaeda1e
9 changed files with 40 additions and 58 deletions

View File

@ -1,21 +1,18 @@
import bus from '@core/bus' import bus from '@core/bus'
import PicGoCore from '~/universal/types/picgo'
import path from 'path'
import { import {
app, globalShortcut
globalShortcut,
BrowserWindow
} from 'electron' } from 'electron'
import logger from '@core/picgo/logger' import logger from '@core/picgo/logger'
import GuiApi from '../../gui' import GuiApi from '../../gui'
import db from '#/datastore' import db from '#/datastore'
import { TOGGLE_SHORTKEY_MODIFIED_MODE } from '#/events/constants'
import shortKeyService from './shortKeyService' import shortKeyService from './shortKeyService'
import picgo from '@core/picgo' import picgo from '@core/picgo'
class ShortKeyHandler { class ShortKeyHandler {
private isInModifiedMode: boolean = false private isInModifiedMode: boolean = false
constructor () { constructor () {
bus.on('toggleShortKeyModifiedMode', flag => { bus.on(TOGGLE_SHORTKEY_MODIFIED_MODE, flag => {
this.isInModifiedMode = flag this.isInModifiedMode = flag
}) })
} }

View File

@ -22,6 +22,3 @@ export const MINI_WINDOW_URL = isDevelopment
export const RENAME_WINDOW_URL = process.env.NODE_ENV === 'development' export const RENAME_WINDOW_URL = process.env.NODE_ENV === 'development'
? `${(process.env.WEBPACK_DEV_SERVER_URL as string)}#rename-page` ? `${(process.env.WEBPACK_DEV_SERVER_URL as string)}#rename-page`
: `picgo://./index.html#rename-page` : `picgo://./index.html#rename-page`
export const DELETE_WINDOW_EVENT = 'DELETE_WINDOW_EVENT'
export const CREATE_WINDOW_EVENT = 'CREATE_WINDOW_EVENT'

View File

@ -1,7 +1,5 @@
import { import {
IWindowList, IWindowList,
DELETE_WINDOW_EVENT,
CREATE_WINDOW_EVENT,
SETTING_WINDOW_URL, SETTING_WINDOW_URL,
TRAY_WINDOW_URL, TRAY_WINDOW_URL,
MINI_WINDOW_URL, MINI_WINDOW_URL,
@ -9,10 +7,10 @@ import {
} from './constants' } from './constants'
import { IWindowListItem } from '#/types/electron' import { IWindowListItem } from '#/types/electron'
import bus from '@core/bus' import bus from '@core/bus'
import db from '#/datastore'
import { getWindowId } from '@core/bus/apis'
import { BrowserWindow, app } from 'electron'
import { CREATE_APP_MENU } from '@core/bus/constants' import { CREATE_APP_MENU } from '@core/bus/constants'
import db from '#/datastore'
import { TOGGLE_SHORTKEY_MODIFIED_MODE } from '#/events/constants'
import { app } from 'electron'
const windowList = new Map<IWindowList, IWindowListItem>() const windowList = new Map<IWindowList, IWindowListItem>()
@ -37,12 +35,7 @@ windowList.set(IWindowList.TRAY_WINDOW, {
} }
}, },
callback (window) { callback (window) {
const id = window.id
window.loadURL(TRAY_WINDOW_URL) window.loadURL(TRAY_WINDOW_URL)
window.on('closed', () => {
bus.emit(DELETE_WINDOW_EVENT, id)
})
window.on('blur', () => { window.on('blur', () => {
window.hide() window.hide()
}) })
@ -81,12 +74,10 @@ windowList.set(IWindowList.SETTING_WINDOW, {
} }
return options return options
}, },
callback (window) { callback (window, windowManager) {
const id = window.id
window.loadURL(SETTING_WINDOW_URL) window.loadURL(SETTING_WINDOW_URL)
window.on('closed', () => { window.on('closed', () => {
bus.emit('toggleShortKeyModifiedMode', false) bus.emit(TOGGLE_SHORTKEY_MODIFIED_MODE, false)
bus.emit(DELETE_WINDOW_EVENT, id)
if (process.platform === 'linux') { if (process.platform === 'linux') {
process.nextTick(() => { process.nextTick(() => {
app.quit() app.quit()
@ -94,7 +85,7 @@ windowList.set(IWindowList.SETTING_WINDOW, {
} }
}) })
bus.emit(CREATE_APP_MENU) bus.emit(CREATE_APP_MENU)
bus.emit(CREATE_WINDOW_EVENT, IWindowList.MINI_WINDOW) windowManager.create(IWindowList.MINI_WINDOW)
} }
}) })
@ -125,11 +116,7 @@ windowList.set(IWindowList.MINI_WINDOW, {
return obj return obj
}, },
callback (window) { callback (window) {
const id = window.id
window.loadURL(MINI_WINDOW_URL) window.loadURL(MINI_WINDOW_URL)
window.on('closed', () => {
bus.emit(DELETE_WINDOW_EVENT, id)
})
} }
}) })
@ -158,10 +145,9 @@ windowList.set(IWindowList.RENAME_WINDOW, {
} }
return options return options
}, },
async callback (window) { async callback (window, windowManager) {
window.loadURL(RENAME_WINDOW_URL) window.loadURL(RENAME_WINDOW_URL)
const currentWindowId = await getWindowId() const currentWindow = windowManager.getAvailableWindow()
const currentWindow = BrowserWindow.fromId(currentWindowId)
if (currentWindow && currentWindow.isVisible()) { if (currentWindow && currentWindow.isVisible()) {
// bounds: { x: 821, y: 75, width: 800, height: 450 } // bounds: { x: 821, y: 75, width: 800, height: 450 }
const bounds = currentWindow.getBounds() const bounds = currentWindow.getBounds()

View File

@ -4,26 +4,20 @@ import {
import { IWindowManager, IWindowListItem } from '#/types/electron' import { IWindowManager, IWindowListItem } from '#/types/electron'
import windowList from './windowList' import windowList from './windowList'
import { import {
IWindowList, IWindowList
DELETE_WINDOW_EVENT,
CREATE_WINDOW_EVENT
} from './constants' } from './constants'
import bus from '@core/bus'
class WindowManager implements IWindowManager { class WindowManager implements IWindowManager {
private windowMap: Map<IWindowList | string, BrowserWindow> = new Map() private windowMap: Map<IWindowList | string, BrowserWindow> = new Map()
private windowIdMap: Map<number, IWindowList | string> = new Map() private windowIdMap: Map<number, IWindowList | string> = new Map()
constructor () { create (name: IWindowList) {
bus.on(DELETE_WINDOW_EVENT, this.deleteById)
bus.on(CREATE_WINDOW_EVENT, this.create)
}
create = (name: IWindowList) => {
const windowConfig: IWindowListItem = windowList.get(name)! const windowConfig: IWindowListItem = windowList.get(name)!
if (windowConfig.isValid) { if (windowConfig.isValid) {
if (!windowConfig.multiple) { if (!windowConfig.multiple) {
if (this.windowMap.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
if (windowConfig.multiple) { if (windowConfig.multiple) {
this.windowMap.set(`${name}_${window.id}`, window) this.windowMap.set(`${name}_${window.id}`, window)
this.windowIdMap.set(window.id, `${name}_${window.id}`) this.windowIdMap.set(window.id, `${name}_${window.id}`)
@ -31,14 +25,17 @@ class WindowManager implements IWindowManager {
this.windowMap.set(name, window) this.windowMap.set(name, window)
this.windowIdMap.set(window.id, name) this.windowIdMap.set(window.id, name)
} }
windowConfig.callback(window) windowConfig.callback(window, this)
window.on('close', () => {
this.deleteById(id)
})
return window return window
} else { } else {
return null return null
} }
} }
get (name: IWindowList) { get (name: IWindowList) {
if (this.windowMap.has(name)) { if (this.has(name)) {
return this.windowMap.get(name)! return this.windowMap.get(name)!
} else { } else {
const window = this.create(name) const window = this.create(name)
@ -48,13 +45,14 @@ class WindowManager implements IWindowManager {
has (name: IWindowList) { has (name: IWindowList) {
return this.windowMap.has(name) return this.windowMap.has(name)
} }
delete = (name: IWindowList) => { // useless
const window = this.windowMap.get(name) // delete (name: IWindowList) {
if (window) { // const window = this.windowMap.get(name)
this.windowIdMap.delete(window.id) // if (window) {
this.windowMap.delete(name) // this.windowIdMap.delete(window.id)
} // this.windowMap.delete(name)
} // }
// }
deleteById = (id: number) => { deleteById = (id: number) => {
const name = this.windowIdMap.get(id) const name = this.windowIdMap.get(id)
if (name) { if (name) {

View File

@ -1,7 +1,6 @@
import { import {
dialog, dialog,
BrowserWindow, BrowserWindow,
clipboard,
Notification, Notification,
ipcMain ipcMain
} from 'electron' } from 'electron'
@ -17,6 +16,7 @@ import {
SHOW_INPUT_BOX SHOW_INPUT_BOX
} from '~/universal/events/constants' } from '~/universal/events/constants'
// Cross-process support may be required in the future
class GuiApi implements IGuiApi { class GuiApi implements IGuiApi {
private windowId: number = -1 private windowId: number = -1
private settingWindowId: number = -1 private settingWindowId: number = -1

View File

@ -1,7 +1,6 @@
import { import {
app, app,
ipcMain, ipcMain,
clipboard,
Notification, Notification,
IpcMainEvent IpcMainEvent
} from 'electron' } from 'electron'
@ -14,6 +13,7 @@ import server from '~/main/server'
import getPicBeds from '~/main/utils/getPicBeds' import getPicBeds from '~/main/utils/getPicBeds'
import shortKeyHandler from 'apis/app/shortKey/shortKeyHandler' import shortKeyHandler from 'apis/app/shortKey/shortKeyHandler'
import bus from '@core/bus' import bus from '@core/bus'
import { TOGGLE_SHORTKEY_MODIFIED_MODE } from '#/events/constants'
import { import {
uploadClipboardFiles, uploadClipboardFiles,
uploadChoosedFiles uploadChoosedFiles
@ -132,8 +132,8 @@ export default {
evt.returnValue = picBeds evt.returnValue = picBeds
}) })
ipcMain.on('toggleShortKeyModifiedMode', (evt: IpcMainEvent, val: boolean) => { ipcMain.on(TOGGLE_SHORTKEY_MODIFIED_MODE, (evt: IpcMainEvent, val: boolean) => {
bus.emit('toggleShortKeyModifiedMode', val) bus.emit(TOGGLE_SHORTKEY_MODIFIED_MODE, val)
}) })
ipcMain.on('updateServer', () => { ipcMain.on('updateServer', () => {

View File

@ -98,6 +98,7 @@
import { Component, Vue, Watch } from 'vue-property-decorator' import { Component, Vue, Watch } from 'vue-property-decorator'
import keyDetect from '@/utils/key-binding' import keyDetect from '@/utils/key-binding'
import { ipcRenderer, IpcRendererEvent } from 'electron' import { ipcRenderer, IpcRendererEvent } from 'electron'
import { TOGGLE_SHORTKEY_MODIFIED_MODE } from '#/events/constants'
@Component({ @Component({
name: 'shortkey-page' name: 'shortkey-page'
@ -119,7 +120,7 @@ export default class extends Vue {
} }
@Watch('keyBindingVisible') @Watch('keyBindingVisible')
onKeyBindingVisibleChange (val: boolean) { onKeyBindingVisibleChange (val: boolean) {
ipcRenderer.send('toggleShortKeyModifiedMode', val) ipcRenderer.send(TOGGLE_SHORTKEY_MODIFIED_MODE, val)
} }
calcOrigin (item: string) { calcOrigin (item: string) {
const [origin] = item.split(':') const [origin] = item.split(':')
@ -162,7 +163,7 @@ export default class extends Vue {
}) })
} }
beforeDestroy () { beforeDestroy () {
ipcRenderer.send('toggleShortKeyModifiedMode', false) ipcRenderer.send(TOGGLE_SHORTKEY_MODIFIED_MODE, false)
} }
} }
</script> </script>

View File

@ -1,2 +1,3 @@
export const SHOW_INPUT_BOX = 'SHOW_INPUT_BOX' export const SHOW_INPUT_BOX = 'SHOW_INPUT_BOX'
export const SHOW_INPUT_BOX_RESPONSE = 'SHOW_INPUT_BOX_RESPONSE' export const SHOW_INPUT_BOX_RESPONSE = 'SHOW_INPUT_BOX_RESPONSE'
export const TOGGLE_SHORTKEY_MODIFIED_MODE = 'TOGGLE_SHORTKEY_MODIFIED_MODE'

View File

@ -9,12 +9,14 @@ declare interface IWindowListItem {
isValid: boolean isValid: boolean
multiple: boolean multiple: boolean
options: () => IBrowserWindowOptions, options: () => IBrowserWindowOptions,
callback: (window: BrowserWindow) => void callback: (window: BrowserWindow, windowManager: IWindowManager) => void
} }
declare interface IWindowManager { declare interface IWindowManager {
create: (name: IWindowList) => BrowserWindow | null create: (name: IWindowList) => BrowserWindow | null
get: (name: IWindowList) => BrowserWindow | null get: (name: IWindowList) => BrowserWindow | null
delete: (name: IWindowList) => void has: (name: IWindowList) => boolean
// delete: (name: IWindowList) => void
deleteById: (id: number) => void
getAvailableWindow: () => BrowserWindow getAvailableWindow: () => BrowserWindow
} }