/ src / settings-ui / Settings.UI / ViewModels / RegistryPreviewViewModel.cs
RegistryPreviewViewModel.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.Globalization;
  7  using System.Text.Json;
  8  
  9  using global::PowerToys.GPOWrapper;
 10  using Microsoft.PowerToys.Settings.UI.Library;
 11  using Microsoft.PowerToys.Settings.UI.Library.Helpers;
 12  using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
 13  using Microsoft.PowerToys.Settings.UI.Library.ViewModels.Commands;
 14  using Microsoft.PowerToys.Settings.UI.SerializationContext;
 15  
 16  namespace Microsoft.PowerToys.Settings.UI.ViewModels
 17  {
 18      public partial class RegistryPreviewViewModel : Observable
 19      {
 20          private GeneralSettings GeneralSettingsConfig { get; set; }
 21  
 22          public ButtonClickCommand LaunchEventHandler => new ButtonClickCommand(Launch);
 23  
 24          public RegistryPreviewViewModel(ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<RegistryPreviewSettings> registryPreviewSettingsRepository, Func<string, int> ipcMSGCallBackFunc)
 25          {
 26              // To obtain the general settings configurations of PowerToys Settings.
 27              ArgumentNullException.ThrowIfNull(settingsRepository);
 28  
 29              GeneralSettingsConfig = settingsRepository.SettingsConfig;
 30  
 31              _settings = registryPreviewSettingsRepository.SettingsConfig;
 32  
 33              InitializeEnabledValue();
 34  
 35              // set the callback functions value to handle outgoing IPC message.
 36              SendConfigMSG = ipcMSGCallBackFunc;
 37          }
 38  
 39          private void InitializeEnabledValue()
 40          {
 41              _enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredRegistryPreviewEnabledValue();
 42              if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
 43              {
 44                  // Get the enabled state from GPO.
 45                  _enabledStateIsGPOConfigured = true;
 46                  _isRegistryPreviewEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
 47              }
 48              else
 49              {
 50                  _isRegistryPreviewEnabled = GeneralSettingsConfig.Enabled.RegistryPreview;
 51              }
 52          }
 53  
 54          public bool IsRegistryPreviewEnabled
 55          {
 56              get => _isRegistryPreviewEnabled;
 57              set
 58              {
 59                  if (_enabledStateIsGPOConfigured)
 60                  {
 61                      // If it's GPO configured, shouldn't be able to change this state.
 62                      return;
 63                  }
 64  
 65                  if (_isRegistryPreviewEnabled != value)
 66                  {
 67                      _isRegistryPreviewEnabled = value;
 68                      OnPropertyChanged(nameof(IsRegistryPreviewEnabled));
 69  
 70                      GeneralSettingsConfig.Enabled.RegistryPreview = value;
 71                      OutGoingGeneralSettings outgoing = new OutGoingGeneralSettings(GeneralSettingsConfig);
 72                      SendConfigMSG(outgoing.ToString());
 73                  }
 74              }
 75          }
 76  
 77          public bool IsRegistryPreviewDefaultRegApp
 78          {
 79              get => _settings.Properties.DefaultRegApp;
 80              set
 81              {
 82                  if (_settings.Properties.DefaultRegApp != value)
 83                  {
 84                      _settings.Properties.DefaultRegApp = value;
 85                      OnPropertyChanged(nameof(IsRegistryPreviewDefaultRegApp));
 86  
 87                      NotifySettingsChanged();
 88                  }
 89              }
 90          }
 91  
 92          public bool IsEnabledGpoConfigured
 93          {
 94              get => _enabledStateIsGPOConfigured;
 95          }
 96  
 97          public void Launch()
 98          {
 99              var actionName = "Launch";
100  
101              SendConfigMSG("{\"action\":{\"RegistryPreview\":{\"action_name\":\"" + actionName + "\", \"value\":\"\"}}}");
102          }
103  
104          private Func<string, int> SendConfigMSG { get; }
105  
106          private GpoRuleConfigured _enabledGpoRuleConfiguration;
107          private bool _enabledStateIsGPOConfigured;
108          private bool _isRegistryPreviewEnabled;
109          private RegistryPreviewSettings _settings;
110  
111          public void RefreshEnabledState()
112          {
113              InitializeEnabledValue();
114              OnPropertyChanged(nameof(IsRegistryPreviewEnabled));
115          }
116  
117          private void NotifySettingsChanged()
118          {
119              // Using InvariantCulture as this is an IPC message
120              SendConfigMSG(
121                     string.Format(
122                         CultureInfo.InvariantCulture,
123                         "{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
124                         RegistryPreviewSettings.ModuleName,
125                         JsonSerializer.Serialize(_settings, SourceGenerationContextContext.Default.RegistryPreviewSettings)));
126          }
127      }
128  }