From 06e451874293704db349a06318d27475eba636e1 Mon Sep 17 00:00:00 2001 From: Laszlo Nemeth <57342539+donlaci@users.noreply.github.com> Date: Tue, 13 Jun 2023 11:10:03 +0200 Subject: [PATCH] [ColorPicker]Store color history in a separated file (#23146) * [ColorPicker] Store color history in a separated file * ColorPicker] Separated file for color history: use the list from the settings.json if there is no ColorHistory.json file (preventing loss of existing color history) * Fix case when there is no history saved or no settings file at all. --- .../ColorPickerUI/Settings/UserSettings.cs | 33 ++++++++++++++----- .../ColorPickerProperties.cs | 2 +- .../ColorPickerSettings.cs | 1 - 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/modules/colorPicker/ColorPickerUI/Settings/UserSettings.cs b/src/modules/colorPicker/ColorPickerUI/Settings/UserSettings.cs index f7f815fd9f..4ab33f09a1 100644 --- a/src/modules/colorPicker/ColorPickerUI/Settings/UserSettings.cs +++ b/src/modules/colorPicker/ColorPickerUI/Settings/UserSettings.cs @@ -3,11 +3,12 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel.Composition; using System.IO; using System.IO.Abstractions; -using System.Linq; +using System.Text.Json; using System.Threading; using ColorPicker.Common; using ManagedCommon; @@ -23,6 +24,7 @@ namespace ColorPicker.Settings { private readonly ISettingsUtils _settingsUtils; private const string ColorPickerModuleName = "ColorPicker"; + private const string ColorPickerHistoryFilename = "colorHistory.json"; private const string DefaultActivationShortcut = "Ctrl + Break"; private const int MaxNumberOfRetry = 5; private const int SettingsReadOnChangeDelayInMs = 300; @@ -56,11 +58,7 @@ namespace ColorPicker.Settings { if (!_loadingColorsHistory) { - var settings = _settingsUtils.GetSettingsOrDefault(ColorPickerModuleName, settingsUpgrader: ColorPickerSettings.UpgradeSettings); - ColorHistory.CollectionChanged -= ColorHistory_CollectionChanged; - settings.Properties.ColorHistory = ColorHistory.ToList(); - ColorHistory.CollectionChanged += ColorHistory_CollectionChanged; - settings.Save(_settingsUtils); + _settingsUtils.SaveSettings(JsonSerializer.Serialize(ColorHistory, new JsonSerializerOptions { WriteIndented = true }), ColorPickerModuleName, ColorPickerHistoryFilename); } } @@ -120,14 +118,31 @@ namespace ColorPicker.Settings ColorHistoryLimit.Value = settings.Properties.ColorHistoryLimit; ShowColorName.Value = settings.Properties.ShowColorName; - if (settings.Properties.ColorHistory == null) + List savedColorHistory = new List(); + try { - settings.Properties.ColorHistory = new System.Collections.Generic.List(); + string filePath = _settingsUtils.GetSettingsFilePath(ColorPickerModuleName, ColorPickerHistoryFilename); + if (!File.Exists(filePath)) + { + if (settings.Properties.ColorHistory != null) + { + savedColorHistory = settings.Properties.ColorHistory; + } + } + else + { + string jsonSettingsString = System.IO.File.ReadAllText(filePath).Trim('\0'); + savedColorHistory = JsonSerializer.Deserialize>(jsonSettingsString); + } + } + catch (Exception) + { + Logger.LogInfo("ColorPicker colorHistory.json was missing or corrupt"); } _loadingColorsHistory = true; ColorHistory.Clear(); - foreach (var item in settings.Properties.ColorHistory) + foreach (var item in savedColorHistory) { ColorHistory.Add(item); } diff --git a/src/settings-ui/Settings.UI.Library/ColorPickerProperties.cs b/src/settings-ui/Settings.UI.Library/ColorPickerProperties.cs index 14606ebb12..1629148f2b 100644 --- a/src/settings-ui/Settings.UI.Library/ColorPickerProperties.cs +++ b/src/settings-ui/Settings.UI.Library/ColorPickerProperties.cs @@ -16,7 +16,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library { ActivationShortcut = new HotkeySettings(true, false, false, true, 0x43); ChangeCursor = false; - ColorHistory = new List(); ColorHistoryLimit = 20; VisibleColorFormats = new Dictionary>(); VisibleColorFormats.Add("HEX", new KeyValuePair(true, ColorFormatHelper.GetDefaultFormat("HEX"))); @@ -50,6 +49,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library [JsonPropertyName("activationaction")] public ColorPickerActivationAction ActivationAction { get; set; } + // Property ColorHistory is not used, the color history is saved separatedly in the colorHistory.json file [JsonPropertyName("colorhistory")] public List ColorHistory { get; set; } diff --git a/src/settings-ui/Settings.UI.Library/ColorPickerSettings.cs b/src/settings-ui/Settings.UI.Library/ColorPickerSettings.cs index 87574e894c..db43fe2bd7 100644 --- a/src/settings-ui/Settings.UI.Library/ColorPickerSettings.cs +++ b/src/settings-ui/Settings.UI.Library/ColorPickerSettings.cs @@ -56,7 +56,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library newSettings.Properties.ActivationShortcut = oldSettings.Properties.ActivationShortcut; newSettings.Properties.ChangeCursor = oldSettings.Properties.ChangeCursor; newSettings.Properties.ActivationAction = oldSettings.Properties.ActivationAction; - newSettings.Properties.ColorHistory = new List(oldSettings.Properties.ColorHistory); newSettings.Properties.ColorHistoryLimit = oldSettings.Properties.ColorHistoryLimit; newSettings.Properties.ShowColorName = oldSettings.Properties.ShowColorName; newSettings.Properties.ActivationShortcut = oldSettings.Properties.ActivationShortcut;