mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 17:56:44 +02:00
CmdPal: Fallback ranking and global results (#43549)
> [!IMPORTANT] > For extension developers, this release includes a new required `string Id` property for `FallbackCommandItem`. While your existing extensions will continue to work, without this `Id` being set, your fallbacks will not display and will not be rankable. > Before this is released, you will want to prepare your extension fallbacks. > > As an example, we are naming our built-in extensions as: > - Calculator extension provider Id: `com.microsoft.cmdpal.builtin.calculator` > - Calculator extension fallback: `com.microsoft.cmdpal.builtin.calculator.fallback` > > While the content of the Id isn't important, what is important is that it is unique to your extension and fallback to avoid conflicting with other extensions. Now the good stuff: ## What the heck does it do!? ### The backstory In PowerToys 0.95, we released performance improvements to Command Palette. One of the many ways we improved its speed is by no longer ranking fallback commands with other "top level" commands. Instead, all fallbacks would surface at the bottom of the results and be listed in the order they were registered with Command Palette. But this was only a temporary solution until the work included in this pull request was ready. In reality, not all fallbacks were treated equally. We marked the calculator and run fallbacks as "special." Special fallbacks **were** ranked like top-level commands and allowed to surface to the top of the results. ### The new "hotness" This PR brings the power of fallback management back to the people. In the Command Palette settings, you, dear user, can specify what order you want fallbacks to display in at the bottom of the results. This keeps those fallbacks unranked by Command Palette but displays them in an order that makes sense for you. But keep in mind, these will still live at the bottom of search results. But alas, we have also heard your cries that you'd like _some_ fallbacks to be ranked by Command Palette and surface to the top of the results. So, this PR allows you to mark any fallback as "special" by choosing to include them in the global results. Special (Global) fallbacks are treated like "top level" commands and appear in the search result based on their title & description. ### Screenshots/video <img width="1005" height="611" alt="image" src="https://github.com/user-attachments/assets/8ba5d861-f887-47ed-8552-ba78937322d2" /> <img width="1501" height="973" alt="image" src="https://github.com/user-attachments/assets/9edb7675-8084-4f14-8bdc-72d7d06d500e" /> <img width="706" height="744" alt="image" src="https://github.com/user-attachments/assets/81ae0252-b87d-4172-a5ea-4d3102134baf" /> <img width="666" height="786" alt="image" src="https://github.com/user-attachments/assets/acb76acf-531d-4e60-bb44-d1edeec77dce" /> ### GitHub issue maintenance details Closes #38312 Closes #38288 Closes #42524 Closes #41024 Closes #40351 Closes #41696 Closes #40193 --------- Co-authored-by: Niels Laute <niels.laute@live.nl> Co-authored-by: Jiří Polášek <me@jiripolasek.com>
This commit is contained in:
@@ -188,11 +188,12 @@ public sealed class CommandProviderWrapper
|
||||
Func<ICommandItem?, bool, TopLevelViewModel> makeAndAdd = (ICommandItem? i, bool fallback) =>
|
||||
{
|
||||
CommandItemViewModel commandItemViewModel = new(new(i), pageContext);
|
||||
TopLevelViewModel topLevelViewModel = new(commandItemViewModel, fallback, ExtensionHost, ProviderId, settings, providerSettings, serviceProvider);
|
||||
TopLevelViewModel topLevelViewModel = new(commandItemViewModel, fallback, ExtensionHost, ProviderId, settings, providerSettings, serviceProvider, i);
|
||||
topLevelViewModel.InitializeProperties();
|
||||
|
||||
return topLevelViewModel;
|
||||
};
|
||||
|
||||
if (commands is not null)
|
||||
{
|
||||
TopLevelItems = commands
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Microsoft.CmdPal.Core.ViewModels.Messages;
|
||||
using Microsoft.CommandPalette.Extensions;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
|
||||
@@ -27,7 +26,13 @@ public sealed partial class BuiltInsCommandProvider : CommandProvider
|
||||
|
||||
public override IFallbackCommandItem[] FallbackCommands() =>
|
||||
[
|
||||
new FallbackCommandItem(quitCommand, displayTitle: Properties.Resources.builtin_quit_subtitle) { Subtitle = Properties.Resources.builtin_quit_subtitle },
|
||||
new FallbackCommandItem(
|
||||
quitCommand,
|
||||
Properties.Resources.builtin_quit_subtitle,
|
||||
quitCommand.Id)
|
||||
{
|
||||
Subtitle = Properties.Resources.builtin_quit_subtitle,
|
||||
},
|
||||
_fallbackReloadItem,
|
||||
_fallbackLogItem,
|
||||
];
|
||||
|
||||
@@ -13,8 +13,10 @@ internal sealed partial class FallbackLogItem : FallbackCommandItem
|
||||
{
|
||||
private readonly LogMessagesPage _logMessagesPage;
|
||||
|
||||
private const string _id = "com.microsoft.cmdpal.log";
|
||||
|
||||
public FallbackLogItem()
|
||||
: base(new LogMessagesPage() { Id = "com.microsoft.cmdpal.log" }, Resources.builtin_log_subtitle)
|
||||
: base(new LogMessagesPage() { Id = _id }, Resources.builtin_log_subtitle, _id)
|
||||
{
|
||||
_logMessagesPage = (LogMessagesPage)Command!;
|
||||
Title = string.Empty;
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// 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 Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
|
||||
namespace Microsoft.CmdPal.UI.ViewModels.BuiltinCommands;
|
||||
@@ -11,10 +10,13 @@ internal sealed partial class FallbackReloadItem : FallbackCommandItem
|
||||
{
|
||||
private readonly ReloadExtensionsCommand _reloadCommand;
|
||||
|
||||
private const string _id = "com.microsoft.cmdpal.reload";
|
||||
|
||||
public FallbackReloadItem()
|
||||
: base(
|
||||
new ReloadExtensionsCommand() { Id = "com.microsoft.cmdpal.reload" },
|
||||
Properties.Resources.builtin_reload_display_title)
|
||||
new ReloadExtensionsCommand() { Id = _id },
|
||||
Properties.Resources.builtin_reload_display_title,
|
||||
_id)
|
||||
{
|
||||
_reloadCommand = (ReloadExtensionsCommand)Command!;
|
||||
Title = string.Empty;
|
||||
|
||||
@@ -17,7 +17,6 @@ using Microsoft.CmdPal.UI.ViewModels.Messages;
|
||||
using Microsoft.CmdPal.UI.ViewModels.Properties;
|
||||
using Microsoft.CommandPalette.Extensions;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Microsoft.CmdPal.UI.ViewModels.MainPage;
|
||||
|
||||
@@ -29,26 +28,17 @@ public partial class MainListPage : DynamicListPage,
|
||||
IRecipient<ClearSearchMessage>,
|
||||
IRecipient<UpdateFallbackItemsMessage>, IDisposable
|
||||
{
|
||||
private readonly string[] _specialFallbacks = [
|
||||
"com.microsoft.cmdpal.builtin.run",
|
||||
"com.microsoft.cmdpal.builtin.calculator",
|
||||
"com.microsoft.cmdpal.builtin.system",
|
||||
"com.microsoft.cmdpal.builtin.core",
|
||||
"com.microsoft.cmdpal.builtin.websearch",
|
||||
"com.microsoft.cmdpal.builtin.windowssettings",
|
||||
"com.microsoft.cmdpal.builtin.datetime",
|
||||
"com.microsoft.cmdpal.builtin.remotedesktop",
|
||||
];
|
||||
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly TopLevelCommandManager _tlcManager;
|
||||
private readonly AliasManager _aliasManager;
|
||||
private readonly SettingsModel _settings;
|
||||
private readonly AppStateModel _appStateModel;
|
||||
private List<Scored<IListItem>>? _filteredItems;
|
||||
private List<Scored<IListItem>>? _filteredApps;
|
||||
private List<Scored<IListItem>>? _fallbackItems;
|
||||
|
||||
// Keep as IEnumerable for deferred execution. Fallback item titles are updated
|
||||
// asynchronously, so scoring must happen lazily when GetItems is called.
|
||||
private IEnumerable<Scored<IListItem>>? _scoredFallbackItems;
|
||||
private IEnumerable<Scored<IListItem>>? _fallbackItems;
|
||||
private bool _includeApps;
|
||||
private bool _filteredItemsIncludesApps;
|
||||
private int _appResultLimit = 10;
|
||||
@@ -58,14 +48,16 @@ public partial class MainListPage : DynamicListPage,
|
||||
|
||||
private CancellationTokenSource? _cancellationTokenSource;
|
||||
|
||||
public MainListPage(IServiceProvider serviceProvider)
|
||||
public MainListPage(TopLevelCommandManager topLevelCommandManager, SettingsModel settings, AliasManager aliasManager, AppStateModel appStateModel)
|
||||
{
|
||||
Title = Resources.builtin_home_name;
|
||||
Icon = IconHelpers.FromRelativePath("Assets\\StoreLogo.scale-200.png");
|
||||
PlaceholderText = Properties.Resources.builtin_main_list_page_searchbar_placeholder;
|
||||
_serviceProvider = serviceProvider;
|
||||
|
||||
_tlcManager = _serviceProvider.GetService<TopLevelCommandManager>()!;
|
||||
_settings = settings;
|
||||
_aliasManager = aliasManager;
|
||||
_appStateModel = appStateModel;
|
||||
_tlcManager = topLevelCommandManager;
|
||||
_tlcManager.PropertyChanged += TlcManager_PropertyChanged;
|
||||
_tlcManager.TopLevelCommands.CollectionChanged += Commands_CollectionChanged;
|
||||
|
||||
@@ -83,7 +75,6 @@ public partial class MainListPage : DynamicListPage,
|
||||
WeakReferenceMessenger.Default.Register<ClearSearchMessage>(this);
|
||||
WeakReferenceMessenger.Default.Register<UpdateFallbackItemsMessage>(this);
|
||||
|
||||
var settings = _serviceProvider.GetService<SettingsModel>()!;
|
||||
settings.SettingsChanged += SettingsChangedHandler;
|
||||
HotReloadSettings(settings);
|
||||
_includeApps = _tlcManager.IsProviderActive(AllAppsCommandProvider.WellKnownId);
|
||||
@@ -163,14 +154,29 @@ public partial class MainListPage : DynamicListPage,
|
||||
{
|
||||
// Either return the top-level commands (no search text), or the merged and
|
||||
// filtered results.
|
||||
return string.IsNullOrEmpty(SearchText)
|
||||
? _tlcManager.TopLevelCommands.Where(tlc => !tlc.IsFallback && !string.IsNullOrEmpty(tlc.Title)).ToArray()
|
||||
: MainListPageResultFactory.Create(
|
||||
if (string.IsNullOrWhiteSpace(SearchText))
|
||||
{
|
||||
return _tlcManager.TopLevelCommands
|
||||
.Where(tlc => !tlc.IsFallback && !string.IsNullOrEmpty(tlc.Title))
|
||||
.ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
var validScoredFallbacks = _scoredFallbackItems?
|
||||
.Where(s => !string.IsNullOrWhiteSpace(s.Item.Title))
|
||||
.ToList();
|
||||
|
||||
var validFallbacks = _fallbackItems?
|
||||
.Where(s => !string.IsNullOrWhiteSpace(s.Item.Title))
|
||||
.ToList();
|
||||
|
||||
return MainListPageResultFactory.Create(
|
||||
_filteredItems,
|
||||
_scoredFallbackItems?.ToList(),
|
||||
validScoredFallbacks,
|
||||
_filteredApps,
|
||||
_fallbackItems,
|
||||
validFallbacks,
|
||||
_appResultLimit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,7 +206,7 @@ public partial class MainListPage : DynamicListPage,
|
||||
// Handle changes to the filter text here
|
||||
if (!string.IsNullOrEmpty(SearchText))
|
||||
{
|
||||
var aliases = _serviceProvider.GetService<AliasManager>()!;
|
||||
var aliases = _aliasManager;
|
||||
|
||||
if (token.IsCancellationRequested)
|
||||
{
|
||||
@@ -236,7 +242,8 @@ public partial class MainListPage : DynamicListPage,
|
||||
}
|
||||
|
||||
// prefilter fallbacks
|
||||
var specialFallbacks = new List<TopLevelViewModel>(_specialFallbacks.Length);
|
||||
var globalFallbacks = _settings.GetGlobalFallbacks();
|
||||
var specialFallbacks = new List<TopLevelViewModel>(globalFallbacks.Length);
|
||||
var commonFallbacks = new List<TopLevelViewModel>();
|
||||
|
||||
foreach (var s in commands)
|
||||
@@ -246,7 +253,7 @@ public partial class MainListPage : DynamicListPage,
|
||||
continue;
|
||||
}
|
||||
|
||||
if (_specialFallbacks.Contains(s.CommandProviderId))
|
||||
if (globalFallbacks.Contains(s.Id))
|
||||
{
|
||||
specialFallbacks.Add(s);
|
||||
}
|
||||
@@ -369,7 +376,7 @@ public partial class MainListPage : DynamicListPage,
|
||||
}
|
||||
}
|
||||
|
||||
var history = _serviceProvider.GetService<AppStateModel>()!.RecentCommands!;
|
||||
var history = _appStateModel.RecentCommands!;
|
||||
Func<string, IListItem, int> scoreItem = (a, b) => { return ScoreTopLevelItem(a, b, history); };
|
||||
|
||||
// Produce a list of everything that matches the current filter.
|
||||
@@ -380,7 +387,7 @@ public partial class MainListPage : DynamicListPage,
|
||||
return;
|
||||
}
|
||||
|
||||
IEnumerable<IListItem> newFallbacksForScoring = commands.Where(s => s.IsFallback && _specialFallbacks.Contains(s.CommandProviderId));
|
||||
IEnumerable<IListItem> newFallbacksForScoring = commands.Where(s => s.IsFallback && globalFallbacks.Contains(s.Id));
|
||||
|
||||
if (token.IsCancellationRequested)
|
||||
{
|
||||
@@ -394,8 +401,8 @@ public partial class MainListPage : DynamicListPage,
|
||||
return;
|
||||
}
|
||||
|
||||
// Defaulting scored to 1 but we'll eventually use user rankings
|
||||
_fallbackItems = [.. newFallbacks.Select(f => new Scored<IListItem> { Item = f, Score = 1 })];
|
||||
Func<string, IListItem, int> scoreFallbackItem = (a, b) => { return ScoreFallbackItem(a, b, _settings.FallbackRanks); };
|
||||
_fallbackItems = [.. ListHelpers.FilterListWithScores<IListItem>(newFallbacks ?? [], SearchText, scoreFallbackItem)];
|
||||
|
||||
if (token.IsCancellationRequested)
|
||||
{
|
||||
@@ -464,9 +471,8 @@ public partial class MainListPage : DynamicListPage,
|
||||
|
||||
private bool ActuallyLoading()
|
||||
{
|
||||
var tlcManager = _serviceProvider.GetService<TopLevelCommandManager>()!;
|
||||
var allApps = AllAppsCommandProvider.Page;
|
||||
return allApps.IsLoading || tlcManager.IsLoading;
|
||||
return allApps.IsLoading || _tlcManager.IsLoading;
|
||||
}
|
||||
|
||||
// Almost verbatim ListHelpers.ScoreListItem, but also accounting for the
|
||||
@@ -558,13 +564,30 @@ public partial class MainListPage : DynamicListPage,
|
||||
return (int)finalScore;
|
||||
}
|
||||
|
||||
internal static int ScoreFallbackItem(string query, IListItem topLevelOrAppItem, string[] fallbackRanks)
|
||||
{
|
||||
// Default to 1 so it always shows in list.
|
||||
var finalScore = 1;
|
||||
|
||||
if (topLevelOrAppItem is TopLevelViewModel topLevelViewModel)
|
||||
{
|
||||
var index = Array.IndexOf(fallbackRanks, topLevelViewModel.Id);
|
||||
|
||||
if (index >= 0)
|
||||
{
|
||||
finalScore = fallbackRanks.Length - index + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return finalScore;
|
||||
}
|
||||
|
||||
public void UpdateHistory(IListItem topLevelOrAppItem)
|
||||
{
|
||||
var id = IdForTopLevelOrAppItem(topLevelOrAppItem);
|
||||
var state = _serviceProvider.GetService<AppStateModel>()!;
|
||||
var history = state.RecentCommands;
|
||||
var history = _appStateModel.RecentCommands;
|
||||
history.AddHistoryItem(id);
|
||||
AppStateModel.SaveState(state);
|
||||
AppStateModel.SaveState(_appStateModel);
|
||||
}
|
||||
|
||||
private static string IdForTopLevelOrAppItem(IListItem topLevelOrAppItem)
|
||||
@@ -596,10 +619,9 @@ public partial class MainListPage : DynamicListPage,
|
||||
_tlcManager.PropertyChanged -= TlcManager_PropertyChanged;
|
||||
_tlcManager.TopLevelCommands.CollectionChanged -= Commands_CollectionChanged;
|
||||
|
||||
var settings = _serviceProvider.GetService<SettingsModel>();
|
||||
if (settings is not null)
|
||||
if (_settings is not null)
|
||||
{
|
||||
settings.SettingsChanged -= SettingsChangedHandler;
|
||||
_settings.SettingsChanged -= SettingsChangedHandler;
|
||||
}
|
||||
|
||||
WeakReferenceMessenger.Default.UnregisterAll(this);
|
||||
|
||||
@@ -29,13 +29,19 @@ internal static class MainListPageResultFactory
|
||||
}
|
||||
|
||||
int len1 = filteredItems?.Count ?? 0;
|
||||
|
||||
// Empty fallbacks are removed prior to this merge.
|
||||
int len2 = scoredFallbackItems?.Count ?? 0;
|
||||
|
||||
// Apps are pre-sorted, so we just need to take the top N, limited by appResultLimit.
|
||||
int len3 = Math.Min(filteredApps?.Count ?? 0, appResultLimit);
|
||||
|
||||
int nonEmptyFallbackCount = fallbackItems?.Count ?? 0;
|
||||
|
||||
// Allocate the exact size of the result array.
|
||||
int totalCount = len1 + len2 + len3 + GetNonEmptyFallbackItemsCount(fallbackItems);
|
||||
// We'll add an extra slot for the fallbacks section header if needed.
|
||||
int totalCount = len1 + len2 + len3 + nonEmptyFallbackCount + (nonEmptyFallbackCount > 0 ? 1 : 0);
|
||||
|
||||
var result = new IListItem[totalCount];
|
||||
|
||||
// Three-way stable merge of already-sorted lists.
|
||||
@@ -119,9 +125,15 @@ internal static class MainListPageResultFactory
|
||||
}
|
||||
|
||||
// Append filtered fallback items. Fallback items are added post-sort so they are
|
||||
// always at the end of the list and eventually ordered based on user preference.
|
||||
// always at the end of the list and are sorted by user settings.
|
||||
if (fallbackItems is not null)
|
||||
{
|
||||
// Create the fallbacks section header
|
||||
if (fallbackItems.Count > 0)
|
||||
{
|
||||
result[writePos++] = new Separator(Properties.Resources.fallbacks);
|
||||
}
|
||||
|
||||
for (int i = 0; i < fallbackItems.Count; i++)
|
||||
{
|
||||
var item = fallbackItems[i].Item;
|
||||
@@ -143,7 +155,7 @@ internal static class MainListPageResultFactory
|
||||
{
|
||||
for (int i = 0; i < fallbackItems.Count; i++)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(fallbackItems[i].Item.Title))
|
||||
if (!string.IsNullOrWhiteSpace(fallbackItems[i].Item.Title))
|
||||
{
|
||||
fallbackItemsCount++;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
// 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.Text.Json.Serialization;
|
||||
|
||||
namespace Microsoft.CmdPal.UI.ViewModels;
|
||||
|
||||
public class FallbackSettings
|
||||
{
|
||||
public bool IsEnabled { get; set; } = true;
|
||||
|
||||
public bool IncludeInGlobalResults { get; set; }
|
||||
|
||||
public FallbackSettings()
|
||||
{
|
||||
}
|
||||
|
||||
public FallbackSettings(bool isBuiltIn)
|
||||
{
|
||||
IncludeInGlobalResults = isBuiltIn;
|
||||
}
|
||||
|
||||
[JsonConstructor]
|
||||
public FallbackSettings(bool isEnabled, bool includeInGlobalResults)
|
||||
{
|
||||
IsEnabled = isEnabled;
|
||||
IncludeInGlobalResults = includeInGlobalResults;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
// 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 CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Microsoft.CmdPal.Core.ViewModels;
|
||||
using Microsoft.CmdPal.UI.ViewModels.Messages;
|
||||
|
||||
namespace Microsoft.CmdPal.UI.ViewModels;
|
||||
|
||||
public partial class FallbackSettingsViewModel : ObservableObject
|
||||
{
|
||||
private readonly SettingsModel _settings;
|
||||
private readonly FallbackSettings _fallbackSettings;
|
||||
|
||||
public string DisplayName { get; private set; } = string.Empty;
|
||||
|
||||
public IconInfoViewModel Icon { get; private set; } = new(null);
|
||||
|
||||
public string Id { get; private set; } = string.Empty;
|
||||
|
||||
public bool IsEnabled
|
||||
{
|
||||
get => _fallbackSettings.IsEnabled;
|
||||
set
|
||||
{
|
||||
if (value != _fallbackSettings.IsEnabled)
|
||||
{
|
||||
_fallbackSettings.IsEnabled = value;
|
||||
|
||||
if (!_fallbackSettings.IsEnabled)
|
||||
{
|
||||
_fallbackSettings.IncludeInGlobalResults = false;
|
||||
}
|
||||
|
||||
Save();
|
||||
OnPropertyChanged(nameof(IsEnabled));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool IncludeInGlobalResults
|
||||
{
|
||||
get => _fallbackSettings.IncludeInGlobalResults;
|
||||
set
|
||||
{
|
||||
if (value != _fallbackSettings.IncludeInGlobalResults)
|
||||
{
|
||||
_fallbackSettings.IncludeInGlobalResults = value;
|
||||
|
||||
if (!_fallbackSettings.IsEnabled)
|
||||
{
|
||||
_fallbackSettings.IsEnabled = true;
|
||||
}
|
||||
|
||||
Save();
|
||||
OnPropertyChanged(nameof(IncludeInGlobalResults));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public FallbackSettingsViewModel(
|
||||
TopLevelViewModel fallback,
|
||||
FallbackSettings fallbackSettings,
|
||||
SettingsModel settingsModel,
|
||||
ProviderSettingsViewModel providerSettings)
|
||||
{
|
||||
_settings = settingsModel;
|
||||
_fallbackSettings = fallbackSettings;
|
||||
|
||||
Id = fallback.Id;
|
||||
DisplayName = string.IsNullOrWhiteSpace(fallback.DisplayTitle)
|
||||
? (string.IsNullOrWhiteSpace(fallback.Title) ? providerSettings.DisplayName : fallback.Title)
|
||||
: fallback.DisplayTitle;
|
||||
|
||||
Icon = new(fallback.InitialIcon);
|
||||
Icon.InitializeProperties();
|
||||
}
|
||||
|
||||
private void Save()
|
||||
{
|
||||
SettingsModel.SaveSettings(_settings);
|
||||
WeakReferenceMessenger.Default.Send<ReloadCommandsMessage>(new());
|
||||
}
|
||||
}
|
||||
@@ -205,7 +205,7 @@ namespace Microsoft.CmdPal.UI.ViewModels.Properties {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Create a new extension.
|
||||
/// Looks up a localized string similar to Create extension.
|
||||
/// </summary>
|
||||
public static string builtin_create_extension_title {
|
||||
get {
|
||||
@@ -349,7 +349,7 @@ namespace Microsoft.CmdPal.UI.ViewModels.Properties {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Creates a project for a new Command Palette extension.
|
||||
/// Looks up a localized string similar to Generate a new Command Palette extension project.
|
||||
/// </summary>
|
||||
public static string builtin_new_extension_subtitle {
|
||||
get {
|
||||
@@ -358,7 +358,7 @@ namespace Microsoft.CmdPal.UI.ViewModels.Properties {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Open Settings.
|
||||
/// Looks up a localized string similar to Open Command Palette settings.
|
||||
/// </summary>
|
||||
public static string builtin_open_settings_name {
|
||||
get {
|
||||
@@ -366,15 +366,6 @@ namespace Microsoft.CmdPal.UI.ViewModels.Properties {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Open Command Palette settings.
|
||||
/// </summary>
|
||||
public static string builtin_open_settings_subtitle {
|
||||
get {
|
||||
return ResourceManager.GetString("builtin_open_settings_subtitle", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Exit Command Palette.
|
||||
/// </summary>
|
||||
@@ -437,5 +428,14 @@ namespace Microsoft.CmdPal.UI.ViewModels.Properties {
|
||||
return ResourceManager.GetString("builtin_settings_extension_n_extensions_installed", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Fallbacks.
|
||||
/// </summary>
|
||||
public static string fallbacks {
|
||||
get {
|
||||
return ResourceManager.GetString("fallbacks", resourceCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,4 +242,7 @@
|
||||
<data name="builtin_settings_appearance_pick_background_image_title" xml:space="preserve">
|
||||
<value>Pick background image</value>
|
||||
</data>
|
||||
<data name="fallbacks" xml:space="preserve">
|
||||
<value>Fallbacks</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -8,9 +8,15 @@ namespace Microsoft.CmdPal.UI.ViewModels;
|
||||
|
||||
public class ProviderSettings
|
||||
{
|
||||
// List of built-in fallbacks that should not have global results enabled by default
|
||||
private readonly string[] _excludedBuiltInFallbacks = [
|
||||
"com.microsoft.cmdpal.builtin.indexer.fallback",
|
||||
"com.microsoft.cmdpal.builtin.calculator.fallback",
|
||||
];
|
||||
|
||||
public bool IsEnabled { get; set; } = true;
|
||||
|
||||
public Dictionary<string, bool> FallbackCommands { get; set; } = [];
|
||||
public Dictionary<string, FallbackSettings> FallbackCommands { get; set; } = new();
|
||||
|
||||
[JsonIgnore]
|
||||
public string ProviderDisplayName { get; set; } = string.Empty;
|
||||
@@ -39,19 +45,21 @@ public class ProviderSettings
|
||||
|
||||
ProviderDisplayName = wrapper.DisplayName;
|
||||
|
||||
if (wrapper.FallbackItems.Length > 0)
|
||||
{
|
||||
foreach (var fallback in wrapper.FallbackItems)
|
||||
{
|
||||
if (!FallbackCommands.ContainsKey(fallback.Id))
|
||||
{
|
||||
var enableGlobalResults = IsBuiltin && !_excludedBuiltInFallbacks.Contains(fallback.Id);
|
||||
FallbackCommands[fallback.Id] = new FallbackSettings(enableGlobalResults);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(ProviderId))
|
||||
{
|
||||
throw new InvalidDataException("Did you add a built-in command and forget to set the Id? Make sure you do that!");
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsFallbackEnabled(TopLevelViewModel command)
|
||||
{
|
||||
return FallbackCommands.TryGetValue(command.Id, out var enabled) ? enabled : true;
|
||||
}
|
||||
|
||||
public void SetFallbackEnabled(TopLevelViewModel command, bool enabled)
|
||||
{
|
||||
FallbackCommands[command.Id] = enabled;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,26 +9,39 @@ using Microsoft.CmdPal.Core.Common.Services;
|
||||
using Microsoft.CmdPal.Core.ViewModels;
|
||||
using Microsoft.CmdPal.UI.ViewModels.Messages;
|
||||
using Microsoft.CmdPal.UI.ViewModels.Properties;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Microsoft.CmdPal.UI.ViewModels;
|
||||
|
||||
public partial class ProviderSettingsViewModel(
|
||||
CommandProviderWrapper _provider,
|
||||
ProviderSettings _providerSettings,
|
||||
IServiceProvider _serviceProvider) : ObservableObject
|
||||
public partial class ProviderSettingsViewModel : ObservableObject
|
||||
{
|
||||
private readonly SettingsModel _settings = _serviceProvider.GetService<SettingsModel>()!;
|
||||
private readonly CommandProviderWrapper _provider;
|
||||
private readonly ProviderSettings _providerSettings;
|
||||
private readonly SettingsModel _settings;
|
||||
|
||||
private readonly Lock _initializeSettingsLock = new();
|
||||
private Task? _initializeSettingsTask;
|
||||
|
||||
public ProviderSettingsViewModel(
|
||||
CommandProviderWrapper provider,
|
||||
ProviderSettings providerSettings,
|
||||
SettingsModel settings)
|
||||
{
|
||||
_provider = provider;
|
||||
_providerSettings = providerSettings;
|
||||
_settings = settings;
|
||||
|
||||
LoadingSettings = _provider.Settings?.HasSettings ?? false;
|
||||
|
||||
BuildFallbackViewModels();
|
||||
}
|
||||
|
||||
public string DisplayName => _provider.DisplayName;
|
||||
|
||||
public string ExtensionName => _provider.Extension?.ExtensionDisplayName ?? "Built-in";
|
||||
|
||||
public string ExtensionSubtext => IsEnabled ?
|
||||
HasFallbackCommands ?
|
||||
$"{ExtensionName}, {TopLevelCommands.Count} commands, {FallbackCommands.Count} fallback commands" :
|
||||
$"{ExtensionName}, {TopLevelCommands.Count} commands, {_provider.FallbackItems?.Length} fallback commands" :
|
||||
$"{ExtensionName}, {TopLevelCommands.Count} commands" :
|
||||
Resources.builtin_disabled_extension;
|
||||
|
||||
@@ -42,7 +55,7 @@ public partial class ProviderSettingsViewModel(
|
||||
public IconInfoViewModel Icon => _provider.Icon;
|
||||
|
||||
[ObservableProperty]
|
||||
public partial bool LoadingSettings { get; set; } = _provider.Settings?.HasSettings ?? false;
|
||||
public partial bool LoadingSettings { get; set; }
|
||||
|
||||
public bool IsEnabled
|
||||
{
|
||||
@@ -145,28 +158,29 @@ public partial class ProviderSettingsViewModel(
|
||||
}
|
||||
|
||||
[field: AllowNull]
|
||||
public List<TopLevelViewModel> FallbackCommands
|
||||
{
|
||||
get
|
||||
{
|
||||
if (field is null)
|
||||
{
|
||||
field = BuildFallbackViewModels();
|
||||
}
|
||||
|
||||
return field;
|
||||
}
|
||||
}
|
||||
public List<FallbackSettingsViewModel> FallbackCommands { get; set; } = [];
|
||||
|
||||
public bool HasFallbackCommands => _provider.FallbackItems?.Length > 0;
|
||||
|
||||
private List<TopLevelViewModel> BuildFallbackViewModels()
|
||||
private void BuildFallbackViewModels()
|
||||
{
|
||||
var thisProvider = _provider;
|
||||
var providersCommands = thisProvider.FallbackItems;
|
||||
var providersFallbackCommands = thisProvider.FallbackItems;
|
||||
|
||||
// Remember! This comes in on the UI thread!
|
||||
return [.. providersCommands];
|
||||
List<FallbackSettingsViewModel> fallbackViewModels = new(providersFallbackCommands.Length);
|
||||
foreach (var fallbackItem in providersFallbackCommands)
|
||||
{
|
||||
if (_providerSettings.FallbackCommands.TryGetValue(fallbackItem.Id, out var fallbackSettings))
|
||||
{
|
||||
fallbackViewModels.Add(new FallbackSettingsViewModel(fallbackItem, fallbackSettings, _settings, this));
|
||||
}
|
||||
else
|
||||
{
|
||||
fallbackViewModels.Add(new FallbackSettingsViewModel(fallbackItem, new(), _settings, this));
|
||||
}
|
||||
}
|
||||
|
||||
FallbackCommands = fallbackViewModels;
|
||||
}
|
||||
|
||||
private void Save() => SettingsModel.SaveSettings(_settings);
|
||||
|
||||
@@ -50,6 +50,8 @@ public partial class SettingsModel : ObservableObject
|
||||
|
||||
public Dictionary<string, ProviderSettings> ProviderSettings { get; set; } = [];
|
||||
|
||||
public string[] FallbackRanks { get; set; } = [];
|
||||
|
||||
public Dictionary<string, CommandAlias> Aliases { get; set; } = [];
|
||||
|
||||
public List<TopLevelHotkey> CommandHotkeys { get; set; } = [];
|
||||
@@ -107,6 +109,25 @@ public partial class SettingsModel : ObservableObject
|
||||
return settings;
|
||||
}
|
||||
|
||||
public string[] GetGlobalFallbacks()
|
||||
{
|
||||
var globalFallbacks = new HashSet<string>();
|
||||
|
||||
foreach (var provider in ProviderSettings.Values)
|
||||
{
|
||||
foreach (var fallback in provider.FallbackCommands)
|
||||
{
|
||||
var fallbackSetting = fallback.Value;
|
||||
if (fallbackSetting.IsEnabled && fallbackSetting.IncludeInGlobalResults)
|
||||
{
|
||||
globalFallbacks.Add(fallback.Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return globalFallbacks.ToArray();
|
||||
}
|
||||
|
||||
public static SettingsModel LoadSettings()
|
||||
{
|
||||
if (string.IsNullOrEmpty(FilePath))
|
||||
|
||||
@@ -4,10 +4,9 @@
|
||||
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using Microsoft.CmdPal.Core.Common.Services;
|
||||
using Microsoft.CmdPal.UI.ViewModels.Services;
|
||||
using Microsoft.CmdPal.UI.ViewModels.Settings;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
|
||||
namespace Microsoft.CmdPal.UI.ViewModels;
|
||||
|
||||
@@ -27,7 +26,7 @@ public partial class SettingsViewModel : INotifyPropertyChanged
|
||||
];
|
||||
|
||||
private readonly SettingsModel _settings;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly TopLevelCommandManager _topLevelCommandManager;
|
||||
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
@@ -174,38 +173,76 @@ public partial class SettingsViewModel : INotifyPropertyChanged
|
||||
}
|
||||
}
|
||||
|
||||
public ObservableCollection<ProviderSettingsViewModel> CommandProviders { get; } = [];
|
||||
public ObservableCollection<ProviderSettingsViewModel> CommandProviders { get; } = new();
|
||||
|
||||
public ObservableCollection<FallbackSettingsViewModel> FallbackRankings { get; set; } = new();
|
||||
|
||||
public SettingsExtensionsViewModel Extensions { get; }
|
||||
|
||||
public SettingsViewModel(SettingsModel settings, IServiceProvider serviceProvider, TaskScheduler scheduler)
|
||||
public SettingsViewModel(SettingsModel settings, TopLevelCommandManager topLevelCommandManager, TaskScheduler scheduler, IThemeService themeService)
|
||||
{
|
||||
_settings = settings;
|
||||
_serviceProvider = serviceProvider;
|
||||
_topLevelCommandManager = topLevelCommandManager;
|
||||
|
||||
var themeService = serviceProvider.GetRequiredService<IThemeService>();
|
||||
Appearance = new AppearanceSettingsViewModel(themeService, _settings);
|
||||
|
||||
var activeProviders = GetCommandProviders();
|
||||
var allProviderSettings = _settings.ProviderSettings;
|
||||
|
||||
var fallbacks = new List<FallbackSettingsViewModel>();
|
||||
var currentRankings = _settings.FallbackRanks;
|
||||
var needsSave = false;
|
||||
|
||||
foreach (var item in activeProviders)
|
||||
{
|
||||
var providerSettings = settings.GetProviderSettings(item);
|
||||
|
||||
var settingsModel = new ProviderSettingsViewModel(item, providerSettings, _serviceProvider);
|
||||
var settingsModel = new ProviderSettingsViewModel(item, providerSettings, _settings);
|
||||
CommandProviders.Add(settingsModel);
|
||||
|
||||
fallbacks.AddRange(settingsModel.FallbackCommands);
|
||||
}
|
||||
|
||||
var fallbackRankings = new List<Scored<FallbackSettingsViewModel>>(fallbacks.Count);
|
||||
foreach (var fallback in fallbacks)
|
||||
{
|
||||
var index = currentRankings.IndexOf(fallback.Id);
|
||||
var score = fallbacks.Count;
|
||||
|
||||
if (index >= 0)
|
||||
{
|
||||
score = index;
|
||||
}
|
||||
|
||||
fallbackRankings.Add(new Scored<FallbackSettingsViewModel>() { Item = fallback, Score = score });
|
||||
|
||||
if (index == -1)
|
||||
{
|
||||
needsSave = true;
|
||||
}
|
||||
}
|
||||
|
||||
FallbackRankings = new ObservableCollection<FallbackSettingsViewModel>(fallbackRankings.OrderBy(o => o.Score).Select(fr => fr.Item));
|
||||
Extensions = new SettingsExtensionsViewModel(CommandProviders, scheduler);
|
||||
|
||||
if (needsSave)
|
||||
{
|
||||
ApplyFallbackSort();
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<CommandProviderWrapper> GetCommandProviders()
|
||||
{
|
||||
var manager = _serviceProvider.GetService<TopLevelCommandManager>()!;
|
||||
var allProviders = manager.CommandProviders;
|
||||
var allProviders = _topLevelCommandManager.CommandProviders;
|
||||
return allProviders;
|
||||
}
|
||||
|
||||
public void ApplyFallbackSort()
|
||||
{
|
||||
_settings.FallbackRanks = FallbackRankings.Select(s => s.Id).ToArray();
|
||||
Save();
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(FallbackRankings)));
|
||||
}
|
||||
|
||||
private void Save() => SettingsModel.SaveSettings(_settings);
|
||||
}
|
||||
|
||||
@@ -4,11 +4,9 @@
|
||||
|
||||
using System.Collections.ObjectModel;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using ManagedCommon;
|
||||
using Microsoft.CmdPal.Core.ViewModels;
|
||||
using Microsoft.CmdPal.Core.ViewModels.Messages;
|
||||
using Microsoft.CmdPal.UI.ViewModels.Messages;
|
||||
using Microsoft.CmdPal.UI.ViewModels.Settings;
|
||||
using Microsoft.CommandPalette.Extensions;
|
||||
using Microsoft.CommandPalette.Extensions.Toolkit;
|
||||
@@ -27,7 +25,9 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem, IEx
|
||||
|
||||
private readonly string _commandProviderId;
|
||||
|
||||
private string IdFromModel => _commandItemViewModel.Command.Id;
|
||||
private string IdFromModel => IsFallback && !string.IsNullOrWhiteSpace(_fallbackId) ? _fallbackId : _commandItemViewModel.Command.Id;
|
||||
|
||||
private string _fallbackId = string.Empty;
|
||||
|
||||
private string _generatedId = string.Empty;
|
||||
|
||||
@@ -41,7 +41,7 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem, IEx
|
||||
[ObservableProperty]
|
||||
public partial ObservableCollection<Tag> Tags { get; set; } = [];
|
||||
|
||||
public string Id => string.IsNullOrEmpty(IdFromModel) ? _generatedId : IdFromModel;
|
||||
public string Id => string.IsNullOrWhiteSpace(IdFromModel) ? _generatedId : IdFromModel;
|
||||
|
||||
public CommandPaletteHost ExtensionHost { get; private set; }
|
||||
|
||||
@@ -158,14 +158,20 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem, IEx
|
||||
|
||||
public bool IsEnabled
|
||||
{
|
||||
get => _providerSettings.IsFallbackEnabled(this);
|
||||
set
|
||||
get
|
||||
{
|
||||
if (value != IsEnabled)
|
||||
if (IsFallback)
|
||||
{
|
||||
_providerSettings.SetFallbackEnabled(this, value);
|
||||
Save();
|
||||
WeakReferenceMessenger.Default.Send<ReloadCommandsMessage>(new());
|
||||
if (_providerSettings.FallbackCommands.TryGetValue(_fallbackId, out var fallbackSettings))
|
||||
{
|
||||
return fallbackSettings.IsEnabled;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return _providerSettings.IsEnabled;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -177,7 +183,8 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem, IEx
|
||||
string commandProviderId,
|
||||
SettingsModel settings,
|
||||
ProviderSettings providerSettings,
|
||||
IServiceProvider serviceProvider)
|
||||
IServiceProvider serviceProvider,
|
||||
ICommandItem? commandItem)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
_settings = settings;
|
||||
@@ -187,6 +194,10 @@ public sealed partial class TopLevelViewModel : ObservableObject, IListItem, IEx
|
||||
|
||||
IsFallback = isFallback;
|
||||
ExtensionHost = extensionHost;
|
||||
if (isFallback && commandItem is FallbackCommandItem fallback)
|
||||
{
|
||||
_fallbackId = fallback.Id;
|
||||
}
|
||||
|
||||
item.PropertyChanged += Item_PropertyChanged;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user