/
githubmirror
/
PowerToys
Обзор
Документация
Войти
/
githubmirror
/
PowerToys
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/ProviderSettings.cs
129 строк
4 KB
Michael Jolley
[CmdPal] Consolidate search ranking changes (#49832)
12 авг 2026, 19:18
Не верифицирован
12 авг 2026, 19:18
888a142
Код
Авторство
О чём код?
// 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.Immutable; using System.Text.Json.Serialization; namespace Microsoft.CmdPal.UI.ViewModels; /// <summary> /// Per-provider relevance nudge applied to main/root page search results. The value is a /// within-tier bonus only: it can reorder items that already share a relevance tier but can /// never move an item across a tier boundary. The underlying integer values are the sign of /// the bonus (Lower subtracts, Higher adds), and <see cref="Normal"/> is 0 so a missing or /// legacy setting deserializes to the neutral default. /// </summary> public enum ProviderSearchWeight { /// <summary>De-prioritize this provider's results within their tier.</summary> Lower = -1, /// <summary>Default. No provider nudge.</summary> Normal = 0, /// <summary>Prioritize this provider's results within their tier.</summary> Higher = 1, } public record ProviderSettings { // List of built-in fallbacks that should not have global results enabled by default private static readonly string[] _excludedBuiltInFallbacks = [ "com.microsoft.cmdpal.builtin.indexer.fallback", "com.microsoft.cmdpal.builtin.calculator.fallback", "com.microsoft.cmdpal.builtin.remotedesktop.fallback", ]; public bool IsEnabled { get; init; } = true; /// <summary> /// Per-provider within-tier ranking nudge for main-page search. Defaults to /// <see cref="ProviderSearchWeight.Normal"/>; missing/legacy values deserialize to Normal. /// </summary> public ProviderSearchWeight SearchWeight { get; init; } = ProviderSearchWeight.Normal; private ImmutableDictionary<string, FallbackSettings>? _fallbackCommands = ImmutableDictionary<string, FallbackSettings>.Empty; public ImmutableDictionary<string, FallbackSettings> FallbackCommands { get => _fallbackCommands ?? ImmutableDictionary<string, FallbackSettings>.Empty; init => _fallbackCommands = value; } private ImmutableList<string>? _pinnedCommandIds = ImmutableList<string>.Empty; public ImmutableList<string> PinnedCommandIds { get => _pinnedCommandIds ?? ImmutableList<string>.Empty; init => _pinnedCommandIds = value; } [JsonIgnore] public string ProviderId { get; init; } = string.Empty; [JsonIgnore] public bool IsBuiltin { get; init; } [JsonIgnore] public string ProviderDisplayName { get; init; } = string.Empty; public ProviderSettings() { } public ProviderSettings(bool isEnabled) { IsEnabled = isEnabled; } /// <summary> /// Returns a new ProviderSettings connected to the given wrapper. /// Returns <see langword="this"/> when the connection produces no changes. /// Pure function — does not mutate this instance. /// </summary> public ProviderSettings WithConnection(CommandProviderWrapper wrapper) { if (string.IsNullOrWhiteSpace(wrapper.ProviderId)) { throw new ArgumentException("ProviderId must not be null, empty, or whitespace.", nameof(wrapper)); } var changed = false; var builder = FallbackCommands.ToBuilder(); if (wrapper.FallbackItems.Length > 0) { foreach (var fallback in wrapper.FallbackItems) { if (!string.IsNullOrEmpty(fallback.Id) && !builder.ContainsKey(fallback.Id)) { var enableGlobalResults = (wrapper.Extension is null) && !_excludedBuiltInFallbacks.Contains(fallback.Id); builder[fallback.Id] = new FallbackSettings(enableGlobalResults); changed = true; } } } var isBuiltin = wrapper.Extension is null; // If nothing changed, return the same instance to avoid unnecessary allocations and saves if (!changed && ProviderId == wrapper.ProviderId && IsBuiltin == isBuiltin && ProviderDisplayName == wrapper.DisplayName) { return this; } return this with { ProviderId = wrapper.ProviderId, IsBuiltin = isBuiltin, ProviderDisplayName = wrapper.DisplayName, FallbackCommands = changed ? builder.ToImmutable() : FallbackCommands, }; } }