microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/autoShell/Handlers/DisplayActionHandler.cs
94lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System; |
| 5 | using System.Text.Json; |
| 6 | using autoShell.Handlers.Generated; |
| 7 | using autoShell.Logging; |
| 8 | using autoShell.Services; |
| 9 | |
| 10 | namespace autoShell.Handlers; |
| 11 | |
| 12 | /// <summary> |
| 13 | /// Handles display commands: ListResolutions, SetScreenResolution, and SetTextSize. |
| 14 | /// </summary> |
| 15 | internal class DisplayActionHandler : ActionHandlerBase |
| 16 | { |
| 17 | private readonly IDisplayService _display; |
| 18 | private readonly ILogger _logger; |
| 19 | |
| 20 | public DisplayActionHandler(IDisplayService display, ILogger logger) |
| 21 | { |
| 22 | _display = display; |
| 23 | _logger = logger; |
| 24 | AddAction("ListResolutions", HandleListResolutions); |
| 25 | AddAction<SetScreenResolutionParams>("SetScreenResolution", HandleSetScreenResolution); |
| 26 | AddAction<SetTextSizeParams>("SetTextSize", HandleSetTextSize); |
| 27 | } |
| 28 | |
| 29 | private ActionResult HandleListResolutions(JsonElement parameters) |
| 30 | { |
| 31 | try |
| 32 | { |
| 33 | string resolutions = _display.ListResolutions(); |
| 34 | using var doc = JsonDocument.Parse(resolutions); |
| 35 | return ActionResult.Ok("Listed resolutions", doc.RootElement.Clone()); |
| 36 | } |
| 37 | catch (Exception ex) |
| 38 | { |
| 39 | _logger.Error(ex); |
| 40 | return ActionResult.Fail($"Failed to list resolutions: {ex.Message}"); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | private ActionResult HandleSetScreenResolution(SetScreenResolutionParams p) |
| 45 | { |
| 46 | try |
| 47 | { |
| 48 | int width = p.Width; |
| 49 | int height = p.Height; |
| 50 | if (width <= 0 || height <= 0) |
| 51 | { |
| 52 | return ActionResult.Fail("Invalid resolution: width and height must be positive"); |
| 53 | } |
| 54 | |
| 55 | uint? refreshRate = null; |
| 56 | if (p.RefreshRate.HasValue) |
| 57 | { |
| 58 | if (p.RefreshRate.Value <= 0) |
| 59 | { |
| 60 | return ActionResult.Fail("Invalid refresh rate: must be positive"); |
| 61 | } |
| 62 | refreshRate = (uint)p.RefreshRate.Value; |
| 63 | } |
| 64 | |
| 65 | string result = _display.SetResolution((uint)width, (uint)height, refreshRate); |
| 66 | return ActionResult.Ok(result); |
| 67 | } |
| 68 | catch (Exception ex) |
| 69 | { |
| 70 | _logger.Error(ex); |
| 71 | return ActionResult.Fail($"Failed to set resolution: {ex.Message}"); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private ActionResult HandleSetTextSize(SetTextSizeParams p) |
| 76 | { |
| 77 | try |
| 78 | { |
| 79 | int textSizePct = p.Size; |
| 80 | if (textSizePct <= 0) |
| 81 | { |
| 82 | return ActionResult.Fail("Invalid text size: size required"); |
| 83 | } |
| 84 | |
| 85 | _display.SetTextSize(textSizePct); |
| 86 | return ActionResult.Ok($"Text size set to {textSizePct}%"); |
| 87 | } |
| 88 | catch (Exception ex) |
| 89 | { |
| 90 | _logger.Error(ex); |
| 91 | return ActionResult.Fail($"Failed to set text size: {ex.Message}"); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |