microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/autoShell/Handlers/VirtualDesktopCommandHandler.cs
91lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System; |
| 5 | using System.Collections.Generic; |
| 6 | using autoShell.Logging; |
| 7 | using autoShell.Services; |
| 8 | using Newtonsoft.Json.Linq; |
| 9 | |
| 10 | namespace autoShell.Handlers; |
| 11 | |
| 12 | /// <summary> |
| 13 | /// Handles virtual desktop commands: CreateDesktop, MoveWindowToDesktop, NextDesktop, |
| 14 | /// PinWindow, PreviousDesktop, and SwitchDesktop. |
| 15 | /// </summary> |
| 16 | internal class VirtualDesktopCommandHandler : ICommandHandler |
| 17 | { |
| 18 | private readonly IAppRegistry _appRegistry; |
| 19 | private readonly ILogger _logger; |
| 20 | private readonly IVirtualDesktopService _virtualDesktop; |
| 21 | private readonly IWindowService _window; |
| 22 | |
| 23 | public VirtualDesktopCommandHandler(IAppRegistry appRegistry, IWindowService window, IVirtualDesktopService virtualDesktop, ILogger logger) |
| 24 | { |
| 25 | _appRegistry = appRegistry; |
| 26 | _logger = logger; |
| 27 | _virtualDesktop = virtualDesktop; |
| 28 | _window = window; |
| 29 | } |
| 30 | |
| 31 | /// <inheritdoc/> |
| 32 | public IEnumerable<string> SupportedCommands { get; } = |
| 33 | [ |
| 34 | "CreateDesktop", |
| 35 | "MoveWindowToDesktop", |
| 36 | "NextDesktop", |
| 37 | "PinWindow", |
| 38 | "PreviousDesktop", |
| 39 | "SwitchDesktop", |
| 40 | ]; |
| 41 | |
| 42 | /// <inheritdoc/> |
| 43 | public void Handle(string key, string value, JToken rawValue) |
| 44 | { |
| 45 | switch (key) |
| 46 | { |
| 47 | case "CreateDesktop": |
| 48 | _virtualDesktop.CreateDesktops(value); |
| 49 | break; |
| 50 | |
| 51 | case "MoveWindowToDesktop": |
| 52 | string process = rawValue.SelectToken("process")?.ToString(); |
| 53 | string desktop = rawValue.SelectToken("desktop")?.ToString(); |
| 54 | if (!string.IsNullOrEmpty(process) && !string.IsNullOrEmpty(desktop)) |
| 55 | { |
| 56 | string resolvedName = _appRegistry.ResolveProcessName(process); |
| 57 | IntPtr hWnd = _window.FindProcessWindowHandle(resolvedName); |
| 58 | if (hWnd != IntPtr.Zero) |
| 59 | { |
| 60 | _virtualDesktop.MoveWindowToDesktop(hWnd, desktop); |
| 61 | } |
| 62 | } |
| 63 | break; |
| 64 | |
| 65 | case "NextDesktop": |
| 66 | _virtualDesktop.NextDesktop(); |
| 67 | break; |
| 68 | |
| 69 | case "PinWindow": |
| 70 | string pinProcess = _appRegistry.ResolveProcessName(value); |
| 71 | IntPtr pinHWnd = _window.FindProcessWindowHandle(pinProcess); |
| 72 | if (pinHWnd != IntPtr.Zero) |
| 73 | { |
| 74 | _virtualDesktop.PinWindow(pinHWnd); |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | _logger.Warning($"The window handle for '{value}' could not be found"); |
| 79 | } |
| 80 | break; |
| 81 | |
| 82 | case "PreviousDesktop": |
| 83 | _virtualDesktop.PreviousDesktop(); |
| 84 | break; |
| 85 | |
| 86 | case "SwitchDesktop": |
| 87 | _virtualDesktop.SwitchDesktop(value); |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |