[cmdpal] Add fallback item for system command. (#38865)

* init

* Remove unused cache

* merge main

---------

Co-authored-by: Yu Leng (from Dev Box) <yuleng@microsoft.com>
This commit is contained in:
Yu Leng
2025-04-21 14:07:43 +08:00
committed by GitHub
parent 311ab88ec3
commit f2a5505601
7 changed files with 88 additions and 53 deletions

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
using Microsoft.CmdPal.Ext.System.Helpers;
using Microsoft.CommandPalette.Extensions.Toolkit;
namespace Microsoft.CmdPal.Ext.Shell;
namespace Microsoft.CmdPal.Ext.System;
public sealed partial class EmptyRecycleBinCommand : InvokableCommand
{

View File

@@ -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.Ext.Shell;
using Microsoft.CmdPal.Ext.System.Helpers;
using Microsoft.CommandPalette.Extensions.Toolkit;

View File

@@ -0,0 +1,72 @@
// 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.Generic;
using Microsoft.CmdPal.Ext.System.Helpers;
using Microsoft.CommandPalette.Extensions;
using Microsoft.CommandPalette.Extensions.Toolkit;
namespace Microsoft.CmdPal.Ext.System;
internal sealed partial class FallbackSystemCommandItem : FallbackCommandItem
{
public FallbackSystemCommandItem(SettingsManager settings)
: base(new NoOpCommand(), Resources.Microsoft_plugin_ext_fallback_display_title)
{
Title = string.Empty;
Subtitle = string.Empty;
var isBootedInUefiMode = Win32Helpers.GetSystemFirmwareType() == FirmwareType.Uefi;
var hideEmptyRB = settings.HideEmptyRecycleBin;
var confirmSystemCommands = settings.ShowDialogToConfirmCommand;
var showSuccessOnEmptyRB = settings.ShowSuccessMessageAfterEmptyingRecycleBin;
systemCommands = Commands.GetSystemCommands(isBootedInUefiMode, hideEmptyRB, confirmSystemCommands, showSuccessOnEmptyRB);
}
private readonly List<IListItem> systemCommands;
public override void UpdateQuery(string query)
{
if (string.IsNullOrWhiteSpace(query))
{
Title = string.Empty;
Subtitle = string.Empty;
return;
}
IListItem? result = null;
var resultScore = 0;
// find the max score for the query
foreach (var command in systemCommands)
{
var title = command.Title;
var subTitle = command.Subtitle;
var titleScore = StringMatcher.FuzzySearch(query, title).Score;
var subTitleScore = StringMatcher.FuzzySearch(query, subTitle).Score;
var maxScore = Math.Max(titleScore, subTitleScore);
if (maxScore > resultScore)
{
resultScore = maxScore;
result = command;
}
}
if (result == null)
{
Title = string.Empty;
Subtitle = string.Empty;
return;
}
Title = result.Title;
Subtitle = result.Subtitle;
Icon = result.Icon;
Command = result.Command;
}
}

View File

@@ -186,6 +186,15 @@ namespace Microsoft.CmdPal.Ext.System {
}
}
/// <summary>
/// Looks up a localized string similar to Open System Command.
/// </summary>
public static string Microsoft_plugin_ext_fallback_display_title {
get {
return ResourceManager.GetString("Microsoft_plugin_ext_fallback_display_title", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Hide disconnected network info.
/// </summary>

View File

@@ -411,4 +411,7 @@
<data name="Microsoft_plugin_command_name_sleep" xml:space="preserve">
<value>Sleep</value>
</data>
<data name="Microsoft_plugin_ext_fallback_display_title" xml:space="preserve">
<value>Open System Command</value>
</data>
</root>

View File

@@ -14,6 +14,7 @@ public partial class SystemCommandExtensionProvider : CommandProvider
private readonly ICommandItem[] _commands;
private static readonly SettingsManager _settingsManager = new();
public static readonly SystemCommandPage Page = new(_settingsManager);
private readonly FallbackSystemCommandItem _fallbackFileItem = new(_settingsManager);
public SystemCommandExtensionProvider()
{
@@ -36,4 +37,6 @@ public partial class SystemCommandExtensionProvider : CommandProvider
{
return _commands;
}
public override IFallbackCommandItem[] FallbackCommands() => [_fallbackFileItem];
}

View File

@@ -1,51 +0,0 @@
// 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.Generic;
using System.Threading.Tasks;
using Microsoft.CmdPal.Ext.System.Helpers;
using Microsoft.CommandPalette.Extensions;
namespace Microsoft.CmdPal.Ext.System;
public sealed partial class SystemCommandsCache
{
public SystemCommandsCache(SettingsManager manager)
{
var list = new List<IListItem>();
var listLock = new object();
var a = Task.Run(() =>
{
var isBootedInUefiMode = Win32Helpers.GetSystemFirmwareType() == FirmwareType.Uefi;
var separateEmptyRB = manager.HideEmptyRecycleBin;
var confirmSystemCommands = manager.ShowDialogToConfirmCommand;
var showSuccessOnEmptyRB = manager.ShowSuccessMessageAfterEmptyingRecycleBin;
// normal system commands are fast and can be returned immediately
var systemCommands = Commands.GetSystemCommands(isBootedInUefiMode, separateEmptyRB, confirmSystemCommands, showSuccessOnEmptyRB);
lock (listLock)
{
list.AddRange(systemCommands);
}
});
var b = Task.Run(() =>
{
// Network (ip and mac) results are slow with many network cards and returned delayed.
// On global queries the first word/part has to be 'ip', 'mac' or 'address' for network results
var networkConnectionResults = Commands.GetNetworkConnectionResults(manager);
lock (listLock)
{
list.AddRange(networkConnectionResults);
}
});
Task.WaitAll(a, b);
CachedCommands = list.ToArray();
}
public IListItem[] CachedCommands { get; }
}