/ src / settings-ui / Settings.UI.Library / PeekSettings.cs
PeekSettings.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.Text.Json;
 8  using System.Text.Json.Serialization;
 9  using ManagedCommon;
10  using Microsoft.PowerToys.Settings.UI.Library.Helpers;
11  using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
12  
13  namespace Microsoft.PowerToys.Settings.UI.Library
14  {
15      public class PeekSettings : BasePTModuleSettings, ISettingsConfig, IHotkeyConfig
16      {
17          public const string ModuleName = "Peek";
18          public const string InitialModuleVersion = "0.0.1";
19          public const string SpaceActivationIntroducedVersion = "0.0.2";
20          public const string CurrentModuleVersion = SpaceActivationIntroducedVersion;
21  
22          private static readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions
23          {
24              WriteIndented = true,
25          };
26  
27          [JsonPropertyName("properties")]
28          public PeekProperties Properties { get; set; }
29  
30          public PeekSettings()
31          {
32              Name = ModuleName;
33              Version = CurrentModuleVersion;
34              Properties = new PeekProperties();
35          }
36  
37          public string GetModuleName()
38          {
39              return Name;
40          }
41  
42          public ModuleType GetModuleType() => ModuleType.Peek;
43  
44          public HotkeyAccessor[] GetAllHotkeyAccessors()
45          {
46              var hotkeyAccessors = new List<HotkeyAccessor>
47              {
48                  new HotkeyAccessor(
49                      () => Properties.ActivationShortcut,
50                      value => Properties.ActivationShortcut = value ?? Properties.DefaultActivationShortcut,
51                      "Activation_Shortcut"),
52              };
53  
54              return hotkeyAccessors.ToArray();
55          }
56  
57          public bool UpgradeSettingsConfiguration()
58          {
59              if (string.IsNullOrEmpty(Version) ||
60                  Version.Equals(InitialModuleVersion, StringComparison.OrdinalIgnoreCase))
61              {
62                  Version = CurrentModuleVersion;
63                  Properties.EnableSpaceToActivate.Value = false;
64                  return true;
65              }
66  
67              return false;
68          }
69  
70          public virtual void Save(SettingsUtils settingsUtils)
71          {
72              // Save settings to file
73              var options = _serializerOptions;
74  
75              ArgumentNullException.ThrowIfNull(settingsUtils);
76  
77              settingsUtils.SaveSettings(JsonSerializer.Serialize(this, options), ModuleName);
78          }
79      }
80  }