CmdPal: Fixes for the build for the fixes 0.95 (#42279)

Closes #42241
Closes #42245 

Apps that were pinned would show in search results twice. Resolved.

Run & Calculator wouldn't show in search results. Resolved.
This commit is contained in:
Michael Jolley
2025-10-09 22:37:23 -05:00
committed by GitHub
parent 4aa27316fb
commit 075bbb46cb
3 changed files with 62 additions and 61 deletions

View File

@@ -10,6 +10,8 @@ using ManagedCommon;
using Microsoft.CmdPal.Core.Common.Helpers; using Microsoft.CmdPal.Core.Common.Helpers;
using Microsoft.CmdPal.Core.ViewModels.Messages; using Microsoft.CmdPal.Core.ViewModels.Messages;
using Microsoft.CmdPal.Ext.Apps; using Microsoft.CmdPal.Ext.Apps;
using Microsoft.CmdPal.Ext.Apps.Programs;
using Microsoft.CmdPal.Ext.Apps.State;
using Microsoft.CmdPal.UI.ViewModels.Messages; using Microsoft.CmdPal.UI.ViewModels.Messages;
using Microsoft.CmdPal.UI.ViewModels.Properties; using Microsoft.CmdPal.UI.ViewModels.Properties;
using Microsoft.CommandPalette.Extensions; using Microsoft.CommandPalette.Extensions;
@@ -36,6 +38,7 @@ public partial class MainListPage : DynamicListPage,
private List<Scored<IListItem>>? _filteredItems; private List<Scored<IListItem>>? _filteredItems;
private List<Scored<IListItem>>? _filteredApps; private List<Scored<IListItem>>? _filteredApps;
private List<Scored<IListItem>>? _fallbackItems; private List<Scored<IListItem>>? _fallbackItems;
private IEnumerable<Scored<IListItem>>? _scoredFallbackItems;
private bool _includeApps; private bool _includeApps;
private bool _filteredItemsIncludesApps; private bool _filteredItemsIncludesApps;
private int _appResultLimit = 10; private int _appResultLimit = 10;
@@ -171,6 +174,7 @@ public partial class MainListPage : DynamicListPage,
var items = Enumerable.Empty<Scored<IListItem>>() var items = Enumerable.Empty<Scored<IListItem>>()
.Concat(_filteredItems is not null ? _filteredItems : []) .Concat(_filteredItems is not null ? _filteredItems : [])
.Concat(_scoredFallbackItems is not null ? _scoredFallbackItems : [])
.Concat(limitedApps) .Concat(limitedApps)
.OrderByDescending(o => o.Score) .OrderByDescending(o => o.Score)
@@ -184,6 +188,14 @@ public partial class MainListPage : DynamicListPage,
} }
} }
private void ClearResults()
{
_filteredItems = null;
_filteredApps = null;
_fallbackItems = null;
_scoredFallbackItems = null;
}
public override void UpdateSearchText(string oldSearch, string newSearch) public override void UpdateSearchText(string oldSearch, string newSearch)
{ {
var timer = new Stopwatch(); var timer = new Stopwatch();
@@ -216,8 +228,7 @@ public partial class MainListPage : DynamicListPage,
lock (_tlcManager.TopLevelCommands) lock (_tlcManager.TopLevelCommands)
{ {
_filteredItemsIncludesApps = _includeApps; _filteredItemsIncludesApps = _includeApps;
_filteredItems = null; ClearResults();
_filteredApps = null;
} }
} }
@@ -244,9 +255,7 @@ public partial class MainListPage : DynamicListPage,
if (string.IsNullOrEmpty(newSearch)) if (string.IsNullOrEmpty(newSearch))
{ {
_filteredItemsIncludesApps = _includeApps; _filteredItemsIncludesApps = _includeApps;
_filteredItems = null; ClearResults();
_filteredApps = null;
_fallbackItems = null;
RaiseItemsChanged(commands.Count); RaiseItemsChanged(commands.Count);
return; return;
} }
@@ -255,17 +264,13 @@ public partial class MainListPage : DynamicListPage,
// re-use previous results. Reset _filteredItems, and keep er moving. // re-use previous results. Reset _filteredItems, and keep er moving.
if (!newSearch.StartsWith(oldSearch, StringComparison.CurrentCultureIgnoreCase)) if (!newSearch.StartsWith(oldSearch, StringComparison.CurrentCultureIgnoreCase))
{ {
_filteredItems = null; ClearResults();
_filteredApps = null;
_fallbackItems = null;
} }
// If the internal state has changed, reset _filteredItems to reset the list. // If the internal state has changed, reset _filteredItems to reset the list.
if (_filteredItemsIncludesApps != _includeApps) if (_filteredItemsIncludesApps != _includeApps)
{ {
_filteredItems = null; ClearResults();
_filteredApps = null;
_fallbackItems = null;
} }
if (token.IsCancellationRequested) if (token.IsCancellationRequested)
@@ -314,7 +319,7 @@ public partial class MainListPage : DynamicListPage,
// We're going to start over with our fallbacks // We're going to start over with our fallbacks
newFallbacks = Enumerable.Empty<IListItem>(); newFallbacks = Enumerable.Empty<IListItem>();
newFilteredItems = commands.Where(s => !s.IsFallback || _specialFallbacks.Contains(s.CommandProviderId)); newFilteredItems = commands.Where(s => !s.IsFallback);
// Fallbacks are always included in the list, even if they // Fallbacks are always included in the list, even if they
// don't match the search text. But we don't want to // don't match the search text. But we don't want to
@@ -330,7 +335,20 @@ public partial class MainListPage : DynamicListPage,
if (_includeApps) if (_includeApps)
{ {
newApps = AllAppsCommandProvider.Page.GetItems().ToList(); var allNewApps = AllAppsCommandProvider.Page.GetItems().ToList();
// We need to remove pinned apps from allNewApps so they don't show twice.
var pinnedApps = PinnedAppsManager.Instance.GetPinnedAppIdentifiers();
if (pinnedApps.Length > 0)
{
newApps = allNewApps.Where(w =>
pinnedApps.IndexOf(((AppListItem)w).AppIdentifier) < 0);
}
else
{
newApps = allNewApps;
}
} }
if (token.IsCancellationRequested) if (token.IsCancellationRequested)
@@ -350,6 +368,20 @@ public partial class MainListPage : DynamicListPage,
return; return;
} }
IEnumerable<IListItem> newFallbacksForScoring = commands.Where(s => s.IsFallback && _specialFallbacks.Contains(s.CommandProviderId));
if (token.IsCancellationRequested)
{
return;
}
_scoredFallbackItems = ListHelpers.FilterListWithScores<IListItem>(newFallbacksForScoring ?? [], SearchText, scoreItem);
if (token.IsCancellationRequested)
{
return;
}
// Defaulting scored to 1 but we'll eventually use user rankings // Defaulting scored to 1 but we'll eventually use user rankings
_fallbackItems = [.. newFallbacks.Select(f => new Scored<IListItem> { Item = f, Score = 1 })]; _fallbackItems = [.. newFallbacks.Select(f => new Scored<IListItem> { Item = f, Score = 1 })];

View File

@@ -13,7 +13,7 @@ using Microsoft.CommandPalette.Extensions.Toolkit;
namespace Microsoft.CmdPal.Ext.Apps.Programs; namespace Microsoft.CmdPal.Ext.Apps.Programs;
internal sealed partial class AppListItem : ListItem public sealed partial class AppListItem : ListItem
{ {
private static readonly Tag _appTag = new("App"); private static readonly Tag _appTag = new("App");

View File

@@ -16,7 +16,6 @@ internal sealed partial class FallbackExecuteItem : FallbackCommandItem, IDispos
private readonly Action<string>? _addToHistory; private readonly Action<string>? _addToHistory;
private readonly ITelemetryService _telemetryService; private readonly ITelemetryService _telemetryService;
private CancellationTokenSource? _cancellationTokenSource; private CancellationTokenSource? _cancellationTokenSource;
private Task? _currentUpdateTask;
public FallbackExecuteItem(SettingsManager settings, Action<string>? addToHistory, ITelemetryService telemetryService) public FallbackExecuteItem(SettingsManager settings, Action<string>? addToHistory, ITelemetryService telemetryService)
: base( : base(
@@ -40,44 +39,22 @@ internal sealed partial class FallbackExecuteItem : FallbackCommandItem, IDispos
try try
{ {
// Save the latest update task DoUpdateQuery(query, cancellationToken);
_currentUpdateTask = DoUpdateQueryAsync(query, cancellationToken);
}
catch (OperationCanceledException)
{
// DO NOTHING HERE
return;
} }
catch (Exception) catch (Exception)
{ {
// Handle other exceptions // Handle other exceptions
return; return;
} }
// Await the task to ensure only the latest one gets processed
_ = ProcessUpdateResultsAsync(_currentUpdateTask);
} }
private async Task ProcessUpdateResultsAsync(Task updateTask) private void DoUpdateQuery(string query, CancellationToken cancellationToken)
{
try
{
await updateTask;
}
catch (OperationCanceledException)
{
// Handle cancellation gracefully
}
catch (Exception)
{
// Handle other exceptions
}
}
private async Task DoUpdateQueryAsync(string query, CancellationToken cancellationToken)
{ {
// Check for cancellation at the start // Check for cancellation at the start
cancellationToken.ThrowIfCancellationRequested(); if (cancellationToken.IsCancellationRequested)
{
return;
}
var searchText = query.Trim(); var searchText = query.Trim();
Expand(ref searchText); Expand(ref searchText);
@@ -105,22 +82,8 @@ internal sealed partial class FallbackExecuteItem : FallbackCommandItem, IDispos
using var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token); using var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token);
var timeoutToken = combinedCts.Token; var timeoutToken = combinedCts.Token;
// Use Task.Run with timeout for file system operations exeExists = ShellListPageHelpers.FileExistInPath(exe, out fullExePath, cancellationToken);
var fileSystemTask = Task.Run( pathIsDir = Directory.Exists(exe);
() =>
{
exeExists = ShellListPageHelpers.FileExistInPath(exe, out fullExePath);
pathIsDir = Directory.Exists(exe);
},
CancellationToken.None);
// Wait for either completion or timeout
await fileSystemTask.WaitAsync(timeoutToken);
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
// Main cancellation token was cancelled, re-throw
throw;
} }
catch (TimeoutException) catch (TimeoutException)
{ {
@@ -139,7 +102,10 @@ internal sealed partial class FallbackExecuteItem : FallbackCommandItem, IDispos
} }
// Check for cancellation before updating UI properties // Check for cancellation before updating UI properties
cancellationToken.ThrowIfCancellationRequested(); if (cancellationToken.IsCancellationRequested)
{
return;
}
if (exeExists) if (exeExists)
{ {
@@ -172,7 +138,10 @@ internal sealed partial class FallbackExecuteItem : FallbackCommandItem, IDispos
} }
// Final cancellation check // Final cancellation check
cancellationToken.ThrowIfCancellationRequested(); if (cancellationToken.IsCancellationRequested)
{
return;
}
} }
public void Dispose() public void Dispose()