/ src / settings-ui / Settings.UI.Library / MouseJumpSettings.cs
MouseJumpSettings.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  using MouseJump.Common.Helpers;
 13  using MouseJump.Common.Models.Settings;
 14  
 15  namespace Microsoft.PowerToys.Settings.UI.Library
 16  {
 17      public class MouseJumpSettings : BasePTModuleSettings, ISettingsConfig, IHotkeyConfig
 18      {
 19          public const string ModuleName = "MouseJump";
 20  
 21          private static readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions
 22          {
 23              WriteIndented = true,
 24          };
 25  
 26          [JsonPropertyName("properties")]
 27          public MouseJumpProperties Properties { get; set; }
 28  
 29          public MouseJumpSettings()
 30          {
 31              Name = ModuleName;
 32              Properties = new MouseJumpProperties();
 33              Version = "1.1";
 34          }
 35  
 36          public void Save(SettingsUtils settingsUtils)
 37          {
 38              // Save settings to file
 39              var options = _serializerOptions;
 40  
 41              ArgumentNullException.ThrowIfNull(settingsUtils);
 42  
 43              settingsUtils.SaveSettings(JsonSerializer.Serialize(this, options), ModuleName);
 44          }
 45  
 46          public string GetModuleName()
 47          {
 48              return Name;
 49          }
 50  
 51          public ModuleType GetModuleType() => ModuleType.MouseJump;
 52  
 53          public HotkeyAccessor[] GetAllHotkeyAccessors()
 54          {
 55              var hotkeyAccessors = new List<HotkeyAccessor>
 56              {
 57                  new HotkeyAccessor(
 58                      () => Properties.ActivationShortcut,
 59                      value => Properties.ActivationShortcut = value ?? Properties.DefaultActivationShortcut,
 60                      "MouseUtils_MouseJump_ActivationShortcut"),
 61              };
 62  
 63              return hotkeyAccessors.ToArray();
 64          }
 65  
 66          // This can be utilized in the future if the settings.json file is to be modified/deleted.
 67          public bool UpgradeSettingsConfiguration()
 68          {
 69              /*
 70                  v1.0 - initial version
 71  
 72                  * DefaultActivationShortcut
 73                  * activation_shortcut
 74                  * thumbnail_size
 75                  * name
 76                  * version
 77              */
 78              var upgraded = false;
 79  
 80              if (this.Version == "1.0")
 81              {
 82                  /*
 83                      v1.1 - added preview style settings
 84  
 85                       * preview_type
 86                       * background_color_1
 87                       * background_color_2
 88                       * border_thickness
 89                       * border_color
 90                       * border_3d_depth
 91                       * border_padding
 92                       * bezel_thickness
 93                       * bezel_color
 94                       * bezel_3d_depth
 95                       * screen_margin
 96                       * screen_color_1
 97                       * screen_color_2
 98                  */
 99                  this.Version = "1.1";
100  
101                  // note - there's an issue where ITwoWayPipeMessageIPCManagedMethods.Send overwrites
102                  // the settings file version as "1.0" regardless of the actual version. as a result,
103                  // the UpgradeSettingsConfiguration can get triggered even if the config has already
104                  // been upgraded, so we need to do an additional check to make sure values haven't
105                  // already been upgraded before we overwrite them with default values.
106                  if (string.IsNullOrEmpty(this.Properties.PreviewType))
107                  {
108                      // set default values for custom preview style
109                      var previewStyle = StyleHelper.BezelledPreviewStyle;
110                      this.Properties.PreviewType = PreviewType.Bezelled.ToString();
111                      this.Properties.BackgroundColor1 = ConfigHelper.SerializeToConfigColorString(
112                         ConfigHelper.ToUnnamedColor(previewStyle.CanvasStyle.BackgroundStyle.Color1));
113                      this.Properties.BackgroundColor2 = ConfigHelper.SerializeToConfigColorString(
114                          ConfigHelper.ToUnnamedColor(previewStyle.CanvasStyle.BackgroundStyle.Color2));
115                      this.Properties.BorderThickness = (int)previewStyle.CanvasStyle.BorderStyle.Top;
116                      this.Properties.BorderColor = ConfigHelper.SerializeToConfigColorString(
117                          ConfigHelper.ToUnnamedColor(previewStyle.CanvasStyle.BorderStyle.Color));
118                      this.Properties.Border3dDepth = (int)previewStyle.CanvasStyle.BorderStyle.Depth;
119                      this.Properties.BorderPadding = (int)previewStyle.CanvasStyle.PaddingStyle.Top;
120                      this.Properties.BezelThickness = (int)previewStyle.ScreenStyle.BorderStyle.Top;
121                      this.Properties.BezelColor = ConfigHelper.SerializeToConfigColorString(
122                          ConfigHelper.ToUnnamedColor(previewStyle.ScreenStyle.BorderStyle.Color));
123                      this.Properties.Bezel3dDepth = (int)previewStyle.ScreenStyle.BorderStyle.Depth;
124                      this.Properties.ScreenMargin = (int)previewStyle.ScreenStyle.MarginStyle.Top;
125                      this.Properties.ScreenColor1 = ConfigHelper.SerializeToConfigColorString(
126                          ConfigHelper.ToUnnamedColor(previewStyle.ScreenStyle.BackgroundStyle.Color1));
127                      this.Properties.ScreenColor2 = ConfigHelper.SerializeToConfigColorString(
128                          ConfigHelper.ToUnnamedColor(previewStyle.ScreenStyle.BackgroundStyle.Color2));
129                  }
130  
131                  // we still need to flag the settings as "upgraded" so that the new version gets written
132                  // back to the config file, even if we didn't actually change and setting values
133                  upgraded = true;
134              }
135  
136              return upgraded;
137          }
138      }
139  }