A Swiss Army knife for developers.
C# blazorcross-platformcsharpdesktopdesktop-appdesktop-applicationdeveloper-toolsextensiblefluentfluent-designlinuxmacosmicasyntax-highlightingtypescriptwindowswindows-10windows-11winui
Persist settings in each tools (#23)
Etienne Baudoux7020ca031c07
diff --git a/src/dev/impl/DevToys/Api/Core/Settings/SettingDefinition.cs b/src/dev/impl/DevToys/Api/Core/Settings/SettingDefinition.csindex 6222f5351..ea7192f92 100644--- a/src/dev/impl/DevToys/Api/Core/Settings/SettingDefinition.cs+++ b/src/dev/impl/DevToys/Api/Core/Settings/SettingDefinition.cs@@ -33,6 +33,13 @@ namespace DevToys.Api.Core.Settings/// <param name="defaultValue">The default value of the setting.</param>public SettingDefinition(string name, bool isRoaming, T defaultValue){+ if (string.IsNullOrEmpty(name) || name.Length > 255)+ {+ // For both LocalSettings and RoamingSettings, the name of each setting can be 255 characters in length at most.+ // see https://docs.microsoft.com/en-us/uwp/api/windows.storage.applicationdata.localsettings?view=winrt-22000#remarks+ throw new ArgumentOutOfRangeException(nameof(name));+ }+IsRoaming = isRoaming;Name = name;DefaultValue = defaultValue;diff --git a/src/dev/impl/DevToys/ViewModels/Tools/Base64EncoderDecoder/Base64EncoderDecoderToolViewModel.cs b/src/dev/impl/DevToys/ViewModels/Tools/Base64EncoderDecoder/Base64EncoderDecoderToolViewModel.csindex 92d738d5f..bb796905f 100644--- a/src/dev/impl/DevToys/ViewModels/Tools/Base64EncoderDecoder/Base64EncoderDecoderToolViewModel.cs+++ b/src/dev/impl/DevToys/ViewModels/Tools/Base64EncoderDecoder/Base64EncoderDecoderToolViewModel.cs@@ -1,5 +1,6 @@#nullable enable+using DevToys.Api.Core.Settings;using DevToys.Api.Tools;using DevToys.Core;using DevToys.Core.Threading;@@ -16,17 +17,35 @@ namespace DevToys.ViewModels.Tools.Base64EncoderDecoder[Export(typeof(Base64EncoderDecoderToolViewModel))]public class Base64EncoderDecoderToolViewModel : ObservableRecipient, IToolViewModel{+ /// <summary>+ /// Whether the tool should encode or decode Base64.+ /// </summary>+ private static readonly SettingDefinition<string> Conversion+ = new(+ name: $"{nameof(Base64EncoderDecoderToolViewModel)}.{nameof(Conversion)}",+ isRoaming: true,+ defaultValue: DefaultConversion);++ /// <summary>+ /// Whether the tool should encode/decode in Unicode or ASCII.+ /// </summary>+ private static readonly SettingDefinition<string> Encoder+ = new(+ name: $"{nameof(Base64EncoderDecoderToolViewModel)}.{nameof(Encoder)}",+ isRoaming: true,+ defaultValue: DefaultEncoding);+private const string DefaultEncoding = "UTF-8";private const string DefaultConversion = "Encode";internal const string DecodeConversion = "Decode";+ private readonly ISettingsProvider _settingsProvider;+ private readonly Queue<string> _conversionQueue = new();+private string? _inputValue;private string? _outputValue;- private string _encodingMode = DefaultEncoding;- private string _conversionMode = DefaultConversion;private bool _conversionInProgress;private bool _setPropertyInProgress;- private readonly Queue<string> _conversionQueue = new();public Type View { get; } = typeof(Base64EncoderDecoderToolPage);@@ -60,14 +79,18 @@ namespace DevToys.ViewModels.Tools.Base64EncoderDecoder/// </summary>internal string ConversionMode{- get => _conversionMode;+ get => _settingsProvider.GetSetting(Conversion);set{if (!_setPropertyInProgress){_setPropertyInProgress = true;ThreadHelper.ThrowIfNotOnUIThread();- SetProperty(ref _conversionMode, value);+ if (!string.Equals(_settingsProvider.GetSetting(Conversion), value, StringComparison.Ordinal))+ {+ _settingsProvider.SetSetting(Conversion, value);+ OnPropertyChanged();+ }InputValue = OutputValue;_setPropertyInProgress = false;}@@ -79,15 +102,25 @@ namespace DevToys.ViewModels.Tools.Base64EncoderDecoder/// </summary>internal string EncodingMode{- get => _encodingMode;+ get => _settingsProvider.GetSetting(Encoder);set{ThreadHelper.ThrowIfNotOnUIThread();- SetProperty(ref _encodingMode, value);- QueueConversionCalculation();+ if (!string.Equals(_settingsProvider.GetSetting(Encoder), value, StringComparison.Ordinal))+ {+ _settingsProvider.SetSetting(Encoder, value);+ OnPropertyChanged();+ QueueConversionCalculation();+ }}}+ [ImportingConstructor]+ public Base64EncoderDecoderToolViewModel(ISettingsProvider settingsProvider)+ {+ _settingsProvider = settingsProvider;+ }+private void QueueConversionCalculation(){_conversionQueue.Enqueue(InputValue ?? string.Empty);diff --git a/src/dev/impl/DevToys/ViewModels/Tools/HashGenerator/HashGeneratorToolViewModel.cs b/src/dev/impl/DevToys/ViewModels/Tools/HashGenerator/HashGeneratorToolViewModel.csindex 7a17679cb..c656d98ce 100644--- a/src/dev/impl/DevToys/ViewModels/Tools/HashGenerator/HashGeneratorToolViewModel.cs+++ b/src/dev/impl/DevToys/ViewModels/Tools/HashGenerator/HashGeneratorToolViewModel.cs@@ -1,5 +1,6 @@#nullable enable+using DevToys.Api.Core.Settings;using DevToys.Api.Tools;using DevToys.Core;using DevToys.Core.Threading;@@ -18,10 +19,19 @@ namespace DevToys.ViewModels.Tools.HashGenerator[Export(typeof(HashGeneratorToolViewModel))]public sealed class HashGeneratorToolViewModel : ObservableRecipient, IToolViewModel{+ /// <summary>+ /// Whether the generated hash should be uppercase or lowercase.+ /// </summary>+ private static readonly SettingDefinition<bool> Uppercase+ = new(+ name: $"{nameof(HashGeneratorToolViewModel)}.{nameof(Uppercase)}",+ isRoaming: true,+ defaultValue: false);++ private readonly ISettingsProvider _settingsProvider;private readonly Queue<string> _hashCalculationQueue = new();private bool _calculationInProgress;- private bool _isUppercase;private string? _input;private string? _md5;private string? _sha1;@@ -34,11 +44,15 @@ namespace DevToys.ViewModels.Tools.HashGeneratorinternal bool IsUppercase{- get => _isUppercase;+ get => _settingsProvider.GetSetting(Uppercase);set{- SetProperty(ref _isUppercase, value);- QueueHashCalculation();+ if (_settingsProvider.GetSetting(Uppercase) != value)+ {+ _settingsProvider.SetSetting(Uppercase, value);+ OnPropertyChanged();+ QueueHashCalculation();+ }}}@@ -76,6 +90,12 @@ namespace DevToys.ViewModels.Tools.HashGeneratorprivate set => SetProperty(ref _sha512, value);}+ [ImportingConstructor]+ public HashGeneratorToolViewModel(ISettingsProvider settingsProvider)+ {+ _settingsProvider = settingsProvider;+ }+private void QueueHashCalculation(){_hashCalculationQueue.Enqueue(Input ?? string.Empty);diff --git a/src/dev/impl/DevToys/ViewModels/Tools/JsonFormatter/JsonFormatterToolViewModel.cs b/src/dev/impl/DevToys/ViewModels/Tools/JsonFormatter/JsonFormatterToolViewModel.csindex 305b9c0f4..841f8ad47 100644--- a/src/dev/impl/DevToys/ViewModels/Tools/JsonFormatter/JsonFormatterToolViewModel.cs+++ b/src/dev/impl/DevToys/ViewModels/Tools/JsonFormatter/JsonFormatterToolViewModel.cs@@ -20,6 +20,15 @@ namespace DevToys.ViewModels.Tools.JsonFormatter[Export(typeof(JsonFormatterToolViewModel))]public sealed class JsonFormatterToolViewModel : ObservableRecipient, IToolViewModel{+ /// <summary>+ /// The indentation to apply while formatting.+ /// </summary>+ private static readonly SettingDefinition<string> Indentation+ = new(+ name: $"{nameof(JsonFormatterToolViewModel)}.{nameof(Indentation)}",+ isRoaming: true,+ defaultValue: TwoSpaceIndentation);+private const string TwoSpaceIndentation = "TwoSpaces";private const string FourSpaceIndentation = "FourSpaces";private const string OneTabIndentation = "OneTab";@@ -30,7 +39,6 @@ namespace DevToys.ViewModels.Tools.JsonFormatterprivate bool _formattingInProgress;private string? _inputValue;private string? _outputValue;- private string _indentation = TwoSpaceIndentation;public Type View { get; } = typeof(JsonFormatterToolPage);@@ -39,13 +47,17 @@ namespace DevToys.ViewModels.Tools.JsonFormatter/// <summary>/// Gets or sets the desired indentation./// </summary>- internal string Indentation+ internal string IndentationMode{- get => _indentation;+ get => SettingsProvider.GetSetting(Indentation);set{- SetProperty(ref _indentation, value);- QueueFormatting();+ if (!string.Equals(SettingsProvider.GetSetting(Indentation), value, StringComparison.Ordinal))+ {+ SettingsProvider.SetSetting(Indentation, value);+ OnPropertyChanged();+ QueueFormatting();+ }}}@@ -124,7 +136,7 @@ namespace DevToys.ViewModels.Tools.JsonFormatterusing (var stringWriter = new StringWriter(stringBuilder))using (var jsonTextWriter = new JsonTextWriter(stringWriter)){- switch (Indentation)+ switch (IndentationMode){case TwoSpaceIndentation:jsonTextWriter.Formatting = Formatting.Indented;@@ -169,7 +181,7 @@ namespace DevToys.ViewModels.Tools.JsonFormatter}catch (Exception ex) //some other exception{- Logger.LogFault("Json formatter", ex, $"Indentation: {Indentation}");+ Logger.LogFault("Json formatter", ex, $"Indentation: {IndentationMode}");output = ex.Message;return false;}diff --git a/src/dev/impl/DevToys/ViewModels/Tools/JsonYaml/JsonYamlToolViewModel.cs b/src/dev/impl/DevToys/ViewModels/Tools/JsonYaml/JsonYamlToolViewModel.csindex f9081dcfd..ddb11e7fd 100644--- a/src/dev/impl/DevToys/ViewModels/Tools/JsonYaml/JsonYamlToolViewModel.cs+++ b/src/dev/impl/DevToys/ViewModels/Tools/JsonYaml/JsonYamlToolViewModel.cs@@ -23,6 +23,24 @@ namespace DevToys.ViewModels.Tools.JsonYaml[Export(typeof(JsonYamlToolViewModel))]public sealed class JsonYamlToolViewModel : ObservableRecipient, IToolViewModel{+ /// <summary>+ /// Whether the tool should convert JSON to YAML or YAML to JSON.+ /// </summary>+ private static readonly SettingDefinition<string> Conversion+ = new(+ name: $"{nameof(JsonYamlToolViewModel)}.{nameof(Conversion)}",+ isRoaming: true,+ defaultValue: JsonToYaml);++ /// <summary>+ /// The indentation to apply while converting.+ /// </summary>+ private static readonly SettingDefinition<string> Indentation+ = new(+ name: $"{nameof(JsonYamlToolViewModel)}.{nameof(Indentation)}",+ isRoaming: true,+ defaultValue: TwoSpaceIndentation);+internal const string JsonToYaml = nameof(JsonToYaml);internal const string YamlToJson = nameof(YamlToJson);private const string TwoSpaceIndentation = "TwoSpaces";@@ -36,8 +54,6 @@ namespace DevToys.ViewModels.Tools.JsonYamlprivate string? _inputValueLanguage;private string? _outputValue;private string? _outputValueLanguage;- private string _indentation = TwoSpaceIndentation;- private string _conversionMode = JsonToYaml;public Type View { get; } = typeof(JsonYamlToolPage);@@ -48,34 +64,38 @@ namespace DevToys.ViewModels.Tools.JsonYaml/// </summary>internal string ConversionMode{- get => _conversionMode;+ get => SettingsProvider.GetSetting(Conversion);set{if (!_setPropertyInProgress){_setPropertyInProgress = true;ThreadHelper.ThrowIfNotOnUIThread();- SetProperty(ref _conversionMode, value);-- if (string.Equals(value, JsonToYaml))+ if (!string.Equals(SettingsProvider.GetSetting(Conversion), value, StringComparison.Ordinal)){- if (JsonHelper.IsValidJson(OutputValue))- {- InputValue = OutputValue;- }+ SettingsProvider.SetSetting(Conversion, value);+ OnPropertyChanged();- InputValueLanguage = "json";- OutputValueLanguage = "yaml";- }- else- {- if (YamlHelper.IsValidYaml(OutputValue))+ if (string.Equals(value, JsonToYaml)){- InputValue = OutputValue;+ if (JsonHelper.IsValidJson(OutputValue))+ {+ InputValue = OutputValue;+ }++ InputValueLanguage = "json";+ OutputValueLanguage = "yaml";}+ else+ {+ if (YamlHelper.IsValidYaml(OutputValue))+ {+ InputValue = OutputValue;+ }- InputValueLanguage = "yaml";- OutputValueLanguage = "json";+ InputValueLanguage = "yaml";+ OutputValueLanguage = "json";+ }}_setPropertyInProgress = false;@@ -86,13 +106,17 @@ namespace DevToys.ViewModels.Tools.JsonYaml/// <summary>/// Gets or sets the desired indentation./// </summary>- internal string Indentation+ internal string IndentationMode{- get => _indentation;+ get => SettingsProvider.GetSetting(Indentation);set{- SetProperty(ref _indentation, value);- QueueConversion();+ if (!string.Equals(SettingsProvider.GetSetting(Indentation), value, StringComparison.Ordinal))+ {+ SettingsProvider.SetSetting(Indentation, value);+ OnPropertyChanged();+ QueueConversion();+ }}}@@ -199,7 +223,7 @@ namespace DevToys.ViewModels.Tools.JsonYamlif (jsonObject is not null && jsonObject is not string){int indent = 0;- switch (Indentation)+ switch (IndentationMode){case TwoSpaceIndentation:indent = 2;@@ -263,7 +287,7 @@ namespace DevToys.ViewModels.Tools.JsonYamlusing (var stringWriter = new StringWriter(stringBuilder))using (var jsonTextWriter = new JsonTextWriter(stringWriter)){- switch (Indentation)+ switch (IndentationMode){case TwoSpaceIndentation:jsonTextWriter.Formatting = Formatting.Indented;diff --git a/src/dev/impl/DevToys/ViewModels/Tools/TextDiff/TextDiffToolViewModel.cs b/src/dev/impl/DevToys/ViewModels/Tools/TextDiff/TextDiffToolViewModel.csindex 06e4302f4..097d2e1f5 100644--- a/src/dev/impl/DevToys/ViewModels/Tools/TextDiff/TextDiffToolViewModel.cs+++ b/src/dev/impl/DevToys/ViewModels/Tools/TextDiff/TextDiffToolViewModel.cs@@ -12,7 +12,15 @@ namespace DevToys.ViewModels.Tools.TextDiff[Export(typeof(TextDiffToolViewModel))]public sealed class TextDiffToolViewModel : ObservableRecipient, IToolViewModel{- private bool _inlineMode;+ /// <summary>+ /// Whether the text difference should be displayed inlined or side by side.+ /// </summary>+ private static readonly SettingDefinition<bool> Inline+ = new(+ name: $"{nameof(TextDiffToolViewModel)}.{nameof(Inline)}",+ isRoaming: true,+ defaultValue: false);+private string? _oldText;private string? _newText;@@ -24,8 +32,15 @@ namespace DevToys.ViewModels.Tools.TextDiffinternal bool InlineMode{- get => _inlineMode;- set => SetProperty(ref _inlineMode, value, broadcast: true);+ get => SettingsProvider.GetSetting(Inline);+ set+ {+ if (SettingsProvider.GetSetting(Inline) != value)+ {+ SettingsProvider.SetSetting(Inline, value);+ OnPropertyChanged();+ }+ }}internal string? OldTextdiff --git a/src/dev/impl/DevToys/Views/Tools/JsonFormatter/JsonFormatterToolPage.xaml b/src/dev/impl/DevToys/Views/Tools/JsonFormatter/JsonFormatterToolPage.xamlindex b6a66e445..ae0bc4a9b 100644--- a/src/dev/impl/DevToys/Views/Tools/JsonFormatter/JsonFormatterToolPage.xaml+++ b/src/dev/impl/DevToys/Views/Tools/JsonFormatter/JsonFormatterToolPage.xaml@@ -5,7 +5,6 @@xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:controls="using:DevToys.UI.Controls"- xmlns:formattedtextblock="using:DevToys.UI.Controls.FormattedTextBlock"xmlns:toolkit="using:Microsoft.Toolkit.Uwp.UI.Controls"mc:Ignorable="d"NavigationCacheMode="Required">@@ -34,7 +33,7 @@</controls:ExpandableSettingControl.Icon><ComboBoxSelectedValuePath="Tag"- SelectedValue="{x:Bind ViewModel.Indentation, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">+ SelectedValue="{x:Bind ViewModel.IndentationMode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"><ComboBoxItem Tag="TwoSpaces" Content="{x:Bind ViewModel.Strings.TwoSpaces}"/><ComboBoxItem Tag="FourSpaces" Content="{x:Bind ViewModel.Strings.FourSpaces}"/><ComboBoxItem Tag="OneTab" Content="{x:Bind ViewModel.Strings.OneTab}"/>diff --git a/src/dev/impl/DevToys/Views/Tools/JsonYaml/JsonYamlToolPage.xaml b/src/dev/impl/DevToys/Views/Tools/JsonYaml/JsonYamlToolPage.xamlindex f76937edb..1d057b747 100644--- a/src/dev/impl/DevToys/Views/Tools/JsonYaml/JsonYamlToolPage.xaml+++ b/src/dev/impl/DevToys/Views/Tools/JsonYaml/JsonYamlToolPage.xaml@@ -5,8 +5,6 @@xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:controls="using:DevToys.UI.Controls"- xmlns:formattedtextblock="using:DevToys.UI.Controls.FormattedTextBlock"- xmlns:texteditor="using:DevToys.UI.Controls.TextEditor"xmlns:ex="using:DevToys.UI.Extensions"xmlns:toolkit="using:Microsoft.Toolkit.Uwp.UI.Controls"mc:Ignorable="d"@@ -61,7 +59,7 @@</controls:ExpandableSettingControl.Icon><ComboBoxSelectedValuePath="Tag"- SelectedValue="{x:Bind ViewModel.Indentation, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">+ SelectedValue="{x:Bind ViewModel.IndentationMode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"><ComboBoxItem Tag="TwoSpaces" Content="{x:Bind ViewModel.Strings.TwoSpaces}"/><ComboBoxItem Tag="FourSpaces" Content="{x:Bind ViewModel.Strings.FourSpaces}"/></ComboBox>diff --git a/src/tests/DevToys.Tests/Providers/Tools/HashGeneratorTests.cs b/src/tests/DevToys.Tests/Providers/Tools/HashGeneratorTests.csindex a923bc25d..e088906c7 100644--- a/src/tests/DevToys.Tests/Providers/Tools/HashGeneratorTests.cs+++ b/src/tests/DevToys.Tests/Providers/Tools/HashGeneratorTests.cs@@ -19,6 +19,7 @@ namespace DevToys.Tests.Providers.Toolsawait ThreadHelper.RunOnUIThreadAsync(() =>{+ viewModel.IsUppercase = false;viewModel.Input = input;});