/ src / settings-ui / Settings.UI / ViewModels / CmdPalViewModel.cs
CmdPalViewModel.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.IO;
  8  using System.IO.Abstractions;
  9  using System.Linq;
 10  using System.Reflection;
 11  using System.Text.Json;
 12  using System.Text.RegularExpressions;
 13  using global::PowerToys.GPOWrapper;
 14  using ManagedCommon;
 15  using Microsoft.PowerToys.Settings.UI.Helpers;
 16  using Microsoft.PowerToys.Settings.UI.Library;
 17  using Microsoft.PowerToys.Settings.UI.Library.Helpers;
 18  using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
 19  using Microsoft.PowerToys.Settings.UI.Library.Utilities;
 20  using Microsoft.PowerToys.Settings.UI.ViewModels.Commands;
 21  using Microsoft.UI.Dispatching;
 22  using Windows.Management.Deployment;
 23  
 24  namespace Microsoft.PowerToys.Settings.UI.ViewModels
 25  {
 26      public class CmdPalViewModel : PageViewModelBase
 27      {
 28          protected override string ModuleName => "CmdPal";
 29  
 30          private GpoRuleConfigured _enabledGpoRuleConfiguration;
 31          private bool _isEnabled;
 32          private HotkeySettings _hotkey;
 33          private IFileSystemWatcher _watcher;
 34          private DispatcherQueue _uiDispatcherQueue;
 35          private CmdPalProperties _cmdPalProperties;
 36  
 37          private GeneralSettings GeneralSettingsConfig { get; set; }
 38  
 39          private Func<string, int> SendConfigMSG { get; }
 40  
 41          public CmdPalViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, Func<string, int> ipcMSGCallBackFunc, DispatcherQueue uiDispatcherQueue)
 42          {
 43              ArgumentNullException.ThrowIfNull(settingsUtils);
 44  
 45              // To obtain the general settings configurations of PowerToys Settings.
 46              ArgumentNullException.ThrowIfNull(settingsRepository);
 47  
 48              GeneralSettingsConfig = settingsRepository.SettingsConfig;
 49  
 50              _uiDispatcherQueue = uiDispatcherQueue;
 51              _cmdPalProperties = new CmdPalProperties();
 52  
 53              InitializeEnabledValue();
 54  
 55              var localAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
 56  
 57  #if DEBUG
 58              var settingsPath = Path.Combine(localAppDataDir, "Packages", "Microsoft.CommandPalette.Dev_8wekyb3d8bbwe", "LocalState", "settings.json");
 59  #else
 60              var settingsPath = Path.Combine(localAppDataDir, "Packages", "Microsoft.CommandPalette_8wekyb3d8bbwe", "LocalState", "settings.json");
 61  #endif
 62  
 63              _hotkey = _cmdPalProperties.Hotkey;
 64  
 65              _watcher = Helper.GetFileWatcher(settingsPath, () =>
 66              {
 67                  _cmdPalProperties.InitializeHotkey();
 68                  _hotkey = _cmdPalProperties.Hotkey;
 69  
 70                  _uiDispatcherQueue.TryEnqueue(() =>
 71                  {
 72                      OnPropertyChanged(nameof(Hotkey));
 73                  });
 74              });
 75  
 76              // set the callback functions value to handle outgoing IPC message.
 77              SendConfigMSG = ipcMSGCallBackFunc;
 78          }
 79  
 80          private void InitializeEnabledValue()
 81          {
 82              _enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredCmdPalEnabledValue();
 83              if (_enabledGpoRuleConfiguration is GpoRuleConfigured.Disabled or GpoRuleConfigured.Enabled)
 84              {
 85                  // Get the enabled state from GPO.
 86                  IsEnabledGpoConfigured = true;
 87                  _isEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled;
 88              }
 89              else
 90              {
 91                  _isEnabled = GeneralSettingsConfig.Enabled.CmdPal;
 92              }
 93          }
 94  
 95          public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings()
 96          {
 97              var hotkeysDict = new Dictionary<string, HotkeySettings[]>
 98              {
 99                  [ModuleName] = [Hotkey],
100              };
101  
102              return hotkeysDict;
103          }
104  
105          public bool IsEnabled
106          {
107              get => _isEnabled;
108  
109              set
110              {
111                  if (IsEnabledGpoConfigured)
112                  {
113                      // If it's GPO configured, shouldn't be able to change this state.
114                      return;
115                  }
116  
117                  if (value != _isEnabled)
118                  {
119                      _isEnabled = value;
120  
121                      // Set the status in the general settings configuration
122                      GeneralSettingsConfig.Enabled.CmdPal = value;
123                      OutGoingGeneralSettings snd = new(GeneralSettingsConfig);
124  
125                      SendConfigMSG(snd.ToString());
126                      OnPropertyChanged(nameof(IsEnabled));
127                  }
128              }
129          }
130  
131          public HotkeySettings Hotkey
132          {
133              get => _hotkey;
134  
135              private set
136              {
137              }
138          }
139  
140          public bool IsEnabledGpoConfigured { get; private set; }
141  
142          public void RefreshEnabledState()
143          {
144              InitializeEnabledValue();
145              OnPropertyChanged(nameof(IsEnabled));
146          }
147      }
148  }