From cce68789df518810d7e58dd1ff7187f442f3ca21 Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Tue, 28 Jan 2025 14:21:49 -0600 Subject: [PATCH] Remove straggling empty fallbacks (#376) Updating the search text for a fallback command is fully async. However, we just fire off all those updates, then immediately update the list of filtered items. That of course is dumb, because most of them haven't yet processed the update before we determine if we show it or not. So, we've added an `UpdateFallbackItemsMessage` that we emit from a fallback command to let the main page know it may need to remove an item from its list. --- .../Commands/MainListPage.cs | 21 ++++++-------- .../Messages/UpdateFallbackItemsMessage.cs | 9 ++++++ .../TopLevelCommandItemWrapper.cs | 28 ++++++++++++------- 3 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Messages/UpdateFallbackItemsMessage.cs diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPage.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPage.cs index 98ee87fbf193..36a4aaf44edc 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPage.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Commands/MainListPage.cs @@ -18,7 +18,8 @@ namespace Microsoft.CmdPal.UI.ViewModels.MainPage; /// TODO: Need to think about how we structure/interop for the page -> section -> item between the main setup, the extensions, and our viewmodels. /// public partial class MainListPage : DynamicListPage, - IRecipient + IRecipient, + IRecipient { private readonly IServiceProvider _serviceProvider; @@ -39,6 +40,7 @@ public MainListPage(IServiceProvider serviceProvider) _tlcManager.TopLevelCommands.CollectionChanged += Commands_CollectionChanged; WeakReferenceMessenger.Default.Register(this); + WeakReferenceMessenger.Default.Register(this); var settings = _serviceProvider.GetService()!; settings.SettingsChanged += SettingsChangedHandler; @@ -195,18 +197,11 @@ private static int ScoreTopLevelItem(string query, IListItem topLevelOrAppItem) return finalScore; // but downweight them } - public void Receive(ClearSearchMessage message) - { - SearchText = string.Empty; - } + public void Receive(ClearSearchMessage message) => SearchText = string.Empty; - private void SettingsChangedHandler(SettingsModel sender, object? args) - { - HotReloadSettings(sender); - } + public void Receive(UpdateFallbackItemsMessage message) => RaiseItemsChanged(_tlcManager.TopLevelCommands.Count); - private void HotReloadSettings(SettingsModel settings) - { - ShowDetails = settings.ShowAppDetails; - } + private void SettingsChangedHandler(SettingsModel sender, object? args) => HotReloadSettings(sender); + + private void HotReloadSettings(SettingsModel settings) => ShowDetails = settings.ShowAppDetails; } diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Messages/UpdateFallbackItemsMessage.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Messages/UpdateFallbackItemsMessage.cs new file mode 100644 index 000000000000..c7503ce1fb81 --- /dev/null +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/Messages/UpdateFallbackItemsMessage.cs @@ -0,0 +1,9 @@ +// 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. + +namespace Microsoft.CmdPal.UI.ViewModels.Messages; + +public record UpdateFallbackItemsMessage() +{ +} diff --git a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelCommandItemWrapper.cs b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelCommandItemWrapper.cs index b55366b5244d..d9f131e5c55d 100644 --- a/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelCommandItemWrapper.cs +++ b/src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelCommandItemWrapper.cs @@ -2,8 +2,10 @@ // 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.Messaging; using Microsoft.CmdPal.Extensions; using Microsoft.CmdPal.Extensions.Helpers; +using Microsoft.CmdPal.UI.ViewModels.Messages; using Microsoft.CmdPal.UI.ViewModels.Models; using Microsoft.Extensions.DependencyInjection; @@ -124,19 +126,25 @@ public void TryUpdateFallbackText(string newQuery) return; } - try - { - _ = Task.Run(() => + _ = Task.Run(() => { - var model = Model.Unsafe; - if (model is IFallbackCommandItem fallback) + try + { + var model = Model.Unsafe; + if (model is IFallbackCommandItem fallback) + { + var wasEmpty = string.IsNullOrEmpty(Title); + fallback.FallbackHandler.UpdateQuery(newQuery); + var isEmpty = string.IsNullOrEmpty(Title); + if (wasEmpty != isEmpty) + { + WeakReferenceMessenger.Default.Send(); + } + } + } + catch (Exception) { - fallback.FallbackHandler.UpdateQuery(newQuery); } }); - } - catch (Exception) - { - } } }