microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/autoShell/Handlers/AppCommandHandler.cs
136lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System; |
| 5 | using System.Collections.Generic; |
| 6 | using System.Diagnostics; |
| 7 | using autoShell.Logging; |
| 8 | using autoShell.Services; |
| 9 | using Newtonsoft.Json; |
| 10 | using Newtonsoft.Json.Linq; |
| 11 | |
| 12 | namespace autoShell.Handlers; |
| 13 | |
| 14 | /// <summary> |
| 15 | /// Handles application lifecycle commands: CloseProgram, LaunchProgram, and ListAppNames. |
| 16 | /// </summary> |
| 17 | internal class AppCommandHandler : ICommandHandler |
| 18 | { |
| 19 | private readonly IAppRegistry _appRegistry; |
| 20 | private readonly IProcessService _processService; |
| 21 | private readonly IWindowService _window; |
| 22 | private readonly ILogger _logger; |
| 23 | |
| 24 | public AppCommandHandler(IAppRegistry appRegistry, IProcessService processService, IWindowService window, ILogger logger) |
| 25 | { |
| 26 | _appRegistry = appRegistry; |
| 27 | _processService = processService; |
| 28 | _window = window; |
| 29 | _logger = logger; |
| 30 | } |
| 31 | |
| 32 | /// <inheritdoc/> |
| 33 | public IEnumerable<string> SupportedCommands { get; } = |
| 34 | [ |
| 35 | "CloseProgram", |
| 36 | "LaunchProgram", |
| 37 | "ListAppNames", |
| 38 | ]; |
| 39 | |
| 40 | /// <inheritdoc/> |
| 41 | public void Handle(string key, string value, JToken rawValue) |
| 42 | { |
| 43 | switch (key) |
| 44 | { |
| 45 | case "CloseProgram": |
| 46 | CloseApplication(value); |
| 47 | break; |
| 48 | |
| 49 | case "LaunchProgram": |
| 50 | OpenApplication(value); |
| 51 | break; |
| 52 | |
| 53 | case "ListAppNames": |
| 54 | Console.WriteLine(JsonConvert.SerializeObject(_appRegistry.GetAllAppNames())); |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | private void CloseApplication(string friendlyName) |
| 60 | { |
| 61 | string processName = _appRegistry.ResolveProcessName(friendlyName); |
| 62 | Process[] processes = _processService.GetProcessesByName(processName); |
| 63 | if (processes.Length != 0) |
| 64 | { |
| 65 | _logger.Debug("Closing " + friendlyName); |
| 66 | foreach (Process p in processes) |
| 67 | { |
| 68 | if (p.MainWindowHandle != IntPtr.Zero) |
| 69 | { |
| 70 | p.CloseMainWindow(); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private void OpenApplication(string friendlyName) |
| 77 | { |
| 78 | string processName = _appRegistry.ResolveProcessName(friendlyName); |
| 79 | Process[] processes = _processService.GetProcessesByName(processName); |
| 80 | |
| 81 | if (processes.Length == 0) |
| 82 | { |
| 83 | _logger.Debug("Starting " + friendlyName); |
| 84 | string path = _appRegistry.GetExecutablePath(friendlyName); |
| 85 | if (path != null) |
| 86 | { |
| 87 | var psi = new ProcessStartInfo |
| 88 | { |
| 89 | FileName = path, |
| 90 | UseShellExecute = true |
| 91 | }; |
| 92 | |
| 93 | string workDirEnvVar = _appRegistry.GetWorkingDirectoryEnvVar(friendlyName); |
| 94 | if (workDirEnvVar != null) |
| 95 | { |
| 96 | psi.WorkingDirectory = Environment.ExpandEnvironmentVariables("%" + workDirEnvVar + "%"); |
| 97 | } |
| 98 | |
| 99 | string arguments = _appRegistry.GetArguments(friendlyName); |
| 100 | if (arguments != null) |
| 101 | { |
| 102 | psi.Arguments = arguments; |
| 103 | } |
| 104 | |
| 105 | try |
| 106 | { |
| 107 | _processService.Start(psi); |
| 108 | } |
| 109 | catch (System.ComponentModel.Win32Exception) |
| 110 | { |
| 111 | psi.FileName = friendlyName; |
| 112 | _processService.Start(psi); |
| 113 | } |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | string appModelUserId = _appRegistry.GetAppUserModelId(friendlyName); |
| 118 | if (appModelUserId != null) |
| 119 | { |
| 120 | try |
| 121 | { |
| 122 | _processService.Start(new ProcessStartInfo("explorer.exe", @" shell:appsFolder\" + appModelUserId)); |
| 123 | } |
| 124 | catch (Exception ex) { _logger.Debug($"Failed to launch UWP app: {ex.Message}"); } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | _logger.Debug("Raising " + friendlyName); |
| 131 | string processName2 = _appRegistry.ResolveProcessName(friendlyName); |
| 132 | string path2 = _appRegistry.GetExecutablePath(friendlyName); |
| 133 | _window.RaiseWindow(processName2, path2); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |