/ src / settings-ui / QuickAccess.UI / QuickAccessLaunchContext.cs
QuickAccessLaunchContext.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.Diagnostics.CodeAnalysis;
 7  
 8  namespace Microsoft.PowerToys.QuickAccess;
 9  
10  public sealed record QuickAccessLaunchContext(string? ShowEventName, string? ExitEventName, string? RunnerPipeName, string? AppPipeName)
11  {
12      public static QuickAccessLaunchContext Parse(string[] args)
13      {
14          string? showEvent = null;
15          string? exitEvent = null;
16          string? runnerPipe = null;
17          string? appPipe = null;
18  
19          foreach (var arg in args)
20          {
21              if (TryReadValue(arg, "--show-event", out var value))
22              {
23                  showEvent = value;
24              }
25              else if (TryReadValue(arg, "--exit-event", out value))
26              {
27                  exitEvent = value;
28              }
29              else if (TryReadValue(arg, "--runner-pipe", out value))
30              {
31                  runnerPipe = value;
32              }
33              else if (TryReadValue(arg, "--app-pipe", out value))
34              {
35                  appPipe = value;
36              }
37          }
38  
39          return new QuickAccessLaunchContext(showEvent, exitEvent, runnerPipe, appPipe);
40      }
41  
42      private static bool TryReadValue(string candidate, string key, [NotNullWhen(true)] out string? value)
43      {
44          if (candidate.StartsWith(key, StringComparison.OrdinalIgnoreCase))
45          {
46              if (candidate.Length == key.Length)
47              {
48                  value = null;
49                  return false;
50              }
51  
52              if (candidate[key.Length] == '=')
53              {
54                  value = candidate[(key.Length + 1)..].Trim('"');
55                  return true;
56              }
57          }
58  
59          value = null;
60          return false;
61      }
62  }