feat: 客户端新增系统托盘图标
48
electron/electron.js
vendored
@ -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"
|
||||||
})
|
})
|
||||||
|
|||||||
@ -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
@ -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()
|
||||||
|
|||||||
BIN
public/images/tray/logo-tray.png
Normal file
|
After Width: | Height: | Size: 813 B |
BIN
public/images/tray/logo-tray@2x.png
Normal file
|
After Width: | Height: | Size: 1.9 KiB |
BIN
public/images/tray/logo-tray@3x.png
Normal file
|
After Width: | Height: | Size: 3.0 KiB |
BIN
public/images/tray/logo-trayTemplate.png
Normal file
|
After Width: | Height: | Size: 373 B |
BIN
public/images/tray/logo-trayTemplate@2x.png
Normal file
|
After Width: | Height: | Size: 767 B |
BIN
public/images/tray/logo-trayTemplate@3x.png
Normal file
|
After Width: | Height: | Size: 1.1 KiB |
BIN
resources/assets/statics/public/images/tray/logo-tray.png
Normal file
|
After Width: | Height: | Size: 813 B |
BIN
resources/assets/statics/public/images/tray/logo-tray@2x.png
Normal file
|
After Width: | Height: | Size: 1.9 KiB |
BIN
resources/assets/statics/public/images/tray/logo-tray@3x.png
Normal file
|
After Width: | Height: | Size: 3.0 KiB |
|
After Width: | Height: | Size: 373 B |
|
After Width: | Height: | Size: 767 B |
|
After Width: | Height: | Size: 1.1 KiB |