mirror of
https://github.com/sourcegit-scm/sourcegit.git
synced 2024-12-24 20:57:19 -08:00
refactor: json serialization
* move all converters to `App.JsonCodeGen.cs` * use `ColorConverter` instead of parsing colors manually
This commit is contained in:
parent
16d9b627f0
commit
7ee3db500a
5 changed files with 64 additions and 50 deletions
|
@ -1,9 +1,64 @@
|
||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Media;
|
||||||
|
|
||||||
namespace SourceGit
|
namespace SourceGit
|
||||||
{
|
{
|
||||||
[JsonSourceGenerationOptions(WriteIndented = true, IgnoreReadOnlyFields = true, IgnoreReadOnlyProperties = true)]
|
public class ColorConverter : JsonConverter<Color>
|
||||||
|
{
|
||||||
|
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<FontFamily>
|
||||||
|
{
|
||||||
|
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<GridLength>
|
||||||
|
{
|
||||||
|
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<Models.InteractiveRebaseJob>))]
|
[JsonSerializable(typeof(List<Models.InteractiveRebaseJob>))]
|
||||||
[JsonSerializable(typeof(Models.JetBrainsState))]
|
[JsonSerializable(typeof(Models.JetBrainsState))]
|
||||||
[JsonSerializable(typeof(Models.ThemeOverrides))]
|
[JsonSerializable(typeof(Models.ThemeOverrides))]
|
||||||
|
|
|
@ -169,23 +169,15 @@ namespace SourceGit
|
||||||
foreach (var kv in overrides.BasicColors)
|
foreach (var kv in overrides.BasicColors)
|
||||||
{
|
{
|
||||||
if (kv.Key.Equals("SystemAccentColor", StringComparison.Ordinal))
|
if (kv.Key.Equals("SystemAccentColor", StringComparison.Ordinal))
|
||||||
resDic["SystemAccentColor"] = Color.Parse(kv.Value);
|
resDic["SystemAccentColor"] = kv.Value;
|
||||||
else
|
else
|
||||||
resDic[$"Color.{kv.Key}"] = Color.Parse(kv.Value);
|
resDic[$"Color.{kv.Key}"] = kv.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (overrides.GraphColors.Count > 0)
|
if (overrides.GraphColors.Count > 0)
|
||||||
{
|
Models.CommitGraph.SetPens(overrides.GraphColors, overrides.GraphPenThickness);
|
||||||
var penColors = new List<Color>();
|
|
||||||
foreach (var c in overrides.GraphColors)
|
|
||||||
penColors.Add(Color.Parse(c));
|
|
||||||
|
|
||||||
Models.CommitGraph.SetPens(penColors, overrides.GraphPenThickness);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
|
||||||
Models.CommitGraph.SetDefaultPens(overrides.GraphPenThickness);
|
Models.CommitGraph.SetDefaultPens(overrides.GraphPenThickness);
|
||||||
}
|
|
||||||
|
|
||||||
app.Resources.MergedDictionaries.Add(resDic);
|
app.Resources.MergedDictionaries.Add(resDic);
|
||||||
app._themeOverrides = resDic;
|
app._themeOverrides = resDic;
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
using Avalonia.Media;
|
||||||
|
|
||||||
namespace SourceGit.Models
|
namespace SourceGit.Models
|
||||||
{
|
{
|
||||||
public class ThemeOverrides
|
public class ThemeOverrides
|
||||||
{
|
{
|
||||||
public Dictionary<string, string> BasicColors { get; set; } = new Dictionary<string, string>();
|
public Dictionary<string, Color> BasicColors { get; set; } = new Dictionary<string, Color>();
|
||||||
public double GraphPenThickness { get; set; } = 1.5;
|
public double GraphPenThickness { get; set; } = 1.5;
|
||||||
public List<string> GraphColors { get; set; } = new List<string>();
|
public List<Color> GraphColors { get; set; } = new List<Color>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,35 +28,30 @@ namespace SourceGit.ViewModels
|
||||||
set;
|
set;
|
||||||
} = WindowState.Normal;
|
} = WindowState.Normal;
|
||||||
|
|
||||||
[JsonConverter(typeof(GridLengthConverter))]
|
|
||||||
public GridLength RepositorySidebarWidth
|
public GridLength RepositorySidebarWidth
|
||||||
{
|
{
|
||||||
get => _repositorySidebarWidth;
|
get => _repositorySidebarWidth;
|
||||||
set => SetProperty(ref _repositorySidebarWidth, value);
|
set => SetProperty(ref _repositorySidebarWidth, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonConverter(typeof(GridLengthConverter))]
|
|
||||||
public GridLength WorkingCopyLeftWidth
|
public GridLength WorkingCopyLeftWidth
|
||||||
{
|
{
|
||||||
get => _workingCopyLeftWidth;
|
get => _workingCopyLeftWidth;
|
||||||
set => SetProperty(ref _workingCopyLeftWidth, value);
|
set => SetProperty(ref _workingCopyLeftWidth, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonConverter(typeof(GridLengthConverter))]
|
|
||||||
public GridLength StashesLeftWidth
|
public GridLength StashesLeftWidth
|
||||||
{
|
{
|
||||||
get => _stashesLeftWidth;
|
get => _stashesLeftWidth;
|
||||||
set => SetProperty(ref _stashesLeftWidth, value);
|
set => SetProperty(ref _stashesLeftWidth, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonConverter(typeof(GridLengthConverter))]
|
|
||||||
public GridLength CommitDetailChangesLeftWidth
|
public GridLength CommitDetailChangesLeftWidth
|
||||||
{
|
{
|
||||||
get => _commitDetailChangesLeftWidth;
|
get => _commitDetailChangesLeftWidth;
|
||||||
set => SetProperty(ref _commitDetailChangesLeftWidth, value);
|
set => SetProperty(ref _commitDetailChangesLeftWidth, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonConverter(typeof(GridLengthConverter))]
|
|
||||||
public GridLength CommitDetailFilesLeftWidth
|
public GridLength CommitDetailFilesLeftWidth
|
||||||
{
|
{
|
||||||
get => _commitDetailFilesLeftWidth;
|
get => _commitDetailFilesLeftWidth;
|
||||||
|
@ -69,18 +64,4 @@ namespace SourceGit.ViewModels
|
||||||
private GridLength _commitDetailChangesLeftWidth = new GridLength(256, GridUnitType.Pixel);
|
private GridLength _commitDetailChangesLeftWidth = new GridLength(256, GridUnitType.Pixel);
|
||||||
private GridLength _commitDetailFilesLeftWidth = new GridLength(256, GridUnitType.Pixel);
|
private GridLength _commitDetailFilesLeftWidth = new GridLength(256, GridUnitType.Pixel);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class GridLengthConverter : JsonConverter<GridLength>
|
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,14 +80,12 @@ namespace SourceGit.ViewModels
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonConverter(typeof(FontFamilyConverter))]
|
|
||||||
public FontFamily DefaultFont
|
public FontFamily DefaultFont
|
||||||
{
|
{
|
||||||
get => _defaultFont;
|
get => _defaultFont;
|
||||||
set => SetProperty(ref _defaultFont, value);
|
set => SetProperty(ref _defaultFont, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonConverter(typeof(FontFamilyConverter))]
|
|
||||||
public FontFamily MonospaceFont
|
public FontFamily MonospaceFont
|
||||||
{
|
{
|
||||||
get => _monospaceFont;
|
get => _monospaceFont;
|
||||||
|
@ -517,18 +515,4 @@ namespace SourceGit.ViewModels
|
||||||
|
|
||||||
private AvaloniaList<RepositoryNode> _repositoryNodes = new AvaloniaList<RepositoryNode>();
|
private AvaloniaList<RepositoryNode> _repositoryNodes = new AvaloniaList<RepositoryNode>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class FontFamilyConverter : JsonConverter<FontFamily>
|
|
||||||
{
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue