mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 20:27:36 +02:00
[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.
This commit is contained in:
@@ -3,11 +3,12 @@
|
|||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.ComponentModel.Composition;
|
using System.ComponentModel.Composition;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.IO.Abstractions;
|
using System.IO.Abstractions;
|
||||||
using System.Linq;
|
using System.Text.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using ColorPicker.Common;
|
using ColorPicker.Common;
|
||||||
using ManagedCommon;
|
using ManagedCommon;
|
||||||
@@ -23,6 +24,7 @@ namespace ColorPicker.Settings
|
|||||||
{
|
{
|
||||||
private readonly ISettingsUtils _settingsUtils;
|
private readonly ISettingsUtils _settingsUtils;
|
||||||
private const string ColorPickerModuleName = "ColorPicker";
|
private const string ColorPickerModuleName = "ColorPicker";
|
||||||
|
private const string ColorPickerHistoryFilename = "colorHistory.json";
|
||||||
private const string DefaultActivationShortcut = "Ctrl + Break";
|
private const string DefaultActivationShortcut = "Ctrl + Break";
|
||||||
private const int MaxNumberOfRetry = 5;
|
private const int MaxNumberOfRetry = 5;
|
||||||
private const int SettingsReadOnChangeDelayInMs = 300;
|
private const int SettingsReadOnChangeDelayInMs = 300;
|
||||||
@@ -56,11 +58,7 @@ namespace ColorPicker.Settings
|
|||||||
{
|
{
|
||||||
if (!_loadingColorsHistory)
|
if (!_loadingColorsHistory)
|
||||||
{
|
{
|
||||||
var settings = _settingsUtils.GetSettingsOrDefault<ColorPickerSettings, ColorPickerSettingsVersion1>(ColorPickerModuleName, settingsUpgrader: ColorPickerSettings.UpgradeSettings);
|
_settingsUtils.SaveSettings(JsonSerializer.Serialize(ColorHistory, new JsonSerializerOptions { WriteIndented = true }), ColorPickerModuleName, ColorPickerHistoryFilename);
|
||||||
ColorHistory.CollectionChanged -= ColorHistory_CollectionChanged;
|
|
||||||
settings.Properties.ColorHistory = ColorHistory.ToList();
|
|
||||||
ColorHistory.CollectionChanged += ColorHistory_CollectionChanged;
|
|
||||||
settings.Save(_settingsUtils);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,14 +118,31 @@ namespace ColorPicker.Settings
|
|||||||
ColorHistoryLimit.Value = settings.Properties.ColorHistoryLimit;
|
ColorHistoryLimit.Value = settings.Properties.ColorHistoryLimit;
|
||||||
ShowColorName.Value = settings.Properties.ShowColorName;
|
ShowColorName.Value = settings.Properties.ShowColorName;
|
||||||
|
|
||||||
if (settings.Properties.ColorHistory == null)
|
List<string> savedColorHistory = new List<string>();
|
||||||
|
try
|
||||||
{
|
{
|
||||||
settings.Properties.ColorHistory = new System.Collections.Generic.List<string>();
|
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<List<string>>(jsonSettingsString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
Logger.LogInfo("ColorPicker colorHistory.json was missing or corrupt");
|
||||||
}
|
}
|
||||||
|
|
||||||
_loadingColorsHistory = true;
|
_loadingColorsHistory = true;
|
||||||
ColorHistory.Clear();
|
ColorHistory.Clear();
|
||||||
foreach (var item in settings.Properties.ColorHistory)
|
foreach (var item in savedColorHistory)
|
||||||
{
|
{
|
||||||
ColorHistory.Add(item);
|
ColorHistory.Add(item);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
|||||||
{
|
{
|
||||||
ActivationShortcut = new HotkeySettings(true, false, false, true, 0x43);
|
ActivationShortcut = new HotkeySettings(true, false, false, true, 0x43);
|
||||||
ChangeCursor = false;
|
ChangeCursor = false;
|
||||||
ColorHistory = new List<string>();
|
|
||||||
ColorHistoryLimit = 20;
|
ColorHistoryLimit = 20;
|
||||||
VisibleColorFormats = new Dictionary<string, KeyValuePair<bool, string>>();
|
VisibleColorFormats = new Dictionary<string, KeyValuePair<bool, string>>();
|
||||||
VisibleColorFormats.Add("HEX", new KeyValuePair<bool, string>(true, ColorFormatHelper.GetDefaultFormat("HEX")));
|
VisibleColorFormats.Add("HEX", new KeyValuePair<bool, string>(true, ColorFormatHelper.GetDefaultFormat("HEX")));
|
||||||
@@ -50,6 +49,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
|||||||
[JsonPropertyName("activationaction")]
|
[JsonPropertyName("activationaction")]
|
||||||
public ColorPickerActivationAction ActivationAction { get; set; }
|
public ColorPickerActivationAction ActivationAction { get; set; }
|
||||||
|
|
||||||
|
// Property ColorHistory is not used, the color history is saved separatedly in the colorHistory.json file
|
||||||
[JsonPropertyName("colorhistory")]
|
[JsonPropertyName("colorhistory")]
|
||||||
public List<string> ColorHistory { get; set; }
|
public List<string> ColorHistory { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,6 @@ namespace Microsoft.PowerToys.Settings.UI.Library
|
|||||||
newSettings.Properties.ActivationShortcut = oldSettings.Properties.ActivationShortcut;
|
newSettings.Properties.ActivationShortcut = oldSettings.Properties.ActivationShortcut;
|
||||||
newSettings.Properties.ChangeCursor = oldSettings.Properties.ChangeCursor;
|
newSettings.Properties.ChangeCursor = oldSettings.Properties.ChangeCursor;
|
||||||
newSettings.Properties.ActivationAction = oldSettings.Properties.ActivationAction;
|
newSettings.Properties.ActivationAction = oldSettings.Properties.ActivationAction;
|
||||||
newSettings.Properties.ColorHistory = new List<string>(oldSettings.Properties.ColorHistory);
|
|
||||||
newSettings.Properties.ColorHistoryLimit = oldSettings.Properties.ColorHistoryLimit;
|
newSettings.Properties.ColorHistoryLimit = oldSettings.Properties.ColorHistoryLimit;
|
||||||
newSettings.Properties.ShowColorName = oldSettings.Properties.ShowColorName;
|
newSettings.Properties.ShowColorName = oldSettings.Properties.ShowColorName;
|
||||||
newSettings.Properties.ActivationShortcut = oldSettings.Properties.ActivationShortcut;
|
newSettings.Properties.ActivationShortcut = oldSettings.Properties.ActivationShortcut;
|
||||||
|
|||||||
Reference in New Issue
Block a user