DetailsCommandsViewModel.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 Microsoft.CmdPal.Core.ViewModels.Models; 6 using Microsoft.CommandPalette.Extensions; 7 8 namespace Microsoft.CmdPal.Core.ViewModels; 9 10 public partial class DetailsCommandsViewModel( 11 IDetailsElement _detailsElement, 12 WeakReference<IPageContext> context) : DetailsElementViewModel(_detailsElement, context) 13 { 14 public List<CommandViewModel> Commands { get; private set; } = []; 15 16 public bool HasCommands => Commands.Count > 0; 17 18 private readonly ExtensionObject<IDetailsCommands> _dataModel = 19 new(_detailsElement.Data as IDetailsCommands); 20 21 public override void InitializeProperties() 22 { 23 base.InitializeProperties(); 24 var model = _dataModel.Unsafe; 25 if (model is null) 26 { 27 return; 28 } 29 30 Commands = model 31 .Commands? 32 .Select(c => 33 { 34 var vm = new CommandViewModel(c, PageContext); 35 vm.InitializeProperties(); 36 return vm; 37 }) 38 .ToList() ?? []; 39 UpdateProperty(nameof(HasCommands)); 40 UpdateProperty(nameof(Commands)); 41 } 42 }