2023-10-21 09:09:49 +02:00
|
|
|
import { Menu, Tray, app, dialog } from 'electron'
|
2023-10-21 10:28:14 +02:00
|
|
|
import { macTrayPath, trayPath } from '@electron/configs/index'
|
2023-10-21 09:09:49 +02:00
|
|
|
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()
|
|
|
|
|
2023-10-21 10:28:14 +02:00
|
|
|
const trayIcon = process.platform === 'darwin' ? macTrayPath : trayPath
|
|
|
|
|
|
|
|
tray = new Tray(trayIcon)
|
2023-10-21 09:09:49 +02:00
|
|
|
|
|
|
|
tray.setToolTip('escrcpy')
|
|
|
|
|
|
|
|
tray.on('click', () => {
|
|
|
|
showApp()
|
|
|
|
})
|
|
|
|
|
|
|
|
const contextMenu = Menu.buildFromTemplate([
|
|
|
|
{
|
|
|
|
label: '打开',
|
|
|
|
click: () => {
|
|
|
|
showApp()
|
|
|
|
},
|
|
|
|
},
|
2023-10-21 10:28:14 +02:00
|
|
|
{
|
|
|
|
label: '重启服务',
|
|
|
|
click: () => {
|
|
|
|
app.relaunch()
|
|
|
|
quitApp()
|
|
|
|
},
|
|
|
|
},
|
2023-10-21 09:09:49 +02:00
|
|
|
{
|
|
|
|
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: '是否记住选择?',
|
|
|
|
})
|
|
|
|
|
2023-10-21 10:28:14 +02:00
|
|
|
// console.log('response', response)
|
|
|
|
// console.log('checkboxChecked', checkboxChecked)
|
2023-10-21 09:09:49 +02:00
|
|
|
|
|
|
|
if (checkboxChecked) {
|
|
|
|
appStore.set('appCloseCode', response)
|
|
|
|
}
|
|
|
|
|
|
|
|
closeApp(response)
|
|
|
|
})
|
|
|
|
}
|