package action import ( "context" "errors" "fmt" "sync" "time" pwin "zerorigin/internal/platform/windows" ) func init() { // 单例:alternate 模式的往返方向状态必须跨执行保持 //(引擎每次执行都通过工厂获取实例,非单例会丢失状态)。 Register("switch_virtual_desktop", func() Action { return switchDesktopShared }) } // switchDesktopShared 为共享的切换桌面动作实例。 var switchDesktopShared = &switchDesktopAction{} // 切换实现变量化,便于单测注入 fake(生产路径仍为平台层函数)。 var ( switchDesktopCOM = pwin.SwitchDesktopCOM switchDesktopViaTaskView = pwin.SwitchDesktopViaTaskView virtualDesktopCount = pwin.VirtualDesktopCount ) // switchDesktopAction 切换虚拟桌面。 // mode: alternate(往返)/ left / right。 // method: com(默认,COM 直调,无快捷键依赖)/ taskview(Win+Tab 导航)/ hotkey(Win+Ctrl+方向键, // 仅当系统快捷键未被占用时可用)。com 普通失败时回退 taskview;单桌面时直接报错, // 避免任务视图停留前台后被全屏检测误判。 type switchDesktopAction struct { mu sync.Mutex next int32 // alternate 模式下上次切换方向,用于往返 } func (a *switchDesktopAction) ID() string { return "switch_virtual_desktop" } func (a *switchDesktopAction) Name() string { return "切换虚拟桌面" } func (a *switchDesktopAction) Schema() ActionSchema { return ActionSchema{Params: []ParamSpec{ {Name: "mode", Label: "切换方向", Type: "select", Required: true, Default: "alternate", Options: []string{"alternate", "left", "right"}, Labels: []string{"交替往返", "上一个桌面", "下一个桌面"}, Help: "alternate:左右交替往返;left/right:固定方向"}, {Name: "method", Label: "切换方式", Type: "select", Required: false, Default: "com", Options: []string{"com", "taskview", "hotkey"}, Labels: []string{"COM 直调(推荐)", "任务视图导航", "Win+Ctrl+方向键"}, Help: "com:COM 直调(推荐);taskview:Win+Tab 导航;hotkey:Win+Ctrl+方向键"}, {Name: "taskview_delay_ms", Label: "Task View 等待(ms)", Type: "int", Required: false, Default: 400, Help: "taskview 方式下等待切换动画的毫秒数;<=0 按 400ms"}, }} } func (a *switchDesktopAction) Validate(params map[string]any) error { mode := GetString(params, "mode") switch mode { case "alternate", "left", "right": default: return fmt.Errorf("switch_virtual_desktop: invalid mode %q (want alternate|left|right)", mode) } method := GetString(params, "method") switch method { case "", "com", "taskview", "hotkey": default: return fmt.Errorf("switch_virtual_desktop: invalid method %q (want com|taskview|hotkey)", method) } if d := GetInt(params, "taskview_delay_ms"); d < 0 { return fmt.Errorf("switch_virtual_desktop: taskview_delay_ms must be >= 0") } return nil } func (a *switchDesktopAction) Execute(ctx context.Context, params map[string]any) error { mode := GetString(params, "mode") var dir int32 switch mode { case "left": dir = -1 case "right": dir = 1 default: // alternate a.mu.Lock() dir = a.next if dir == 0 { dir = 1 // 首次默认向右 } a.next = -dir a.mu.Unlock() } method := GetString(params, "method") delay := time.Duration(GetInt(params, "taskview_delay_ms")) * time.Millisecond // 单桌面时 taskview / hotkey 都无法切换;任务视图还会停留在前台, // 被全屏检测误判后抑制全部规则,因此提前返回明确错误。 if method == "taskview" || method == "hotkey" { if n, err := virtualDesktopCount(); err == nil && n < 2 { return fmt.Errorf("%w (found %d)", pwin.ErrNotEnoughDesktops, n) } } switch method { case "hotkey": return pwin.SwitchVirtualDesktop(dir) case "taskview": return switchDesktopViaTaskView(dir, delay) default: // com:COM 直调;仅非单桌面错误才回退 taskview if err := switchDesktopCOM(dir); err == nil { return nil } else if errors.Is(err, pwin.ErrNotEnoughDesktops) { return err } return switchDesktopViaTaskView(dir, delay) } }