feat: 客户端新增系统托盘图标

This commit is contained in:
kuaifan 2022-04-01 09:18:48 +08:00
parent 3c71af064c
commit 38c1a768fc
15 changed files with 50 additions and 15 deletions

48
electron/electron.js vendored
View File

@ -1,7 +1,7 @@
const fs = require('fs') const fs = require('fs')
const os = require("os"); const os = require("os");
const path = require('path') const path = require('path')
const {app, BrowserWindow, ipcMain, dialog, clipboard, nativeImage, shell} = require('electron') const {app, BrowserWindow, ipcMain, dialog, clipboard, nativeImage, shell, Tray, Menu} = require('electron')
const {autoUpdater} = require("electron-updater") const {autoUpdater} = require("electron-updater")
const log = require("electron-log"); const log = require("electron-log");
const fsProm = require('fs/promises'); const fsProm = require('fs/promises');
@ -12,6 +12,7 @@ const utils = require('./utils');
const config = require('./package.json'); const config = require('./package.json');
let mainWindow = null, let mainWindow = null,
mainTray = null,
subWindow = [], subWindow = [],
willQuitApp = false, willQuitApp = false,
devloadUrl = "", devloadUrl = "",
@ -136,21 +137,34 @@ function createSubWindow(args) {
} }
} }
const getTheLock = app.requestSingleInstanceLock() const getTheLock = app.requestSingleInstanceLock()
if (!getTheLock) { if (!getTheLock) {
app.quit() app.quit()
} else { } else {
app.on('second-instance', () => { app.on('second-instance', () => {
if (mainWindow) { utils.setShowWindow(mainWindow)
if (mainWindow.isMinimized()) { })
mainWindow.restore() app.on('ready', () => {
// 创建主窗口
createMainWindow()
// 创建托盘
if (['darwin', 'win32'].includes(process.platform)) {
mainTray = new Tray(process.platform === 'darwin' ? config.build.mac.trayIcon : config.build.win.icon);
mainTray.on('click', () => {
utils.setShowWindow(mainWindow)
})
mainTray.setToolTip(config.name)
if (process.platform === 'win32') {
const trayMenu = Menu.buildFromTemplate([{
label: '退出',
click: () => {
app.quit()
}
}])
mainTray.setContextMenu(trayMenu)
} }
mainWindow.focus()
mainWindow.show()
} }
}) })
app.on('ready', createMainWindow)
} }
app.on('activate', () => { app.on('activate', () => {
@ -194,10 +208,10 @@ ipcMain.on('windowRouter', (event, args) => {
}) })
/** /**
* 隐藏窗口mac隐藏其他关闭 * 隐藏窗口mac|win隐藏其他关闭
*/ */
ipcMain.on('windowHidden', (event) => { ipcMain.on('windowHidden', (event) => {
if (process.platform === 'darwin') { if (['darwin', 'win32'].includes(process.platform)) {
app.hide(); app.hide();
} else { } else {
app.quit(); app.quit();
@ -330,10 +344,16 @@ ipcMain.on('setDockBadge', (event, args) => {
// Mac only // Mac only
return; return;
} }
if (utils.runNum(args) > 0) { let num = args;
app.dock.setBadge(String(args)) let tray = true;
} else { if (utils.isJson(args)) {
app.dock.setBadge("") num = args.num
tray = !!args.tray
}
let text = utils.runNum(num) > 0 ? String(num) : ""
app.dock.setBadge(text)
if (tray && mainTray) {
mainTray.setTitle(text)
} }
event.returnValue = "ok" event.returnValue = "ok"
}) })

View File

@ -65,6 +65,7 @@
"afterSign": "./notarize.js", "afterSign": "./notarize.js",
"mac": { "mac": {
"icon": "../resources/assets/statics/public/images/logo-app.png", "icon": "../resources/assets/statics/public/images/logo-app.png",
"trayIcon": "../resources/assets/statics/public/images/tray/logo-trayTemplate.png",
"entitlements": "entitlements.plist", "entitlements": "entitlements.plist",
"entitlementsInherit": "entitlements.plist", "entitlementsInherit": "entitlements.plist",
"category": "public.app-category.productivity", "category": "public.app-category.productivity",

16
electron/utils.js vendored
View File

@ -270,6 +270,20 @@ module.exports = {
return Math.round(time / 1000) return Math.round(time / 1000)
}, },
/**
* 显示窗口
* @param win
*/
setShowWindow(win) {
if (win) {
if (win.isMinimized()) {
win.restore()
}
win.focus()
win.show()
}
},
/** /**
* 窗口关闭事件 * 窗口关闭事件
* @param event * @param event
@ -283,7 +297,7 @@ module.exports = {
if (typeof app === "undefined") { if (typeof app === "undefined") {
sender.destroy() sender.destroy()
} else { } else {
if (process.platform === 'darwin') { if (['darwin', 'win32'].includes(process.platform)) {
app.hide() app.hide()
} else { } else {
app.quit() app.quit()

Binary file not shown.

After

Width:  |  Height:  |  Size: 813 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 373 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 767 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 813 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 373 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 767 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB