/ src / modules / cmdpal / Microsoft.CmdPal.UI.ViewModels / MainWindowViewModel.cs
MainWindowViewModel.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.UI.ViewModels.Services;
 7  using Microsoft.UI.Dispatching;
 8  using Microsoft.UI.Xaml.Media;
 9  using Windows.UI;
10  
11  namespace Microsoft.CmdPal.UI.ViewModels;
12  
13  public partial class MainWindowViewModel : ObservableObject, IDisposable
14  {
15      private readonly IThemeService _themeService;
16      private readonly DispatcherQueue _uiDispatcherQueue = DispatcherQueue.GetForCurrentThread()!;
17  
18      [ObservableProperty]
19      public partial ImageSource? BackgroundImageSource { get; private set; }
20  
21      [ObservableProperty]
22      public partial Stretch BackgroundImageStretch { get; private set; } = Stretch.Fill;
23  
24      [ObservableProperty]
25      public partial double BackgroundImageOpacity { get; private set; }
26  
27      [ObservableProperty]
28      public partial Color BackgroundImageTint { get; private set; }
29  
30      [ObservableProperty]
31      public partial double BackgroundImageTintIntensity { get; private set; }
32  
33      [ObservableProperty]
34      public partial int BackgroundImageBlurAmount { get; private set; }
35  
36      [ObservableProperty]
37      public partial double BackgroundImageBrightness { get; private set; }
38  
39      [ObservableProperty]
40      public partial bool ShowBackgroundImage { get; private set; }
41  
42      public MainWindowViewModel(IThemeService themeService)
43      {
44          _themeService = themeService;
45          _themeService.ThemeChanged += ThemeService_ThemeChanged;
46      }
47  
48      private void ThemeService_ThemeChanged(object? sender, ThemeChangedEventArgs e)
49      {
50          _uiDispatcherQueue.TryEnqueue(() =>
51          {
52              BackgroundImageSource = _themeService.Current.BackgroundImageSource;
53              BackgroundImageStretch = _themeService.Current.BackgroundImageStretch;
54              BackgroundImageOpacity = _themeService.Current.BackgroundImageOpacity;
55  
56              BackgroundImageBrightness = _themeService.Current.BackgroundBrightness;
57              BackgroundImageTint = _themeService.Current.Tint;
58              BackgroundImageTintIntensity = _themeService.Current.TintIntensity;
59              BackgroundImageBlurAmount = _themeService.Current.BlurAmount;
60  
61              ShowBackgroundImage = BackgroundImageSource != null;
62          });
63      }
64  
65      public void Dispose()
66      {
67          _themeService.ThemeChanged -= ThemeService_ThemeChanged;
68          GC.SuppressFinalize(this);
69      }
70  }