fix: quick ask blocks app update (#2310)

This commit is contained in:
Louis 2024-03-11 22:58:18 +07:00 committed by GitHub
parent 86af902d19
commit c8ea16e661
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 62 additions and 33 deletions

View File

@ -7,6 +7,7 @@ import {
autoUpdater, autoUpdater,
} from 'electron-updater' } from 'electron-updater'
import { AppEvent } from '@janhq/core' import { AppEvent } from '@janhq/core'
import { trayManager } from '../managers/tray'
export let waitingToInstallVersion: string | undefined = undefined export let waitingToInstallVersion: string | undefined = undefined
@ -22,6 +23,7 @@ export function handleAppUpdates() {
message: 'Would you like to download and install it now?', message: 'Would you like to download and install it now?',
buttons: ['Download', 'Later'], buttons: ['Download', 'Later'],
}) })
trayManager.destroyCurrentTray()
if (action.response === 0) await autoUpdater.downloadUpdate() if (action.response === 0) await autoUpdater.downloadUpdate()
}) })

View File

@ -1,4 +1,4 @@
import { app, BrowserWindow, Menu, Tray } from 'electron' import { app, BrowserWindow, Tray } from 'electron'
import { join } from 'path' import { join } from 'path'
/** /**
@ -27,7 +27,7 @@ import { setupReactDevTool } from './utils/dev'
import { cleanLogs } from './utils/log' import { cleanLogs } from './utils/log'
import { registerShortcut } from './utils/selectedText' import { registerShortcut } from './utils/selectedText'
import { createSystemTray } from './utils/tray' import { trayManager } from './managers/tray'
const preloadPath = join(__dirname, 'preload.js') const preloadPath = join(__dirname, 'preload.js')
const rendererPath = join(__dirname, '..', 'renderer') const rendererPath = join(__dirname, '..', 'renderer')
@ -39,6 +39,8 @@ const quickAskUrl = `${mainUrl}/search`
const quickAskHotKey = 'CommandOrControl+J' const quickAskHotKey = 'CommandOrControl+J'
const gotTheLock = app.requestSingleInstanceLock()
app app
.whenReady() .whenReady()
.then(setupReactDevTool) .then(setupReactDevTool)
@ -56,11 +58,23 @@ app
windowManager.mainWindow?.webContents.openDevTools() windowManager.mainWindow?.webContents.openDevTools()
} }
}) })
.then(() => process.env.CI !== 'e2e' && createSystemTray()) .then(() => process.env.CI !== 'e2e' && trayManager.createSystemTray())
.then(() => { .then(() => {
log(`Version: ${app.getVersion()}`) log(`Version: ${app.getVersion()}`)
}) })
.then(() => { .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', () => { app.on('activate', () => {
if (!BrowserWindow.getAllWindows().length) { if (!BrowserWindow.getAllWindows().length) {
createMainWindow() createMainWindow()
@ -73,6 +87,10 @@ app.on('ready', () => {
registerGlobalShortcuts() registerGlobalShortcuts()
}) })
app.on('before-quit', function (evt) {
trayManager.destroyCurrentTray()
})
app.once('quit', () => { app.once('quit', () => {
cleanUpAndQuit() cleanUpAndQuit()
}) })
@ -89,12 +107,7 @@ function createMainWindow() {
function registerGlobalShortcuts() { function registerGlobalShortcuts() {
const ret = registerShortcut(quickAskHotKey, (selectedText: string) => { const ret = registerShortcut(quickAskHotKey, (selectedText: string) => {
if (!windowManager.isQuickAskWindowVisible()) { windowManager.showMainWindow()
windowManager.showQuickAskWindow()
windowManager.sendQuickAskSelectedText(selectedText)
} else {
windowManager.hideQuickAskWindow()
}
}) })
if (!ret) { if (!ret) {

38
electron/managers/tray.ts Normal file
View File

@ -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()

View File

@ -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)
}