|
| 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) |
0 commit comments