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>.


----
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 ;
using System.Collections.Concurrent ;
using System.Collections.Generic ;
using System.Collections.ObjectModel ;
using System.IO ;
using System.IO.Abstractions ;
using System.Threading.Tasks ;
using Microsoft.CmdPal.Ext.Apps.Programs ;
using Win32Program = Microsoft . CmdPal . Ext . Apps . Programs . Win32Program ;
namespace Microsoft.CmdPal.Ext.Apps.Storage ;
2025-04-30 22:53:06 +08:00
internal sealed partial class Win32ProgramRepository : ListRepository < Programs . Win32Program > , IProgramRepository
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>.


----
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
{
private static readonly IFileSystem FileSystem = new FileSystem ( ) ;
private static readonly IPath Path = FileSystem . Path ;
private const string LnkExtension = ".lnk" ;
private const string UrlExtension = ".url" ;
private AllAppsSettings _settings ;
private IList < IFileSystemWatcherWrapper > _fileSystemWatcherHelpers ;
private string [ ] _pathsToWatch ;
private int _numberOfPathsToWatch ;
private Collection < string > extensionsToWatch = new Collection < string > { "*.exe" , $"*{LnkExtension}" , "*.appref-ms" , $"*{UrlExtension}" } ;
private bool _isDirty ;
private static ConcurrentQueue < string > commonEventHandlingQueue = new ConcurrentQueue < string > ( ) ;
public Win32ProgramRepository ( IList < IFileSystemWatcherWrapper > fileSystemWatcherHelpers , AllAppsSettings settings , string [ ] pathsToWatch )
{
_fileSystemWatcherHelpers = fileSystemWatcherHelpers ;
_settings = settings ? ? throw new ArgumentNullException ( nameof ( settings ) , "Win32ProgramRepository requires an initialized settings object" ) ;
_pathsToWatch = pathsToWatch ;
_numberOfPathsToWatch = pathsToWatch . Length ;
InitializeFileSystemWatchers ( ) ;
// This task would always run in the background trying to dequeue file paths from the queue at regular intervals.
_ = Task . Run ( async ( ) = >
{
while ( true )
{
var dequeueDelay = 500 ;
var appPath = await EventHandler . GetAppPathFromQueueAsync ( commonEventHandlingQueue , dequeueDelay ) . ConfigureAwait ( false ) ;
// To allow for the installation process to finish.
await Task . Delay ( 5000 ) . ConfigureAwait ( false ) ;
if ( ! string . IsNullOrEmpty ( appPath ) )
{
Win32Program ? app = Win32Program . GetAppFromPath ( appPath ) ;
if ( app ! = null )
{
Add ( app ) ;
_isDirty = true ;
}
}
}
} ) . ConfigureAwait ( false ) ;
}
public bool ShouldReload ( )
{
return _isDirty ;
}
public void ResetReloadFlag ( )
{
_isDirty = false ;
}
private void InitializeFileSystemWatchers ( )
{
for ( var index = 0 ; index < _numberOfPathsToWatch ; index + + )
{
// To set the paths to monitor
_fileSystemWatcherHelpers [ index ] . Path = _pathsToWatch [ index ] ;
// to be notified when there is a change to a file
_fileSystemWatcherHelpers [ index ] . NotifyFilter = NotifyFilters . FileName | NotifyFilters . LastWrite ;
// filtering the app types that we want to monitor
_fileSystemWatcherHelpers [ index ] . Filters = extensionsToWatch ;
// Registering the event handlers
_fileSystemWatcherHelpers [ index ] . Created + = OnAppCreated ;
_fileSystemWatcherHelpers [ index ] . Deleted + = OnAppDeleted ;
_fileSystemWatcherHelpers [ index ] . Renamed + = OnAppRenamed ;
_fileSystemWatcherHelpers [ index ] . Changed + = OnAppChanged ;
// Enable the file system watcher
_fileSystemWatcherHelpers [ index ] . EnableRaisingEvents = true ;
// Enable it to search in sub folders as well
_fileSystemWatcherHelpers [ index ] . IncludeSubdirectories = true ;
}
}
private async Task OnAppRenamedAsync ( object sender , RenamedEventArgs e )
{
var oldPath = e . OldFullPath ;
var newPath = e . FullPath ;
// fix for https://github.com/microsoft/PowerToys/issues/34391
// the msi installer creates a shortcut, which is detected by the PT Run and ends up in calling this OnAppRenamed method
// the thread needs to be halted for a short time to avoid locking the new shortcut file as we read it, otherwise the lock causes
// in the issue scenario that a warning is popping up during the msi install process.
await Task . Delay ( 1000 ) . ConfigureAwait ( false ) ;
var extension = Path . GetExtension ( newPath ) ;
Win32Program . ApplicationType oldAppType = Win32Program . GetAppTypeFromPath ( oldPath ) ;
Programs . Win32Program ? newApp = Win32Program . GetAppFromPath ( newPath ) ;
Programs . Win32Program ? oldApp = null ;
// Once the shortcut application is renamed, the old app does not exist and therefore when we try to get the FullPath we get the lnk path instead of the exe path
// This changes the hashCode() of the old application.
// Therefore, instead of retrieving the old app using the GetAppFromPath(), we construct the application ourself
// This situation is not encountered for other application types because the fullPath is the path itself, instead of being computed by using the path to the app.
try
{
if ( oldAppType = = Win32Program . ApplicationType . ShortcutApplication | | oldAppType = = Win32Program . ApplicationType . InternetShortcutApplication )
{
oldApp = new Win32Program ( ) { Name = Path . GetFileNameWithoutExtension ( e . OldName ) ? ? string . Empty , ExecutableName = Path . GetFileName ( e . OldName ) ? ? string . Empty , FullPath = newApp ? . FullPath ? ? oldPath } ;
}
else
{
oldApp = Win32Program . GetAppFromPath ( oldPath ) ;
}
}
catch ( Exception )
{
}
// To remove the old app which has been renamed and to add the new application.
if ( oldApp ! = null )
{
if ( string . IsNullOrWhiteSpace ( oldApp . Name ) | | string . IsNullOrWhiteSpace ( oldApp . ExecutableName ) | | string . IsNullOrWhiteSpace ( oldApp . FullPath ) )
{
}
else
{
Remove ( oldApp ) ;
_isDirty = true ;
}
}
if ( newApp ! = null )
{
Add ( newApp ) ;
_isDirty = true ;
}
}
private void OnAppRenamed ( object sender , RenamedEventArgs e )
{
Task . Run ( async ( ) = >
{
await OnAppRenamedAsync ( sender , e ) . ConfigureAwait ( false ) ;
} ) . ConfigureAwait ( false ) ;
}
private void OnAppDeleted ( object sender , FileSystemEventArgs e )
{
var path = e . FullPath ;
var extension = Path . GetExtension ( path ) ;
Win32Program ? app = null ;
try
{
// To mitigate the issue of not having a FullPath for a shortcut app, we iterate through the items and find the app with the same hashcode.
// Using OrdinalIgnoreCase since this is used internally
if ( extension . Equals ( LnkExtension , StringComparison . OrdinalIgnoreCase ) )
{
app = GetAppWithSameLnkFilePath ( path ) ;
if ( app = = null )
{
// Cancelled links won't have a resolved path.
app = GetAppWithSameNameAndExecutable ( Path . GetFileNameWithoutExtension ( path ) , Path . GetFileName ( path ) ) ;
}
}
else if ( extension . Equals ( UrlExtension , StringComparison . OrdinalIgnoreCase ) )
{
app = GetAppWithSameNameAndExecutable ( Path . GetFileNameWithoutExtension ( path ) , Path . GetFileName ( path ) ) ;
}
else
{
app = Programs . Win32Program . GetAppFromPath ( path ) ;
}
}
catch ( Exception )
{
}
if ( app ! = null )
{
Remove ( app ) ;
_isDirty = true ;
}
}
// When a URL application is deleted, we can no longer get the HashCode directly from the path because the FullPath a Url app is the URL obtained from reading the file
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1309:Use ordinal string comparison", Justification = "Using CurrentCultureIgnoreCase since application names could be dependent on currentculture See: https://github.com/microsoft/PowerToys/pull/5847/files#r468245190")]
private Win32Program ? GetAppWithSameNameAndExecutable ( string name , string executableName )
{
foreach ( Win32Program app in Items )
{
// Using CurrentCultureIgnoreCase since application names could be dependent on currentculture See: https://github.com/microsoft/PowerToys/pull/5847/files#r468245190
if ( name . Equals ( app . Name , StringComparison . CurrentCultureIgnoreCase ) & & executableName . Equals ( app . ExecutableName , StringComparison . CurrentCultureIgnoreCase ) )
{
return app ;
}
}
return null ;
}
// To mitigate the issue faced (as stated above) when a shortcut application is renamed, the Exe FullPath and executable name must be obtained.
// Unlike the rename event args, since we do not have a newPath, we iterate through all the programs and find the one with the same LnkResolved path.
private Programs . Win32Program ? GetAppWithSameLnkFilePath ( string lnkFilePath )
{
foreach ( Programs . Win32Program app in Items )
{
// Using Invariant / OrdinalIgnoreCase since we're comparing paths
if ( lnkFilePath . ToUpperInvariant ( ) . Equals ( app . LnkFilePath , StringComparison . OrdinalIgnoreCase ) )
{
return app ;
}
}
return null ;
}
private void OnAppCreated ( object sender , FileSystemEventArgs e )
{
var path = e . FullPath ;
// Using OrdinalIgnoreCase since we're comparing extensions
if ( ! Path . GetExtension ( path ) . Equals ( UrlExtension , StringComparison . OrdinalIgnoreCase ) & & ! Path . GetExtension ( path ) . Equals ( LnkExtension , StringComparison . OrdinalIgnoreCase ) )
{
Programs . Win32Program ? app = Programs . Win32Program . GetAppFromPath ( path ) ;
if ( app ! = null )
{
Add ( app ) ;
_isDirty = true ;
}
}
}
private void OnAppChanged ( object sender , FileSystemEventArgs e )
{
var path = e . FullPath ;
// Using OrdinalIgnoreCase since we're comparing extensions
if ( Path . GetExtension ( path ) . Equals ( UrlExtension , StringComparison . OrdinalIgnoreCase ) | | Path . GetExtension ( path ) . Equals ( LnkExtension , StringComparison . OrdinalIgnoreCase ) )
{
// When a url or lnk app is installed, multiple created and changed events are triggered.
// To prevent the code from acting on the first such event (which may still be during app installation), the events are added a common queue and dequeued by a background task at regular intervals - https://github.com/microsoft/PowerToys/issues/6429.
commonEventHandlingQueue . Enqueue ( path ) ;
}
}
public void IndexPrograms ( )
{
var applications = Programs . Win32Program . All ( _settings ) ;
SetList ( applications ) ;
}
}