/ src / common / PowerToys.ModuleContracts / IModuleService.cs
IModuleService.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 Common.UI;
 6  
 7  namespace PowerToys.ModuleContracts;
 8  
 9  /// <summary>
10  /// Base contract for PowerToys modules exposed to the Command Palette.
11  /// </summary>
12  public interface IModuleService
13  {
14      /// <summary>
15      /// Gets module identifier (e.g., Workspaces, Awake).
16      /// </summary>
17      string Key { get; }
18  
19      Task<OperationResult> LaunchAsync(CancellationToken cancellationToken = default);
20  
21      Task<OperationResult> OpenSettingsAsync(CancellationToken cancellationToken = default);
22  }
23  
24  /// <summary>
25  /// Helper base to reduce duplication for simple modules.
26  /// </summary>
27  public abstract class ModuleServiceBase : IModuleService
28  {
29      public abstract string Key { get; }
30  
31      protected abstract SettingsDeepLink.SettingsWindow SettingsWindow { get; }
32  
33      public abstract Task<OperationResult> LaunchAsync(CancellationToken cancellationToken = default);
34  
35      public virtual Task<OperationResult> OpenSettingsAsync(CancellationToken cancellationToken = default)
36      {
37          try
38          {
39              SettingsDeepLink.OpenSettings(SettingsWindow);
40              return Task.FromResult(OperationResult.Ok());
41          }
42          catch (Exception ex)
43          {
44              return Task.FromResult(OperationResult.Fail($"Failed to open settings for {Key}: {ex.Message}"));
45          }
46      }
47  }