From 7ee3db500abb1524df079d5e78753029305bb18b Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 8 Jul 2024 16:45:51 +0800 Subject: [PATCH] refactor: json serialization * move all converters to `App.JsonCodeGen.cs` * use `ColorConverter` instead of parsing colors manually --- src/App.JsonCodeGen.cs | 59 ++++++++++++++++++++++++++++++++++-- src/App.axaml.cs | 14 ++------- src/Models/ThemeOverrides.cs | 6 ++-- src/ViewModels/LayoutInfo.cs | 19 ------------ src/ViewModels/Preference.cs | 16 ---------- 5 files changed, 64 insertions(+), 50 deletions(-) diff --git a/src/App.JsonCodeGen.cs b/src/App.JsonCodeGen.cs index ba6bd96c..fd4d274e 100644 --- a/src/App.JsonCodeGen.cs +++ b/src/App.JsonCodeGen.cs @@ -1,9 +1,64 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Text.Json; using System.Text.Json.Serialization; +using Avalonia.Controls; +using Avalonia.Media; + namespace SourceGit { - [JsonSourceGenerationOptions(WriteIndented = true, IgnoreReadOnlyFields = true, IgnoreReadOnlyProperties = true)] + public class ColorConverter : JsonConverter + { + public override Color Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return Color.Parse(reader.GetString()); + } + + public override void Write(Utf8JsonWriter writer, Color value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToString()); + } + } + + public class FontFamilyConverter : JsonConverter + { + public override FontFamily Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var name = reader.GetString(); + return new FontFamily(name); + } + + public override void Write(Utf8JsonWriter writer, FontFamily value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToString()); + } + } + + public class GridLengthConverter : JsonConverter + { + public override GridLength Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var size = reader.GetDouble(); + return new GridLength(size, GridUnitType.Pixel); + } + + public override void Write(Utf8JsonWriter writer, GridLength value, JsonSerializerOptions options) + { + writer.WriteNumberValue(value.Value); + } + } + + [JsonSourceGenerationOptions( + WriteIndented = true, + IgnoreReadOnlyFields = true, + IgnoreReadOnlyProperties = true, + Converters = [ + typeof(ColorConverter), + typeof(FontFamilyConverter), + typeof(GridLengthConverter), + ] + )] [JsonSerializable(typeof(List))] [JsonSerializable(typeof(Models.JetBrainsState))] [JsonSerializable(typeof(Models.ThemeOverrides))] diff --git a/src/App.axaml.cs b/src/App.axaml.cs index 8c9738fa..b03e4f03 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -169,23 +169,15 @@ namespace SourceGit foreach (var kv in overrides.BasicColors) { if (kv.Key.Equals("SystemAccentColor", StringComparison.Ordinal)) - resDic["SystemAccentColor"] = Color.Parse(kv.Value); + resDic["SystemAccentColor"] = kv.Value; else - resDic[$"Color.{kv.Key}"] = Color.Parse(kv.Value); + resDic[$"Color.{kv.Key}"] = kv.Value; } if (overrides.GraphColors.Count > 0) - { - var penColors = new List(); - foreach (var c in overrides.GraphColors) - penColors.Add(Color.Parse(c)); - - Models.CommitGraph.SetPens(penColors, overrides.GraphPenThickness); - } + Models.CommitGraph.SetPens(overrides.GraphColors, overrides.GraphPenThickness); else - { Models.CommitGraph.SetDefaultPens(overrides.GraphPenThickness); - } app.Resources.MergedDictionaries.Add(resDic); app._themeOverrides = resDic; diff --git a/src/Models/ThemeOverrides.cs b/src/Models/ThemeOverrides.cs index 96e48f82..b231353c 100644 --- a/src/Models/ThemeOverrides.cs +++ b/src/Models/ThemeOverrides.cs @@ -1,11 +1,13 @@ using System.Collections.Generic; +using Avalonia.Media; + namespace SourceGit.Models { public class ThemeOverrides { - public Dictionary BasicColors { get; set; } = new Dictionary(); + public Dictionary BasicColors { get; set; } = new Dictionary(); public double GraphPenThickness { get; set; } = 1.5; - public List GraphColors { get; set; } = new List(); + public List GraphColors { get; set; } = new List(); } } diff --git a/src/ViewModels/LayoutInfo.cs b/src/ViewModels/LayoutInfo.cs index e78ad2cf..30be27de 100644 --- a/src/ViewModels/LayoutInfo.cs +++ b/src/ViewModels/LayoutInfo.cs @@ -28,35 +28,30 @@ namespace SourceGit.ViewModels set; } = WindowState.Normal; - [JsonConverter(typeof(GridLengthConverter))] public GridLength RepositorySidebarWidth { get => _repositorySidebarWidth; set => SetProperty(ref _repositorySidebarWidth, value); } - [JsonConverter(typeof(GridLengthConverter))] public GridLength WorkingCopyLeftWidth { get => _workingCopyLeftWidth; set => SetProperty(ref _workingCopyLeftWidth, value); } - [JsonConverter(typeof(GridLengthConverter))] public GridLength StashesLeftWidth { get => _stashesLeftWidth; set => SetProperty(ref _stashesLeftWidth, value); } - [JsonConverter(typeof(GridLengthConverter))] public GridLength CommitDetailChangesLeftWidth { get => _commitDetailChangesLeftWidth; set => SetProperty(ref _commitDetailChangesLeftWidth, value); } - [JsonConverter(typeof(GridLengthConverter))] public GridLength CommitDetailFilesLeftWidth { get => _commitDetailFilesLeftWidth; @@ -69,18 +64,4 @@ namespace SourceGit.ViewModels private GridLength _commitDetailChangesLeftWidth = new GridLength(256, GridUnitType.Pixel); private GridLength _commitDetailFilesLeftWidth = new GridLength(256, GridUnitType.Pixel); } - - public class GridLengthConverter : JsonConverter - { - public override GridLength Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - var size = reader.GetDouble(); - return new GridLength(size, GridUnitType.Pixel); - } - - public override void Write(Utf8JsonWriter writer, GridLength value, JsonSerializerOptions options) - { - writer.WriteNumberValue(value.Value); - } - } } diff --git a/src/ViewModels/Preference.cs b/src/ViewModels/Preference.cs index 431fd8d8..00808a48 100644 --- a/src/ViewModels/Preference.cs +++ b/src/ViewModels/Preference.cs @@ -80,14 +80,12 @@ namespace SourceGit.ViewModels } } - [JsonConverter(typeof(FontFamilyConverter))] public FontFamily DefaultFont { get => _defaultFont; set => SetProperty(ref _defaultFont, value); } - [JsonConverter(typeof(FontFamilyConverter))] public FontFamily MonospaceFont { get => _monospaceFont; @@ -517,18 +515,4 @@ namespace SourceGit.ViewModels private AvaloniaList _repositoryNodes = new AvaloniaList(); } - - public class FontFamilyConverter : JsonConverter - { - public override FontFamily Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - var name = reader.GetString(); - return new FontFamily(name); - } - - public override void Write(Utf8JsonWriter writer, FontFamily value, JsonSerializerOptions options) - { - writer.WriteStringValue(value.ToString()); - } - } }