Skip to content

Commit

Permalink
Remove straggling empty fallbacks (#376)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zadjii-msft authored Jan 28, 2025
1 parent 0fba556 commit cce6878
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
public partial class MainListPage : DynamicListPage,
IRecipient<ClearSearchMessage>
IRecipient<ClearSearchMessage>,
IRecipient<UpdateFallbackItemsMessage>
{
private readonly IServiceProvider _serviceProvider;

Expand All @@ -39,6 +40,7 @@ public MainListPage(IServiceProvider serviceProvider)
_tlcManager.TopLevelCommands.CollectionChanged += Commands_CollectionChanged;

WeakReferenceMessenger.Default.Register<ClearSearchMessage>(this);
WeakReferenceMessenger.Default.Register<UpdateFallbackItemsMessage>(this);

var settings = _serviceProvider.GetService<SettingsModel>()!;
settings.SettingsChanged += SettingsChangedHandler;
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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()
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<UpdateFallbackItemsMessage>();
}
}
}
catch (Exception)
{
fallback.FallbackHandler.UpdateQuery(newQuery);
}
});
}
catch (Exception)
{
}
}
}

0 comments on commit cce6878

Please sign in to comment.