🔨 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 PicGoCore from '~/universal/types/picgo'
import path from 'path'
import {
app,
globalShortcut,
BrowserWindow
globalShortcut
} from 'electron'
import logger from '@core/picgo/logger'
import GuiApi from '../../gui'
import db from '#/datastore'
import { TOGGLE_SHORTKEY_MODIFIED_MODE } from '#/events/constants'
import shortKeyService from './shortKeyService'
import picgo from '@core/picgo'
class ShortKeyHandler {
private isInModifiedMode: boolean = false
constructor () {
bus.on('toggleShortKeyModifiedMode', flag => {
bus.on(TOGGLE_SHORTKEY_MODIFIED_MODE, 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'
? `${(process.env.WEBPACK_DEV_SERVER_URL as string)}#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 {
IWindowList,
DELETE_WINDOW_EVENT,
CREATE_WINDOW_EVENT,
SETTING_WINDOW_URL,
TRAY_WINDOW_URL,
MINI_WINDOW_URL,
@ -9,10 +7,10 @@ import {
} from './constants'
import { IWindowListItem } from '#/types/electron'
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 db from '#/datastore'
import { TOGGLE_SHORTKEY_MODIFIED_MODE } from '#/events/constants'
import { app } from 'electron'
const windowList = new Map<IWindowList, IWindowListItem>()
@ -37,12 +35,7 @@ windowList.set(IWindowList.TRAY_WINDOW, {
}
},
callback (window) {
const id = window.id
window.loadURL(TRAY_WINDOW_URL)
window.on('closed', () => {
bus.emit(DELETE_WINDOW_EVENT, id)
})
window.on('blur', () => {
window.hide()
})
@ -81,12 +74,10 @@ windowList.set(IWindowList.SETTING_WINDOW, {
}
return options
},
callback (window) {
const id = window.id
callback (window, windowManager) {
window.loadURL(SETTING_WINDOW_URL)
window.on('closed', () => {
bus.emit('toggleShortKeyModifiedMode', false)
bus.emit(DELETE_WINDOW_EVENT, id)
bus.emit(TOGGLE_SHORTKEY_MODIFIED_MODE, false)
if (process.platform === 'linux') {
process.nextTick(() => {
app.quit()
@ -94,7 +85,7 @@ windowList.set(IWindowList.SETTING_WINDOW, {
}
})
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
},
callback (window) {
const id = window.id
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
},
async callback (window) {
async callback (window, windowManager) {
window.loadURL(RENAME_WINDOW_URL)
const currentWindowId = await getWindowId()
const currentWindow = BrowserWindow.fromId(currentWindowId)
const currentWindow = windowManager.getAvailableWindow()
if (currentWindow && currentWindow.isVisible()) {
// bounds: { x: 821, y: 75, width: 800, height: 450 }
const bounds = currentWindow.getBounds()

View File

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

View File

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

View File

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

View File

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

View File

@ -1,2 +1,3 @@
export const SHOW_INPUT_BOX = 'SHOW_INPUT_BOX'
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
multiple: boolean
options: () => IBrowserWindowOptions,
callback: (window: BrowserWindow) => void
callback: (window: BrowserWindow, windowManager: IWindowManager) => void
}
declare interface IWindowManager {
create: (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
}