mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-01-23 06:38:13 -05:00
🔨 Refactor: guiApi without creating with webContents
This commit is contained in:
parent
81e6acb29b
commit
9a8b95e356
@ -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<string> => {
|
||||
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') {
|
||||
|
@ -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'
|
||||
|
@ -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<number> => {
|
||||
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<number> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
bus.emit(GET_SETTING_WINDOW_ID)
|
||||
bus.once(GET_SETTING_WINDOW_ID_RESPONSE, (id: number) => {
|
||||
resolve(id)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -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<string>((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<string>((resolve, reject) => {
|
||||
dialog.showOpenDialog(BrowserWindow.fromWebContents(this[WEBCONTENTS]), options, (filename: string) => {
|
||||
return new Promise<string>(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<IShowMessageBoxResult>((resolve, reject) => {
|
||||
return new Promise<IShowMessageBoxResult>(async (resolve, reject) => {
|
||||
this.windowId = await getWindowId()
|
||||
dialog.showMessageBox(
|
||||
BrowserWindow.fromWebContents(this[WEBCONTENTS]),
|
||||
BrowserWindow.fromId(this.windowId),
|
||||
options
|
||||
).then((res) => {
|
||||
resolve({
|
||||
|
@ -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)
|
||||
|
@ -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}`)
|
||||
|
Loading…
Reference in New Issue
Block a user