From ab040cab961abd784c6890c253060eac610642a4 Mon Sep 17 00:00:00 2001 From: Hu Gang <18768366022@163.com> Date: Mon, 31 Mar 2025 11:06:35 +0800 Subject: [PATCH] feat: add tray --- electron/main/index.ts | 31 ++++++++++++++++--------------- electron/main/window/index.ts | 3 ++- electron/main/window/options.ts | 13 +++++++------ electron/main/window/tray.ts | 27 +++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 22 deletions(-) create mode 100644 electron/main/window/tray.ts diff --git a/electron/main/index.ts b/electron/main/index.ts index 2babc5b..021f692 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -1,20 +1,21 @@ -import { app, ipcMain } from 'electron' -import { createDefaultWindow } from './window' +import { app, ipcMain } from 'electron'; +import { createDefaultWindow, createTray } from './window'; app.whenReady().then(() => { - createDefaultWindow() - // const defaultWindow = createDefaultWindow() + const tray = createTray(); - // 监听渲染进程崩溃或被杀死,重新运行程序 - // defaultWindow.webContents.on('render-process-gone', () => { - // app.relaunch() - // app.exit(0) - // }) -}) + const win = createDefaultWindow(); + + tray.on('click', () => { + win.show(); + }); + + win.on('close', (event) => { + event.preventDefault(); + win.hide(); + }); +}); app.on('window-all-closed', () => { - ipcMain.removeAllListeners() - if (process.platform !== 'darwin') { - app.quit() - } -}) + ipcMain.removeAllListeners(); +}); diff --git a/electron/main/window/index.ts b/electron/main/window/index.ts index fd953c6..f23de22 100644 --- a/electron/main/window/index.ts +++ b/electron/main/window/index.ts @@ -1 +1,2 @@ -export { createWindow, createDefaultWindow } from './create' +export { createWindow, createDefaultWindow } from './create'; +export { createTray } from './tray'; diff --git a/electron/main/window/options.ts b/electron/main/window/options.ts index 06e9bfb..ff63cf9 100644 --- a/electron/main/window/options.ts +++ b/electron/main/window/options.ts @@ -1,23 +1,24 @@ export interface allWindowType { [propName: string]: { - window: Electron.BrowserWindowConstructorOptions - hash: string - } + window: Electron.BrowserWindowConstructorOptions; + hash: string; + }; } export const options: allWindowType = { defaultWin: { window: { - width: 800, - height: 600, + width: 1700, + height: 900, resizable: true, show: true, alwaysOnTop: false, useContentSize: true, + autoHideMenuBar: true, frame: true, backgroundColor: '#ffffff', icon: 'dist/favicon.ico', }, hash: 'defaultWin', }, -} +}; diff --git a/electron/main/window/tray.ts b/electron/main/window/tray.ts new file mode 100644 index 0000000..84a2eba --- /dev/null +++ b/electron/main/window/tray.ts @@ -0,0 +1,27 @@ +import path from 'node:path'; +import { app, Tray, Menu } from 'electron'; +import type { MenuItemConstructorOptions } from 'electron'; + +export function createTray(): Tray { + let appTray: Tray | null = null; + + if (appTray) return appTray; + + const trayMenus: MenuItemConstructorOptions[] = [ + { + label: '退出', + click: () => { + app.exit(); + }, + }, + ]; + + const iconPath = path.join(__dirname, '../app_favicon.ico'); + appTray = new Tray(iconPath); + + const contextMenu = Menu.buildFromTemplate(trayMenus); + appTray.setToolTip('Eulercopilot'); + + appTray.setContextMenu(contextMenu); + return appTray; +} -- Gitee