mirror of
https://github.com/jahruz67/wisp-open.git
synced 2026-08-08 18:14:08 +00:00
7
This commit is contained in:
@@ -81,18 +81,35 @@ func (l *Listener) Start() {
|
||||
}
|
||||
|
||||
l.hk = xhk.New(mods, key)
|
||||
if err := l.hk.Register(); err != nil {
|
||||
logger.Error("Failed to register hotkey %s: %v", l.shortcut, err)
|
||||
l.hk = nil
|
||||
l.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
l.isListening = true
|
||||
hkToRegister := l.hk
|
||||
shortcutToRegister := l.shortcut
|
||||
l.mu.Unlock()
|
||||
logger.Info("Hotkey listener started, waiting for %s", l.shortcut)
|
||||
|
||||
go l.eventLoop(l.hk)
|
||||
go func() {
|
||||
if err := hkToRegister.Register(); err != nil {
|
||||
logger.Error("Failed to register hotkey %s: %v", shortcutToRegister, err)
|
||||
l.mu.Lock()
|
||||
if l.hk == hkToRegister {
|
||||
l.hk = nil
|
||||
l.isListening = false
|
||||
}
|
||||
l.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
l.mu.Lock()
|
||||
if l.hk != hkToRegister {
|
||||
// Was stopped or updated while we were registering
|
||||
l.mu.Unlock()
|
||||
hkToRegister.Unregister()
|
||||
return
|
||||
}
|
||||
l.mu.Unlock()
|
||||
|
||||
logger.Info("Hotkey listener started, waiting for %s", shortcutToRegister)
|
||||
go l.eventLoop(hkToRegister)
|
||||
}()
|
||||
}
|
||||
|
||||
// Stop terminates the hotkey listener.
|
||||
|
||||
@@ -11,15 +11,38 @@ const appName = "wis-free-v3"
|
||||
|
||||
// desktopFileTemplate is filled with execLine built from the absolute binary path.
|
||||
// Paths with spaces must be quoted per the Desktop Entry spec.
|
||||
const desktopFileTemplate = `[Desktop Entry]
|
||||
const baseDesktopFileTemplate = `[Desktop Entry]
|
||||
Type=Application
|
||||
Name=WIS Free V3
|
||||
%s
|
||||
Terminal=false
|
||||
Categories=Utility;
|
||||
X-GNOME-Autostart-enabled=true
|
||||
`
|
||||
|
||||
const autostartDesktopFileTemplate = baseDesktopFileTemplate + "X-GNOME-Autostart-enabled=true\n"
|
||||
|
||||
func EnsureDesktopFile() error {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
appsDir := filepath.Join(home, ".local", "share", "applications")
|
||||
if err := os.MkdirAll(appsDir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
desktopPath := filepath.Join(appsDir, appName+".desktop")
|
||||
|
||||
exePath, err := getExecutablePath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
content := fmt.Sprintf(baseDesktopFileTemplate, desktopExecField(exePath))
|
||||
return os.WriteFile(desktopPath, []byte(content), 0644)
|
||||
}
|
||||
|
||||
func getAutostartPath() (string, error) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
@@ -80,7 +103,7 @@ func AddToStartup() error {
|
||||
return fmt.Errorf("failed to get executable path: %w", err)
|
||||
}
|
||||
|
||||
content := fmt.Sprintf(desktopFileTemplate, desktopExecField(exePath))
|
||||
content := fmt.Sprintf(autostartDesktopFileTemplate, desktopExecField(exePath))
|
||||
if err := os.WriteFile(autostartPath, []byte(content), 0644); err != nil {
|
||||
return fmt.Errorf("failed to write autostart file: %w", err)
|
||||
}
|
||||
|
||||
@@ -30,3 +30,7 @@ func IsInStartup() bool {
|
||||
func IsProcessRunning(pid int) bool {
|
||||
return linux.IsProcessRunning(pid)
|
||||
}
|
||||
|
||||
func EnsureDesktopFile() error {
|
||||
return linux.EnsureDesktopFile()
|
||||
}
|
||||
|
||||
@@ -30,3 +30,8 @@ func IsInStartup() bool {
|
||||
func IsProcessRunning(pid int) bool {
|
||||
return windows.IsProcessRunning(pid)
|
||||
}
|
||||
|
||||
func EnsureDesktopFile() error {
|
||||
// Not applicable on Windows
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -37,6 +37,8 @@ var trayLabel = "wis-free-v3"
|
||||
|
||||
// Menu item references for dynamic updates
|
||||
var statusMenuItem *systray.MenuItem
|
||||
var triggerCountItem *systray.MenuItem
|
||||
var triggerCount int
|
||||
|
||||
// Start initializes and runs the system tray.
|
||||
// This function blocks until the tray is terminated.
|
||||
@@ -74,6 +76,9 @@ func onReady(app App) {
|
||||
statusMenuItem = systray.AddMenuItem("Status: Ready", "Current application status")
|
||||
statusMenuItem.Disable()
|
||||
|
||||
triggerCountItem = systray.AddMenuItem("Shortcut detected: 0 times", "Troubleshooting counter")
|
||||
triggerCountItem.Disable()
|
||||
|
||||
systray.AddSeparator()
|
||||
|
||||
menuSettings := systray.AddMenuItem("Settings", "Open settings window")
|
||||
@@ -159,6 +164,14 @@ func UpdateStatus(status string) {
|
||||
}
|
||||
}
|
||||
|
||||
// IncrementTriggerCount increments the troubleshooting counter in the tray.
|
||||
func IncrementTriggerCount() {
|
||||
triggerCount++
|
||||
if triggerCountItem != nil {
|
||||
triggerCountItem.SetTitle(fmt.Sprintf("Shortcut detected: %d times", triggerCount))
|
||||
}
|
||||
}
|
||||
|
||||
// onExit is called when the system tray is shutting down.
|
||||
func onExit() {
|
||||
logger.Info("System tray terminated")
|
||||
|
||||
Reference in New Issue
Block a user