MouseWithoutBordersSettings.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.Diagnostics; 8 using System.Text.Json; 9 using System.Text.Json.Serialization; 10 using ManagedCommon; 11 using Microsoft.PowerToys.Settings.UI.Library.Helpers; 12 using Microsoft.PowerToys.Settings.UI.Library.Interfaces; 13 14 namespace Microsoft.PowerToys.Settings.UI.Library 15 { 16 public class MouseWithoutBordersSettings : BasePTModuleSettings, ISettingsConfig, IHotkeyConfig 17 { 18 public const string ModuleName = "MouseWithoutBorders"; 19 20 private static readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions 21 { 22 WriteIndented = true, 23 MaxDepth = 0, 24 IncludeFields = true, 25 }; 26 27 [JsonPropertyName("properties")] 28 public MouseWithoutBordersProperties Properties { get; set; } 29 30 public MouseWithoutBordersSettings() 31 { 32 Name = ModuleName; 33 Properties = new MouseWithoutBordersProperties(); 34 Version = "1.1"; 35 } 36 37 public string GetModuleName() 38 { 39 return Name; 40 } 41 42 public ModuleType GetModuleType() => ModuleType.MouseWithoutBorders; 43 44 public HotkeyAccessor[] GetAllHotkeyAccessors() 45 { 46 var hotkeyAccessors = new List<HotkeyAccessor> 47 { 48 new HotkeyAccessor( 49 () => Properties.ToggleEasyMouseShortcut, 50 value => Properties.ToggleEasyMouseShortcut = value ?? MouseWithoutBordersProperties.DefaultHotKeyToggleEasyMouse, 51 "MouseWithoutBorders_ToggleEasyMouseShortcut"), 52 new HotkeyAccessor( 53 () => Properties.LockMachineShortcut, 54 value => Properties.LockMachineShortcut = value ?? MouseWithoutBordersProperties.DefaultHotKeyLockMachine, 55 "MouseWithoutBorders_LockMachinesShortcut"), 56 new HotkeyAccessor( 57 () => Properties.Switch2AllPCShortcut, 58 value => Properties.Switch2AllPCShortcut = value ?? MouseWithoutBordersProperties.DefaultHotKeySwitch2AllPC, 59 "MouseWithoutBorders_Switch2AllPcShortcut"), 60 new HotkeyAccessor( 61 () => Properties.ReconnectShortcut, 62 value => Properties.ReconnectShortcut = value ?? MouseWithoutBordersProperties.DefaultHotKeyReconnect, 63 "MouseWithoutBorders_ReconnectShortcut"), 64 }; 65 66 return hotkeyAccessors.ToArray(); 67 } 68 69 public HotkeySettings ConvertMouseWithoutBordersHotKeyToPowerToys(int value) 70 { 71 // VK_A <= value <= VK_Z 72 if (value >= 0x41 && value <= 0x5A) 73 { 74 return new HotkeySettings(false, true, true, false, value); 75 } 76 else 77 { 78 // Disabled state 79 return new HotkeySettings(false, false, false, false, 0); 80 } 81 } 82 83 // This can be utilized in the future if the settings.json file is to be modified/deleted. 84 public bool UpgradeSettingsConfiguration() 85 { 86 #pragma warning disable CS0618 // We use obsolete members to upgrade them 87 bool downgradedThenUpgraded = Version != "1.0" && (Properties.HotKeyToggleEasyMouse != null || 88 Properties.HotKeyLockMachine != null || 89 Properties.HotKeyReconnect != null || 90 Properties.HotKeySwitch2AllPC != null); 91 92 if (Version == "1.0" || downgradedThenUpgraded) 93 { 94 Version = "1.1"; 95 96 if (Properties.HotKeyToggleEasyMouse != null) 97 { 98 Properties.ToggleEasyMouseShortcut = ConvertMouseWithoutBordersHotKeyToPowerToys(Properties.HotKeyToggleEasyMouse.Value); 99 } 100 101 if (Properties.HotKeyLockMachine != null) 102 { 103 Properties.LockMachineShortcut = ConvertMouseWithoutBordersHotKeyToPowerToys(Properties.HotKeyLockMachine.Value); 104 } 105 106 if (Properties.HotKeyReconnect != null) 107 { 108 Properties.ReconnectShortcut = ConvertMouseWithoutBordersHotKeyToPowerToys(Properties.HotKeyReconnect.Value); 109 } 110 111 if (Properties.HotKeySwitch2AllPC != null) 112 { 113 Properties.Switch2AllPCShortcut = ConvertMouseWithoutBordersHotKeyToPowerToys(Properties.HotKeySwitch2AllPC.Value); 114 } 115 116 Properties.HotKeyToggleEasyMouse = null; 117 Properties.HotKeyLockMachine = null; 118 Properties.HotKeyReconnect = null; 119 Properties.HotKeySwitch2AllPC = null; 120 121 return true; 122 } 123 124 return false; 125 #pragma warning restore CS0618 126 } 127 128 public virtual void Save(SettingsUtils settingsUtils) 129 { 130 // Save settings to file 131 var options = _serializerOptions; 132 133 ArgumentNullException.ThrowIfNull(settingsUtils); 134 135 settingsUtils.SaveSettings(JsonSerializer.Serialize(this, options), ModuleName); 136 } 137 } 138 }