Skip to content

Commit

Permalink
🔨 Refactor: move guiApi to singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
Molunerfinn committed Jul 9, 2021
1 parent 06b67e5 commit 8e5e9ec
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/main/apis/app/shortKey/shortKeyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ class ShortKeyHandler {
} else if (command.includes('picgo-plugin-')) {
const handler = shortKeyService.getShortKeyHandler(command)
if (handler) {
const guiApi = new GuiApi()
return handler(picgo, guiApi)
return handler(picgo, GuiApi.getInstance())
}
} else {
logger.warn(`can not find command: ${command}`)
Expand Down
10 changes: 10 additions & 0 deletions src/main/apis/gui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@ import {

// Cross-process support may be required in the future
class GuiApi implements IGuiApi {
private static instance: GuiApi
private windowId: number = -1
private settingWindowId: number = -1
private constructor () {
console.log('init guiapi')
}
public static getInstance (): GuiApi {
if (!GuiApi.instance) {
GuiApi.instance = new GuiApi()
}
return GuiApi.instance
}
private async showSettingWindow () {
this.settingWindowId = await getSettingWindowId()
const settingWindow = BrowserWindow.fromId(this.settingWindowId)
Expand Down
6 changes: 2 additions & 4 deletions src/main/events/picgoCoreIPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,11 @@ const handleGetPicBedConfig = () => {
const handlePluginActions = () => {
ipcMain.on('pluginActions', (event: IpcMainEvent, name: string, label: string) => {
const plugin = picgo.pluginLoader.getPlugin(name)
const guiApi = new GuiApi()
if (plugin?.guiMenu?.(picgo)?.length) {
const menu: GuiMenuItem[] = plugin.guiMenu(picgo)
menu.forEach(item => {
if (item.label === label) {
item.handle(picgo, guiApi)
item.handle(picgo, GuiApi.getInstance())
}
})
}
Expand All @@ -224,9 +223,8 @@ const handlePluginActions = () => {

const handleRemoveFiles = () => {
ipcMain.on('removeFiles', (event: IpcMainEvent, files: ImgInfo[]) => {
const guiApi = new GuiApi()
setTimeout(() => {
picgo.emit('remove', files, guiApi)
picgo.emit('remove', files, GuiApi.getInstance())
}, 500)
})
}
Expand Down

0 comments on commit 8e5e9ec

Please sign in to comment.