Initial commit
This commit is contained in:
68
src/main/tray.ts
Normal file
68
src/main/tray.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { Tray, Menu, nativeImage, app } from 'electron'
|
||||
import { join } from 'node:path'
|
||||
import { showMainWindow } from './windows'
|
||||
import { isPaused, setPaused, forceCheck } from './scheduler'
|
||||
import { snoozeAll } from './state-actions'
|
||||
|
||||
let tray: Tray | null = null
|
||||
|
||||
function resolveTrayIcon(): Electron.NativeImage {
|
||||
// Try resources/, fallback to a transparent 16x16 if missing during dev.
|
||||
const candidates = [
|
||||
join(process.resourcesPath, 'tray.png'),
|
||||
join(__dirname, '../../resources/tray.png'),
|
||||
join(app.getAppPath(), 'resources/tray.png')
|
||||
]
|
||||
for (const p of candidates) {
|
||||
const img = nativeImage.createFromPath(p)
|
||||
if (!img.isEmpty()) return img
|
||||
}
|
||||
return nativeImage.createEmpty()
|
||||
}
|
||||
|
||||
export function createTray(): Tray {
|
||||
if (tray) return tray
|
||||
const icon = resolveTrayIcon()
|
||||
tray = new Tray(icon)
|
||||
tray.setToolTip('Exercise Reminder')
|
||||
refreshMenu()
|
||||
tray.on('click', () => showMainWindow())
|
||||
tray.on('double-click', () => showMainWindow())
|
||||
return tray
|
||||
}
|
||||
|
||||
export function refreshMenu(): void {
|
||||
if (!tray) return
|
||||
const paused = isPaused()
|
||||
const menu = Menu.buildFromTemplate([
|
||||
{ label: 'Открыть', click: () => showMainWindow() },
|
||||
{ type: 'separator' },
|
||||
{
|
||||
label: paused ? 'Возобновить напоминания' : 'Пауза напоминаний',
|
||||
click: () => {
|
||||
setPaused(!paused)
|
||||
refreshMenu()
|
||||
if (!paused) forceCheck()
|
||||
}
|
||||
},
|
||||
{
|
||||
label: 'Отложить все на 15 мин',
|
||||
click: () => snoozeAll(15)
|
||||
},
|
||||
{ type: 'separator' },
|
||||
{
|
||||
label: 'Выход',
|
||||
click: () => {
|
||||
app.quit()
|
||||
}
|
||||
}
|
||||
])
|
||||
tray.setContextMenu(menu)
|
||||
}
|
||||
|
||||
export function destroyTray(): void {
|
||||
if (tray) {
|
||||
tray.destroy()
|
||||
tray = null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user