1
0

settings_gui.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // ============================================================================
  2. //go:build !service
  3. // settings_gui.go — 应用配置的 GUI 侧运行时应用
  4. //
  5. // 把配置应用到 GUI 运行时:重新注册全局热键、窗口置顶、开机启动同步、
  6. // 内置 API 服务启停、滚动条窗口同步。这些操作依赖 Wails 的 app/mainWindow
  7. // 或 GUI 专属功能,独立服务模式(-tags service)不编译本文件。
  8. // ============================================================================
  9. package main
  10. import (
  11. "log"
  12. "strings"
  13. "sync"
  14. )
  15. // 当前已注册的热键(用于保存设置时先注销旧热键)
  16. var (
  17. hotkeyMu sync.Mutex
  18. activeHotkey string
  19. activeQuickHotkey string
  20. activeProxyHotkey string
  21. // hotkeyStatus 记录各热键最近的注册结果,供设置页提示:
  22. // key 为 hotkey / quickHotkey / proxyHotkey,value 为失败原因(空=已生效)。
  23. // 典型失败:该组合键已被其他程序占用(Register 返回冲突错误)。
  24. hotkeyStatus = map[string]string{
  25. "hotkey": "",
  26. "quickHotkey": "",
  27. "proxyHotkey": "",
  28. }
  29. )
  30. // mergeRuntimeState 保留磁盘上由运行时自动保存的窗口尺寸与滚动条位置,
  31. // 避免前端保存其他设置时用页面加载时的旧值覆盖它们。
  32. func mergeRuntimeState(settings AppSettings) AppSettings {
  33. prev := loadSettings()
  34. settings.WindowWidth, settings.WindowHeight = prev.WindowWidth, prev.WindowHeight
  35. settings.StockTicker.X, settings.StockTicker.Y = prev.StockTicker.X, prev.StockTicker.Y
  36. settings.NewsTicker.X, settings.NewsTicker.Y = prev.NewsTicker.X, prev.NewsTicker.Y
  37. return settings
  38. }
  39. // hotkeyConflictMessage 把热键注册失败原因整理为设置页可展示的中文提示。
  40. // 最常见原因:组合键已被其他程序占用(Register 返回冲突错误),
  41. // 此时引导用户换一个组合键;其余错误原样保留供排查。
  42. func hotkeyConflictMessage(hotkey string, err error) string {
  43. if err == nil {
  44. return ""
  45. }
  46. msg := strings.ToLower(err.Error())
  47. if strings.Contains(msg, "already registered") || strings.Contains(msg, "already in use") {
  48. return "该组合键已被其他程序占用,请换一个组合键"
  49. }
  50. return "注册失败:" + err.Error()
  51. }
  52. // GetHotkeyStatus 返回三个热键的注册状态(空字符串=已生效;非空=失败原因),
  53. // 供设置页在热键输入框旁显示冲突提示。key:hotkey / quickHotkey / proxyHotkey。
  54. func GetHotkeyStatus() map[string]string {
  55. hotkeyMu.Lock()
  56. defer hotkeyMu.Unlock()
  57. out := make(map[string]string, len(hotkeyStatus))
  58. for k, v := range hotkeyStatus {
  59. out[k] = v
  60. }
  61. return out
  62. }
  63. // applySettings 把配置应用到运行时:重新注册热键、设置窗口置顶、同步开机启动
  64. func applySettings(settings AppSettings) {
  65. hotkeyMu.Lock()
  66. if settings.Hotkey != activeHotkey {
  67. if activeHotkey != "" {
  68. _ = app.GlobalShortcut.Unregister(activeHotkey) // 注销旧热键,避免冲突
  69. }
  70. if err := app.GlobalShortcut.Register(settings.Hotkey, showMainWindow); err != nil {
  71. log.Printf("register hotkey %q failed: %v", settings.Hotkey, err)
  72. hotkeyStatus["hotkey"] = hotkeyConflictMessage(settings.Hotkey, err) // 记录失败原因,设置页提示
  73. } else {
  74. activeHotkey = settings.Hotkey // 记录当前生效的热键
  75. hotkeyStatus["hotkey"] = "" // 注册成功,清除提示
  76. }
  77. }
  78. // 「快捷」菜单热键:独立注册,触发时在鼠标位置弹出快捷启动菜单
  79. if settings.QuickHotkey != activeQuickHotkey {
  80. if activeQuickHotkey != "" {
  81. _ = app.GlobalShortcut.Unregister(activeQuickHotkey)
  82. }
  83. if err := app.GlobalShortcut.Register(settings.QuickHotkey, showQuickMenuAtCursor); err != nil {
  84. log.Printf("register quick hotkey %q failed: %v", settings.QuickHotkey, err)
  85. hotkeyStatus["quickHotkey"] = hotkeyConflictMessage(settings.QuickHotkey, err)
  86. } else {
  87. activeQuickHotkey = settings.QuickHotkey
  88. hotkeyStatus["quickHotkey"] = "" // 注册成功,清除提示
  89. }
  90. }
  91. // 「网络代理」开关热键:留空则不设热键(注销旧热键并跳过注册)
  92. if settings.ProxyHotkey != activeProxyHotkey {
  93. if activeProxyHotkey != "" {
  94. _ = app.GlobalShortcut.Unregister(activeProxyHotkey)
  95. }
  96. if strings.TrimSpace(settings.ProxyHotkey) == "" {
  97. activeProxyHotkey = "" // 用户清空热键:不注册,仅记录空值
  98. hotkeyStatus["proxyHotkey"] = ""
  99. } else if err := app.GlobalShortcut.Register(settings.ProxyHotkey, toggleProxyByHotkey); err != nil {
  100. log.Printf("register proxy hotkey %q failed: %v", settings.ProxyHotkey, err)
  101. activeProxyHotkey = "" // 注册失败:视为未设,避免下次比较时误判
  102. hotkeyStatus["proxyHotkey"] = hotkeyConflictMessage(settings.ProxyHotkey, err)
  103. } else {
  104. activeProxyHotkey = settings.ProxyHotkey
  105. hotkeyStatus["proxyHotkey"] = "" // 注册成功,清除提示
  106. }
  107. }
  108. hotkeyMu.Unlock()
  109. if mainWindow != nil {
  110. mainWindow.SetAlwaysOnTop(settings.AlwaysOnTop) // 窗口置顶开关
  111. }
  112. if settings.Autostart {
  113. _ = app.Autostart.Enable() // 写入系统开机启动项
  114. } else {
  115. _ = app.Autostart.Disable() // 移除开机启动项
  116. }
  117. // 内置 API 服务:按「服务自动启动」开关决定启动或停止(地址变化时重启)
  118. if settings.APIAutoStart {
  119. ensureAPIServer(settings.APIAddr)
  120. } else {
  121. StopAPIServer()
  122. }
  123. // 服务端口兜底:Windows 服务未运行时由界面进程接管,保证电源 API 始终可用
  124. ensureUIServiceAPI(settings.ServiceAddr)
  125. syncTickerWindows(settings) // 按配置创建 / 关闭 / 刷新滚动条窗口
  126. }