untangle the context menu from global state a bit.

This commit is contained in:
Mike Griese
2026-02-05 11:45:31 -06:00
parent 965520ffe6
commit ab2346866d
3 changed files with 20 additions and 21 deletions

View File

@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation
// 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.
@@ -13,15 +13,20 @@ using Windows.System;
namespace Microsoft.CmdPal.Core.ViewModels;
public partial class ContextMenuViewModel : ObservableObject,
IRecipient<UpdateCommandBarMessage>
public partial class ContextMenuViewModel : ObservableObject
{
public ICommandBarContext? SelectedItem
{
get => field;
set
{
if (field == value)
{
return;
}
field = value;
OnPropertyChanged(nameof(SelectedItem));
UpdateContextItems();
}
}
@@ -39,16 +44,6 @@ public partial class ContextMenuViewModel : ObservableObject,
private string _lastSearchText = string.Empty;
public ContextMenuViewModel()
{
WeakReferenceMessenger.Default.Register<UpdateCommandBarMessage>(this);
}
public void Receive(UpdateCommandBarMessage message)
{
SelectedItem = message.ViewModel;
}
public void UpdateContextItems()
{
if (SelectedItem is not null)

View File

@@ -40,6 +40,16 @@ public sealed partial class CommandBar : UserControl,
WeakReferenceMessenger.Default.Register<OpenContextMenuMessage>(this);
WeakReferenceMessenger.Default.Register<CloseContextMenuMessage>(this);
WeakReferenceMessenger.Default.Register<TryCommandKeybindingMessage>(this);
ViewModel.PropertyChanged += ViewModel_PropertyChanged;
}
private void ViewModel_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(CommandBarViewModel.SelectedItem))
{
ContextControl.ViewModel.SelectedItem = ViewModel.SelectedItem;
}
}
public void Receive(OpenContextMenuMessage message)

View File

@@ -18,7 +18,6 @@ namespace Microsoft.CmdPal.UI.Controls;
public sealed partial class ContextMenu : UserControl,
IRecipient<OpenContextMenuMessage>,
IRecipient<UpdateCommandBarMessage>,
IRecipient<TryCommandKeybindingMessage>
{
public ContextMenuViewModel ViewModel { get; } = new();
@@ -29,7 +28,6 @@ public sealed partial class ContextMenu : UserControl,
// RegisterAll isn't AOT compatible
WeakReferenceMessenger.Default.Register<OpenContextMenuMessage>(this);
WeakReferenceMessenger.Default.Register<UpdateCommandBarMessage>(this);
WeakReferenceMessenger.Default.Register<TryCommandKeybindingMessage>(this);
if (ViewModel is not null)
@@ -46,11 +44,6 @@ public sealed partial class ContextMenu : UserControl,
UpdateUiForStackChange();
}
public void Receive(UpdateCommandBarMessage message)
{
UpdateUiForStackChange();
}
public void Receive(TryCommandKeybindingMessage msg)
{
var result = ViewModel?.CheckKeybinding(msg.Ctrl, msg.Alt, msg.Shift, msg.Win, msg.Key);
@@ -138,7 +131,8 @@ public sealed partial class ContextMenu : UserControl,
{
var prop = e.PropertyName;
if (prop == nameof(ContextMenuViewModel.FilteredItems))
if (prop == nameof(ContextMenuViewModel.FilteredItems) ||
prop == nameof(ContextMenuViewModel.SelectedItem))
{
UpdateUiForStackChange();
}