2025-07-09 14:53:47 -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.Messaging;
|
2025-09-16 03:36:38 +02:00
|
|
|
|
using ManagedCommon;
|
2025-07-15 12:21:44 -05:00
|
|
|
|
using Microsoft.CmdPal.Core.ViewModels.Messages;
|
2025-07-09 14:53:47 -05:00
|
|
|
|
using Microsoft.CommandPalette.Extensions;
|
|
|
|
|
|
using Microsoft.CommandPalette.Extensions.Toolkit;
|
|
|
|
|
|
using Windows.System;
|
|
|
|
|
|
|
2025-07-15 12:21:44 -05:00
|
|
|
|
namespace Microsoft.CmdPal.Core.ViewModels;
|
2025-07-09 14:53:47 -05:00
|
|
|
|
|
|
|
|
|
|
public partial class ContextMenuViewModel : ObservableObject,
|
2025-07-28 18:46:16 -05:00
|
|
|
|
IRecipient<UpdateCommandBarMessage>
|
2025-07-09 14:53:47 -05:00
|
|
|
|
{
|
|
|
|
|
|
public ICommandBarContext? SelectedItem
|
|
|
|
|
|
{
|
|
|
|
|
|
get => field;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
field = value;
|
2025-07-16 06:25:24 -05:00
|
|
|
|
UpdateContextItems();
|
2025-07-09 14:53:47 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private partial ObservableCollection<List<IContextItemViewModel>> ContextMenuStack { get; set; } = [];
|
|
|
|
|
|
|
|
|
|
|
|
private List<IContextItemViewModel>? CurrentContextMenu => ContextMenuStack.LastOrDefault();
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
public partial ObservableCollection<IContextItemViewModel> FilteredItems { get; set; } = [];
|
|
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
public partial bool FilterOnTop { get; set; } = false;
|
|
|
|
|
|
|
|
|
|
|
|
private string _lastSearchText = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
public ContextMenuViewModel()
|
|
|
|
|
|
{
|
|
|
|
|
|
WeakReferenceMessenger.Default.Register<UpdateCommandBarMessage>(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Receive(UpdateCommandBarMessage message)
|
|
|
|
|
|
{
|
|
|
|
|
|
SelectedItem = message.ViewModel;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void UpdateContextItems()
|
|
|
|
|
|
{
|
2025-08-18 06:07:28 -05:00
|
|
|
|
if (SelectedItem is not null)
|
2025-07-09 14:53:47 -05:00
|
|
|
|
{
|
|
|
|
|
|
if (SelectedItem.MoreCommands.Count() > 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
ContextMenuStack.Clear();
|
|
|
|
|
|
PushContextStack(SelectedItem.AllCommands);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void SetSearchText(string searchText)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (searchText == _lastSearchText)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-18 06:07:28 -05:00
|
|
|
|
if (SelectedItem is null)
|
2025-07-09 14:53:47 -05:00
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_lastSearchText = searchText;
|
|
|
|
|
|
|
2025-08-18 06:07:28 -05:00
|
|
|
|
if (CurrentContextMenu is null)
|
2025-07-09 14:53:47 -05:00
|
|
|
|
{
|
|
|
|
|
|
ListHelpers.InPlaceUpdateList(FilteredItems, []);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(searchText))
|
|
|
|
|
|
{
|
|
|
|
|
|
ListHelpers.InPlaceUpdateList(FilteredItems, [.. CurrentContextMenu]);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var commands = CurrentContextMenu
|
|
|
|
|
|
.OfType<CommandContextItemViewModel>()
|
|
|
|
|
|
.Where(c => c.ShouldBeVisible);
|
|
|
|
|
|
|
|
|
|
|
|
var newResults = ListHelpers.FilterList<CommandContextItemViewModel>(commands, searchText, ScoreContextCommand);
|
|
|
|
|
|
ListHelpers.InPlaceUpdateList(FilteredItems, newResults);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static int ScoreContextCommand(string query, CommandContextItemViewModel item)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(query) || string.IsNullOrWhiteSpace(query))
|
|
|
|
|
|
{
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(item.Title))
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
CmdPal go brrrr (performance improvements) (#41959)
Still a WIP, but here's the deets so far:
## No more throwing canceled tokens
Throwing exceptions is expensive and since we essentially cancel tokens
anytime someone is typing beyond the debounce, we could be throwing
exceptions a ton during search. Since we don't care about those past
executions, now they just `return`.
## Reduced number of apps returned in search
While users can specify how many apps (no limit, 1, 5), if they specify
no limit, we hard limit it at 10. For a few reasons, fuzzy search gets
_really_ fuzzy sometimes and gives answers that users would think is
just plain wrong and they make the response list longer than it needs to
be.
## Fuzzy search: still fuzzy, but faster
Replaced `StringMatcher` class with `FuzzyStringMatcher`.
`FuzzyStringMatcher` is a C# port by @zadjii-msft of the Rust port by
@lhecker for [microsoft/edit](https://github.com/microsoft/edit), which
I believe originally came from [VS
Code](https://github.com/microsoft/vscode). It's a whole fuzzy rabbit
hole. But it's faster than the `StringMatcher` class it replaced.
## Fallbacks, you need to fall back
"In the beginning, fallbacks were created. This had made many people
very angry and has been widely regarded as a bad move."
Hitchhiker's Guide to the Galaxy jokes aside, fallbacks are one cause of
slower search results. A few modifications have been made to get them
out of the way without reverting their ability to do things dynamically.
1. Fallbacks are no longer scored and will always* appear at the bottom
of the search results
2. In updating their search text, we now use a cancellation token to
stop processing previous searches when a new keypress is recorded.
## * But Calculator & Run are special
So, remember when I said that all fallbacks will not be ranked and
always display at the bottom of the results? Surprise, some will be
ranked and displayed based on that score. Specifically, Calculator and
Run are fallbacks that are whitelisted from the restrictions mentioned
above. They will continue to act as they do today.
We do have the ability to add future fallbacks to that whitelist as
well.
---
## Current preview
Updated: 2025-09-24
https://github.com/user-attachments/assets/c74c9a8e-e438-4101-840b-1408d2acaefd
---
Closes #39763
Closes #39239
Closes #39948
Closes #38594
Closes #40330
2025-09-25 13:48:13 -05:00
|
|
|
|
var nameMatch = FuzzyStringMatcher.ScoreFuzzy(query, item.Title);
|
2025-07-09 14:53:47 -05:00
|
|
|
|
|
CmdPal go brrrr (performance improvements) (#41959)
Still a WIP, but here's the deets so far:
## No more throwing canceled tokens
Throwing exceptions is expensive and since we essentially cancel tokens
anytime someone is typing beyond the debounce, we could be throwing
exceptions a ton during search. Since we don't care about those past
executions, now they just `return`.
## Reduced number of apps returned in search
While users can specify how many apps (no limit, 1, 5), if they specify
no limit, we hard limit it at 10. For a few reasons, fuzzy search gets
_really_ fuzzy sometimes and gives answers that users would think is
just plain wrong and they make the response list longer than it needs to
be.
## Fuzzy search: still fuzzy, but faster
Replaced `StringMatcher` class with `FuzzyStringMatcher`.
`FuzzyStringMatcher` is a C# port by @zadjii-msft of the Rust port by
@lhecker for [microsoft/edit](https://github.com/microsoft/edit), which
I believe originally came from [VS
Code](https://github.com/microsoft/vscode). It's a whole fuzzy rabbit
hole. But it's faster than the `StringMatcher` class it replaced.
## Fallbacks, you need to fall back
"In the beginning, fallbacks were created. This had made many people
very angry and has been widely regarded as a bad move."
Hitchhiker's Guide to the Galaxy jokes aside, fallbacks are one cause of
slower search results. A few modifications have been made to get them
out of the way without reverting their ability to do things dynamically.
1. Fallbacks are no longer scored and will always* appear at the bottom
of the search results
2. In updating their search text, we now use a cancellation token to
stop processing previous searches when a new keypress is recorded.
## * But Calculator & Run are special
So, remember when I said that all fallbacks will not be ranked and
always display at the bottom of the results? Surprise, some will be
ranked and displayed based on that score. Specifically, Calculator and
Run are fallbacks that are whitelisted from the restrictions mentioned
above. They will continue to act as they do today.
We do have the ability to add future fallbacks to that whitelist as
well.
---
## Current preview
Updated: 2025-09-24
https://github.com/user-attachments/assets/c74c9a8e-e438-4101-840b-1408d2acaefd
---
Closes #39763
Closes #39239
Closes #39948
Closes #38594
Closes #40330
2025-09-25 13:48:13 -05:00
|
|
|
|
var descriptionMatch = FuzzyStringMatcher.ScoreFuzzy(query, item.Subtitle);
|
2025-07-09 14:53:47 -05:00
|
|
|
|
|
CmdPal go brrrr (performance improvements) (#41959)
Still a WIP, but here's the deets so far:
## No more throwing canceled tokens
Throwing exceptions is expensive and since we essentially cancel tokens
anytime someone is typing beyond the debounce, we could be throwing
exceptions a ton during search. Since we don't care about those past
executions, now they just `return`.
## Reduced number of apps returned in search
While users can specify how many apps (no limit, 1, 5), if they specify
no limit, we hard limit it at 10. For a few reasons, fuzzy search gets
_really_ fuzzy sometimes and gives answers that users would think is
just plain wrong and they make the response list longer than it needs to
be.
## Fuzzy search: still fuzzy, but faster
Replaced `StringMatcher` class with `FuzzyStringMatcher`.
`FuzzyStringMatcher` is a C# port by @zadjii-msft of the Rust port by
@lhecker for [microsoft/edit](https://github.com/microsoft/edit), which
I believe originally came from [VS
Code](https://github.com/microsoft/vscode). It's a whole fuzzy rabbit
hole. But it's faster than the `StringMatcher` class it replaced.
## Fallbacks, you need to fall back
"In the beginning, fallbacks were created. This had made many people
very angry and has been widely regarded as a bad move."
Hitchhiker's Guide to the Galaxy jokes aside, fallbacks are one cause of
slower search results. A few modifications have been made to get them
out of the way without reverting their ability to do things dynamically.
1. Fallbacks are no longer scored and will always* appear at the bottom
of the search results
2. In updating their search text, we now use a cancellation token to
stop processing previous searches when a new keypress is recorded.
## * But Calculator & Run are special
So, remember when I said that all fallbacks will not be ranked and
always display at the bottom of the results? Surprise, some will be
ranked and displayed based on that score. Specifically, Calculator and
Run are fallbacks that are whitelisted from the restrictions mentioned
above. They will continue to act as they do today.
We do have the ability to add future fallbacks to that whitelist as
well.
---
## Current preview
Updated: 2025-09-24
https://github.com/user-attachments/assets/c74c9a8e-e438-4101-840b-1408d2acaefd
---
Closes #39763
Closes #39239
Closes #39948
Closes #38594
Closes #40330
2025-09-25 13:48:13 -05:00
|
|
|
|
return new[] { nameMatch, (descriptionMatch - 4) / 2, 0 }.Max();
|
2025-07-09 14:53:47 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Generates a mapping of key -> command item for this particular item's
|
|
|
|
|
|
/// MoreCommands. (This won't include the primary Command, but it will
|
|
|
|
|
|
/// include the secondary one). This map can be used to quickly check if a
|
2025-09-16 03:36:38 +02:00
|
|
|
|
/// shortcut key was pressed. In case there are duplicate keybindings, the first
|
|
|
|
|
|
/// one is used and the rest are ignored.
|
2025-07-09 14:53:47 -05:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>a dictionary of KeyChord -> Context commands, for all commands
|
|
|
|
|
|
/// that have a shortcut key set.</returns>
|
2025-09-16 03:36:38 +02:00
|
|
|
|
private Dictionary<KeyChord, CommandContextItemViewModel> Keybindings()
|
2025-07-09 14:53:47 -05:00
|
|
|
|
{
|
2025-09-16 03:36:38 +02:00
|
|
|
|
var result = new Dictionary<KeyChord, CommandContextItemViewModel>();
|
|
|
|
|
|
|
|
|
|
|
|
var menu = CurrentContextMenu;
|
|
|
|
|
|
if (menu is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var item in menu)
|
2025-07-09 14:53:47 -05:00
|
|
|
|
{
|
2025-09-16 03:36:38 +02:00
|
|
|
|
if (item is CommandContextItemViewModel cmd && cmd.HasRequestedShortcut)
|
|
|
|
|
|
{
|
|
|
|
|
|
var key = cmd.RequestedShortcut ?? new KeyChord(0, 0, 0);
|
|
|
|
|
|
var added = result.TryAdd(key, cmd);
|
|
|
|
|
|
if (!added)
|
|
|
|
|
|
{
|
|
|
|
|
|
Logger.LogWarning($"Ignoring duplicate keyboard shortcut {KeyChordHelpers.FormatForDebug(key)} on command '{cmd.Title ?? cmd.Name ?? "(unknown)"}'");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-09 14:53:47 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-16 03:36:38 +02:00
|
|
|
|
return result;
|
2025-07-09 14:53:47 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ContextKeybindingResult? CheckKeybinding(bool ctrl, bool alt, bool shift, bool win, VirtualKey key)
|
|
|
|
|
|
{
|
|
|
|
|
|
var keybindings = Keybindings();
|
2025-09-16 03:36:38 +02:00
|
|
|
|
|
|
|
|
|
|
// Does the pressed key match any of the keybindings?
|
|
|
|
|
|
var pressedKeyChord = KeyChordHelpers.FromModifiers(ctrl, alt, shift, win, key, 0);
|
|
|
|
|
|
if (keybindings.TryGetValue(pressedKeyChord, out var item))
|
2025-07-09 14:53:47 -05:00
|
|
|
|
{
|
2025-09-16 03:36:38 +02:00
|
|
|
|
return InvokeCommand(item);
|
2025-07-09 14:53:47 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool CanPopContextStack()
|
|
|
|
|
|
{
|
|
|
|
|
|
return ContextMenuStack.Count > 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void PopContextStack()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ContextMenuStack.Count > 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
ContextMenuStack.RemoveAt(ContextMenuStack.Count - 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OnPropertyChanging(nameof(CurrentContextMenu));
|
|
|
|
|
|
OnPropertyChanged(nameof(CurrentContextMenu));
|
|
|
|
|
|
|
|
|
|
|
|
ListHelpers.InPlaceUpdateList(FilteredItems, [.. CurrentContextMenu!]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void PushContextStack(IEnumerable<IContextItemViewModel> commands)
|
|
|
|
|
|
{
|
|
|
|
|
|
ContextMenuStack.Add(commands.ToList());
|
|
|
|
|
|
OnPropertyChanging(nameof(CurrentContextMenu));
|
|
|
|
|
|
OnPropertyChanged(nameof(CurrentContextMenu));
|
|
|
|
|
|
|
|
|
|
|
|
ListHelpers.InPlaceUpdateList(FilteredItems, [.. CurrentContextMenu!]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-28 18:46:16 -05:00
|
|
|
|
public void ResetContextMenu()
|
2025-07-09 14:53:47 -05:00
|
|
|
|
{
|
|
|
|
|
|
while (ContextMenuStack.Count > 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
ContextMenuStack.RemoveAt(ContextMenuStack.Count - 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OnPropertyChanging(nameof(CurrentContextMenu));
|
|
|
|
|
|
OnPropertyChanged(nameof(CurrentContextMenu));
|
|
|
|
|
|
|
2025-08-18 06:07:28 -05:00
|
|
|
|
if (CurrentContextMenu is not null)
|
2025-07-09 14:53:47 -05:00
|
|
|
|
{
|
|
|
|
|
|
ListHelpers.InPlaceUpdateList(FilteredItems, [.. CurrentContextMenu!]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ContextKeybindingResult InvokeCommand(CommandItemViewModel? command)
|
|
|
|
|
|
{
|
2025-08-18 06:07:28 -05:00
|
|
|
|
if (command is null)
|
2025-07-09 14:53:47 -05:00
|
|
|
|
{
|
|
|
|
|
|
return ContextKeybindingResult.Unhandled;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (command.HasMoreCommands)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Display the commands child commands
|
|
|
|
|
|
PushContextStack(command.AllCommands);
|
|
|
|
|
|
OnPropertyChanging(nameof(FilteredItems));
|
|
|
|
|
|
OnPropertyChanged(nameof(FilteredItems));
|
|
|
|
|
|
return ContextKeybindingResult.KeepOpen;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
WeakReferenceMessenger.Default.Send<PerformCommandMessage>(new(command.Command.Model, command.Model));
|
|
|
|
|
|
UpdateContextItems();
|
|
|
|
|
|
return ContextKeybindingResult.Hide;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|