Skip to content

Commit

Permalink
perf: 🚀 支持所有平台在点击关闭按钮时选择是否保存到托盘中的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Oct 21, 2023
1 parent 200ae4b commit 8360198
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@
6. 支持自定义设备名称,以及偏好设置的导出及导入 ✅
7. 定制化,支持对单个设备进行独立配置 ✅
8. 添加 macOS 及 linux 操作系统的支持 ✅
9. 支持语言国际化功能 🚧
10. 添加对游戏的增强功能,如游戏键位映射 🚧
9. 对深色模式的支持 🚧
10. 支持语言国际化功能 🚧
11. 添加对游戏的增强功能,如游戏键位映射 🚧

## 常见问题

Expand Down
2 changes: 2 additions & 0 deletions electron/events/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
96 changes: 96 additions & 0 deletions electron/events/tray/index.js
Original file line number Diff line number Diff line change
@@ -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)
})
}

0 comments on commit 8360198

Please sign in to comment.