microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/autoShell/Handlers/VirtualDesktopActionHandler.cs
97lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System; |
| 5 | using autoShell.Handlers.Generated; |
| 6 | using autoShell.Logging; |
| 7 | using autoShell.Services; |
| 8 | |
| 9 | namespace autoShell.Handlers; |
| 10 | |
| 11 | /// <summary> |
| 12 | /// Handles virtual desktop commands: CreateDesktop, MoveWindowToDesktop, NextDesktop, |
| 13 | /// PinWindow, PreviousDesktop, and SwitchDesktop. |
| 14 | /// </summary> |
| 15 | internal class VirtualDesktopActionHandler : ActionHandlerBase |
| 16 | { |
| 17 | private readonly IAppRegistry _appRegistry; |
| 18 | private readonly ILogger _logger; |
| 19 | private readonly IVirtualDesktopService _virtualDesktop; |
| 20 | private readonly IWindowService _window; |
| 21 | |
| 22 | public VirtualDesktopActionHandler(IAppRegistry appRegistry, IWindowService window, IVirtualDesktopService virtualDesktop, ILogger logger) |
| 23 | { |
| 24 | _appRegistry = appRegistry; |
| 25 | _logger = logger; |
| 26 | _virtualDesktop = virtualDesktop; |
| 27 | _window = window; |
| 28 | AddAction<CreateDesktopParams>("CreateDesktop", HandleCreateDesktop); |
| 29 | AddAction<MoveWindowToDesktopParams>("MoveWindowToDesktop", HandleMoveWindowToDesktop); |
| 30 | AddAction<NextDesktopParams>("NextDesktop", HandleNextDesktop); |
| 31 | AddAction<PinWindowParams>("PinWindow", HandlePinWindow); |
| 32 | AddAction<PreviousDesktopParams>("PreviousDesktop", HandlePreviousDesktop); |
| 33 | AddAction<SwitchDesktopParams>("SwitchDesktop", HandleSwitchDesktop); |
| 34 | } |
| 35 | |
| 36 | private ActionResult HandleCreateDesktop(CreateDesktopParams p) |
| 37 | { |
| 38 | string namesJson = p.Names != null |
| 39 | ? System.Text.Json.JsonSerializer.Serialize(p.Names) |
| 40 | : "[\"desktop 1\"]"; |
| 41 | _virtualDesktop.CreateDesktops(namesJson); |
| 42 | return ActionResult.Ok("Created new desktop(s)"); |
| 43 | } |
| 44 | |
| 45 | private ActionResult HandleMoveWindowToDesktop(MoveWindowToDesktopParams p) |
| 46 | { |
| 47 | string process = p.Name; |
| 48 | string desktop = p.DesktopId.ToString(); |
| 49 | if (string.IsNullOrEmpty(process) || string.IsNullOrEmpty(desktop)) |
| 50 | { |
| 51 | return ActionResult.Fail("MoveWindowToDesktop requires name and desktopId"); |
| 52 | } |
| 53 | |
| 54 | string resolvedName = _appRegistry.ResolveProcessName(process); |
| 55 | IntPtr hWnd = _window.FindProcessWindowHandle(resolvedName); |
| 56 | if (hWnd == IntPtr.Zero) |
| 57 | { |
| 58 | return ActionResult.Fail($"Could not find window for '{process}'"); |
| 59 | } |
| 60 | |
| 61 | _virtualDesktop.MoveWindowToDesktop(hWnd, desktop); |
| 62 | return ActionResult.Ok($"Moved {process} to desktop {desktop}"); |
| 63 | } |
| 64 | |
| 65 | private ActionResult HandleNextDesktop(NextDesktopParams p) |
| 66 | { |
| 67 | _virtualDesktop.NextDesktop(); |
| 68 | return ActionResult.Ok("Switched to next desktop"); |
| 69 | } |
| 70 | |
| 71 | private ActionResult HandlePinWindow(PinWindowParams p) |
| 72 | { |
| 73 | string name = p.Name; |
| 74 | string pinProcess = _appRegistry.ResolveProcessName(name); |
| 75 | IntPtr pinHWnd = _window.FindProcessWindowHandle(pinProcess); |
| 76 | if (pinHWnd == IntPtr.Zero) |
| 77 | { |
| 78 | return ActionResult.Fail($"Could not find window for '{name}'"); |
| 79 | } |
| 80 | |
| 81 | _virtualDesktop.PinWindow(pinHWnd); |
| 82 | return ActionResult.Ok($"Pinned '{name}' to all desktops"); |
| 83 | } |
| 84 | |
| 85 | private ActionResult HandlePreviousDesktop(PreviousDesktopParams p) |
| 86 | { |
| 87 | _virtualDesktop.PreviousDesktop(); |
| 88 | return ActionResult.Ok("Switched to previous desktop"); |
| 89 | } |
| 90 | |
| 91 | private ActionResult HandleSwitchDesktop(SwitchDesktopParams p) |
| 92 | { |
| 93 | string desktopId = p.DesktopId.ToString(); |
| 94 | _virtualDesktop.SwitchDesktop(desktopId); |
| 95 | return ActionResult.Ok($"Switched to desktop {desktopId}"); |
| 96 | } |
| 97 | } |
| 98 | |