Skip to content

Commit

Permalink
chore: 🤖 系统托盘
Browse files Browse the repository at this point in the history
  • Loading branch information
meetqy committed Jun 19, 2023
1 parent 464e642 commit 07af740
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
38 changes: 11 additions & 27 deletions apps/electron/main/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -69,6 +70,8 @@ app.on("activate", () => {
});

let tray: Tray;
// create menu
createMenu();

/**
* Create the application window when the background process is ready.
Expand All @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions apps/electron/main/src/menu.ts
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 11 additions & 0 deletions apps/electron/main/src/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export const getTrayIcon = () => {
}
};

/**
* 系统托盘
* @returns {Tray}
*/
const createTray = () => {
// 托盘图标
const tray = new Tray(getTrayIcon());
Expand All @@ -21,6 +25,13 @@ const createTray = () => {
app.show();
},
},
{
label: "退出",
type: "normal",
click: () => {
app.quit();
},
},
]);
tray.setContextMenu(contextMenu);

Expand Down

0 comments on commit 07af740

Please sign in to comment.