microsoft/TypeAgent

Public

mirrored from https://github.com/microsoft/TypeAgentAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
f6da8916bd40d461b5d29c4f7fbda09d178d99fc

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

dotnet/autoShell/Handlers/DisplayActionHandler.cs

95lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4using System;
5using System.Text.Json;
6using autoShell.Handlers.Generated;
7using autoShell.Logging;
8using autoShell.Services;
9
10namespace autoShell.Handlers;
11
12/// <summary>
13/// Handles display commands: ListResolutions, SetScreenResolution, and SetTextSize.
14/// </summary>
15internal 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 using var doc = JsonDocument.Parse(result);
67 return ActionResult.Ok($"Screen resolution set to {width}x{height}", doc.RootElement.Clone());
68 }
69 catch (Exception ex)
70 {
71 _logger.Error(ex);
72 return ActionResult.Fail($"Failed to set resolution: {ex.Message}");
73 }
74 }
75
76 private ActionResult HandleSetTextSize(SetTextSizeParams p)
77 {
78 try
79 {
80 int textSizePct = p.Size;
81 if (textSizePct <= 0)
82 {
83 return ActionResult.Fail("Invalid text size: size required");
84 }
85
86 _display.SetTextSize(textSizePct);
87 return ActionResult.Ok($"Text size set to {textSizePct}%");
88 }
89 catch (Exception ex)
90 {
91 _logger.Error(ex);
92 return ActionResult.Fail($"Failed to set text size: {ex.Message}");
93 }
94 }
95}