ShortcutGuideViewModel.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.SerializationContext; 16 17 namespace Microsoft.PowerToys.Settings.UI.ViewModels 18 { 19 public partial class ShortcutGuideViewModel : PageViewModelBase 20 { 21 protected override string ModuleName => ShortcutGuideSettings.ModuleName; 22 23 private SettingsUtils SettingsUtils { get; set; } 24 25 private GeneralSettings GeneralSettingsConfig { get; set; } 26 27 private ShortcutGuideSettings Settings { get; set; } 28 29 private Func<string, int> SendConfigMSG { get; } 30 31 private string _settingsConfigFileFolder = string.Empty; 32 private string _disabledApps; 33 34 public ShortcutGuideViewModel(SettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, ISettingsRepository<ShortcutGuideSettings> moduleSettingsRepository, Func<string, int> ipcMSGCallBackFunc, string configFileSubfolder = "") 35 { 36 SettingsUtils = settingsUtils; 37 38 // Update Settings file folder: 39 _settingsConfigFileFolder = configFileSubfolder; 40 41 // To obtain the general PowerToys settings. 42 ArgumentNullException.ThrowIfNull(settingsRepository); 43 44 GeneralSettingsConfig = settingsRepository.SettingsConfig; 45 46 // To obtain the shortcut guide settings, if the file exists. 47 // If not, to create a file with the default settings and to return the default configurations. 48 ArgumentNullException.ThrowIfNull(moduleSettingsRepository); 49 50 Settings = moduleSettingsRepository.SettingsConfig; 51 52 // set the callback functions value to handle outgoing IPC message. 53 SendConfigMSG = ipcMSGCallBackFunc; 54 55 InitializeEnabledValue(); 56 57 _useLegacyPressWinKeyBehavior = Settings.Properties.UseLegacyPressWinKeyBehavior.Value; 58 _pressTimeForGlobalWindowsShortcuts = Settings.Properties.PressTimeForGlobalWindowsShortcuts.Value; 59 _pressTimeForTaskbarIconShortcuts = Settings.Properties.PressTimeForTaskbarIconShortcuts.Value; 60 _opacity = Settings.Properties.OverlayOpacity.Value; 61 _disabledApps = Settings.Properties.DisabledApps.Value; 62 63 switch (Settings.Properties.Theme.Value) 64 { 65 case "dark": _themeIndex = 0; break; 66 case "light": _themeIndex = 1; break; 67 case "system": _themeIndex = 2; break; 68 } 69 } 70 71 private void InitializeEnabledValue() 72 { 73 _enabledGpoRuleConfiguration = GPOWrapper.GetConfiguredShortcutGuideEnabledValue(); 74 if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled) 75 { 76 // Get the enabled state from GPO. 77 _enabledStateIsGPOConfigured = true; 78 _isEnabled = _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled; 79 } 80 else 81 { 82 _isEnabled = GeneralSettingsConfig.Enabled.ShortcutGuide; 83 } 84 } 85 86 public override Dictionary<string, HotkeySettings[]> GetAllHotkeySettings() 87 { 88 var hotkeysDict = new Dictionary<string, HotkeySettings[]> 89 { 90 [ModuleName] = [OpenShortcutGuide], 91 }; 92 93 return hotkeysDict; 94 } 95 96 private GpoRuleConfigured _enabledGpoRuleConfiguration; 97 private bool _enabledStateIsGPOConfigured; 98 private bool _isEnabled; 99 private int _themeIndex; 100 private bool _useLegacyPressWinKeyBehavior; 101 private int _pressTimeForGlobalWindowsShortcuts; 102 private int _pressTimeForTaskbarIconShortcuts; 103 private int _opacity; 104 105 public bool IsEnabled 106 { 107 get 108 { 109 return _isEnabled; 110 } 111 112 set 113 { 114 if (_enabledStateIsGPOConfigured) 115 { 116 // If it's GPO configured, shouldn't be able to change this state. 117 return; 118 } 119 120 if (value != _isEnabled) 121 { 122 _isEnabled = value; 123 124 // To update the status of shortcut guide in General PowerToy settings. 125 GeneralSettingsConfig.Enabled.ShortcutGuide = value; 126 OutGoingGeneralSettings snd = new OutGoingGeneralSettings(GeneralSettingsConfig); 127 128 SendConfigMSG(snd.ToString()); 129 OnPropertyChanged(nameof(IsEnabled)); 130 } 131 } 132 } 133 134 public bool IsEnabledGpoConfigured 135 { 136 get => _enabledStateIsGPOConfigured; 137 } 138 139 public HotkeySettings OpenShortcutGuide 140 { 141 get 142 { 143 return Settings.Properties.OpenShortcutGuide; 144 } 145 146 set 147 { 148 if (Settings.Properties.OpenShortcutGuide != value) 149 { 150 Settings.Properties.OpenShortcutGuide = value ?? Settings.Properties.DefaultOpenShortcutGuide; 151 NotifyPropertyChanged(); 152 } 153 } 154 } 155 156 public int ThemeIndex 157 { 158 get 159 { 160 return _themeIndex; 161 } 162 163 set 164 { 165 if (_themeIndex != value) 166 { 167 switch (value) 168 { 169 case 0: Settings.Properties.Theme.Value = "dark"; break; 170 case 1: Settings.Properties.Theme.Value = "light"; break; 171 case 2: Settings.Properties.Theme.Value = "system"; break; 172 } 173 174 _themeIndex = value; 175 NotifyPropertyChanged(); 176 } 177 } 178 } 179 180 public int OverlayOpacity 181 { 182 get 183 { 184 return _opacity; 185 } 186 187 set 188 { 189 if (_opacity != value) 190 { 191 _opacity = value; 192 Settings.Properties.OverlayOpacity.Value = value; 193 NotifyPropertyChanged(); 194 } 195 } 196 } 197 198 public bool UseLegacyPressWinKeyBehavior 199 { 200 get 201 { 202 return _useLegacyPressWinKeyBehavior; 203 } 204 205 set 206 { 207 if (_useLegacyPressWinKeyBehavior != value) 208 { 209 _useLegacyPressWinKeyBehavior = value; 210 Settings.Properties.UseLegacyPressWinKeyBehavior.Value = value; 211 NotifyPropertyChanged(); 212 } 213 } 214 } 215 216 public int PressTime 217 { 218 get 219 { 220 return _pressTimeForGlobalWindowsShortcuts; 221 } 222 223 set 224 { 225 if (_pressTimeForGlobalWindowsShortcuts != value) 226 { 227 _pressTimeForGlobalWindowsShortcuts = value; 228 Settings.Properties.PressTimeForGlobalWindowsShortcuts.Value = value; 229 NotifyPropertyChanged(); 230 } 231 } 232 } 233 234 public int DelayTime 235 { 236 get 237 { 238 return _pressTimeForTaskbarIconShortcuts; 239 } 240 241 set 242 { 243 if (_pressTimeForTaskbarIconShortcuts != value) 244 { 245 _pressTimeForTaskbarIconShortcuts = value; 246 Settings.Properties.PressTimeForTaskbarIconShortcuts.Value = value; 247 NotifyPropertyChanged(); 248 } 249 } 250 } 251 252 public string DisabledApps 253 { 254 get 255 { 256 return _disabledApps; 257 } 258 259 set 260 { 261 if (value != _disabledApps) 262 { 263 _disabledApps = value; 264 Settings.Properties.DisabledApps.Value = value; 265 NotifyPropertyChanged(); 266 } 267 } 268 } 269 270 public string GetSettingsSubPath() 271 { 272 return _settingsConfigFileFolder + "\\" + ModuleName; 273 } 274 275 public void NotifyPropertyChanged([CallerMemberName] string propertyName = null) 276 { 277 OnPropertyChanged(propertyName); 278 279 SndShortcutGuideSettings outsettings = new SndShortcutGuideSettings(Settings); 280 SndModuleSettings<SndShortcutGuideSettings> ipcMessage = new SndModuleSettings<SndShortcutGuideSettings>(outsettings); 281 SendConfigMSG(ipcMessage.ToJsonString()); 282 SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName); 283 } 284 285 public void RefreshEnabledState() 286 { 287 InitializeEnabledValue(); 288 OnPropertyChanged(nameof(IsEnabled)); 289 } 290 } 291 }