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