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 os = require("os");
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 log = require("electron-log");
const fsProm = require('fs/promises');
@ -12,6 +12,7 @@ const utils = require('./utils');
const config = require('./package.json');
let mainWindow = null,
mainTray = null,
subWindow = [],
willQuitApp = false,
devloadUrl = "",
@ -136,21 +137,34 @@ function createSubWindow(args) {
}
}
const getTheLock = app.requestSingleInstanceLock()
if (!getTheLock) {
app.quit()
} else {
app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore()
utils.setShowWindow(mainWindow)
})
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', () => {
@ -194,10 +208,10 @@ ipcMain.on('windowRouter', (event, args) => {
})
/**
* 隐藏窗口mac隐藏其他关闭
* 隐藏窗口mac|win隐藏其他关闭
*/
ipcMain.on('windowHidden', (event) => {
if (process.platform === 'darwin') {
if (['darwin', 'win32'].includes(process.platform)) {
app.hide();
} else {
app.quit();
@ -330,10 +344,16 @@ ipcMain.on('setDockBadge', (event, args) => {
// Mac only
return;
}
if (utils.runNum(args) > 0) {
app.dock.setBadge(String(args))
} else {
app.dock.setBadge("")
let num = args;
let tray = true;
if (utils.isJson(args)) {
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"
})

View File

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

16
electron/utils.js vendored
View File

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