From c8ea16e661bc4ae74e50e4b1bbd01a9e7961b06f Mon Sep 17 00:00:00 2001 From: Louis Date: Mon, 11 Mar 2024 22:58:18 +0700 Subject: [PATCH] fix: quick ask blocks app update (#2310) --- electron/handlers/update.ts | 2 ++ electron/main.ts | 31 +++++++++++++++++++++--------- electron/managers/tray.ts | 38 +++++++++++++++++++++++++++++++++++++ electron/utils/tray.ts | 24 ----------------------- 4 files changed, 62 insertions(+), 33 deletions(-) create mode 100644 electron/managers/tray.ts delete mode 100644 electron/utils/tray.ts diff --git a/electron/handlers/update.ts b/electron/handlers/update.ts index 3f52c401e..5ea261e54 100644 --- a/electron/handlers/update.ts +++ b/electron/handlers/update.ts @@ -7,6 +7,7 @@ import { autoUpdater, } from 'electron-updater' import { AppEvent } from '@janhq/core' +import { trayManager } from '../managers/tray' export let waitingToInstallVersion: string | undefined = undefined @@ -22,6 +23,7 @@ export function handleAppUpdates() { message: 'Would you like to download and install it now?', buttons: ['Download', 'Later'], }) + trayManager.destroyCurrentTray() if (action.response === 0) await autoUpdater.downloadUpdate() }) diff --git a/electron/main.ts b/electron/main.ts index 8fe4247d6..e72c7f95a 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -1,4 +1,4 @@ -import { app, BrowserWindow, Menu, Tray } from 'electron' +import { app, BrowserWindow, Tray } from 'electron' import { join } from 'path' /** @@ -27,7 +27,7 @@ import { setupReactDevTool } from './utils/dev' import { cleanLogs } from './utils/log' import { registerShortcut } from './utils/selectedText' -import { createSystemTray } from './utils/tray' +import { trayManager } from './managers/tray' const preloadPath = join(__dirname, 'preload.js') const rendererPath = join(__dirname, '..', 'renderer') @@ -39,6 +39,8 @@ const quickAskUrl = `${mainUrl}/search` const quickAskHotKey = 'CommandOrControl+J' +const gotTheLock = app.requestSingleInstanceLock() + app .whenReady() .then(setupReactDevTool) @@ -56,11 +58,23 @@ app windowManager.mainWindow?.webContents.openDevTools() } }) - .then(() => process.env.CI !== 'e2e' && createSystemTray()) + .then(() => process.env.CI !== 'e2e' && trayManager.createSystemTray()) .then(() => { log(`Version: ${app.getVersion()}`) }) .then(() => { + if (!gotTheLock) { + app.quit() + } else { + app.on('second-instance', (_event, _commandLine, _workingDirectory) => { + // Someone tried to run a second instance, we should focus our window. + if (windowManager.mainWindow) { + if (windowManager.mainWindow.isMinimized()) + windowManager.mainWindow.restore() + windowManager.mainWindow.focus() + } + }) + } app.on('activate', () => { if (!BrowserWindow.getAllWindows().length) { createMainWindow() @@ -73,6 +87,10 @@ app.on('ready', () => { registerGlobalShortcuts() }) +app.on('before-quit', function (evt) { + trayManager.destroyCurrentTray() +}) + app.once('quit', () => { cleanUpAndQuit() }) @@ -89,12 +107,7 @@ function createMainWindow() { function registerGlobalShortcuts() { const ret = registerShortcut(quickAskHotKey, (selectedText: string) => { - if (!windowManager.isQuickAskWindowVisible()) { - windowManager.showQuickAskWindow() - windowManager.sendQuickAskSelectedText(selectedText) - } else { - windowManager.hideQuickAskWindow() - } + windowManager.showMainWindow() }) if (!ret) { diff --git a/electron/managers/tray.ts b/electron/managers/tray.ts new file mode 100644 index 000000000..18661e58e --- /dev/null +++ b/electron/managers/tray.ts @@ -0,0 +1,38 @@ +import { join } from 'path' +import { Tray, app, Menu } from 'electron' +import { windowManager } from '../managers/window' + +class TrayManager { + currentTray: Tray | undefined + + createSystemTray = () => { + if (this.currentTray) { + return + } + const iconPath = join(app.getAppPath(), 'icons', 'icon-tray.png') + const tray = new Tray(iconPath) + tray.setToolTip(app.getName()) + + const contextMenu = Menu.buildFromTemplate([ + { + label: 'Open Jan', + type: 'normal', + click: () => windowManager.showMainWindow(), + }, + { + label: 'Open Quick Ask', + type: 'normal', + click: () => windowManager.showQuickAskWindow(), + }, + { label: 'Quit', type: 'normal', click: () => app.quit() }, + ]) + tray.setContextMenu(contextMenu) + } + + destroyCurrentTray() { + this.currentTray?.destroy() + this.currentTray = undefined + } +} + +export const trayManager = new TrayManager() diff --git a/electron/utils/tray.ts b/electron/utils/tray.ts deleted file mode 100644 index 2ab3e6dcc..000000000 --- a/electron/utils/tray.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { join } from 'path' -import { Tray, app, Menu } from 'electron' -import { windowManager } from '../managers/window' - -export const createSystemTray = () => { - const iconPath = join(app.getAppPath(), 'icons', 'icon-tray.png') - const tray = new Tray(iconPath) - tray.setToolTip(app.getName()) - - const contextMenu = Menu.buildFromTemplate([ - { - label: 'Open Jan', - type: 'normal', - click: () => windowManager.showMainWindow(), - }, - { - label: 'Open Quick Ask', - type: 'normal', - click: () => windowManager.showQuickAskWindow(), - }, - { label: 'Quit', type: 'normal', click: () => app.quit() }, - ]) - tray.setContextMenu(contextMenu) -}