243 lines
8.1 KiB
C#
Raw Normal View History

Add the Command Palette module (#37908) Windows Command Palette ("CmdPal") is the next iteration of PowerToys Run. With extensibility at its core, the Command Palette is your one-stop launcher to start _anything_. By default, CmdPal is bound to <kbd>Win+Alt+Space</kbd>. ![cmdpal-pr-002](https://github.com/user-attachments/assets/5077ec04-1009-478a-92d6-0a30989d44ac) ![cmdpal-pr-003](https://github.com/user-attachments/assets/63b4762a-9c19-48eb-9242-18ea48240ba0) ---- This brings the current preview version of CmdPal into the upstream PowerToys repo. There are still lots of bugs to work out, but it's reached the state we're ready to start sharing it with the world. From here, we can further collaborate with the community on the features that are important, and ensuring that we've got a most robust API to enable developers to build whatever extensions they want. Most of the built-in PT Run modules have already been ported to CmdPal's extension API. Those include: * Installed apps * Shell commands * File search (powered by the indexer) * Windows Registry search * Web search * Windows Terminal Profiles * Windows Services * Windows settings There are a couple new extensions built-in * You can now search for packages on `winget` and install them right from the palette. This also powers searching for extensions for the palette * The calculator has an entirely new implementation. This is currently less feature complete than the original PT Run one - we're looking forward to updating it to be more complete for future ingestion in Windows * "Bookmarks" allow you to save shortcuts to files, folders, and webpages as top-level commands in the palette. We've got a bunch of other samples too, in this repo and elsewhere ### PowerToys specific notes CmdPal will eventually graduate out of PowerToys to live as its own application, which is why it's implemented just a little differently than most other modules. Enabling CmdPal will install its `msix` package. The CI was minorly changed to support CmdPal version numbers independent of PowerToys itself. It doesn't make sense for us to start CmdPal at v0.90, and in the future, we want to be able to rev CmdPal independently of PT itself. Closes #3200, closes #3600, closes #7770, closes #34273, closes #36471, closes #20976, closes #14495 ----- TODOs et al **Blocking:** - [ ] Images and descriptions in Settings and OOBE need to be properly defined, as mentioned before - [ ] Niels is on it - [x] Doesn't start properly from PowerToys unless the fix PR is merged. - https://github.com/zadjii-msft/PowerToys/pull/556 merged - [x] I seem to lose focus a lot when I press on some limits, like between the search bar and the results. - This is https://github.com/zadjii-msft/PowerToys/issues/427 - [x] Turned off an extension like Calculator and it was still working. - Need to get rid of that toggle, it doesn't do anything currently - [x] `ListViewModel.<FetchItems>` crash - Pretty confident that was fixed in https://github.com/zadjii-msft/PowerToys/pull/553 **Not blocking / improvements:** - Show the shortcut through settings, as mentioned before, or create a button that would open CmdPalette settings. - When PowerToys starts, CmdPalette is always shown if enabled. That's weird when just starting PowerToys/ logging in to the computer with PowerToys auto-start activated. I think this should at least be a setting. - Needing to double press a result for it to do the default action seems quirky. If one is already selected, I think just pressing should be enough for it to do the action. - This is currently a setting, though we're thinking of changing the setting even more: https://github.com/zadjii-msft/PowerToys/issues/392 - There's no URI extension. Was surprised when typing a URL that it only proposed a web search. - [x] There's no System commands extension. Was expecting to be able to quickly restart the computer by typing restart but it wasn't there. - This is in PR https://github.com/zadjii-msft/PowerToys/pull/452 --------- Co-authored-by: joadoumie <98557455+joadoumie@users.noreply.github.com> Co-authored-by: Jordi Adoumie <jordiadoumie@microsoft.com> Co-authored-by: Mike Griese <zadjii@gmail.com> Co-authored-by: Niels Laute <niels.laute@live.nl> Co-authored-by: Michael Hawker <24302614+michael-hawker@users.noreply.github.com> Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com> Co-authored-by: Seraphima <zykovas91@gmail.com> Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Kristen Schau <47155823+krschau@users.noreply.github.com> Co-authored-by: Eric Johnson <ericjohnson327@gmail.com> Co-authored-by: Ethan Fang <ethanfang@microsoft.com> Co-authored-by: Yu Leng (from Dev Box) <yuleng@microsoft.com> Co-authored-by: Clint Rutkas <clint@rutkas.com>
2025-03-19 03:39:57 -05:00
// Copyright (c) Microsoft Corporation
// 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.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.CmdPal.UI.ViewModels.Models;
using Microsoft.CommandPalette.Extensions;
namespace Microsoft.CmdPal.UI.ViewModels;
public partial class PageViewModel : ExtensionObjectViewModel, IPageContext
{
public TaskScheduler Scheduler { get; private set; }
private readonly ExtensionObject<IPage> _pageModel;
public bool IsLoading => ModelIsLoading || (!IsInitialized);
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsLoading))]
public virtual partial bool IsInitialized { get; protected set; }
[ObservableProperty]
public partial string ErrorMessage { get; protected set; } = string.Empty;
[ObservableProperty]
public partial bool IsNested { get; set; } = true;
// This is set from the SearchBar
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ShowSuggestion))]
public partial string Filter { get; set; } = string.Empty;
[ObservableProperty]
public virtual partial string PlaceholderText { get; private set; } = "Type here to search...";
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ShowSuggestion))]
public virtual partial string TextToSuggest { get; protected set; } = string.Empty;
public bool ShowSuggestion => !string.IsNullOrEmpty(TextToSuggest) && TextToSuggest != Filter;
[ObservableProperty]
public partial CommandPaletteHost ExtensionHost { get; private set; }
public bool HasStatusMessage => MostRecentStatusMessage != null;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(HasStatusMessage))]
public partial StatusMessageViewModel? MostRecentStatusMessage { get; private set; } = null;
public ObservableCollection<StatusMessageViewModel> StatusMessages => ExtensionHost.StatusMessages;
// These are properties that are "observable" from the extension object
// itself, in the sense that they get raised by PropChanged events from the
// extension. However, we don't want to actually make them
// [ObservableProperty]s, because PropChanged comes in off the UI thread,
// and ObservableProperty is not smart enough to raise the PropertyChanged
// on the UI thread.
public string Name { get; protected set; } = string.Empty;
public string Title { get => string.IsNullOrEmpty(field) ? Name : field; protected set; } = string.Empty;
// This property maps to `IPage.IsLoading`, but we want to expose our own
// `IsLoading` property as a combo of this value and `IsInitialized`
public bool ModelIsLoading { get; protected set; } = true;
public IconInfoViewModel Icon { get; protected set; }
public PageViewModel(IPage? model, TaskScheduler scheduler, CommandPaletteHost extensionHost)
: base((IPageContext?)null)
{
_pageModel = new(model);
Scheduler = scheduler;
PageContext = new(this);
ExtensionHost = extensionHost;
Icon = new(null);
ExtensionHost.StatusMessages.CollectionChanged += StatusMessages_CollectionChanged;
UpdateHasStatusMessage();
}
private void StatusMessages_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) => UpdateHasStatusMessage();
private void UpdateHasStatusMessage()
{
if (ExtensionHost.StatusMessages.Any())
{
var last = ExtensionHost.StatusMessages.Last();
MostRecentStatusMessage = last;
}
else
{
MostRecentStatusMessage = null;
}
}
//// Run on background thread from ListPage.xaml.cs
[RelayCommand]
internal Task<bool> InitializeAsync()
Add the Command Palette module (#37908) Windows Command Palette ("CmdPal") is the next iteration of PowerToys Run. With extensibility at its core, the Command Palette is your one-stop launcher to start _anything_. By default, CmdPal is bound to <kbd>Win+Alt+Space</kbd>. ![cmdpal-pr-002](https://github.com/user-attachments/assets/5077ec04-1009-478a-92d6-0a30989d44ac) ![cmdpal-pr-003](https://github.com/user-attachments/assets/63b4762a-9c19-48eb-9242-18ea48240ba0) ---- This brings the current preview version of CmdPal into the upstream PowerToys repo. There are still lots of bugs to work out, but it's reached the state we're ready to start sharing it with the world. From here, we can further collaborate with the community on the features that are important, and ensuring that we've got a most robust API to enable developers to build whatever extensions they want. Most of the built-in PT Run modules have already been ported to CmdPal's extension API. Those include: * Installed apps * Shell commands * File search (powered by the indexer) * Windows Registry search * Web search * Windows Terminal Profiles * Windows Services * Windows settings There are a couple new extensions built-in * You can now search for packages on `winget` and install them right from the palette. This also powers searching for extensions for the palette * The calculator has an entirely new implementation. This is currently less feature complete than the original PT Run one - we're looking forward to updating it to be more complete for future ingestion in Windows * "Bookmarks" allow you to save shortcuts to files, folders, and webpages as top-level commands in the palette. We've got a bunch of other samples too, in this repo and elsewhere ### PowerToys specific notes CmdPal will eventually graduate out of PowerToys to live as its own application, which is why it's implemented just a little differently than most other modules. Enabling CmdPal will install its `msix` package. The CI was minorly changed to support CmdPal version numbers independent of PowerToys itself. It doesn't make sense for us to start CmdPal at v0.90, and in the future, we want to be able to rev CmdPal independently of PT itself. Closes #3200, closes #3600, closes #7770, closes #34273, closes #36471, closes #20976, closes #14495 ----- TODOs et al **Blocking:** - [ ] Images and descriptions in Settings and OOBE need to be properly defined, as mentioned before - [ ] Niels is on it - [x] Doesn't start properly from PowerToys unless the fix PR is merged. - https://github.com/zadjii-msft/PowerToys/pull/556 merged - [x] I seem to lose focus a lot when I press on some limits, like between the search bar and the results. - This is https://github.com/zadjii-msft/PowerToys/issues/427 - [x] Turned off an extension like Calculator and it was still working. - Need to get rid of that toggle, it doesn't do anything currently - [x] `ListViewModel.<FetchItems>` crash - Pretty confident that was fixed in https://github.com/zadjii-msft/PowerToys/pull/553 **Not blocking / improvements:** - Show the shortcut through settings, as mentioned before, or create a button that would open CmdPalette settings. - When PowerToys starts, CmdPalette is always shown if enabled. That's weird when just starting PowerToys/ logging in to the computer with PowerToys auto-start activated. I think this should at least be a setting. - Needing to double press a result for it to do the default action seems quirky. If one is already selected, I think just pressing should be enough for it to do the action. - This is currently a setting, though we're thinking of changing the setting even more: https://github.com/zadjii-msft/PowerToys/issues/392 - There's no URI extension. Was surprised when typing a URL that it only proposed a web search. - [x] There's no System commands extension. Was expecting to be able to quickly restart the computer by typing restart but it wasn't there. - This is in PR https://github.com/zadjii-msft/PowerToys/pull/452 --------- Co-authored-by: joadoumie <98557455+joadoumie@users.noreply.github.com> Co-authored-by: Jordi Adoumie <jordiadoumie@microsoft.com> Co-authored-by: Mike Griese <zadjii@gmail.com> Co-authored-by: Niels Laute <niels.laute@live.nl> Co-authored-by: Michael Hawker <24302614+michael-hawker@users.noreply.github.com> Co-authored-by: Stefan Markovic <57057282+stefansjfw@users.noreply.github.com> Co-authored-by: Seraphima <zykovas91@gmail.com> Co-authored-by: Jaime Bernardo <jaime@janeasystems.com> Co-authored-by: Kristen Schau <47155823+krschau@users.noreply.github.com> Co-authored-by: Eric Johnson <ericjohnson327@gmail.com> Co-authored-by: Ethan Fang <ethanfang@microsoft.com> Co-authored-by: Yu Leng (from Dev Box) <yuleng@microsoft.com> Co-authored-by: Clint Rutkas <clint@rutkas.com>
2025-03-19 03:39:57 -05:00
{
// TODO: We may want a SemaphoreSlim lock here.
// TODO: We may want to investigate using some sort of AsyncEnumerable or populating these as they come into the UI layer
// Though we have to think about threading here and circling back to the UI thread with a TaskScheduler.
try
{
InitializeProperties();
}
catch (Exception ex)
{
ShowException(ex, _pageModel?.Unsafe?.Name);
return Task.FromResult(false);
}
// Notify we're done back on the UI Thread.
Task.Factory.StartNew(
() =>
{
IsInitialized = true;
// TODO: Do we want an event/signal here that the Page Views can listen to? (i.e. ListPage setting the selected index to 0, however, in async world the user may have already started navigating around page...)
},
CancellationToken.None,
TaskCreationOptions.None,
Scheduler);
return Task.FromResult(true);
}
public override void InitializeProperties()
{
var page = _pageModel.Unsafe;
if (page == null)
{
return; // throw?
}
Name = page.Name;
ModelIsLoading = page.IsLoading;
Title = page.Title;
Icon = new(page.Icon);
Icon.InitializeProperties();
// Let the UI know about our initial properties too.
UpdateProperty(nameof(Name));
UpdateProperty(nameof(Title));
UpdateProperty(nameof(ModelIsLoading));
UpdateProperty(nameof(IsLoading));
UpdateProperty(nameof(Icon));
page.PropChanged += Model_PropChanged;
}
private void Model_PropChanged(object sender, IPropChangedEventArgs args)
{
try
{
var propName = args.PropertyName;
FetchProperty(propName);
}
catch (Exception ex)
{
ShowException(ex, _pageModel?.Unsafe?.Name);
}
}
partial void OnFilterChanged(string oldValue, string newValue) => OnFilterUpdated(newValue);
protected virtual void OnFilterUpdated(string filter)
{
// The base page has no notion of data, so we do nothing here...
// subclasses should override.
}
protected virtual void FetchProperty(string propertyName)
{
var model = this._pageModel.Unsafe;
if (model == null)
{
return; // throw?
}
switch (propertyName)
{
case nameof(Name):
this.Name = model.Name ?? string.Empty;
UpdateProperty(nameof(Title));
break;
case nameof(Title):
this.Title = model.Title ?? string.Empty;
break;
case nameof(IsLoading):
this.ModelIsLoading = model.IsLoading;
UpdateProperty(nameof(ModelIsLoading));
break;
case nameof(Icon):
this.Icon = new(model.Icon);
break;
}
UpdateProperty(propertyName);
}
public new void ShowException(Exception ex, string? extensionHint = null)
{
// Set the extensionHint to the Page Title (if we have one, and one not provided).
// extensionHint ??= _pageModel?.Unsafe?.Title;
extensionHint ??= ExtensionHost.Extension?.ExtensionDisplayName ?? Title;
Task.Factory.StartNew(
() =>
{
ErrorMessage += $"A bug occurred in {$"the \"{extensionHint}\"" ?? "an unknown's"} extension's code:\n{ex.Message}\n{ex.Source}\n{ex.StackTrace}\n\n";
},
CancellationToken.None,
TaskCreationOptions.None,
Scheduler);
}
public override string ToString() => $"{Title} ViewModel";
protected override void UnsafeCleanup()
{
base.UnsafeCleanup();
ExtensionHost.StatusMessages.CollectionChanged -= StatusMessages_CollectionChanged;
var model = _pageModel.Unsafe;
if (model != null)
{
model.PropChanged -= Model_PropChanged;
}
}
}
public interface IPageContext
{
public void ShowException(Exception ex, string? extensionHint = null);
public TaskScheduler Scheduler { get; }
}