package action import ( "fmt" "runtime" "sync" "sync/atomic" "time" pwin "zerorigin/internal/platform/windows" ) // toastEnabled 为全局通知总开关(配置 global.notify,默认开启)。 // 所有右下角系统 Toast(自动调节提示与显式的 show_notification)都受它 // 控制;屏幕底部的调节提示条(internal/osd)不受影响。 var toastEnabled atomic.Bool func init() { toastEnabled.Store(true) } // SetToastEnabled 设置是否允许自动调节类 Toast。 // 关闭时立即取消尚未显示的待发通知,避免"关掉后还弹最后一个"。 func SetToastEnabled(on bool) { toastEnabled.Store(on) if on { return } toastMu.Lock() if toastTimer != nil { toastTimer.Stop() toastTimer = nil } toastNext = nil toastMu.Unlock() } // 音量 Toast 去抖实现:快速连按时合并为最后一次显示, // 避免 Win11 系统 OSD 数字太小/消失太快时 Toast 弹窗闪烁。 // // 行为(默认窗口 500ms): // - 首次调用 → 启动计时器,记录待显示内容; // - 窗口内再次调用 → 取消旧计时器,更新待显示内容,重启计时器; // - 计时器到期 → 只显示合并后的最终内容(最后一次的状态)。 // // 因此单次按键会在 ~500ms 后弹一个 Toast;连按 5 次只在最后一次后 // 500ms 弹 1 个 Toast。 // volumeToastDebounceWindow 音量 Toast 去抖窗口(测试可替换)。 var volumeToastDebounceWindow = 500 * time.Millisecond const ( // volumeToastTitle 音量调节 Toast 标题。 volumeToastTitle = "音量调节" // volumeToastMuteTitle 静音 Toast 标题。 volumeToastMuteTitle = "静音" ) // notifyFunc 底层系统通知函数(变量便于测试注入替换)。 var notifyFunc = notifyDesktop // toastContent 待显示的 Toast 内容。 type toastContent struct { title string text string } var ( toastMu sync.Mutex toastTimer *time.Timer toastNext *toastContent ) // notifyVolume 在音量/静音动作成功后显示系统 Toast(默认 500ms 去抖合并)。 // level: 调节后的音量百分比(仅 up/down 使用;静音动作传 0,文本不依赖 level)。 // kind: up | down | mute | unmute | mute_unknown。 // // 保留 allowed=true 的便捷入口给测试与内部默认路径;动作实现应调用 // notifyVolumeAllowed,把规则级开关明确带进来。 func notifyVolume(level int, kind string) { notifyVolumeAllowed(level, kind, true) } // notifyVolumeAllowed 按规则级通知开关显示音量/静音 Toast。 func notifyVolumeAllowed(level int, kind string, allowed bool) { var title, text string switch kind { case "up", "down": title, text = volumeToastTitle, fmt.Sprintf("音量: %d%%", level) case "mute": title, text = volumeToastMuteTitle, "已静音" case "unmute": title, text = volumeToastMuteTitle, "未静音" default: title, text = volumeToastMuteTitle, "静音已切换" } scheduleToast(title, text, allowed) } // scheduleToast 去抖调度:窗口内多次调用只显示最后一次内容。 // allowed 是调用方在异步路径启动前捕获的规则级开关;全局开关关闭时 // 同样直接忽略,不创建计时器。 func scheduleToast(title, text string, allowed bool) { if !allowed || !toastEnabled.Load() { return } toastMu.Lock() defer toastMu.Unlock() toastNext = &toastContent{title: title, text: text} if toastTimer != nil { toastTimer.Stop() } toastTimer = time.AfterFunc(volumeToastDebounceWindow, flushToast) } // flushToast 计时器到期:显示合并后的最终内容并清空待显示状态。 // ⚠️ 运行在 time.AfterFunc 的 goroutine 中,无上层 recover: // panic 会直接拖垮整个进程(Go panic 崩溃不产生任何系统记录)。 // 因此这里必须兜底:recover 后记录崩溃到 crash.log,不中断进程。 func flushToast() { defer func() { if r := recover(); r != nil { pwin.LogGoPanic("volumeToast.flushToast", r, debugStackBuf()) } }() toastMu.Lock() next := toastNext toastNext = nil toastMu.Unlock() if next == nil || !toastEnabled.Load() { return } if err := notifyFunc(next.title, next.text); err != nil { logger.Warn("volume toast failed", "err", err) } } // debugStackBuf 返回当前 goroutine 堆栈(崩溃取证用)。 func debugStackBuf() []byte { buf := make([]byte, 32<<10) n := runtime.Stack(buf, false) return buf[:n] }