/ src / settings-ui / Settings.UI / ViewModels / CropAndLockViewModel.cs
CropAndLockViewModel.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;
  6  using System.Collections.Generic;
  7  using System.Globalization;
  8  using System.Runtime.CompilerServices;
  9  using System.Text.Json;
 10  using global::PowerToys.GPOWrapper;
 11  using Microsoft.PowerToys.Settings.UI.Helpers;
 12  using Microsoft.PowerToys.Settings.UI.Library;
 13  using Microsoft.PowerToys.Settings.UI.Library.Helpers;
 14  using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
 15  using Microsoft.PowerToys.Settings.UI.Library.Utilities;
 16  using Microsoft.PowerToys.Settings.UI.SerializationContext;
 17  
 18  namespace Microsoft.PowerToys.Settings.UI.ViewModels
 19  {
 20      public partial class CropAndLockViewModel : PageViewModelBase
 21      {
 22          protected override string ModuleName => CropAndLockSettings.ModuleName;
 23  
 24          private SettingsUtils SettingsUtils { get; set; }
 25  
 26          private GeneralSettings GeneralSettingsConfig { get; set; }
 27  
 28          private CropAndLockSettings Settings { get; set; }
 29  
 30          private Func<string, int> SendConfigMSG { get; }
 31  
 32          public CropAndLockViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<CropAndLockSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc)
 33          {
 34              ArgumentNullException.ThrowIfNull(settingsUtils);
 35  
 36              SettingsUtils = settingsUtils;
 37  
 38              // To obtain the general settings configurations of PowerToys Settings.
 39              ArgumentNullException.ThrowIfNull(settingsRepository);
 40  
 41              GeneralSettingsConfig = settingsRepository.SettingsConfig;
 42  
 43              InitializeEnabledValue();
 44  
 45              // To obtain the settings configurations of CropAndLock.
 46              ArgumentNullException.ThrowIfNull(moduleSettingsRepository);
 47  
 48              Settings = moduleSettingsRepository.SettingsConfig;
 49  
 50              _reparentHotkey = Settings.Properties.ReparentHotkey.Value;
 51              _thumbnailHotkey = Settings.Properties.ThumbnailHotkey.Value;
 52              _screenshotHotkey = Settings.Properties.ScreenshotHotkey.Value;
 53  
 54              // set the callback functions value to handle outgoing IPC message.
 55              SendConfigMSG = ipcMSGCallBackFunc;
 56          }
 57  
 58          private void InitializeEnabledValue()
 59          {
 60              _enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredCropAndLockEnabledValue();
 61              if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
 62              {
 63                  // Get the enabled state from GPO.
 64                  _enabledStateIsGPOConfigured = true;
 65                  _isEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
 66              }
 67              else
 68              {
 69                  _isEnabled = GeneralSettingsConfig.Enabled.CropAndLock;
 70              }
 71          }
 72  
 73          public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
 74          {
 75              var hotkeysDict = new Dictionary<string, HotkeySettings[]>
 76              {
 77                  [ModuleName] = [ReparentActivationShortcut, ThumbnailActivationShortcut, ScreenshotActivationShortcut],
 78              };
 79  
 80              return hotkeysDict;
 81          }
 82  
 83          public bool IsEnabled
 84          {
 85              get => _isEnabled;
 86  
 87              set
 88              {
 89                  if (_enabledStateIsGPOConfigured)
 90                  {
 91                      // If it's GPO configured, shouldn't be able to change this state.
 92                      return;
 93                  }
 94  
 95                  if (value != _isEnabled)
 96                  {
 97                      _isEnabled = value;
 98  
 99                      // Set the status in the general settings configuration
100                      GeneralSettingsConfig.Enabled.CropAndLock = value;
101                      OutGoingGeneralSettings snd = new OutGoingGeneralSettings(GeneralSettingsConfig);
102  
103                      SendConfigMSG(snd.ToString());
104                      OnPropertyChanged(nameof(IsEnabled));
105                  }
106              }
107          }
108  
109          public bool IsEnabledGpoConfigured
110          {
111              get => _enabledStateIsGPOConfigured;
112          }
113  
114          public HotkeySettings ReparentActivationShortcut
115          {
116              get => _reparentHotkey;
117  
118              set
119              {
120                  if (value != _reparentHotkey)
121                  {
122                      if (value == null)
123                      {
124                          _reparentHotkey = CropAndLockProperties.DefaultReparentHotkeyValue;
125                      }
126                      else
127                      {
128                          _reparentHotkey = value;
129                      }
130  
131                      Settings.Properties.ReparentHotkey.Value = _reparentHotkey;
132                      NotifyPropertyChanged();
133  
134                      // Using InvariantCulture as this is an IPC message
135                      SendConfigMSG(
136                          string.Format(
137                              CultureInfo.InvariantCulture,
138                              "{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
139                              CropAndLockSettings.ModuleName,
140                              JsonSerializer.Serialize(Settings, SourceGenerationContextContext.Default.CropAndLockSettings)));
141                  }
142              }
143          }
144  
145          public HotkeySettings ThumbnailActivationShortcut
146          {
147              get => _thumbnailHotkey;
148  
149              set
150              {
151                  if (value != _thumbnailHotkey)
152                  {
153                      if (value == null)
154                      {
155                          _thumbnailHotkey = CropAndLockProperties.DefaultThumbnailHotkeyValue;
156                      }
157                      else
158                      {
159                          _thumbnailHotkey = value;
160                      }
161  
162                      Settings.Properties.ThumbnailHotkey.Value = _thumbnailHotkey;
163                      NotifyPropertyChanged();
164  
165                      // Using InvariantCulture as this is an IPC message
166                      SendConfigMSG(
167                          string.Format(
168                              CultureInfo.InvariantCulture,
169                              "{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
170                              CropAndLockSettings.ModuleName,
171                              JsonSerializer.Serialize(Settings, SourceGenerationContextContext.Default.CropAndLockSettings)));
172                  }
173              }
174          }
175  
176          public HotkeySettings ScreenshotActivationShortcut
177          {
178              get => _screenshotHotkey;
179              set
180              {
181                  if (value != _screenshotHotkey)
182                  {
183                      if (value == null)
184                      {
185                          _screenshotHotkey = CropAndLockProperties.DefaultScreenshotHotkeyValue;
186                      }
187                      else
188                      {
189                          _screenshotHotkey = value;
190                      }
191  
192                      Settings.Properties.ScreenshotHotkey.Value = _screenshotHotkey;
193                      NotifyPropertyChanged();
194  
195                      // Using InvariantCulture as this is an IPC message
196                      SendConfigMSG(
197                          string.Format(
198                              CultureInfo.InvariantCulture,
199                              "{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
200                              CropAndLockSettings.ModuleName,
201                              JsonSerializer.Serialize(Settings, SourceGenerationContextContext.Default.CropAndLockSettings)));
202                  }
203              }
204          }
205  
206          public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
207          {
208              OnPropertyChanged(propertyName);
209              SettingsUtils.SaveSettings(Settings.ToJsonString(), CropAndLockSettings.ModuleName);
210          }
211  
212          public void RefreshEnabledState()
213          {
214              InitializeEnabledValue();
215              OnPropertyChanged(nameof(IsEnabled));
216          }
217  
218          private GpoRuleConfigured _enabledGpoRuleConfiguration;
219          private bool _enabledStateIsGPOConfigured;
220          private bool _isEnabled;
221          private HotkeySettings _reparentHotkey;
222          private HotkeySettings _thumbnailHotkey;
223          private HotkeySettings _screenshotHotkey;
224      }
225  }