mirror of
https://github.com/Kuingsmile/PicList.git
synced 2025-02-02 11:08: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,
|
||||||
UPLOAD_WITH_FILES_RESPONSE,
|
UPLOAD_WITH_FILES_RESPONSE,
|
||||||
UPLOAD_WITH_CLIPBOARD_FILES,
|
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'
|
} from '~/main/utils/busApi/constants'
|
||||||
import server from '~/main/server/index'
|
import server from '~/main/server/index'
|
||||||
|
|
||||||
@ -376,12 +380,7 @@ const showWindow = (bounds: IBounds) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const uploadClipboardFiles = async (): Promise<string> => {
|
const uploadClipboardFiles = async (): Promise<string> => {
|
||||||
let win
|
const win = getAvailableWindow()
|
||||||
if (miniWindow && miniWindow!.isVisible()) {
|
|
||||||
win = miniWindow
|
|
||||||
} else {
|
|
||||||
win = settingWindow || window || createSettingWindow()
|
|
||||||
}
|
|
||||||
let img = await uploader.setWebContents(win!.webContents).upload()
|
let img = await uploader.setWebContents(win!.webContents).upload()
|
||||||
if (img !== false) {
|
if (img !== false) {
|
||||||
if (img.length > 0) {
|
if (img.length > 0) {
|
||||||
@ -577,12 +576,7 @@ if (!gotTheLock) {
|
|||||||
if (files === null) {
|
if (files === null) {
|
||||||
uploadClipboardFiles()
|
uploadClipboardFiles()
|
||||||
} else {
|
} else {
|
||||||
let win
|
const win = getAvailableWindow()
|
||||||
if (miniWindow && miniWindow.isVisible()) {
|
|
||||||
win = miniWindow
|
|
||||||
} else {
|
|
||||||
win = settingWindow || window || createSettingWindow()
|
|
||||||
}
|
|
||||||
uploadChoosedFiles(win.webContents, files)
|
uploadChoosedFiles(win.webContents, files)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -639,12 +633,7 @@ app.on('ready', async () => {
|
|||||||
if (files === null) {
|
if (files === null) {
|
||||||
uploadClipboardFiles()
|
uploadClipboardFiles()
|
||||||
} else {
|
} else {
|
||||||
let win
|
const win = getAvailableWindow()
|
||||||
if (miniWindow && miniWindow.isVisible()) {
|
|
||||||
win = miniWindow
|
|
||||||
} else {
|
|
||||||
win = settingWindow || window || createSettingWindow()
|
|
||||||
}
|
|
||||||
uploadChoosedFiles(win.webContents, files)
|
uploadChoosedFiles(win.webContents, files)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -680,25 +669,24 @@ app.setLoginItemSettings({
|
|||||||
function initEventCenter () {
|
function initEventCenter () {
|
||||||
const eventList: any = {
|
const eventList: any = {
|
||||||
'picgo:upload': uploadClipboardFiles,
|
'picgo:upload': uploadClipboardFiles,
|
||||||
'createSettingWindow': shortKeyRequestSettingWindow,
|
|
||||||
hideMiniWindow,
|
|
||||||
[UPLOAD_WITH_CLIPBOARD_FILES]: busCallUploadClipboardFiles,
|
[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) {
|
for (let i in eventList) {
|
||||||
bus.on(i, eventList[i])
|
bus.on(i, eventList[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function shortKeyRequestSettingWindow (command: string) {
|
function getAvailableWindow () {
|
||||||
if (!settingWindow) createSettingWindow()
|
let win
|
||||||
bus.emit('createSettingWindowDone', command, settingWindow!.id)
|
|
||||||
}
|
|
||||||
|
|
||||||
function hideMiniWindow () {
|
|
||||||
if (miniWindow && miniWindow.isVisible()) {
|
if (miniWindow && miniWindow.isVisible()) {
|
||||||
miniWindow.hide()
|
win = miniWindow
|
||||||
|
} else {
|
||||||
|
win = settingWindow || window || createSettingWindow()
|
||||||
}
|
}
|
||||||
|
return win
|
||||||
}
|
}
|
||||||
|
|
||||||
async function busCallUploadClipboardFiles () {
|
async function busCallUploadClipboardFiles () {
|
||||||
@ -707,16 +695,21 @@ async function busCallUploadClipboardFiles () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function busCallUploadFiles (pathList: IFileWithPath[]) {
|
async function busCallUploadFiles (pathList: IFileWithPath[]) {
|
||||||
let win
|
const win = getAvailableWindow()
|
||||||
if (miniWindow && miniWindow.isVisible()) {
|
|
||||||
win = miniWindow
|
|
||||||
} else {
|
|
||||||
win = settingWindow || window || createSettingWindow()
|
|
||||||
}
|
|
||||||
const urls = await uploadChoosedFiles(win.webContents, pathList)
|
const urls = await uploadChoosedFiles(win.webContents, pathList)
|
||||||
bus.emit(UPLOAD_WITH_FILES_RESPONSE, urls)
|
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.
|
// Exit cleanly on request from parent process in development mode.
|
||||||
if (isDevelopment) {
|
if (isDevelopment) {
|
||||||
if (process.platform === 'win32') {
|
if (process.platform === 'win32') {
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
export const GET_SETTING_WINDOW = 'GET_SETTING_WINDOW'
|
export const GET_WINDOW_ID = 'GET_WINDOW_ID' // get a current window
|
||||||
export const GET_MINI_WINDOW = 'GET_SETTING_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 = 'UPLOAD_WITH_FILES'
|
||||||
export const UPLOAD_WITH_FILES_RESPONSE = 'UPLOAD_WITH_FILES_RESPONSE'
|
export const UPLOAD_WITH_FILES_RESPONSE = 'UPLOAD_WITH_FILES_RESPONSE'
|
||||||
export const UPLOAD_WITH_CLIPBOARD_FILES = 'UPLOAD_WITH_CLIPBOARD_FILES'
|
export const UPLOAD_WITH_CLIPBOARD_FILES = 'UPLOAD_WITH_CLIPBOARD_FILES'
|
||||||
|
@ -3,7 +3,11 @@ import {
|
|||||||
UPLOAD_WITH_FILES,
|
UPLOAD_WITH_FILES,
|
||||||
UPLOAD_WITH_FILES_RESPONSE,
|
UPLOAD_WITH_FILES_RESPONSE,
|
||||||
UPLOAD_WITH_CLIPBOARD_FILES,
|
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'
|
} from './constants'
|
||||||
|
|
||||||
export const uploadWithClipboardFiles = (): Promise<{
|
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,
|
clipboard,
|
||||||
Notification,
|
Notification,
|
||||||
WebContents,
|
WebContents,
|
||||||
ipcMain
|
ipcMain,
|
||||||
|
webContents
|
||||||
} from 'electron'
|
} from 'electron'
|
||||||
import db from '#/datastore'
|
import db from '#/datastore'
|
||||||
import uploader from './uploader'
|
import uploader from './uploader'
|
||||||
import pasteTemplate from '#/utils/pasteTemplate'
|
import pasteTemplate from '#/utils/pasteTemplate'
|
||||||
const WEBCONTENTS = Symbol('WEBCONTENTS')
|
import {
|
||||||
|
getWindowId,
|
||||||
|
getSettingWindowId
|
||||||
|
} from '~/main/utils/busApi'
|
||||||
|
|
||||||
class GuiApi implements IGuiApi {
|
class GuiApi implements IGuiApi {
|
||||||
private [WEBCONTENTS]: WebContents
|
private windowId: number = -1
|
||||||
constructor (webcontents: WebContents) {
|
private settingWindowId: number = -1
|
||||||
this[WEBCONTENTS] = webcontents
|
|
||||||
}
|
|
||||||
|
|
||||||
private async showSettingWindow () {
|
private async showSettingWindow () {
|
||||||
const settingWindow = BrowserWindow.fromWebContents(this[WEBCONTENTS])
|
this.settingWindowId = await getSettingWindowId()
|
||||||
|
const settingWindow = BrowserWindow.fromId(this.settingWindowId)
|
||||||
if (settingWindow.isVisible()) {
|
if (settingWindow.isVisible()) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -30,12 +32,17 @@ class GuiApi implements IGuiApi {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getWebcontentsByWindowId (id: number) {
|
||||||
|
return BrowserWindow.fromId(id).webContents
|
||||||
|
}
|
||||||
|
|
||||||
async showInputBox (options: IShowInputBoxOption = {
|
async showInputBox (options: IShowInputBoxOption = {
|
||||||
title: '',
|
title: '',
|
||||||
placeholder: ''
|
placeholder: ''
|
||||||
}) {
|
}) {
|
||||||
await this.showSettingWindow()
|
await this.showSettingWindow()
|
||||||
this[WEBCONTENTS].send('showInputBox', options)
|
this.getWebcontentsByWindowId(this.settingWindowId)
|
||||||
|
.send('showInputBox', options)
|
||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<string>((resolve, reject) => {
|
||||||
ipcMain.once('showInputBox', (event: Event, value: string) => {
|
ipcMain.once('showInputBox', (event: Event, value: string) => {
|
||||||
resolve(value)
|
resolve(value)
|
||||||
@ -44,15 +51,18 @@ class GuiApi implements IGuiApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
showFileExplorer (options: IShowFileExplorerOption = {}) {
|
showFileExplorer (options: IShowFileExplorerOption = {}) {
|
||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<string>(async (resolve, reject) => {
|
||||||
dialog.showOpenDialog(BrowserWindow.fromWebContents(this[WEBCONTENTS]), options, (filename: string) => {
|
this.windowId = await getWindowId()
|
||||||
|
dialog.showOpenDialog(BrowserWindow.fromId(this.windowId), options, (filename: string) => {
|
||||||
resolve(filename)
|
resolve(filename)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async upload (input: IUploadOption) {
|
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) {
|
if (imgs !== false) {
|
||||||
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
|
const pasteStyle = db.get('settings.pasteStyle') || 'markdown'
|
||||||
let pasteText = ''
|
let pasteText = ''
|
||||||
@ -69,8 +79,8 @@ class GuiApi implements IGuiApi {
|
|||||||
db.insert('uploaded', imgs[i])
|
db.insert('uploaded', imgs[i])
|
||||||
}
|
}
|
||||||
clipboard.writeText(pasteText)
|
clipboard.writeText(pasteText)
|
||||||
this[WEBCONTENTS].send('uploadFiles', imgs)
|
webContents.send('uploadFiles', imgs)
|
||||||
this[WEBCONTENTS].send('updateGallery')
|
webContents.send('updateGallery')
|
||||||
return imgs
|
return imgs
|
||||||
}
|
}
|
||||||
return []
|
return []
|
||||||
@ -93,9 +103,10 @@ class GuiApi implements IGuiApi {
|
|||||||
type: 'info',
|
type: 'info',
|
||||||
buttons: ['Yes', 'No']
|
buttons: ['Yes', 'No']
|
||||||
}) {
|
}) {
|
||||||
return new Promise<IShowMessageBoxResult>((resolve, reject) => {
|
return new Promise<IShowMessageBoxResult>(async (resolve, reject) => {
|
||||||
|
this.windowId = await getWindowId()
|
||||||
dialog.showMessageBox(
|
dialog.showMessageBox(
|
||||||
BrowserWindow.fromWebContents(this[WEBCONTENTS]),
|
BrowserWindow.fromId(this.windowId),
|
||||||
options
|
options
|
||||||
).then((res) => {
|
).then((res) => {
|
||||||
resolve({
|
resolve({
|
||||||
|
@ -186,7 +186,7 @@ const handleGetPicBedConfig = () => {
|
|||||||
const handlePluginActions = () => {
|
const handlePluginActions = () => {
|
||||||
ipcMain.on('pluginActions', (event: IpcMainEvent, name: string, label: string) => {
|
ipcMain.on('pluginActions', (event: IpcMainEvent, name: string, label: string) => {
|
||||||
const plugin = picgo.pluginLoader.getPlugin(`picgo-plugin-${name}`)
|
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) {
|
if (plugin.guiMenu && plugin.guiMenu(picgo).length > 0) {
|
||||||
const menu: GuiMenuItem[] = plugin.guiMenu(picgo)
|
const menu: GuiMenuItem[] = plugin.guiMenu(picgo)
|
||||||
menu.forEach(item => {
|
menu.forEach(item => {
|
||||||
@ -200,7 +200,7 @@ const handlePluginActions = () => {
|
|||||||
|
|
||||||
const handleRemoveFiles = () => {
|
const handleRemoveFiles = () => {
|
||||||
ipcMain.on('removeFiles', (event: IpcMainEvent, files: ImgInfo[]) => {
|
ipcMain.on('removeFiles', (event: IpcMainEvent, files: ImgInfo[]) => {
|
||||||
const guiApi = new GuiApi(event.sender)
|
const guiApi = new GuiApi()
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
picgo.emit('remove', files, guiApi)
|
picgo.emit('remove', files, guiApi)
|
||||||
}, 500)
|
}, 500)
|
||||||
|
@ -124,15 +124,8 @@ class ShortKeyHandler {
|
|||||||
} else if (command.includes('picgo-plugin-')) {
|
} else if (command.includes('picgo-plugin-')) {
|
||||||
const handler = shortKeyService.getShortKeyHandler(command)
|
const handler = shortKeyService.getShortKeyHandler(command)
|
||||||
if (handler) {
|
if (handler) {
|
||||||
// make sure settingWindow is created
|
const guiApi = new GuiApi()
|
||||||
bus.once('createSettingWindowDone', (cmd: string, settingWindowId: number) => {
|
return handler(picgo, guiApi)
|
||||||
if (cmd === command) {
|
|
||||||
const webContents = BrowserWindow.fromId(settingWindowId).webContents
|
|
||||||
const guiApi = new GuiApi(webContents)
|
|
||||||
return handler(picgo, guiApi)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
bus.emit('createSettingWindow', command)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logger.warn(`can not find command: ${command}`)
|
logger.warn(`can not find command: ${command}`)
|
||||||
|
Loading…
Reference in New Issue
Block a user