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,
} 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()
})

View File

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

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