2024-05-15 05:16:02 +02:00
|
|
|
import { trayPath } from '$electron/configs/index.js'
|
|
|
|
import { executeI18n } from '$electron/helpers/index.js'
|
2024-09-07 19:55:54 +02:00
|
|
|
import appStore from '$electron/helpers/store.js'
|
|
|
|
import { app, dialog, Menu, Tray } from 'electron'
|
2023-10-21 09:09:49 +02:00
|
|
|
|
|
|
|
export default (mainWindow) => {
|
2023-10-27 13:18:09 +02:00
|
|
|
const t = value => executeI18n(mainWindow, value)
|
|
|
|
|
2023-10-21 09:09:49 +02:00
|
|
|
let tray = null
|
|
|
|
|
|
|
|
const showApp = () => {
|
2023-10-24 10:46:51 +02:00
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
app.dock.show()
|
|
|
|
}
|
|
|
|
|
2023-10-21 09:09:49 +02:00
|
|
|
mainWindow.show()
|
|
|
|
|
2023-11-03 09:33:36 +01:00
|
|
|
if (tray) {
|
|
|
|
tray.destroy()
|
|
|
|
tray = null
|
|
|
|
}
|
|
|
|
|
2023-10-21 09:09:49 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-10-24 10:46:51 +02:00
|
|
|
const hideApp = () => {
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
app.dock.hide()
|
|
|
|
}
|
|
|
|
|
|
|
|
mainWindow.hide()
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-10-21 09:09:49 +02:00
|
|
|
const quitApp = () => {
|
|
|
|
app.isQuiting = true
|
2023-10-24 10:46:51 +02:00
|
|
|
|
2023-10-21 09:09:49 +02:00
|
|
|
app.quit()
|
2023-10-24 10:46:51 +02:00
|
|
|
|
2023-10-21 09:09:49 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-10-27 13:18:09 +02:00
|
|
|
const closeApp = async (response) => {
|
2023-10-21 09:09:49 +02:00
|
|
|
if (response === 0) {
|
|
|
|
quitApp()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
else if (response === 1) {
|
2023-10-24 10:46:51 +02:00
|
|
|
hideApp()
|
2023-10-21 09:09:49 +02:00
|
|
|
|
2023-10-24 05:08:27 +02:00
|
|
|
tray = new Tray(trayPath)
|
2023-10-21 09:09:49 +02:00
|
|
|
|
|
|
|
tray.setToolTip('escrcpy')
|
|
|
|
|
|
|
|
tray.on('click', () => {
|
|
|
|
showApp()
|
|
|
|
})
|
|
|
|
|
|
|
|
const contextMenu = Menu.buildFromTemplate([
|
|
|
|
{
|
2023-10-27 13:18:09 +02:00
|
|
|
label: await t('common.open'),
|
2023-10-21 09:09:49 +02:00
|
|
|
click: () => {
|
|
|
|
showApp()
|
|
|
|
},
|
|
|
|
},
|
2023-10-21 10:28:14 +02:00
|
|
|
{
|
2023-10-27 13:18:09 +02:00
|
|
|
label: await t('common.restart'),
|
2023-10-21 10:28:14 +02:00
|
|
|
click: () => {
|
|
|
|
app.relaunch()
|
|
|
|
quitApp()
|
|
|
|
},
|
|
|
|
},
|
2023-10-21 09:09:49 +02:00
|
|
|
{
|
2023-10-27 13:18:09 +02:00
|
|
|
label: await t('close.quit'),
|
2023-10-21 09:09:49 +02:00
|
|
|
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',
|
2023-10-27 13:18:09 +02:00
|
|
|
buttons: [
|
|
|
|
await t('close.quit'),
|
|
|
|
await t('close.minimize'),
|
|
|
|
await t('close.quit.cancel'),
|
|
|
|
],
|
|
|
|
title: await t('common.tips'),
|
|
|
|
message: await t('close.message'),
|
2023-10-21 09:09:49 +02:00
|
|
|
checkboxChecked: false,
|
2023-10-27 13:18:09 +02:00
|
|
|
checkboxLabel: await t('close.remember'),
|
2023-10-21 09:09:49 +02:00
|
|
|
})
|
|
|
|
|
2024-06-13 13:33:49 +02:00
|
|
|
if (checkboxChecked && [0, 1].includes(response)) {
|
2023-10-21 09:09:49 +02:00
|
|
|
appStore.set('appCloseCode', response)
|
|
|
|
}
|
|
|
|
|
|
|
|
closeApp(response)
|
|
|
|
})
|
|
|
|
}
|