microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/autoShell/Handlers/DisplayCommandHandler.cs
112lines · 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 display commands: ListResolutions, SetScreenResolution, and SetTextSize. |
| 14 | /// </summary> |
| 15 | internal class DisplayCommandHandler : ICommandHandler |
| 16 | { |
| 17 | private readonly IDisplayService _display; |
| 18 | private readonly ILogger _logger; |
| 19 | |
| 20 | public DisplayCommandHandler(IDisplayService display, ILogger logger) |
| 21 | { |
| 22 | _display = display; |
| 23 | _logger = logger; |
| 24 | } |
| 25 | |
| 26 | /// <inheritdoc/> |
| 27 | public IEnumerable<string> SupportedCommands { get; } = |
| 28 | [ |
| 29 | "ListResolutions", |
| 30 | "SetScreenResolution", |
| 31 | "SetTextSize", |
| 32 | ]; |
| 33 | |
| 34 | /// <inheritdoc/> |
| 35 | public void Handle(string key, string value, JToken rawValue) |
| 36 | { |
| 37 | switch (key) |
| 38 | { |
| 39 | case "ListResolutions": |
| 40 | try |
| 41 | { |
| 42 | Console.WriteLine(_display.ListResolutions()); |
| 43 | } |
| 44 | catch (Exception ex) |
| 45 | { |
| 46 | _logger.Error(ex); |
| 47 | } |
| 48 | break; |
| 49 | |
| 50 | case "SetScreenResolution": |
| 51 | try |
| 52 | { |
| 53 | uint width; |
| 54 | uint height; |
| 55 | uint? refreshRate = null; |
| 56 | |
| 57 | if (rawValue.Type == JTokenType.Object) |
| 58 | { |
| 59 | width = rawValue.Value<uint>("width"); |
| 60 | height = rawValue.Value<uint>("height"); |
| 61 | if (rawValue["refreshRate"] != null) |
| 62 | { |
| 63 | refreshRate = rawValue.Value<uint>("refreshRate"); |
| 64 | } |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | string resString = rawValue.ToString(); |
| 69 | string[] parts = resString.ToLowerInvariant().Split('x', '@'); |
| 70 | if (parts.Length < 2) |
| 71 | { |
| 72 | _logger.Warning("Invalid resolution format. Use 'WIDTHxHEIGHT' or 'WIDTHxHEIGHT@REFRESH' (e.g., '1920x1080' or '1920x1080@60')"); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | if (!uint.TryParse(parts[0].Trim(), out width) || !uint.TryParse(parts[1].Trim(), out height)) |
| 77 | { |
| 78 | _logger.Warning("Invalid resolution values. Width and height must be positive integers."); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | if (parts.Length >= 3 && uint.TryParse(parts[2].Trim(), out uint parsedRefresh)) |
| 83 | { |
| 84 | refreshRate = parsedRefresh; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | string result = _display.SetResolution(width, height, refreshRate); |
| 89 | Console.WriteLine(result); |
| 90 | } |
| 91 | catch (Exception ex) |
| 92 | { |
| 93 | _logger.Error(ex); |
| 94 | } |
| 95 | break; |
| 96 | |
| 97 | case "SetTextSize": |
| 98 | try |
| 99 | { |
| 100 | if (int.TryParse(value, out int textSizePct)) |
| 101 | { |
| 102 | _display.SetTextSize(textSizePct); |
| 103 | } |
| 104 | } |
| 105 | catch (Exception ex) |
| 106 | { |
| 107 | _logger.Error(ex); |
| 108 | } |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |