Skip to content

Commit

Permalink
fix: quick ask blocks app update (janhq#2310)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan authored Mar 11, 2024
1 parent 86af902 commit c8ea16e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 33 deletions.
2 changes: 2 additions & 0 deletions electron/handlers/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
})

Expand Down
31 changes: 22 additions & 9 deletions electron/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, BrowserWindow, Menu, Tray } from 'electron'
import { app, BrowserWindow, Tray } from 'electron'

import { join } from 'path'
/**
Expand Down Expand Up @@ -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')
Expand All @@ -39,6 +39,8 @@ const quickAskUrl = `${mainUrl}/search`

const quickAskHotKey = 'CommandOrControl+J'

const gotTheLock = app.requestSingleInstanceLock()

app
.whenReady()
.then(setupReactDevTool)
Expand All @@ -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()
Expand All @@ -73,6 +87,10 @@ app.on('ready', () => {
registerGlobalShortcuts()
})

app.on('before-quit', function (evt) {
trayManager.destroyCurrentTray()
})

app.once('quit', () => {
cleanUpAndQuit()
})
Expand All @@ -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) {
Expand Down
38 changes: 38 additions & 0 deletions electron/managers/tray.ts
Original file line number Diff line number Diff line change
@@ -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()
24 changes: 0 additions & 24 deletions electron/utils/tray.ts

This file was deleted.

0 comments on commit c8ea16e

Please sign in to comment.