package action import ( "context" "fmt" "time" pwin "zerorigin/internal/platform/windows" ) func init() { Register("volume_up", func() Action { return &volumeStepAction{dir: 1} }) Register("volume_down", func() Action { return &volumeStepAction{dir: -1} }) Register("toggle_mute", func() Action { return &muteAction{} }) } // volumeStepAction 音量步进。 // steps: 百分比步进(默认 1 = ±1%)。 // method: media(默认,SendInput 媒体键 VK_VOLUME_UP/DOWN,由系统 Shell 处理: // // Win11 显示完整 OSD(设备名+滑块+数字+应用入口)且立即生效)/ // // com(IAudioEndpointVolume 直调,精确百分比,但 OSD 简化、个别设备无效)/ // hotkey(media 的别名,兼容旧配置)。 // // 背景:COM 直调返回 S_OK 且读回验证通过,但 Win11 音频服务未将其视为 // "系统级音量调节"→ OSD 简化为最小版,且若默认端点与应用实际聆听设备 // 不一致则听感无变化。媒体键走系统 Shell 路径,无此问题。 type volumeStepAction struct { dir int32 } func (a *volumeStepAction) ID() string { if a.dir > 0 { return "volume_up" } return "volume_down" } func (a *volumeStepAction) Name() string { if a.dir > 0 { return "音量增大" } return "音量减小" } func (a *volumeStepAction) Schema() ActionSchema { return ActionSchema{Params: []ParamSpec{ {Name: "steps", Type: "int", Required: false, Default: 1}, {Name: "method", Type: "string", Required: false, Default: "media"}, }} } func (a *volumeStepAction) Validate(params map[string]any) error { if steps := GetInt(params, "steps"); steps < 0 { return fmt.Errorf("volume: steps must be >= 0") } // 校验原始值(不能先归一化:否则非法值会被 volumeMethod 兜底成 media 而漏检)。 switch GetString(params, "method") { case "", "media", "com", "hotkey": default: return fmt.Errorf("volume: invalid method %q (want media|com|hotkey)", GetString(params, "method")) } return nil } func (a *volumeStepAction) Execute(ctx context.Context, params map[string]any) error { steps := GetInt(params, "steps") if steps <= 0 { steps = 1 } delta := a.dir * int32(steps) if volumeMethod(GetString(params, "method")) == "com" { // COM 直调(精确百分比,用户显式选择);失败时记录原因并回退媒体键。 if err := pwin.VolumeAdjustPercent(delta); err == nil { // 诊断读回:确认程序视角下音量确实变化(区分"程序没改到"与"改的不是聆听设备")。 if after, err2 := pwin.GetMasterVolumePercent(); err2 == nil { logger.Debug("volume adjusted", "action", a.ID(), "after_percent", after) notifyVolume(after, volumeStepKind(a.dir)) } return nil } else { logger.Warn("volume COM failed, falling back to media key", "action", a.ID(), "err", err) } } // 媒体键路径(默认):由系统 Shell 处理 → 完整 OSD + 立即生效。 n := volumeStepCount(delta) for i := 0; i < n; i++ { select { case <-ctx.Done(): return ctx.Err() default: } if err := pwin.VolumeStep(delta); err != nil { return err } // 连续按键留出间隔,避免系统吞掉相邻的相同按键事件。 if i < n-1 { time.Sleep(30 * time.Millisecond) } } // 调节成功后弹出系统 Toast 显示当前音量(带去抖,见 volumeToastDebounce.go)。 // 媒体键由系统 Shell 异步应用,稍候再读回,避免 Toast 显示切换前的旧值。 time.Sleep(60 * time.Millisecond) if after, err := pwin.GetMasterVolumePercent(); err == nil { notifyVolume(after, volumeStepKind(a.dir)) } else { logger.Debug("volume toast: read volume failed", "err", err) } return nil } // volumeStepKind 返回音量调节方向标签(供 Toast 内容使用)。 func volumeStepKind(dir int32) string { if dir > 0 { return "up" } return "down" } // muteAction 静音切换。 type muteAction struct{} func (a *muteAction) ID() string { return "toggle_mute" } func (a *muteAction) Name() string { return "静音切换" } func (a *muteAction) Schema() ActionSchema { return ActionSchema{Params: []ParamSpec{ {Name: "method", Type: "string", Required: false, Default: "media"}, }} } func (a *muteAction) Validate(params map[string]any) error { switch GetString(params, "method") { case "", "media", "com", "hotkey": return nil } return fmt.Errorf("toggle_mute: invalid method %q (want media|com|hotkey)", GetString(params, "method")) } func (a *muteAction) Execute(ctx context.Context, params map[string]any) error { if volumeMethod(GetString(params, "method")) == "com" { // COM 直调(精确,用户显式选择);失败时记录原因并回退媒体键。 if err := pwin.ToggleMuteCOM(); err == nil { notifyMuteResult() return nil } else { logger.Warn("mute COM failed, falling back to media key", "err", err) } } if err := pwin.VolumeMute(); err != nil { return err } notifyMuteResult() return nil } // notifyMuteResult 读回当前静音状态并弹出系统 Toast(去抖由 notifyVolume 处理)。 // 媒体键由 Shell 异步处理,先短暂等待再读回,避免读到切换前状态。 func notifyMuteResult() { time.Sleep(80 * time.Millisecond) for i := 0; i < 3; i++ { muted, err := pwin.GetMuteState() if err == nil { if muted { notifyVolume(0, "mute") } else { notifyVolume(0, "unmute") } return } time.Sleep(40 * time.Millisecond) } logger.Debug("volume toast: read mute state failed") notifyVolume(0, "mute_unknown") } // volumeMethod 归一化 method 参数:""(默认)→ media;hotkey 是 media 的别名。 // 非法值同样归一化为 media(Validate 会先行拦截,此处仅为兜底)。 func volumeMethod(raw string) string { switch raw { case "", "media", "hotkey": return "media" case "com": return "com" } return "media" } // volumeStepCount 返回媒体键路径需要发送的按键次数(delta 的绝对值)。 func volumeStepCount(delta int32) int { if delta < 0 { return int(-delta) } return int(delta) }