/ src / settings-ui / Settings.UI.Library / CmdPalProperties.cs
CmdPalProperties.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.IO;
 7  using System.Text.Json;
 8  
 9  namespace Microsoft.PowerToys.Settings.UI.Library
10  {
11      public class CmdPalProperties
12      {
13          // Default shortcut - Win + Alt + Space
14          public static readonly HotkeySettings DefaultHotkeyValue = new HotkeySettings(true, false, true, false, 32);
15  
16  #pragma warning disable SA1401 // Fields should be private
17  #pragma warning disable CA1051 // Do not declare visible instance fields
18          public HotkeySettings Hotkey;
19  #pragma warning restore CA1051 // Do not declare visible instance fields
20  #pragma warning restore SA1401 // Fields should be private
21  
22          private string _settingsFilePath;
23  
24          public CmdPalProperties()
25          {
26              var localAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
27  
28  #if DEBUG
29              _settingsFilePath = Path.Combine(localAppDataDir, "Packages", "Microsoft.CommandPalette.Dev_8wekyb3d8bbwe", "LocalState", "settings.json");
30  #else
31              _settingsFilePath = Path.Combine(localAppDataDir, "Packages", "Microsoft.CommandPalette_8wekyb3d8bbwe", "LocalState", "settings.json");
32  #endif
33  
34              InitializeHotkey();
35          }
36  
37          public void InitializeHotkey()
38          {
39              try
40              {
41                  string json = File.ReadAllText(_settingsFilePath); // Read JSON file
42                  using JsonDocument doc = JsonDocument.Parse(json);
43  
44                  if (doc.RootElement.TryGetProperty(nameof(Hotkey), out JsonElement hotkeyElement))
45                  {
46                      Hotkey = JsonSerializer.Deserialize(hotkeyElement.GetRawText(), SettingsSerializationContext.Default.HotkeySettings);
47                  }
48              }
49              catch (Exception)
50              {
51              }
52  
53              Hotkey ??= DefaultHotkeyValue;
54          }
55      }
56  }