diff --git a/electron/main/index.ts b/electron/main/index.ts index 2babc5b1d0adc7521942c9bdf0b10ca3f6953c27..021f6924e8259677ff1e873ced82087f6368e9c3 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 fd953c62341ff8612431a4b0218dfb40c6dff5b9..f23de228a0deeca2ca830fb31f691244fd80d06f 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 06e9bfb7654c28c2f1b2f7d86c80c36d9af5218f..ff63cf9c425d570b8d068b8dfa8d99b1174649d3 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 0000000000000000000000000000000000000000..84a2eba8469f9c935221ba8d9c332e225f0d6caa --- /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; +}