ColorPickerSettings.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.Linq; 8 using System.Text.Json; 9 using System.Text.Json.Serialization; 10 using ManagedCommon; 11 using Microsoft.PowerToys.Settings.UI.Library.Enumerations; 12 using Microsoft.PowerToys.Settings.UI.Library.Helpers; 13 using Microsoft.PowerToys.Settings.UI.Library.Interfaces; 14 15 namespace Microsoft.PowerToys.Settings.UI.Library 16 { 17 public class ColorPickerSettings : BasePTModuleSettings, ISettingsConfig, IHotkeyConfig 18 { 19 public const string ModuleName = "ColorPicker"; 20 21 [JsonPropertyName("properties")] 22 public ColorPickerProperties Properties { get; set; } 23 24 public ColorPickerSettings() 25 { 26 Properties = new ColorPickerProperties(); 27 Version = "2.1"; 28 Name = ModuleName; 29 } 30 31 private static readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions 32 { 33 WriteIndented = true, 34 }; 35 36 public virtual 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 => Name; 48 49 // This can be utilized in the future if the settings.json file is to be modified/deleted. 50 public bool UpgradeSettingsConfiguration() 51 { 52 // Upgrading V1 to V2 doesn't set the version to 2.0, therefore V2 settings still report Version == 1.0 53 if (Version == "1.0") 54 { 55 if (!Enum.IsDefined(Properties.ActivationAction)) 56 { 57 Properties.ActivationAction = ColorPickerActivationAction.OpenColorPicker; 58 } 59 60 Version = "2.1"; 61 return true; 62 } 63 64 return false; 65 } 66 67 public ModuleType GetModuleType() => ModuleType.ColorPicker; 68 69 public HotkeyAccessor[] GetAllHotkeyAccessors() 70 { 71 var hotkeyAccessors = new List<HotkeyAccessor> 72 { 73 new HotkeyAccessor( 74 () => Properties.ActivationShortcut, 75 value => Properties.ActivationShortcut = value ?? Properties.DefaultActivationShortcut, 76 "Activation_Shortcut"), 77 }; 78 79 return hotkeyAccessors.ToArray(); 80 } 81 82 public static object UpgradeSettings(object oldSettingsObject) 83 { 84 ColorPickerSettingsVersion1 oldSettings = (ColorPickerSettingsVersion1)oldSettingsObject; 85 ColorPickerSettings newSettings = new ColorPickerSettings(); 86 newSettings.Properties.ActivationShortcut = oldSettings.Properties.ActivationShortcut; 87 newSettings.Properties.ChangeCursor = oldSettings.Properties.ChangeCursor; 88 newSettings.Properties.ActivationAction = oldSettings.Properties.ActivationAction; 89 newSettings.Properties.ColorHistoryLimit = oldSettings.Properties.ColorHistoryLimit; 90 newSettings.Properties.ShowColorName = oldSettings.Properties.ShowColorName; 91 newSettings.Properties.ActivationShortcut = oldSettings.Properties.ActivationShortcut; 92 newSettings.Properties.VisibleColorFormats = new Dictionary<string, KeyValuePair<bool, string>>(); 93 foreach (KeyValuePair<string, bool> oldValue in oldSettings.Properties.VisibleColorFormats) 94 { 95 newSettings.Properties.VisibleColorFormats.Add(oldValue.Key, new KeyValuePair<bool, string>(oldValue.Value, ColorFormatHelper.GetDefaultFormat(oldValue.Key))); 96 } 97 98 newSettings.Properties.CopiedColorRepresentation = newSettings.Properties.VisibleColorFormats.ElementAt((int)oldSettings.Properties.CopiedColorRepresentation).Key; 99 return newSettings; 100 } 101 } 102 }