From 80f72676ecd7eaffd5bb9d4bf205b7a74a643ec0 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 2 Jul 2024 22:54:26 +0800 Subject: [PATCH] ux: new custom theme configuration format * supports customize the commit graph --- src/App.JsonCodeGen.cs | 6 ++-- src/App.axaml.cs | 19 ++++++++++-- src/Models/CommitGraph.cs | 52 ++++++++++++++++++++++++--------- src/Models/CustomColorSchema.cs | 10 +++++++ src/ViewModels/Repository.cs | 2 +- 5 files changed, 68 insertions(+), 21 deletions(-) create mode 100644 src/Models/CustomColorSchema.cs diff --git a/src/App.JsonCodeGen.cs b/src/App.JsonCodeGen.cs index 50d589a2..901a9b5b 100644 --- a/src/App.JsonCodeGen.cs +++ b/src/App.JsonCodeGen.cs @@ -4,10 +4,10 @@ using System.Text.Json.Serialization; namespace SourceGit { [JsonSourceGenerationOptions(WriteIndented = true, IgnoreReadOnlyFields = true, IgnoreReadOnlyProperties = true)] - [JsonSerializable(typeof(Models.Version))] - [JsonSerializable(typeof(Models.JetBrainsState))] [JsonSerializable(typeof(List))] - [JsonSerializable(typeof(Dictionary))] + [JsonSerializable(typeof(Models.JetBrainsState))] + [JsonSerializable(typeof(Models.Version))] + [JsonSerializable(typeof(Models.CustomColorSchema))] [JsonSerializable(typeof(ViewModels.Preference))] [JsonSerializable(typeof(ViewModels.RepositorySettings))] internal partial class JsonCodeGen : JsonSerializerContext { } diff --git a/src/App.axaml.cs b/src/App.axaml.cs index db0a4644..df438ecc 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Reflection; @@ -159,15 +160,27 @@ namespace SourceGit app._colorOverrides = null; } + Models.CommitGraph.SetDefaultPens(); + if (!string.IsNullOrEmpty(colorsFile) && File.Exists(colorsFile)) { try { var resDic = new ResourceDictionary(); - var schema = JsonSerializer.Deserialize(File.ReadAllText(colorsFile), JsonCodeGen.Default.DictionaryStringString); - foreach (var kv in schema) - resDic[kv.Key] = Color.Parse(kv.Value); + var schema = JsonSerializer.Deserialize(File.ReadAllText(colorsFile), JsonCodeGen.Default.CustomColorSchema); + foreach (var kv in schema.Basic) + resDic[$"Color.{kv.Key}"] = Color.Parse(kv.Value); + + if (schema.Graph.Count > 0) + { + var penColors = new List(); + + foreach (var c in schema.Graph) + penColors.Add(Color.Parse(c)); + + Models.CommitGraph.SetPenColors(penColors); + } app.Resources.MergedDictionaries.Add(resDic); app._colorOverrides = resDic; diff --git a/src/Models/CommitGraph.cs b/src/Models/CommitGraph.cs index 6b26eba5..bc0ea8e1 100644 --- a/src/Models/CommitGraph.cs +++ b/src/Models/CommitGraph.cs @@ -8,17 +8,6 @@ namespace SourceGit.Models { public class CommitGraph { - public static readonly Pen[] Pens = [ - new Pen(Brushes.Orange, 2), - new Pen(Brushes.ForestGreen, 2), - new Pen(Brushes.Gold, 2), - new Pen(Brushes.Magenta, 2), - new Pen(Brushes.Red, 2), - new Pen(Brushes.Gray, 2), - new Pen(Brushes.Turquoise, 2), - new Pen(Brushes.Olive, 2), - ]; - public class Path { public List Points = new List(); @@ -113,7 +102,28 @@ namespace SourceGit.Models public List Links { get; set; } = new List(); public List Dots { get; set; } = new List(); - public static CommitGraph Parse(List commits, int colorCount) + public static List Pens + { + get; + private set; + } = new List(); + + public static void SetDefaultPens() + { + SetPenColors(_defaultPenColors); + } + + public static void SetPenColors(List colors) + { + Pens.Clear(); + + foreach (var c in colors) + Pens.Add(new Pen(c.ToUInt32(), 2)); + + _penCount = colors.Count; + } + + public static CommitGraph Parse(List commits) { double UNIT_WIDTH = 12; double HALF_WIDTH = 6; @@ -184,7 +194,7 @@ namespace SourceGit.Models major = new PathHelper(commit.Parents[0], isMerged, colorIdx, new Point(offsetX, offsetY)); unsolved.Add(major); temp.Paths.Add(major.Path); - colorIdx = (colorIdx + 1) % colorCount; + colorIdx = (colorIdx + 1) % _penCount; } // Calculate link position of this commit. @@ -223,7 +233,7 @@ namespace SourceGit.Models var l = new PathHelper(commit.Parents[j], isMerged, colorIdx, position, new Point(offsetX, position.Y + HALF_HEIGHT)); unsolved.Add(l); temp.Paths.Add(l.Path); - colorIdx = (colorIdx + 1) % colorCount; + colorIdx = (colorIdx + 1) % _penCount; } } @@ -257,5 +267,19 @@ namespace SourceGit.Models return temp; } + + private static int _penCount = 0; + private static readonly List _defaultPenColors = [ + Colors.Orange, + Colors.ForestGreen, + Colors.Gold, + Colors.Magenta, + Colors.Red, + Colors.Gray, + Colors.Turquoise, + Colors.Olive, + Colors.Khaki, + Colors.Lime, + ]; } } diff --git a/src/Models/CustomColorSchema.cs b/src/Models/CustomColorSchema.cs new file mode 100644 index 00000000..4266b98e --- /dev/null +++ b/src/Models/CustomColorSchema.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; + +namespace SourceGit.Models +{ + public class CustomColorSchema + { + public Dictionary Basic { get; set; } = new Dictionary(); + public List Graph { get; set; } = new List(); + } +} diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index 86f47656..0a51c384 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -709,7 +709,7 @@ namespace SourceGit.ViewModels } var commits = new Commands.QueryCommits(FullPath, limits).Result(); - var graph = Models.CommitGraph.Parse(commits, 8); + var graph = Models.CommitGraph.Parse(commits); Dispatcher.UIThread.Invoke(() => {