CommandContextItemViewModel.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.Diagnostics.CodeAnalysis; 6 using Microsoft.CmdPal.Core.ViewModels.Models; 7 using Microsoft.CommandPalette.Extensions; 8 using Microsoft.CommandPalette.Extensions.Toolkit; 9 10 namespace Microsoft.CmdPal.Core.ViewModels; 11 12 [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] 13 public partial class CommandContextItemViewModel(ICommandContextItem contextItem, WeakReference<IPageContext> context) : CommandItemViewModel(new(contextItem), context), IContextItemViewModel 14 { 15 private readonly KeyChord nullKeyChord = new(0, 0, 0); 16 17 public new ExtensionObject<ICommandContextItem> Model { get; } = new(contextItem); 18 19 public bool IsCritical { get; private set; } 20 21 public KeyChord? RequestedShortcut { get; private set; } 22 23 public bool HasRequestedShortcut => RequestedShortcut is not null && (RequestedShortcut.Value != nullKeyChord); 24 25 public override void InitializeProperties() 26 { 27 if (IsInitialized) 28 { 29 return; 30 } 31 32 base.InitializeProperties(); 33 34 var contextItem = Model.Unsafe; 35 if (contextItem is null) 36 { 37 return; // throw? 38 } 39 40 IsCritical = contextItem.IsCritical; 41 42 RequestedShortcut = new( 43 contextItem.RequestedShortcut.Modifiers, 44 contextItem.RequestedShortcut.Vkey, 45 contextItem.RequestedShortcut.ScanCode); 46 } 47 }