| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- // ============================================================================
- //go:build !service
- // settings_gui.go — 应用配置的 GUI 侧运行时应用
- //
- // 把配置应用到 GUI 运行时:重新注册全局热键、窗口置顶、开机启动同步、
- // 内置 API 服务启停、滚动条窗口同步。这些操作依赖 Wails 的 app/mainWindow
- // 或 GUI 专属功能,独立服务模式(-tags service)不编译本文件。
- // ============================================================================
- package main
- import (
- "log"
- "strings"
- "sync"
- )
- // 当前已注册的热键(用于保存设置时先注销旧热键)
- var (
- hotkeyMu sync.Mutex
- activeHotkey string
- activeQuickHotkey string
- activeProxyHotkey string
- // hotkeyStatus 记录各热键最近的注册结果,供设置页提示:
- // key 为 hotkey / quickHotkey / proxyHotkey,value 为失败原因(空=已生效)。
- // 典型失败:该组合键已被其他程序占用(Register 返回冲突错误)。
- hotkeyStatus = map[string]string{
- "hotkey": "",
- "quickHotkey": "",
- "proxyHotkey": "",
- }
- )
- // mergeRuntimeState 保留磁盘上由运行时自动保存的窗口尺寸与滚动条位置,
- // 避免前端保存其他设置时用页面加载时的旧值覆盖它们。
- func mergeRuntimeState(settings AppSettings) AppSettings {
- prev := loadSettings()
- settings.WindowWidth, settings.WindowHeight = prev.WindowWidth, prev.WindowHeight
- settings.StockTicker.X, settings.StockTicker.Y = prev.StockTicker.X, prev.StockTicker.Y
- settings.NewsTicker.X, settings.NewsTicker.Y = prev.NewsTicker.X, prev.NewsTicker.Y
- return settings
- }
- // hotkeyConflictMessage 把热键注册失败原因整理为设置页可展示的中文提示。
- // 最常见原因:组合键已被其他程序占用(Register 返回冲突错误),
- // 此时引导用户换一个组合键;其余错误原样保留供排查。
- func hotkeyConflictMessage(hotkey string, err error) string {
- if err == nil {
- return ""
- }
- msg := strings.ToLower(err.Error())
- if strings.Contains(msg, "already registered") || strings.Contains(msg, "already in use") {
- return "该组合键已被其他程序占用,请换一个组合键"
- }
- return "注册失败:" + err.Error()
- }
- // GetHotkeyStatus 返回三个热键的注册状态(空字符串=已生效;非空=失败原因),
- // 供设置页在热键输入框旁显示冲突提示。key:hotkey / quickHotkey / proxyHotkey。
- func GetHotkeyStatus() map[string]string {
- hotkeyMu.Lock()
- defer hotkeyMu.Unlock()
- out := make(map[string]string, len(hotkeyStatus))
- for k, v := range hotkeyStatus {
- out[k] = v
- }
- return out
- }
- // applySettings 把配置应用到运行时:重新注册热键、设置窗口置顶、同步开机启动
- func applySettings(settings AppSettings) {
- hotkeyMu.Lock()
- if settings.Hotkey != activeHotkey {
- if activeHotkey != "" {
- _ = app.GlobalShortcut.Unregister(activeHotkey) // 注销旧热键,避免冲突
- }
- if err := app.GlobalShortcut.Register(settings.Hotkey, showMainWindow); err != nil {
- log.Printf("register hotkey %q failed: %v", settings.Hotkey, err)
- hotkeyStatus["hotkey"] = hotkeyConflictMessage(settings.Hotkey, err) // 记录失败原因,设置页提示
- } else {
- activeHotkey = settings.Hotkey // 记录当前生效的热键
- hotkeyStatus["hotkey"] = "" // 注册成功,清除提示
- }
- }
- // 「快捷」菜单热键:独立注册,触发时在鼠标位置弹出快捷启动菜单
- if settings.QuickHotkey != activeQuickHotkey {
- if activeQuickHotkey != "" {
- _ = app.GlobalShortcut.Unregister(activeQuickHotkey)
- }
- if err := app.GlobalShortcut.Register(settings.QuickHotkey, showQuickMenuAtCursor); err != nil {
- log.Printf("register quick hotkey %q failed: %v", settings.QuickHotkey, err)
- hotkeyStatus["quickHotkey"] = hotkeyConflictMessage(settings.QuickHotkey, err)
- } else {
- activeQuickHotkey = settings.QuickHotkey
- hotkeyStatus["quickHotkey"] = "" // 注册成功,清除提示
- }
- }
- // 「网络代理」开关热键:留空则不设热键(注销旧热键并跳过注册)
- if settings.ProxyHotkey != activeProxyHotkey {
- if activeProxyHotkey != "" {
- _ = app.GlobalShortcut.Unregister(activeProxyHotkey)
- }
- if strings.TrimSpace(settings.ProxyHotkey) == "" {
- activeProxyHotkey = "" // 用户清空热键:不注册,仅记录空值
- hotkeyStatus["proxyHotkey"] = ""
- } else if err := app.GlobalShortcut.Register(settings.ProxyHotkey, toggleProxyByHotkey); err != nil {
- log.Printf("register proxy hotkey %q failed: %v", settings.ProxyHotkey, err)
- activeProxyHotkey = "" // 注册失败:视为未设,避免下次比较时误判
- hotkeyStatus["proxyHotkey"] = hotkeyConflictMessage(settings.ProxyHotkey, err)
- } else {
- activeProxyHotkey = settings.ProxyHotkey
- hotkeyStatus["proxyHotkey"] = "" // 注册成功,清除提示
- }
- }
- hotkeyMu.Unlock()
- if mainWindow != nil {
- mainWindow.SetAlwaysOnTop(settings.AlwaysOnTop) // 窗口置顶开关
- }
- if settings.Autostart {
- _ = app.Autostart.Enable() // 写入系统开机启动项
- } else {
- _ = app.Autostart.Disable() // 移除开机启动项
- }
- // 内置 API 服务:按「服务自动启动」开关决定启动或停止(地址变化时重启)
- if settings.APIAutoStart {
- ensureAPIServer(settings.APIAddr)
- } else {
- StopAPIServer()
- }
- // 服务端口兜底:Windows 服务未运行时由界面进程接管,保证电源 API 始终可用
- ensureUIServiceAPI(settings.ServiceAddr)
- syncTickerWindows(settings) // 按配置创建 / 关闭 / 刷新滚动条窗口
- }
|