Skip to content

Commit

Permalink
add tray icon and exit on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
zerob13 committed Feb 19, 2025
1 parent 0b6ee33 commit cce6b65
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 4 deletions.
Binary file added resources/mac-tray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/win-tray.ico
Binary file not shown.
4 changes: 0 additions & 4 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import { app, BrowserWindow } from 'electron'
import { electronApp, optimizer } from '@electron-toolkit/utils'
import { presenter } from './presenter'

const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.exit()
}
app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required')
app.commandLine.appendSwitch('webrtc-max-cpu-consumption-percentage', '100')
app.commandLine.appendSwitch('js-flags', '--max-old-space-size=4096')
Expand Down
55 changes: 55 additions & 0 deletions src/main/presenter/trayPresenter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Tray, Menu, app } from 'electron'
import path from 'path'
import { WindowPresenter } from './windowPresenter'

export class TrayPresenter {
private tray: Tray | null = null
private windowPresenter: WindowPresenter

constructor(windowPresenter: WindowPresenter) {
this.windowPresenter = windowPresenter
this.createTray()
}

private createTray() {
// 根据平台选择不同的图标
const basePath = path.join(app.getAppPath(), 'resources')
const iconPath = path.join(
basePath,
process.platform === 'win32' ? 'win-tray.ico' : 'mac-tray.png'
)
console.log('iconPath', iconPath)

this.tray = new Tray(iconPath)
this.tray.setToolTip('DeepChat')

const contextMenu = Menu.buildFromTemplate([
{
label: '打开',
click: () => {
this.windowPresenter.show()
}
},
{
label: '退出',
click: () => {
app.quit()
}
}
])

this.tray.setContextMenu(contextMenu)

// 点击托盘图标时显示窗口
this.tray.on('click', () => {
this.windowPresenter.show()
})
}

destroy() {
if (this.tray) {
this.tray.destroy()
this.tray = null
}
}
}
36 changes: 36 additions & 0 deletions src/main/presenter/windowPresenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,56 @@ import { is } from '@electron-toolkit/utils'
import { IWindowPresenter } from '@shared/presenter'
import { eventBus } from '@/eventbus'
import { ConfigPresenter } from './configPresenter'
import { TrayPresenter } from './trayPresenter'

export const MAIN_WIN = 'main'
export class WindowPresenter implements IWindowPresenter {
windows: Map<string, BrowserWindow>
private configPresenter: ConfigPresenter
private isQuitting: boolean = false
private trayPresenter: TrayPresenter | null = null

constructor(configPresenter: ConfigPresenter) {
this.windows = new Map()
this.configPresenter = configPresenter

// 检查是否为第二个实例
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit()
return
}

// 处理第二个实例的启动
app.on('second-instance', () => {
const mainWindow = this.mainWindow
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore()
}
mainWindow.show()
mainWindow.focus()
}
})

// 监听应用退出事件
app.on('before-quit', () => {
console.log('before-quit')
this.isQuitting = true
if (this.trayPresenter) {
this.trayPresenter.destroy()
}
})

// 监听强制退出事件
eventBus.on('force-quit-app', () => {
console.log('force-quit-app')
this.isQuitting = true
if (this.trayPresenter) {
this.trayPresenter.destroy()
}
})

console.log('WindowPresenter constructor', this.configPresenter)
}

Expand Down Expand Up @@ -94,6 +124,12 @@ export class WindowPresenter implements IWindowPresenter {
mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
}
this.windows.set(MAIN_WIN, mainWindow)

// 初始化托盘
if (!this.trayPresenter) {
this.trayPresenter = new TrayPresenter(this)
}

return mainWindow
}
getWindow(windowName: string): BrowserWindow | undefined {
Expand Down

0 comments on commit cce6b65

Please sign in to comment.