Skip to content

Commit

Permalink
Introduce an Id property for command providers (microsoft#269)
Browse files Browse the repository at this point in the history
Originally a part of microsoft#264 

I need command providers to have an ID, so that we can differentiate between multiple providers in the same package.

It's really only relevant for _us_, but I don't know what the future has in store for us. 

Also includes a minor refactoring that combines a couple of the built-ins into a single provider. Reload and quit aren't really separate from _the app itself_.
  • Loading branch information
zadjii authored Jan 7, 2025
1 parent 86c614d commit de7fc85
Show file tree
Hide file tree
Showing 17 changed files with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public partial class AllAppsCommandProvider : CommandProvider

public AllAppsCommandProvider()
{
DisplayName = "All Apps";
Id = "AllApps";
DisplayName = "Installed apps";
Icon = new("\ue71d");

_listItem = new(Page) { Subtitle = "Search installed apps" };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public partial class BookmarksCommandProvider : CommandProvider

public BookmarksCommandProvider()
{
Id = "Bookmarks";
DisplayName = "Bookmarks";

_addNewCommand.AddedAction += AddNewCommand_AddedAction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public partial class CalculatorCommandProvider : CommandProvider

public CalculatorCommandProvider()
{
Id = "Calculator";
DisplayName = "Calculator";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using Microsoft.CmdPal.Extensions;
using Microsoft.CmdPal.Extensions.Helpers;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public partial class RegistryCommandsProvider : CommandProvider
{
public RegistryCommandsProvider()
{
Id = "Windows.Registry";
DisplayName = $"Windows Registry";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public partial class ShellCommandsProvider : CommandProvider

public ShellCommandsProvider()
{
Id = "Run";
DisplayName = Resources.cmd_plugin_name;
_fallbackItem = new FallbackExecuteItem(_settingsManager);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public partial class WindowWalkerCommandsProvider : CommandProvider

public WindowWalkerCommandsProvider()
{
Id = "WindowWalker";
DisplayName = Resources.windowwalker_name;
_windowWalkerPageItem = new CommandItem(new WindowWalkerListPage())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public partial class WindowsServicesCommandsProvider : CommandProvider
{
public WindowsServicesCommandsProvider()
{
Id = "Windows.Services";
DisplayName = $"Windows Services";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public partial class WindowsSettingsCommandsProvider : CommandProvider

public WindowsSettingsCommandsProvider()
{
Id = "Windows.Settings";
DisplayName = $"Windows Settings";

_windowsSettings = JsonSettingsListHelper.ReadAllPossibleSettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public partial class WindowsTerminalCommandsProvider : CommandProvider

public WindowsTerminalCommandsProvider()
{
Id = "WindowsTerminalProfiles";
DisplayName = Resources.extension_name;

_terminalCommand = new TerminalTopLevelCommandItem(_settingsManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@ namespace Microsoft.CmdPal.UI.ViewModels.BuiltinCommands;
/// <summary>
/// Built-in Provider for a top-level command which can quit the application. Invokes the <see cref="QuitAction"/>, which sends a <see cref="QuitMessage"/>.
/// </summary>
public partial class QuitCommandProvider : CommandProvider
public partial class BuiltInsCommandProvider : CommandProvider
{
private readonly QuitAction quitAction = new();
private readonly ReloadExtensionsAction reloadAction = new();

public override ICommandItem[] TopLevelCommands() => [];

public override IFallbackCommandItem[] FallbackCommands() =>
[new FallbackCommandItem(quitAction) { Subtitle = "Exit Command Palette" }];
[
new FallbackCommandItem(quitAction) { Subtitle = "Exit Command Palette" },
new FallbackCommandItem(reloadAction) { Subtitle = "Reload Command Palette extensions" },
];

public BuiltInsCommandProvider()
{
Id = "Core";
DisplayName = "Built-in commands";
Icon = new(Path.Combine(AppDomain.CurrentDomain.BaseDirectory.ToString(), "Assets\\StoreLogo.scale-200.png"));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Microsoft.CmdPal.UI.ViewModels.Messages;

/// <summary>
/// Message which closes the application. Used by <see cref="QuitAction"/> via <see cref="QuitCommandProvider"/>.
/// Message which closes the application. Used by <see cref="QuitAction"/> via <see cref="BuiltInsCommandProvider"/>.
/// </summary>
public record QuitMessage()
{
Expand Down
5 changes: 2 additions & 3 deletions src/modules/cmdpal/Microsoft.CmdPal.UI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,15 @@ private static ServiceProvider ConfigureServices()
services.AddSingleton<ICommandProvider, AllAppsCommandProvider>();
services.AddSingleton<ICommandProvider, ShellCommandsProvider>();
services.AddSingleton<ICommandProvider, CalculatorCommandProvider>();
services.AddSingleton<ICommandProvider, IndexerCommandsProvider>();
services.AddSingleton<ICommandProvider, BookmarksCommandProvider>();
services.AddSingleton<ICommandProvider, BuiltInsCommandProvider>();
services.AddSingleton<ICommandProvider, SettingsCommandProvider>();
services.AddSingleton<ICommandProvider, QuitCommandProvider>();
services.AddSingleton<ICommandProvider, ReloadExtensionsCommandProvider>();
services.AddSingleton<ICommandProvider, WindowsTerminalCommandsProvider>();
services.AddSingleton<ICommandProvider, WindowsServicesCommandsProvider>();
services.AddSingleton<ICommandProvider, RegistryCommandsProvider>();
services.AddSingleton<ICommandProvider, WindowWalkerCommandsProvider>();
services.AddSingleton<ICommandProvider, WindowsSettingsCommandsProvider>();
services.AddSingleton<ICommandProvider, IndexerCommandsProvider>();

// Models
services.AddSingleton<TopLevelCommandManager>();
Expand Down
8 changes: 8 additions & 0 deletions src/modules/cmdpal/doc/initial-sdk-spec/initial-sdk-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,7 @@ interface IFallbackCommandItem requires ICommandItem {

interface ICommandProvider requires Windows.Foundation.IClosable
{
String Id { get; };
String DisplayName { get; };
IconDataType Icon { get; };
ICommandSettings Settings { get; };
Expand All @@ -1378,6 +1379,13 @@ actions, or they can be pages that the user can navigate to.
simpler form of `IListItem`, which can be displayed even as a stub (as described
in [Caching](#caching)), before the extension process is loaded.

`Id` is only necessary to set if your extension implements multiple providers in
the same package identity. This is an uncommon scenario which most developers
shouldn't need to worry about. If you do set `Id`, it should be a stable string
across package versions. DevPal will use this Id for tracking settings for each
provider within a package. Changing this string will result in the user's
settings for your extension being lost.

#### Fallback commands

Providers may also specify a set of `FallbackCommands`[^2]. These are special
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ namespace Microsoft.CmdPal.Extensions.Helpers;

public abstract partial class CommandProvider : ICommandProvider
{
private string _id = string.Empty;

private string _displayName = string.Empty;

private IconDataType _icon = new(string.Empty);

private ICommandSettings? _settings;

public string Id { get => _id; protected set => _id = value; }

public string DisplayName { get => _displayName; protected set => _displayName = value; }

public IconDataType Icon { get => _icon; protected set => _icon = value; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ namespace Microsoft.CmdPal.Extensions
[contract(Microsoft.CmdPal.Extensions.ExtensionsContract, 1)]
interface ICommandProvider requires Windows.Foundation.IClosable
{
String Id { get; };
String DisplayName { get; };
IconDataType Icon { get; };
ICommandSettings Settings { get; };
Expand Down

0 comments on commit de7fc85

Please sign in to comment.