mirror of
https://github.com/jahruz67/wisp-open.git
synced 2026-08-08 18:14:08 +00:00
132 lines
3.1 KiB
Go
132 lines
3.1 KiB
Go
// Package tray provides system tray functionality for the application.
|
|
// It displays an icon in the Windows notification area with a context menu.
|
|
package tray
|
|
|
|
import (
|
|
_ "embed"
|
|
"os"
|
|
|
|
"wis-free-v3/internal/config"
|
|
"wis-free-v3/internal/logger"
|
|
"wis-free-v3/internal/system/startup"
|
|
|
|
"github.com/getlantern/systray"
|
|
)
|
|
|
|
//go:embed icon.ico
|
|
var iconData []byte
|
|
|
|
// App defines the interface required by the tray package to interact with the main application.
|
|
type App interface {
|
|
Quit()
|
|
GetConfig() *config.Config
|
|
ShowSettings()
|
|
}
|
|
|
|
// Menu item references for dynamic updates
|
|
var statusMenuItem *systray.MenuItem
|
|
|
|
// Start initializes and runs the system tray.
|
|
// This function blocks until the tray is terminated.
|
|
func Start(app App) {
|
|
systray.Run(
|
|
func() { onReady(app) },
|
|
onExit,
|
|
)
|
|
}
|
|
|
|
// onReady is called when the system tray is ready to be configured.
|
|
func onReady(app App) {
|
|
// Configure tray icon and tooltip
|
|
systray.SetIcon(iconData)
|
|
systray.SetTitle("wis-free-v3")
|
|
systray.SetTooltip(buildTooltip(app))
|
|
|
|
// Build menu structure
|
|
statusMenuItem = systray.AddMenuItem("Status: Ready", "Current application status")
|
|
statusMenuItem.Disable()
|
|
|
|
systray.AddSeparator()
|
|
|
|
menuSettings := systray.AddMenuItem("Settings", "Open settings window")
|
|
menuStartup := systray.AddMenuItemCheckbox(
|
|
"Start with Windows",
|
|
"Automatically start when Windows boots",
|
|
startup.IsInStartup(),
|
|
)
|
|
|
|
systray.AddSeparator()
|
|
|
|
menuExit := systray.AddMenuItem("Exit", "Close the application")
|
|
|
|
// Handle menu events in background
|
|
go handleMenuEvents(app, menuSettings, menuStartup, menuExit)
|
|
}
|
|
|
|
// handleMenuEvents processes menu item click events.
|
|
func handleMenuEvents(app App, settings, startupItem, exit *systray.MenuItem) {
|
|
for {
|
|
select {
|
|
case <-settings.ClickedCh:
|
|
app.ShowSettings()
|
|
|
|
case <-startupItem.ClickedCh:
|
|
toggleStartup(startupItem)
|
|
|
|
case <-exit.ClickedCh:
|
|
handleExit(app)
|
|
}
|
|
}
|
|
}
|
|
|
|
// toggleStartup handles the startup toggle menu item.
|
|
func toggleStartup(item *systray.MenuItem) {
|
|
if item.Checked() {
|
|
if err := startup.RemoveFromStartup(); err != nil {
|
|
logger.Error("Failed to remove from startup: %v", err)
|
|
} else {
|
|
item.Uncheck()
|
|
logger.Info("Removed from Windows startup")
|
|
}
|
|
} else {
|
|
if err := startup.AddToStartup(); err != nil {
|
|
logger.Error("Failed to add to startup: %v", err)
|
|
} else {
|
|
item.Check()
|
|
logger.Info("Added to Windows startup")
|
|
}
|
|
}
|
|
}
|
|
|
|
// handleExit cleanly shuts down the application.
|
|
func handleExit(app App) {
|
|
logger.Info("User requested application exit")
|
|
app.Quit()
|
|
systray.Quit()
|
|
os.Exit(0)
|
|
}
|
|
|
|
// buildTooltip creates the tray icon tooltip text.
|
|
func buildTooltip(app App) string {
|
|
shortcut := "Ctrl+K"
|
|
if cfg := app.GetConfig(); cfg != nil && cfg.Shortcut != "" {
|
|
shortcut = cfg.Shortcut
|
|
}
|
|
return "wis-free-v3 - " + shortcut + " to record"
|
|
}
|
|
|
|
// UpdateStatus updates the status text displayed in the tray menu.
|
|
func UpdateStatus(status string) {
|
|
if statusMenuItem != nil {
|
|
statusMenuItem.SetTitle("Status: " + status)
|
|
systray.SetTooltip("wis-free-v3 - " + status)
|
|
}
|
|
}
|
|
|
|
// onExit is called when the system tray is shutting down.
|
|
func onExit() {
|
|
logger.Info("System tray terminated")
|
|
}
|
|
|
|
|