perf: 🚀 支持所有平台在点击关闭按钮时选择是否保存到托盘中的功能

This commit is contained in:
viarotel 2023-10-21 15:09:49 +08:00
parent 200ae4ba75
commit 83601984ec
3 changed files with 101 additions and 2 deletions

View File

@ -130,8 +130,9 @@
6. 支持自定义设备名称,以及偏好设置的导出及导入 ✅
7. 定制化,支持对单个设备进行独立配置 ✅
8. 添加 macOS 及 linux 操作系统的支持 ✅
9. 支持语言国际化功能 🚧
10. 添加对游戏的增强功能,如游戏键位映射 🚧
9. 对深色模式的支持 🚧
10. 支持语言国际化功能 🚧
11. 添加对游戏的增强功能,如游戏键位映射 🚧
## 常见问题

View File

@ -2,10 +2,12 @@ import { app, ipcMain } from 'electron'
import updater from './updater/index.js'
import handles from './handles/index.js'
import tray from './tray/index.js'
export default (mainWindow) => {
handles(mainWindow)
updater(mainWindow)
tray(mainWindow)
ipcMain.on('restart-app', () => {
app.relaunch()

View File

@ -0,0 +1,96 @@
import { Menu, Tray, app, dialog } from 'electron'
import { logoPath } from '@electron/configs/index'
import appStore from '@electron/helpers/store.js'
export default (mainWindow) => {
let tray = null
const showApp = () => {
if (tray) {
tray.destroy()
tray = null
}
mainWindow.show()
return true
}
const quitApp = () => {
app.isQuiting = true
app.quit()
return true
}
const closeApp = (response) => {
if (response === 0) {
quitApp()
return true
}
else if (response === 1) {
mainWindow.hide()
tray = new Tray(logoPath)
tray.setToolTip('escrcpy')
tray.on('click', () => {
showApp()
})
const contextMenu = Menu.buildFromTemplate([
{
label: '打开',
click: () => {
showApp()
},
},
{
label: '退出',
click: () => {
quitApp()
},
},
])
tray.setContextMenu(contextMenu)
return true
}
return false
}
mainWindow.on('close', async (event) => {
if (app.isQuiting) {
mainWindow = null
return true
}
event.preventDefault()
const appCloseCode = appStore.get('appCloseCode')
if (typeof appCloseCode === 'number') {
closeApp(appCloseCode)
return true
}
const { response, checkboxChecked } = await dialog.showMessageBox({
type: 'question',
buttons: ['退出', '最小化到托盘', '取消退出'],
title: '提示',
message: '确定要退出吗?',
checkboxChecked: false,
checkboxLabel: '是否记住选择?',
})
console.log('response', response)
console.log('checkboxChecked', checkboxChecked)
if (checkboxChecked) {
appStore.set('appCloseCode', response)
}
closeApp(response)
})
}