diff --git a/README.md b/README.md index f282ea2d..e8e4ae10 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@
-APM Logo +APM Logo **星火 APM 琥珀软件包管理器 - 桌面应用商店** diff --git a/electron-builder.yml b/electron-builder.yml index 0e2fd118..f6ab8b5b 100644 --- a/electron-builder.yml +++ b/electron-builder.yml @@ -10,9 +10,12 @@ files: extraFiles: - from: "extras" to: "extras" +extraResources: + - from: "icons" + to: "icons" linux: - icon: "public/amber-pm-logo.icns" + icon: "icons/amber-pm-logo.icns" category: "System" executableName: "apm-app-store" desktop: diff --git a/public/amber-pm-logo.png b/electron/assets/amber-pm-logo.png similarity index 100% rename from public/amber-pm-logo.png rename to electron/assets/amber-pm-logo.png diff --git a/electron/main/index.ts b/electron/main/index.ts index 32cc2e54..41a32c07 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -1,8 +1,9 @@ -import { app, BrowserWindow, ipcMain, shell } from 'electron' +import { app, BrowserWindow, ipcMain, Menu, shell, Tray } from 'electron' import { createRequire } from 'node:module' import { fileURLToPath } from 'node:url' import path from 'node:path' import os from 'node:os' +import fs from 'node:fs' import pino from 'pino' import { handleCommandLine } from './deeplink.js' import { isLoaded } from '../global.js' @@ -93,6 +94,14 @@ async function createWindow() { return { action: 'deny' } }) // win.webContents.on('will-navigate', (event, url) => { }) #344 + + win.on('close', (event) => { + // 截获 close 默认行为 + event.preventDefault(); + // 点击关闭时触发close事件,我们按照之前的思路在关闭时,隐藏窗口,隐藏任务栏窗口 + win.hide(); + win.setSkipTaskbar(true); + }) } ipcMain.on('renderer-ready', (event, args) => { @@ -128,6 +137,55 @@ app.on('activate', () => { } }) +// 设置托盘 +// 获取图标路径 +function getIconPath() { + let iconPath = ""; + const iconFile = process.platform === "win32" ? "amber-pm-logo.ico" : "amber-pm-logo.png"; // 图标文件名,linux下需要png格式,不然会不显示 + // 判断是否在打包模式 + if (app.isPackaged) { + // 打包模式 + iconPath = path.join(process.resourcesPath, "icons", iconFile); // 路径根据自身情况调整 + } else { + // 开发模式 + const projectRoot = path.join(__dirname, "../.."); // __dirname 指向 dist-electron/main,但资源在项目根目录,所以..指向上一级 + iconPath = path.join(projectRoot, "icons", iconFile); + } + + // 检查文件是否存在 + if (fs.existsSync(iconPath)) { + logger.info("图标文件存在:" + iconPath); + return iconPath; + } else { + logger.error("图标文件不存在:" + iconPath); + // 返回一个默认图标路径或null + return null; + } +} + +let tray = null; +app.whenReady().then(() => { + tray = new Tray(getIconPath()); + const contextMenu = Menu.buildFromTemplate([{ + label: '显示主界面', + click: () => { win.show() } + }, + { + label: '退出程序', + click: () => { win.destroy() } + } + ]); + tray.setToolTip('APM 应用商店') + tray.setContextMenu(contextMenu) + // 双击触发 + tray.on('click', () => { + // 双击通知区图标实现应用的显示或隐藏 + win.isVisible() ? win.hide() : win.show() + win.isVisible() ? win.setSkipTaskbar(false) : win.setSkipTaskbar(true); + }); +}) + + // New window example arg: new windows url // ipcMain.handle('open-win', (_, arg) => { // const childWindow = new BrowserWindow({ diff --git a/public/amber-pm-logo.icns b/icons/amber-pm-logo.icns similarity index 100% rename from public/amber-pm-logo.icns rename to icons/amber-pm-logo.icns diff --git a/icons/amber-pm-logo.png b/icons/amber-pm-logo.png new file mode 100644 index 00000000..812b1c96 Binary files /dev/null and b/icons/amber-pm-logo.png differ