Skip to content

Commit

Permalink
feat: 客户端新增系统托盘图标
Browse files Browse the repository at this point in the history
  • Loading branch information
kuaifan committed Apr 1, 2022
1 parent 3c71af0 commit 38c1a76
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 15 deletions.
48 changes: 34 additions & 14 deletions electron/electron.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -12,6 +12,7 @@ const utils = require('./utils');
const config = require('./package.json');

let mainWindow = null,
mainTray = null,
subWindow = [],
willQuitApp = false,
devloadUrl = "",
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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"
})
Expand Down
1 change: 1 addition & 0 deletions electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 15 additions & 1 deletion electron/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
Binary file added public/images/tray/logo-tray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/tray/logo-tray@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/tray/logo-tray@3x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/tray/logo-trayTemplate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/tray/logo-trayTemplate@2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/tray/logo-trayTemplate@3x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 38c1a76

Please sign in to comment.