/ src / settings-ui / Settings.UI.Library / GeneralSettings.cs
GeneralSettings.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.Text.Json;
  7  using System.Text.Json.Serialization;
  8  
  9  using ManagedCommon;
 10  using Microsoft.PowerToys.Settings.UI.Library.Helpers;
 11  using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
 12  using Microsoft.PowerToys.Settings.UI.Library.Utilities;
 13  using Settings.UI.Library.Attributes;
 14  
 15  namespace Microsoft.PowerToys.Settings.UI.Library
 16  {
 17      public enum DashboardSortOrder
 18      {
 19          Alphabetical,
 20          ByStatus,
 21      }
 22  
 23      public class GeneralSettings : ISettingsConfig, IHotkeyConfig
 24      {
 25          // Gets or sets a value indicating whether run powertoys on start-up.
 26          [JsonPropertyName("startup")]
 27          public bool Startup { get; set; }
 28  
 29          // Gets or sets a value indicating whether the powertoys system tray icon should be hidden.
 30          [JsonPropertyName("show_tray_icon")]
 31          public bool ShowSysTrayIcon { get; set; }
 32  
 33          // Gets or sets a value indicating whether the powertoys system tray icon should show a theme adaptive icon
 34          [JsonPropertyName("show_theme_adaptive_tray_icon")]
 35          public bool ShowThemeAdaptiveTrayIcon { get; set; }
 36  
 37          // Gets or sets a value indicating whether the powertoy elevated.
 38          [CmdConfigureIgnoreAttribute]
 39          [JsonPropertyName("is_elevated")]
 40          public bool IsElevated { get; set; }
 41  
 42          // Gets or sets a value indicating whether powertoys should run elevated.
 43          [JsonPropertyName("run_elevated")]
 44          [CmdConfigureIgnoreAttribute]
 45          public bool RunElevated { get; set; }
 46  
 47          // Gets or sets a value indicating whether is admin.
 48          [JsonPropertyName("is_admin")]
 49          [CmdConfigureIgnoreAttribute]
 50          public bool IsAdmin { get; set; }
 51  
 52          // Gets or sets a value indicating whether is warnings of elevated apps enabled.
 53          [JsonPropertyName("enable_warnings_elevated_apps")]
 54          public bool EnableWarningsElevatedApps { get; set; }
 55  
 56          // Gets or sets a value indicating whether Quick Access is enabled.
 57          [JsonPropertyName("enable_quick_access")]
 58          public bool EnableQuickAccess { get; set; }
 59  
 60          // Gets or sets Quick Access shortcut.
 61          [JsonPropertyName("quick_access_shortcut")]
 62          public HotkeySettings QuickAccessShortcut { get; set; }
 63  
 64          // Gets or sets theme Name.
 65          [JsonPropertyName("theme")]
 66          public string Theme { get; set; }
 67  
 68          // Gets or sets system theme name.
 69          [JsonPropertyName("system_theme")]
 70          [CmdConfigureIgnore]
 71          public string SystemTheme { get; set; }
 72  
 73          // Gets or sets powertoys version number.
 74          [JsonPropertyName("powertoys_version")]
 75          [CmdConfigureIgnore]
 76          public string PowertoysVersion { get; set; }
 77  
 78          [JsonPropertyName("action_name")]
 79          [CmdConfigureIgnore]
 80          public string CustomActionName { get; set; }
 81  
 82          [JsonPropertyName("enabled")]
 83          [CmdConfigureIgnore]
 84          public EnabledModules Enabled { get; set; }
 85  
 86          [JsonPropertyName("show_new_updates_toast_notification")]
 87          public bool ShowNewUpdatesToastNotification { get; set; }
 88  
 89          [JsonPropertyName("download_updates_automatically")]
 90          public bool AutoDownloadUpdates { get; set; }
 91  
 92          [JsonPropertyName("show_whats_new_after_updates")]
 93          public bool ShowWhatsNewAfterUpdates { get; set; }
 94  
 95          [JsonPropertyName("enable_experimentation")]
 96          public bool EnableExperimentation { get; set; }
 97  
 98          [JsonPropertyName("dashboard_sort_order")]
 99          public DashboardSortOrder DashboardSortOrder { get; set; }
100  
101          [JsonPropertyName("ignored_conflict_properties")]
102          public ShortcutConflictProperties IgnoredConflictProperties { get; set; }
103  
104          public GeneralSettings()
105          {
106              Startup = false;
107              ShowSysTrayIcon = true;
108              IsAdmin = false;
109              EnableWarningsElevatedApps = true;
110              EnableQuickAccess = true;
111              QuickAccessShortcut = new HotkeySettings();
112              IsElevated = false;
113              ShowNewUpdatesToastNotification = true;
114              AutoDownloadUpdates = false;
115              EnableExperimentation = true;
116              DashboardSortOrder = DashboardSortOrder.Alphabetical;
117              Theme = "system";
118              SystemTheme = "light";
119              try
120              {
121                  PowertoysVersion = DefaultPowertoysVersion();
122              }
123              catch (Exception e)
124              {
125                  Logger.LogError("Exception encountered when getting PowerToys version", e);
126                  PowertoysVersion = "v0.0.0";
127              }
128  
129              Enabled = new EnabledModules();
130              CustomActionName = string.Empty;
131              IgnoredConflictProperties = new ShortcutConflictProperties();
132          }
133  
134          public HotkeyAccessor[] GetAllHotkeyAccessors()
135          {
136              return new HotkeyAccessor[]
137              {
138                  new HotkeyAccessor(
139                      () => QuickAccessShortcut,
140                      (hotkey) => { QuickAccessShortcut = hotkey; },
141                      "GeneralPage_QuickAccessShortcut"),
142              };
143          }
144  
145          public ModuleType GetModuleType()
146          {
147              return ModuleType.GeneralSettings;
148          }
149  
150          // converts the current to a json string.
151          public string ToJsonString()
152          {
153              return JsonSerializer.Serialize(this, SettingsSerializationContext.Default.GeneralSettings);
154          }
155  
156          private static string DefaultPowertoysVersion()
157          {
158              return global::PowerToys.Interop.CommonManaged.GetProductVersion();
159          }
160  
161          // This function is to implement the ISettingsConfig interface.
162          // This interface helps in getting the settings configurations.
163          public string GetModuleName()
164          {
165              // The SettingsUtils functions access general settings when the module name is an empty string.
166              return string.Empty;
167          }
168  
169          public bool UpgradeSettingsConfiguration()
170          {
171              try
172              {
173                  if (Helper.CompareVersions(PowertoysVersion, Helper.GetProductVersion()) != 0)
174                  {
175                      // Update settings
176                      PowertoysVersion = Helper.GetProductVersion();
177                      return true;
178                  }
179              }
180              catch (FormatException)
181              {
182                  // If there is an issue with the version number format, don't migrate settings.
183              }
184  
185              // Ensure IgnoredConflictProperties is initialized (for backward compatibility)
186              if (IgnoredConflictProperties == null)
187              {
188                  IgnoredConflictProperties = new ShortcutConflictProperties();
189                  return true; // Indicate that settings were upgraded
190              }
191  
192              return false;
193          }
194  
195          public void AddEnabledModuleChangeNotification(Action callBack)
196          {
197              Enabled.AddEnabledModuleChangeNotification(callBack);
198          }
199      }
200  }