diff --git a/apps/electron/main/index.ts b/apps/electron/main/index.ts index 970f3db9..a58a9df3 100644 --- a/apps/electron/main/index.ts +++ b/apps/electron/main/index.ts @@ -1,4 +1,4 @@ -import { app, ipcMain, nativeTheme, shell, type IpcMain, type Tray } from "electron"; +import { Menu, MenuItem, app, ipcMain, nativeTheme, shell, type IpcMain, type Tray } from "electron"; import "./security-restrictions"; import type cp from "child_process"; @@ -12,6 +12,7 @@ import LibraryIPC from "./ipc/library"; import { syncIpc } from "./ipc/sync"; import { pageUrl, restoreOrCreateWindow } from "./mainWindow"; import { createWebServer } from "./src/createWebServer"; +import createMenu from "./src/menu"; import createTray, { getTrayIcon } from "./src/tray"; let nextjsWebChild: cp.ChildProcess | undefined; @@ -40,15 +41,15 @@ app.disableHardwareAcceleration(); */ app.on("window-all-closed", () => { console.log("window-all-closed"); - // if (process.platform !== "darwin") { - // app.quit(); - // } + if (process.platform !== "darwin") { + app.quit(); + } }); -app.on("before-quit", (e) => { - e.preventDefault(); - app.hide(); -}); +// app.on("will-quit", (e) => { +// e.preventDefault(); +// app.hide(); +// }); app.on("quit", () => { closeAssetsServer(); @@ -69,6 +70,8 @@ app.on("activate", () => { }); let tray: Tray; +// create menu +createMenu(); /** * Create the application window when the background process is ready. @@ -80,25 +83,6 @@ app .then(() => { // 托盘图标 tray = createTray(); - - // 系统菜单 https://www.electronjs.org/docs/latest/api/menu - // const menu = Menu.buildFromTemplate([ - // { - // label: app.name, - // submenu: [ - // { - // label: "Quit", - // accelerator: "CmdOrCtrl+Q", - // click: () => { - // app.hide(); - // }, - // }, - // ], - // }, - // ]); - - // Menu.setApplicationMenu(menu); - // app.dock.hide(); }) .catch((err) => { throw err; diff --git a/apps/electron/main/src/menu.ts b/apps/electron/main/src/menu.ts new file mode 100644 index 00000000..7476ee04 --- /dev/null +++ b/apps/electron/main/src/menu.ts @@ -0,0 +1,25 @@ +import { Menu, app } from "electron"; + +/** + * 创建菜单 + */ +const createMenu = () => { + const menu = Menu.buildFromTemplate([ + { + label: app.name, + submenu: [ + { + label: "Quit", + accelerator: "CmdOrCtrl+Q", + click: () => { + app.hide(); + }, + }, + ], + }, + ]); + + Menu.setApplicationMenu(menu); +}; + +export default createMenu; diff --git a/apps/electron/main/src/tray.ts b/apps/electron/main/src/tray.ts index d1073f5c..03683266 100644 --- a/apps/electron/main/src/tray.ts +++ b/apps/electron/main/src/tray.ts @@ -9,6 +9,10 @@ export const getTrayIcon = () => { } }; +/** + * 系统托盘 + * @returns {Tray} + */ const createTray = () => { // 托盘图标 const tray = new Tray(getTrayIcon()); @@ -21,6 +25,13 @@ const createTray = () => { app.show(); }, }, + { + label: "退出", + type: "normal", + click: () => { + app.quit(); + }, + }, ]); tray.setContextMenu(contextMenu);