From 9a8b95e356120cb3f538ac5009f592fef90feff9 Mon Sep 17 00:00:00 2001 From: Molunerfinn Date: Wed, 1 Jan 2020 00:47:47 +0800 Subject: [PATCH] :hammer: Refactor: guiApi without creating with webContents --- src/background.ts | 63 +++++++++++++----------------- src/main/utils/busApi/constants.ts | 6 ++- src/main/utils/busApi/index.ts | 27 ++++++++++++- src/main/utils/guiApi.ts | 43 ++++++++++++-------- src/main/utils/picgoCoreIPC.ts | 4 +- src/main/utils/shortKeyHandler.ts | 11 +----- 6 files changed, 89 insertions(+), 65 deletions(-) diff --git a/src/background.ts b/src/background.ts index 1b68007..23a2983 100644 --- a/src/background.ts +++ b/src/background.ts @@ -40,7 +40,11 @@ import { UPLOAD_WITH_FILES, UPLOAD_WITH_FILES_RESPONSE, UPLOAD_WITH_CLIPBOARD_FILES, - UPLOAD_WITH_CLIPBOARD_FILES_RESPONSE + UPLOAD_WITH_CLIPBOARD_FILES_RESPONSE, + GET_WINDOW_ID, + GET_WINDOW_ID_REPONSE, + GET_SETTING_WINDOW_ID, + GET_SETTING_WINDOW_ID_RESPONSE } from '~/main/utils/busApi/constants' import server from '~/main/server/index' @@ -376,12 +380,7 @@ const showWindow = (bounds: IBounds) => { } const uploadClipboardFiles = async (): Promise => { - let win - if (miniWindow && miniWindow!.isVisible()) { - win = miniWindow - } else { - win = settingWindow || window || createSettingWindow() - } + const win = getAvailableWindow() let img = await uploader.setWebContents(win!.webContents).upload() if (img !== false) { if (img.length > 0) { @@ -577,12 +576,7 @@ if (!gotTheLock) { if (files === null) { uploadClipboardFiles() } else { - let win - if (miniWindow && miniWindow.isVisible()) { - win = miniWindow - } else { - win = settingWindow || window || createSettingWindow() - } + const win = getAvailableWindow() uploadChoosedFiles(win.webContents, files) } } else { @@ -639,12 +633,7 @@ app.on('ready', async () => { if (files === null) { uploadClipboardFiles() } else { - let win - if (miniWindow && miniWindow.isVisible()) { - win = miniWindow - } else { - win = settingWindow || window || createSettingWindow() - } + const win = getAvailableWindow() uploadChoosedFiles(win.webContents, files) } } @@ -680,25 +669,24 @@ app.setLoginItemSettings({ function initEventCenter () { const eventList: any = { 'picgo:upload': uploadClipboardFiles, - 'createSettingWindow': shortKeyRequestSettingWindow, - hideMiniWindow, [UPLOAD_WITH_CLIPBOARD_FILES]: busCallUploadClipboardFiles, - [UPLOAD_WITH_FILES]: busCallUploadFiles + [UPLOAD_WITH_FILES]: busCallUploadFiles, + [GET_WINDOW_ID]: busCallGetWindowId, + [GET_SETTING_WINDOW_ID]: busCallGetSettingWindowId } for (let i in eventList) { bus.on(i, eventList[i]) } } -function shortKeyRequestSettingWindow (command: string) { - if (!settingWindow) createSettingWindow() - bus.emit('createSettingWindowDone', command, settingWindow!.id) -} - -function hideMiniWindow () { +function getAvailableWindow () { + let win if (miniWindow && miniWindow.isVisible()) { - miniWindow.hide() + win = miniWindow + } else { + win = settingWindow || window || createSettingWindow() } + return win } async function busCallUploadClipboardFiles () { @@ -707,16 +695,21 @@ async function busCallUploadClipboardFiles () { } async function busCallUploadFiles (pathList: IFileWithPath[]) { - let win - if (miniWindow && miniWindow.isVisible()) { - win = miniWindow - } else { - win = settingWindow || window || createSettingWindow() - } + const win = getAvailableWindow() const urls = await uploadChoosedFiles(win.webContents, pathList) bus.emit(UPLOAD_WITH_FILES_RESPONSE, urls) } +function busCallGetWindowId () { + const win = getAvailableWindow() + bus.emit(GET_WINDOW_ID_REPONSE, win.id) +} + +function busCallGetSettingWindowId () { + if (!settingWindow) createSettingWindow() + bus.emit(GET_SETTING_WINDOW_ID_RESPONSE, settingWindow!.id) +} + // Exit cleanly on request from parent process in development mode. if (isDevelopment) { if (process.platform === 'win32') { diff --git a/src/main/utils/busApi/constants.ts b/src/main/utils/busApi/constants.ts index 445d9c3..d012729 100644 --- a/src/main/utils/busApi/constants.ts +++ b/src/main/utils/busApi/constants.ts @@ -1,5 +1,7 @@ -export const GET_SETTING_WINDOW = 'GET_SETTING_WINDOW' -export const GET_MINI_WINDOW = 'GET_SETTING_WINDOW' +export const GET_WINDOW_ID = 'GET_WINDOW_ID' // get a current window +export const GET_WINDOW_ID_REPONSE = 'GET_WINDOW_ID_REPONSE' +export const GET_SETTING_WINDOW_ID = 'GET_SETTING_WINDOW_ID' // get setting window +export const GET_SETTING_WINDOW_ID_RESPONSE = 'GET_SETTING_WINDOW_ID_RESPONSE' export const UPLOAD_WITH_FILES = 'UPLOAD_WITH_FILES' export const UPLOAD_WITH_FILES_RESPONSE = 'UPLOAD_WITH_FILES_RESPONSE' export const UPLOAD_WITH_CLIPBOARD_FILES = 'UPLOAD_WITH_CLIPBOARD_FILES' diff --git a/src/main/utils/busApi/index.ts b/src/main/utils/busApi/index.ts index 099f720..23df2f1 100644 --- a/src/main/utils/busApi/index.ts +++ b/src/main/utils/busApi/index.ts @@ -3,7 +3,11 @@ import { UPLOAD_WITH_FILES, UPLOAD_WITH_FILES_RESPONSE, UPLOAD_WITH_CLIPBOARD_FILES, - UPLOAD_WITH_CLIPBOARD_FILES_RESPONSE + UPLOAD_WITH_CLIPBOARD_FILES_RESPONSE, + GET_WINDOW_ID, + GET_WINDOW_ID_REPONSE, + GET_SETTING_WINDOW_ID, + GET_SETTING_WINDOW_ID_RESPONSE } from './constants' export const uploadWithClipboardFiles = (): Promise<{ @@ -47,3 +51,24 @@ export const uploadWithFiles = (pathList: IFileWithPath[]): Promise<{ }) }) } + +// get available window id: +// miniWindow or settingWindow or trayWindow +export const getWindowId = (): Promise => { + return new Promise((resolve, reject) => { + bus.emit(GET_WINDOW_ID) + bus.once(GET_WINDOW_ID_REPONSE, (id: number) => { + resolve(id) + }) + }) +} + +// get settingWindow id: +export const getSettingWindowId = (): Promise => { + return new Promise((resolve, reject) => { + bus.emit(GET_SETTING_WINDOW_ID) + bus.once(GET_SETTING_WINDOW_ID_RESPONSE, (id: number) => { + resolve(id) + }) + }) +} diff --git a/src/main/utils/guiApi.ts b/src/main/utils/guiApi.ts index a0fd243..0269611 100644 --- a/src/main/utils/guiApi.ts +++ b/src/main/utils/guiApi.ts @@ -4,21 +4,23 @@ import { clipboard, Notification, WebContents, - ipcMain + ipcMain, + webContents } from 'electron' import db from '#/datastore' import uploader from './uploader' import pasteTemplate from '#/utils/pasteTemplate' -const WEBCONTENTS = Symbol('WEBCONTENTS') +import { + getWindowId, + getSettingWindowId +} from '~/main/utils/busApi' class GuiApi implements IGuiApi { - private [WEBCONTENTS]: WebContents - constructor (webcontents: WebContents) { - this[WEBCONTENTS] = webcontents - } - + private windowId: number = -1 + private settingWindowId: number = -1 private async showSettingWindow () { - const settingWindow = BrowserWindow.fromWebContents(this[WEBCONTENTS]) + this.settingWindowId = await getSettingWindowId() + const settingWindow = BrowserWindow.fromId(this.settingWindowId) if (settingWindow.isVisible()) { return true } @@ -30,12 +32,17 @@ class GuiApi implements IGuiApi { }) } + private getWebcontentsByWindowId (id: number) { + return BrowserWindow.fromId(id).webContents + } + async showInputBox (options: IShowInputBoxOption = { title: '', placeholder: '' }) { await this.showSettingWindow() - this[WEBCONTENTS].send('showInputBox', options) + this.getWebcontentsByWindowId(this.settingWindowId) + .send('showInputBox', options) return new Promise((resolve, reject) => { ipcMain.once('showInputBox', (event: Event, value: string) => { resolve(value) @@ -44,15 +51,18 @@ class GuiApi implements IGuiApi { } showFileExplorer (options: IShowFileExplorerOption = {}) { - return new Promise((resolve, reject) => { - dialog.showOpenDialog(BrowserWindow.fromWebContents(this[WEBCONTENTS]), options, (filename: string) => { + return new Promise(async (resolve, reject) => { + this.windowId = await getWindowId() + dialog.showOpenDialog(BrowserWindow.fromId(this.windowId), options, (filename: string) => { resolve(filename) }) }) } async upload (input: IUploadOption) { - const imgs = await uploader.setWebContents(this[WEBCONTENTS]).upload(input) + this.windowId = await getWindowId() + const webContents = this.getWebcontentsByWindowId(this.windowId) + const imgs = await uploader.setWebContents(webContents).upload(input) if (imgs !== false) { const pasteStyle = db.get('settings.pasteStyle') || 'markdown' let pasteText = '' @@ -69,8 +79,8 @@ class GuiApi implements IGuiApi { db.insert('uploaded', imgs[i]) } clipboard.writeText(pasteText) - this[WEBCONTENTS].send('uploadFiles', imgs) - this[WEBCONTENTS].send('updateGallery') + webContents.send('uploadFiles', imgs) + webContents.send('updateGallery') return imgs } return [] @@ -93,9 +103,10 @@ class GuiApi implements IGuiApi { type: 'info', buttons: ['Yes', 'No'] }) { - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { + this.windowId = await getWindowId() dialog.showMessageBox( - BrowserWindow.fromWebContents(this[WEBCONTENTS]), + BrowserWindow.fromId(this.windowId), options ).then((res) => { resolve({ diff --git a/src/main/utils/picgoCoreIPC.ts b/src/main/utils/picgoCoreIPC.ts index 7b36928..24dea9e 100644 --- a/src/main/utils/picgoCoreIPC.ts +++ b/src/main/utils/picgoCoreIPC.ts @@ -186,7 +186,7 @@ const handleGetPicBedConfig = () => { const handlePluginActions = () => { ipcMain.on('pluginActions', (event: IpcMainEvent, name: string, label: string) => { const plugin = picgo.pluginLoader.getPlugin(`picgo-plugin-${name}`) - const guiApi = new GuiApi(event.sender) + const guiApi = new GuiApi() if (plugin.guiMenu && plugin.guiMenu(picgo).length > 0) { const menu: GuiMenuItem[] = plugin.guiMenu(picgo) menu.forEach(item => { @@ -200,7 +200,7 @@ const handlePluginActions = () => { const handleRemoveFiles = () => { ipcMain.on('removeFiles', (event: IpcMainEvent, files: ImgInfo[]) => { - const guiApi = new GuiApi(event.sender) + const guiApi = new GuiApi() setTimeout(() => { picgo.emit('remove', files, guiApi) }, 500) diff --git a/src/main/utils/shortKeyHandler.ts b/src/main/utils/shortKeyHandler.ts index c9d26a4..7a7a4ef 100644 --- a/src/main/utils/shortKeyHandler.ts +++ b/src/main/utils/shortKeyHandler.ts @@ -124,15 +124,8 @@ class ShortKeyHandler { } else if (command.includes('picgo-plugin-')) { const handler = shortKeyService.getShortKeyHandler(command) if (handler) { - // make sure settingWindow is created - bus.once('createSettingWindowDone', (cmd: string, settingWindowId: number) => { - if (cmd === command) { - const webContents = BrowserWindow.fromId(settingWindowId).webContents - const guiApi = new GuiApi(webContents) - return handler(picgo, guiApi) - } - }) - bus.emit('createSettingWindow', command) + const guiApi = new GuiApi() + return handler(picgo, guiApi) } } else { logger.warn(`can not find command: ${command}`)