/ src / settings-ui / Settings.UI / ViewModels / WorkspacesViewModel.cs
WorkspacesViewModel.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.ViewModels.Commands;
 16  using Microsoft.PowerToys.Settings.UI.SerializationContext;
 17  
 18  namespace Microsoft.PowerToys.Settings.UI.ViewModels
 19  {
 20      public partial class WorkspacesViewModel : PageViewModelBase
 21      {
 22          protected override string ModuleName => WorkspacesSettings.ModuleName;
 23  
 24          private SettingsUtils SettingsUtils { get; set; }
 25  
 26          private GeneralSettings GeneralSettingsConfig { get; set; }
 27  
 28          private WorkspacesSettings Settings { get; set; }
 29  
 30          private Func<string, int> SendConfigMSG { get; }
 31  
 32          public ButtonClickCommand LaunchEditorEventHandler { get; set; }
 33  
 34          public WorkspacesViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<WorkspacesSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc)
 35          {
 36              ArgumentNullException.ThrowIfNull(settingsUtils);
 37  
 38              SettingsUtils = settingsUtils;
 39  
 40              // To obtain the general settings configurations of PowerToys Settings.
 41              ArgumentNullException.ThrowIfNull(settingsRepository);
 42  
 43              GeneralSettingsConfig = settingsRepository.SettingsConfig;
 44  
 45              InitializeEnabledValue();
 46  
 47              // To obtain the settings configurations of Workspaces.
 48              ArgumentNullException.ThrowIfNull(moduleSettingsRepository);
 49  
 50              Settings = moduleSettingsRepository.SettingsConfig;
 51  
 52              _hotkey = Settings.Properties.Hotkey.Value;
 53  
 54              // set the callback functions value to handle outgoing IPC message.
 55              SendConfigMSG = ipcMSGCallBackFunc;
 56  
 57              LaunchEditorEventHandler = new ButtonClickCommand(LaunchEditor);
 58          }
 59  
 60          private void LaunchEditor()
 61          {
 62              // send message to launch the editor;
 63              SendConfigMSG("{\"action\":{\"Workspaces\":{\"action_name\":\"LaunchEditor\", \"value\":\"\"}}}");
 64          }
 65  
 66          private void InitializeEnabledValue()
 67          {
 68              _enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredWorkspacesEnabledValue();
 69              if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled)
 70              {
 71                  // Get the enabled state from GPO.
 72                  _enabledStateIsGPOConfigured = true;
 73                  _isEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
 74              }
 75              else
 76              {
 77                  _isEnabled = GeneralSettingsConfig.Enabled.Workspaces;
 78              }
 79          }
 80  
 81          public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
 82          {
 83              var hotkeysDict = new Dictionary<string, HotkeySettings[]>
 84              {
 85                  [ModuleName] = [Hotkey],
 86              };
 87  
 88              return hotkeysDict;
 89          }
 90  
 91          public bool IsEnabled
 92          {
 93              get => _isEnabled;
 94  
 95              set
 96              {
 97                  if (_enabledStateIsGPOConfigured)
 98                  {
 99                      // If it's GPO configured, shouldn't be able to change this state.
100                      return;
101                  }
102  
103                  if (value != _isEnabled)
104                  {
105                      _isEnabled = value;
106  
107                      // Set the status in the general settings configuration
108                      GeneralSettingsConfig.Enabled.Workspaces = value;
109                      OutGoingGeneralSettings snd = new OutGoingGeneralSettings(GeneralSettingsConfig);
110  
111                      SendConfigMSG(snd.ToString());
112                      OnPropertyChanged(nameof(IsEnabled));
113                  }
114              }
115          }
116  
117          public bool IsEnabledGpoConfigured
118          {
119              get => _enabledStateIsGPOConfigured;
120          }
121  
122          public HotkeySettings Hotkey
123          {
124              get => _hotkey;
125  
126              set
127              {
128                  if (value != _hotkey)
129                  {
130                      if (value == null)
131                      {
132                          _hotkey = WorkspacesProperties.DefaultHotkeyValue;
133                      }
134                      else
135                      {
136                          _hotkey = value;
137                      }
138  
139                      Settings.Properties.Hotkey.Value = _hotkey;
140                      NotifyPropertyChanged();
141  
142                      // Using InvariantCulture as this is an IPC message
143                      SendConfigMSG(
144                          string.Format(
145                              CultureInfo.InvariantCulture,
146                              "{{ \"powertoys\": {{ \"{0}\": {1} }} }}",
147                              WorkspacesSettings.ModuleName,
148                              JsonSerializer.Serialize(Settings, SourceGenerationContextContext.Default.WorkspacesSettings)));
149                  }
150              }
151          }
152  
153          public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
154          {
155              OnPropertyChanged(propertyName);
156              SettingsUtils.SaveSettings(Settings.ToJsonString(), WorkspacesSettings.ModuleName);
157          }
158  
159          public void RefreshEnabledState()
160          {
161              InitializeEnabledValue();
162              OnPropertyChanged(nameof(IsEnabled));
163          }
164  
165          private GpoRuleConfigured _enabledGpoRuleConfiguration;
166          private bool _enabledStateIsGPOConfigured;
167          private bool _isEnabled;
168          private HotkeySettings _hotkey;
169      }
170  }