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) - { - } } }