microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/autoShell/Handlers/AudioCommandHandler.cs
58lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Collections.Generic; |
| 5 | using autoShell.Services; |
| 6 | using Newtonsoft.Json.Linq; |
| 7 | |
| 8 | namespace autoShell.Handlers; |
| 9 | |
| 10 | /// <summary> |
| 11 | /// Handles audio commands: Mute, RestoreVolume, and Volume. |
| 12 | /// </summary> |
| 13 | internal class AudioCommandHandler : ICommandHandler |
| 14 | { |
| 15 | private readonly IAudioService _audio; |
| 16 | private double _savedVolumePct; |
| 17 | |
| 18 | public AudioCommandHandler(IAudioService audio) |
| 19 | { |
| 20 | _audio = audio; |
| 21 | } |
| 22 | |
| 23 | /// <inheritdoc/> |
| 24 | public IEnumerable<string> SupportedCommands { get; } = |
| 25 | [ |
| 26 | "Mute", |
| 27 | "RestoreVolume", |
| 28 | "Volume", |
| 29 | ]; |
| 30 | |
| 31 | /// <inheritdoc/> |
| 32 | public void Handle(string key, string value, JToken rawValue) |
| 33 | { |
| 34 | switch (key) |
| 35 | { |
| 36 | case "Mute": |
| 37 | if (bool.TryParse(value, out bool mute)) |
| 38 | { |
| 39 | _audio.SetMute(mute); |
| 40 | } |
| 41 | break; |
| 42 | case "RestoreVolume": |
| 43 | _audio.SetVolume((int)_savedVolumePct); |
| 44 | break; |
| 45 | case "Volume": |
| 46 | if (int.TryParse(value, out int pct)) |
| 47 | { |
| 48 | int currentVolume = _audio.GetVolume(); |
| 49 | if (currentVolume > 0) |
| 50 | { |
| 51 | _savedVolumePct = currentVolume; |
| 52 | } |
| 53 | _audio.SetVolume(pct); |
| 54 | } |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |