microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/autoShell/CommandDispatcher.cs
138lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System; |
| 5 | using System.Collections.Generic; |
| 6 | using autoShell.Handlers; |
| 7 | using autoShell.Handlers.Settings; |
| 8 | using autoShell.Logging; |
| 9 | using autoShell.Services; |
| 10 | using Newtonsoft.Json.Linq; |
| 11 | |
| 12 | namespace autoShell; |
| 13 | |
| 14 | /// <summary> |
| 15 | /// Routes incoming JSON commands to the appropriate handler via a direct dictionary lookup. |
| 16 | /// </summary> |
| 17 | internal class CommandDispatcher |
| 18 | { |
| 19 | private readonly Dictionary<string, ICommandHandler> _handlers = new(StringComparer.OrdinalIgnoreCase); |
| 20 | private readonly ILogger _logger; |
| 21 | |
| 22 | public CommandDispatcher(ILogger logger) |
| 23 | { |
| 24 | _logger = logger; |
| 25 | } |
| 26 | |
| 27 | /// <summary> |
| 28 | /// Creates a <see cref="CommandDispatcher"/> with all production services and handlers registered. |
| 29 | /// </summary> |
| 30 | public static CommandDispatcher Create(ILogger logger) |
| 31 | { |
| 32 | return Create( |
| 33 | logger, |
| 34 | new WindowsRegistryService(), |
| 35 | new WindowsSystemParametersService(), |
| 36 | new WindowsProcessService(), |
| 37 | new WindowsAudioService(logger), |
| 38 | new WindowsAppRegistry(logger), |
| 39 | new WindowsDebuggerService(), |
| 40 | new WindowsBrightnessService(logger), |
| 41 | new WindowsDisplayService(logger), |
| 42 | new WindowsWindowService(logger), |
| 43 | new WindowsNetworkService(logger), |
| 44 | new WindowsVirtualDesktopService(logger) |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | /// <summary> |
| 49 | /// Creates a <see cref="CommandDispatcher"/> with the specified services, enabling integration testing |
| 50 | /// with mock services while exercising real handler wiring. |
| 51 | /// </summary> |
| 52 | internal static CommandDispatcher Create( |
| 53 | ILogger logger, |
| 54 | IRegistryService registry, |
| 55 | ISystemParametersService systemParams, |
| 56 | IProcessService process, |
| 57 | IAudioService audio, |
| 58 | IAppRegistry appRegistry, |
| 59 | IDebuggerService debugger, |
| 60 | IBrightnessService brightness, |
| 61 | IDisplayService display, |
| 62 | IWindowService window, |
| 63 | INetworkService network, |
| 64 | IVirtualDesktopService virtualDesktop) |
| 65 | { |
| 66 | var dispatcher = new CommandDispatcher(logger); |
| 67 | dispatcher.Register( |
| 68 | new AudioCommandHandler(audio), |
| 69 | new AppCommandHandler(appRegistry, process, window, logger), |
| 70 | new WindowCommandHandler(appRegistry, window), |
| 71 | new ThemeCommandHandler(registry, process, systemParams), |
| 72 | new VirtualDesktopCommandHandler(appRegistry, window, virtualDesktop, logger), |
| 73 | new NetworkCommandHandler(network, process, logger), |
| 74 | new DisplayCommandHandler(display, logger), |
| 75 | new TaskbarSettingsHandler(registry), |
| 76 | new DisplaySettingsHandler(registry, process, brightness, logger), |
| 77 | new PersonalizationSettingsHandler(registry, process), |
| 78 | new MouseSettingsHandler(systemParams, process, logger), |
| 79 | new AccessibilitySettingsHandler(registry, process), |
| 80 | new PrivacySettingsHandler(registry), |
| 81 | new PowerSettingsHandler(registry, process), |
| 82 | new FileExplorerSettingsHandler(registry), |
| 83 | new SystemSettingsHandler(registry, process, logger), |
| 84 | new SystemCommandHandler(process, debugger) |
| 85 | ); |
| 86 | |
| 87 | return dispatcher; |
| 88 | } |
| 89 | |
| 90 | /// <summary> |
| 91 | /// Registers one or more command handlers with the dispatcher. |
| 92 | /// </summary> |
| 93 | public void Register(params ICommandHandler[] handlers) |
| 94 | { |
| 95 | foreach (var handler in handlers) |
| 96 | { |
| 97 | foreach (string command in handler.SupportedCommands) |
| 98 | { |
| 99 | _handlers[command] = handler; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// <summary> |
| 105 | /// Dispatches all commands in a JSON object to their handlers. |
| 106 | /// </summary> |
| 107 | /// <returns>True if a "quit" command was encountered; otherwise false.</returns> |
| 108 | public bool Dispatch(JObject root) |
| 109 | { |
| 110 | foreach (var kvp in root) |
| 111 | { |
| 112 | string key = kvp.Key; |
| 113 | |
| 114 | if (key == "quit") |
| 115 | { |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | try |
| 120 | { |
| 121 | if (_handlers.TryGetValue(key, out ICommandHandler handler)) |
| 122 | { |
| 123 | string value = kvp.Value?.ToString(); |
| 124 | handler.Handle(key, value, kvp.Value); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | _logger.Debug("Unknown command: " + key); |
| 129 | } |
| 130 | } |
| 131 | catch (Exception ex) |
| 132 | { |
| 133 | _logger.Error(ex); |
| 134 | } |
| 135 | } |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | |