Skip to content

Commit a80bae5

Browse files
committed
Docs through CommandResult
1 parent 64950ef commit a80bae5

File tree

5 files changed

+125
-7
lines changed

5 files changed

+125
-7
lines changed

hub/dev-environment/toc.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ items:
133133
items:
134134
- name: Creating an extension
135135
href: ../powertoys/command-palette/creating-an-extension.md
136+
- name: Adding commands
137+
href: ../powertoys/command-palette/adding-commands.md
136138
- name: Update a list of commands
137139
href: ../powertoys/command-palette/update-a-list-of-commands.md
138140
- name: Command results
@@ -143,10 +145,10 @@ items:
143145
href: ../powertoys/command-palette/using-markdown-content.md
144146
- name: Get user input with forms
145147
href: ../powertoys/command-palette/using-form-pages.md
146-
- name: Handle the search text
147-
href: ../powertoys/command-palette/dynamic-lists.md
148-
- name: "Advanced: Adding an extension to your package"
149-
href: ../powertoys/command-palette/adding-an-extension-to-your-package.md
148+
# - name: Handle the search text
149+
# href: ../powertoys/command-palette/dynamic-lists.md
150+
# - name: "Advanced: Adding an extension to your package"
151+
# href: ../powertoys/command-palette/adding-an-extension-to-your-package.md
150152
- name: Publishing your extension
151153
href: ../powertoys/command-palette/publish-extension.md
152154
- name: Extension samples

hub/powertoys/command-palette/add-top-level-commands-to-your-extension.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ no-loc: [PowerToys, Windows, Insider]
99

1010
# Adding top-level commands
1111

12-
**Previous**: [Command Results](command-results.md)
12+
**Previous**: [Update a list of commands](update-a-list-of-commands.md).
1313

1414
So far, we've only added commands to a single page, within your extension. You can also add more commands directly to the top-level list of commands too.
1515

@@ -57,7 +57,7 @@ Once you've determined that you need to change the top level list, call [`RaiseI
5757
> [!TIP]
5858
> Create the `CommandItem`s for the top-level commands before calling `RaiseItemsChanged()`. This will ensure that the new commands are available when Command Palette asks for the top-level commands. This will help keep the work being done in call to `TopLevelCommands()` method to a minimum.
5959
60-
### Next up: [Display markdown content](using-markdown-content.md)
60+
### Next up: [Command Results](command-results.md)
6161

6262
## Related content
6363

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
title: Command results
3+
description: Learn how to add new commands to your Command Palette extension.
4+
ms.date: 3/23/2025
5+
ms.topic: concept-article
6+
no-loc: [PowerToys, Windows, Insider]
7+
# Customer intent: As a Windows developer, I want to learn how to develop an extension for the Command Palette.
8+
---
9+
10+
# Command results
11+
12+
**Previous**: [Add top-level commands to your extension](add-top-level-commands-to-your-extension.md)
13+
14+
15+
[`IInvokableCommand`](./microsoft-commandpalette-extensions/iinvokablecommand.md)s are the fundamental units of "do something" in the Command Palette. The [`Invoke`](./microsoft-commandpalette-extensions/iinvokablecommand.md) method is called when the user selects the command, and it's where you _do something_ in your extension. The `Invoke` method returns an `ICommandResult`, which tells the Command Palette what to do after the command has been invoked. This page details what's possible with each kind of command result.
16+
17+
The toolkit provides a number of helper methods to create command results. These are all static methods on the `CommandResult` class. Calling these methods on their own won't do anything - you must return those objects as the result of a `Invoke` method, for Command Palette to handle them.
18+
19+
<!-- GoToPage currently omitted from these docs, because it's not remotely implemented -->
20+
21+
## `KeepOpen`
22+
23+
This command result does nothing. It leaves the palette in its current state, with the current page stack and query. This can be useful for commands that want to keep the the user in the Command Palette, to keep working with the current page.
24+
25+
> [!NOTE]
26+
> Even when returing `KeepOpen`, launching a new app or window from the Command Palette will automatically hide the palette the next window recieves focus.
27+
28+
## `Hide`
29+
30+
This command result keeps the current page open, but hides the Command Palette. This can be useful for commands that want to take the user briefly out of the Command Palette, but then come back to this context.
31+
32+
## `GoBack`
33+
34+
This result takes the user back a page in the Command Palette, and keeps the window visible. This is perfect for form pages, where doing the command should take you the user back to the previous context.
35+
36+
## `GoHome`
37+
38+
This result takes the user back to the main page of the Command Palette. It will leave the Palette visible (unless the palette otherwise loses focus). Consider using this for scenarios where you've changed your top-level commands.
39+
40+
## `Dismiss`
41+
42+
This result hides the Command Palette after the action is executed, and takes it back to the hme page. On the next launch, the Command Palette will start from the main page with a blank query. This is useful for commands that are one-off actions, or that don't need to keep the Command Palette open.
43+
44+
If you don't know what else to use, this should be your default. Ideally, users should come into the palette, find what they need, and be done with it.
45+
46+
## `ShowToast`
47+
48+
This result displays a transient desktop-level message to the user. This is especially useful for displaying confirmation that an action took place when the palette will be closed.
49+
50+
Consider the [CopyTextCommand](../microsoft-commandpalette-extensions-toolkit/copytextcommand.md) in the helpers - this command will show a toast with the text "Copied to clipboard", then dismiss the palette.
51+
52+
By default, [`CommandResult.ShowToast(string)`](./microsoft-commandpalette-extensions-toolkit/commandresult_showtoast_string.md) helper will have a `Result` of `CommandResult.Dismiss`. However, you can instead change the result to any of the other results if you want. This allows you to display a toast and keep the palette open, if you'd like.
53+
54+
## `Confirm`
55+
56+
This result displays a confirmation dialog to the user. If the user confirms the dialog, then the `PrimaryCommand` of the `ConfirmationArgs` will be performed.
57+
58+
This is useful for commands that might have destructive actions, or that need to confirm user intent.
59+
60+
## Example
61+
62+
As an example, here's a page with one command for each kind of command result:
63+
64+
```csharp
65+
66+
using Microsoft.CommandPalette.Extensions.Toolkit;
67+
68+
internal sealed partial class CommandResultsPage : ListPage
69+
{
70+
public CommandResultsPage()
71+
{
72+
Icon = IconHelpers.FromRelativePath("Assets\\StoreLogo.png");
73+
Title = "Example command results";
74+
Name = "Open";
75+
}
76+
77+
public override IListItem[] GetItems()
78+
{
79+
ConfirmationArgs confirmArgs = new()
80+
{
81+
PrimaryCommand = new AnonymousCommand(
82+
() =>
83+
{
84+
ToastStatusMessage t = new("The dialog was confirmed");
85+
t.Show();
86+
})
87+
{
88+
Name = "Confirm",
89+
Result = CommandResult.KeepOpen(),
90+
},
91+
Title = "You can set a title for the dialog",
92+
Description = "Are you really sure you want to do the thing?",
93+
};
94+
95+
return
96+
[
97+
new ListItem(new AnonymousCommand(null) { Result = CommandResult.KeepOpen() }) { Title = "Keep the palette open" },
98+
new ListItem(new AnonymousCommand(null) { Result = CommandResult.Hide() }) { Title = "Hide the palette" },
99+
new ListItem(new AnonymousCommand(null) { Result = CommandResult.GoBack() }) { Title = "Go back" },
100+
new ListItem(new AnonymousCommand(null) { Result = CommandResult.GoHome() }) { Title = "Go home" },
101+
new ListItem(new AnonymousCommand(null) { Result = CommandResult.Dismiss() }) { Title = "Dismiss the palette" },
102+
new ListItem(new AnonymousCommand(null) { Result = CommandResult.ShowToast("What's up") }) { Title = "Show a toast" },
103+
new ListItem(new AnonymousCommand(null) { Result = CommandResult.Confirm(confirmArgs) }) { Title = "Confirm something" },
104+
];
105+
}
106+
}
107+
```
108+
109+
### Next up: [Display markdown content](using-markdown-content.md)
110+
111+
## Related content
112+
113+
- [PowerToys Command Palette utility](overview.md)
114+
- [Extensibility overview](extensibility-overview.md)
115+
- [Extension samples](samples.md)

hub/powertoys/command-palette/microsoft-commandpalette-extensions-toolkit/commandprovider_raiseitemschanged.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Namespace: [Microsoft.CommandPalette.Extensions.Toolkit](microsoft-commandpalett
1414

1515
The **RaiseItemsChanged** method raises the **ItemsChanged** event with the specified change type. This method is typically used to notify the Command Palette of changes in the command provider's items, such as when new commands are added or existing commands are removed.
1616

17+
1718
<!-- ## Parameters
1819
1920
*totalItems* **Int**

hub/powertoys/command-palette/update-a-list-of-commands.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ internal void Increment()
143143

144144
Best practice is to set `IsLoading` to `true` before starting the work. Then do all the work to build all the new `ListItems` you need to display to the user. Then, once the items are ready, call `RaiseItemsChanged` and set `IsLoading` back to `false`. This will ensure that the loading spinner is shown for the entire duration of the work, and that the UI is updated as soon as the work is done (without waiting for your extension to construct new `ListItem` objects).
145145

146-
### Next up: [Command results](command-results.md)
146+
### Next up: [Add top-level commands to your extension](add-top-level-commands-to-your-extension.md)
147147

148148
## Related content
149149

0 commit comments

Comments
 (0)