/ src / modules / cmdpal / Core / Microsoft.CmdPal.Core.ViewModels / IconInfoViewModel.cs
IconInfoViewModel.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 CommunityToolkit.Mvvm.ComponentModel;
 6  using Microsoft.CmdPal.Core.ViewModels.Models;
 7  using Microsoft.CommandPalette.Extensions;
 8  
 9  namespace Microsoft.CmdPal.Core.ViewModels;
10  
11  public partial class IconInfoViewModel : ObservableObject, IIconInfo
12  {
13      private readonly ExtensionObject<IIconInfo> _model = new(null);
14  
15      // These are properties that are "observable" from the extension object
16      // itself, in the sense that they get raised by PropChanged events from the
17      // extension. However, we don't want to actually make them
18      // [ObservableProperty]s, because PropChanged comes in off the UI thread,
19      // and ObservableProperty is not smart enough to raise the PropertyChanged
20      // on the UI thread.
21      public IconDataViewModel Light { get; private set; }
22  
23      public IconDataViewModel Dark { get; private set; }
24  
25      public IconDataViewModel IconForTheme(bool light) => Light = light ? Light : Dark;
26  
27      public bool HasIcon(bool light) => IconForTheme(light).HasIcon;
28  
29      public bool IsSet => _model.Unsafe is not null;
30  
31      IIconData? IIconInfo.Dark => Dark;
32  
33      IIconData? IIconInfo.Light => Light;
34  
35      public IconInfoViewModel(IIconInfo? icon)
36      {
37          _model = new(icon);
38          Light = new(null);
39          Dark = new(null);
40      }
41  
42      // Unsafe, needs to be called on BG thread
43      public void InitializeProperties()
44      {
45          var model = _model.Unsafe;
46          if (model is null)
47          {
48              return;
49          }
50  
51          Light = new(model.Light);
52          Light.InitializeProperties();
53  
54          Dark = new(model.Dark);
55          Dark.InitializeProperties();
56      }
57  }