From 986c27e1cb37c9f5d8159de0fb878d267238c01e Mon Sep 17 00:00:00 2001 From: leo Date: Sun, 29 Sep 2024 22:44:50 +0800 Subject: [PATCH] code_style: code style of `Models.CommitGraph` --- src/Commands/Command.cs | 2 +- src/Models/CommitGraph.cs | 356 +++++++++++++++++++------------------- src/Views/Blame.axaml.cs | 2 +- 3 files changed, 176 insertions(+), 184 deletions(-) diff --git a/src/Commands/Command.cs b/src/Commands/Command.cs index 4d5efaa4..55fc6d17 100644 --- a/src/Commands/Command.cs +++ b/src/Commands/Command.cs @@ -126,7 +126,7 @@ namespace SourceGit.Commands } return false; } - + return true; } diff --git a/src/Models/CommitGraph.cs b/src/Models/CommitGraph.cs index 98f7e2b8..3872fcc6 100644 --- a/src/Models/CommitGraph.cs +++ b/src/Models/CommitGraph.cs @@ -8,130 +8,27 @@ namespace SourceGit.Models { public class CommitGraph { - public class Path + public static List Pens { get; } = []; + + public static void SetDefaultPens(double thickness = 2) { - public List Points = new List(); - public int Color = 0; + SetPens(s_defaultPenColors, thickness); } - public class PathHelper + public static void SetPens(List colors, double thickness) { - public string Next; - public bool IsMerged; - public double LastX; - public double LastY; - public Path Path; + Pens.Clear(); - public PathHelper(string next, bool isMerged, int color, Point start) - { - Next = next; - IsMerged = isMerged; - LastX = start.X; - LastY = start.Y; + foreach (var c in colors) + Pens.Add(new Pen(c.ToUInt32(), thickness)); - Path = new Path(); - Path.Color = color; - Path.Points.Add(start); - } + s_penCount = colors.Count; + } - public PathHelper(string next, bool isMerged, int color, Point start, Point to) - { - Next = next; - IsMerged = isMerged; - LastX = to.X; - LastY = to.Y; - - Path = new Path(); - Path.Color = color; - Path.Points.Add(start); - Path.Points.Add(to); - } - - /// - /// A path that just passed this row. - /// - /// - /// - /// - public void Pass(double x, double y, double halfHeight) - { - if (x > LastX) - { - Add(LastX, LastY); - Add(x, y - halfHeight); - } - else if (x < LastX) - { - Add(LastX, y - halfHeight); - y += halfHeight; - Add(x, y); - } - - LastX = x; - LastY = y; - } - - /// - /// A path that has commit in this row but not ended - /// - /// - /// - /// - public void Goto(double x, double y, double halfHeight) - { - if (x > LastX) - { - Add(LastX, LastY); - Add(x, y - halfHeight); - } - else if (x < LastX) - { - var minY = y - halfHeight; - if (minY > LastY) - minY -= halfHeight; - - Add(LastX, minY); - Add(x, y); - } - - LastX = x; - LastY = y; - } - - /// - /// A path that has commit in this row and end. - /// - /// - /// - /// - public void End(double x, double y, double halfHeight) - { - if (x > LastX) - { - Add(LastX, LastY); - Add(x, y - halfHeight); - } - else if (x < LastX) - { - Add(LastX, y - halfHeight); - } - - Add(x, y); - - LastX = x; - LastY = y; - } - - private void Add(double x, double y) - { - if (_endY < y) - { - Path.Points.Add(new Point(x, y)); - _endY = y; - } - } - - private double _endY = 0; + public class Path(int color) + { + public List Points { get; } = []; + public int Color { get; } = color; } public class Link @@ -156,43 +53,21 @@ namespace SourceGit.Models public int Color; } - public List Paths { get; set; } = new List(); - public List Links { get; set; } = new List(); - public List Dots { get; set; } = new List(); - - public static List Pens - { - get; - private set; - } = new List(); - - public static void SetDefaultPens(double thickness = 2) - { - SetPens(_defaultPenColors, thickness); - } - - public static void SetPens(List colors, double thickness) - { - Pens.Clear(); - - foreach (var c in colors) - Pens.Add(new Pen(c.ToUInt32(), thickness)); - - _penCount = colors.Count; - } + public List Paths { get; } = []; + public List Links { get; } = []; + public List Dots { get; } = []; public static CommitGraph Parse(List commits, bool firstParentOnlyEnabled) { - double UNIT_WIDTH = 12; - double HALF_WIDTH = 6; - double UNIT_HEIGHT = 28; - double HALF_HEIGHT = 14; - double H_MARGIN = 2; + const double unitWidth = 12; + const double halfWidth = 6; + const double unitHeight = 28; + const double halfHeight = 14; var temp = new CommitGraph(); var unsolved = new List(); var ended = new List(); - var offsetY = -HALF_HEIGHT; + var offsetY = -halfHeight; var colorIdx = 0; foreach (var commit in commits) @@ -201,34 +76,34 @@ namespace SourceGit.Models var isMerged = commit.IsMerged; // Update current y offset - offsetY += UNIT_HEIGHT; + offsetY += unitHeight; // Find first curves that links to this commit and marks others that links to this commit ended. - var offsetX = 4 - HALF_WIDTH; - var maxOffsetOld = unsolved.Count > 0 ? unsolved[^1].LastX : offsetX + UNIT_WIDTH; + var offsetX = 4 - halfWidth; + var maxOffsetOld = unsolved.Count > 0 ? unsolved[^1].LastX : offsetX + unitWidth; foreach (var l in unsolved) { - if (l.Next == commit.SHA) + if (l.Next.Equals(commit.SHA, StringComparison.Ordinal)) { if (major == null) { - offsetX += UNIT_WIDTH; + offsetX += unitWidth; major = l; if (commit.Parents.Count > 0) { major.Next = commit.Parents[0]; - major.Goto(offsetX, offsetY, HALF_HEIGHT); + major.Goto(offsetX, offsetY, halfHeight); } else { - major.End(offsetX, offsetY, HALF_HEIGHT); + major.End(offsetX, offsetY, halfHeight); ended.Add(l); } } else { - l.End(major.LastX, offsetY, HALF_HEIGHT); + l.End(major.LastX, offsetY, halfHeight); ended.Add(l); } @@ -237,8 +112,8 @@ namespace SourceGit.Models } else { - offsetX += UNIT_WIDTH; - l.Pass(offsetX, offsetY, HALF_HEIGHT); + offsetX += unitWidth; + l.Pass(offsetX, offsetY, halfHeight); } } @@ -250,7 +125,7 @@ namespace SourceGit.Models // Create new curve for branch head if (major == null) { - offsetX += UNIT_WIDTH; + offsetX += unitWidth; if (commit.Parents.Count > 0) { @@ -259,13 +134,13 @@ namespace SourceGit.Models temp.Paths.Add(major.Path); } - colorIdx = (colorIdx + 1) % _penCount; + colorIdx = (colorIdx + 1) % s_penCount; } // Calculate link position of this commit. - Point position = new Point(major?.LastX ?? offsetX, offsetY); - int dotColor = major?.Path.Color ?? 0; - Dot anchor = new Dot() { Center = position, Color = dotColor }; + var position = new Point(major?.LastX ?? offsetX, offsetY); + var dotColor = major?.Path.Color ?? 0; + var anchor = new Dot() { Center = position, Color = dotColor }; if (commit.IsCurrentHead) anchor.Type = DotType.Head; else if (commit.Parents.Count > 1) @@ -279,59 +154,176 @@ namespace SourceGit.Models { for (int j = 1; j < commit.Parents.Count; j++) { - var parentSHA = commit.Parents[j]; - var parent = unsolved.Find(x => x.Next.Equals(parentSHA, StringComparison.Ordinal)); + var parentHash = commit.Parents[j]; + var parent = unsolved.Find(x => x.Next.Equals(parentHash, StringComparison.Ordinal)); if (parent != null) { // Try to change the merge state of linked graph - var l = parent; if (isMerged) - l.IsMerged = true; + parent.IsMerged = true; - var link = new Link(); - link.Start = position; - link.End = new Point(l.LastX, offsetY + HALF_HEIGHT); - link.Control = new Point(link.End.X, link.Start.Y); - link.Color = l.Path.Color; - - temp.Links.Add(link); + temp.Links.Add(new Link + { + Start = position, + End = new Point(parent.LastX, offsetY + halfHeight), + Control = new Point(parent.LastX, position.Y), + Color = parent.Path.Color + }); } else { - offsetX += UNIT_WIDTH; + offsetX += unitWidth; // Create new curve for parent commit that not includes before - var l = new PathHelper(parentSHA, isMerged, colorIdx, position, new Point(offsetX, position.Y + HALF_HEIGHT)); + var l = new PathHelper(parentHash, isMerged, colorIdx, position, new Point(offsetX, position.Y + halfHeight)); unsolved.Add(l); temp.Paths.Add(l.Path); - colorIdx = (colorIdx + 1) % _penCount; + colorIdx = (colorIdx + 1) % s_penCount; } } } // Margins & merge state (used by Views.Histories). commit.IsMerged = isMerged; - commit.Margin = new Thickness(Math.Max(offsetX, maxOffsetOld) + HALF_WIDTH + H_MARGIN, 0, 0, 0); + commit.Margin = new Thickness(Math.Max(offsetX, maxOffsetOld) + halfWidth + 2, 0, 0, 0); } // Deal with curves haven't ended yet. - for (int i = 0; i < unsolved.Count; i++) + for (var i = 0; i < unsolved.Count; i++) { var path = unsolved[i]; - var endY = (commits.Count - 0.5) * UNIT_HEIGHT; + var endY = (commits.Count - 0.5) * unitHeight; if (path.Path.Points.Count == 1 && Math.Abs(path.Path.Points[0].Y - endY) < 0.0001) continue; - path.End((i + 0.5) * UNIT_WIDTH + 4, endY + HALF_HEIGHT, HALF_HEIGHT); + path.End((i + 0.5) * unitWidth + 4, endY + halfHeight, halfHeight); } unsolved.Clear(); return temp; } - private static int _penCount = 0; - private static readonly List _defaultPenColors = [ + private class PathHelper + { + public Path Path { get; } + public string Next { get; set; } + public bool IsMerged { get; set; } + public double LastX { get; private set; } + + public PathHelper(string next, bool isMerged, int color, Point start) + { + Next = next; + IsMerged = isMerged; + LastX = start.X; + _lastY = start.Y; + + Path = new Path(color); + Path.Points.Add(start); + } + + public PathHelper(string next, bool isMerged, int color, Point start, Point to) + { + Next = next; + IsMerged = isMerged; + LastX = to.X; + _lastY = to.Y; + + Path = new Path(color); + Path.Points.Add(start); + Path.Points.Add(to); + } + + /// + /// A path that just passed this row. + /// + /// + /// + /// + public void Pass(double x, double y, double halfHeight) + { + if (x > LastX) + { + Add(LastX, _lastY); + Add(x, y - halfHeight); + } + else if (x < LastX) + { + Add(LastX, y - halfHeight); + y += halfHeight; + Add(x, y); + } + + LastX = x; + _lastY = y; + } + + /// + /// A path that has commit in this row but not ended + /// + /// + /// + /// + public void Goto(double x, double y, double halfHeight) + { + if (x > LastX) + { + Add(LastX, _lastY); + Add(x, y - halfHeight); + } + else if (x < LastX) + { + var minY = y - halfHeight; + if (minY > _lastY) + minY -= halfHeight; + + Add(LastX, minY); + Add(x, y); + } + + LastX = x; + _lastY = y; + } + + /// + /// A path that has commit in this row and end. + /// + /// + /// + /// + public void End(double x, double y, double halfHeight) + { + if (x > LastX) + { + Add(LastX, _lastY); + Add(x, y - halfHeight); + } + else if (x < LastX) + { + Add(LastX, y - halfHeight); + } + + Add(x, y); + + LastX = x; + _lastY = y; + } + + private void Add(double x, double y) + { + if (_endY < y) + { + Path.Points.Add(new Point(x, y)); + _endY = y; + } + } + + private double _lastY = 0; + private double _endY = 0; + } + + private static int s_penCount = 0; + private static readonly List s_defaultPenColors = [ Colors.Orange, Colors.ForestGreen, Colors.Gold, diff --git a/src/Views/Blame.axaml.cs b/src/Views/Blame.axaml.cs index 85af8f5e..f96870e0 100644 --- a/src/Views/Blame.axaml.cs +++ b/src/Views/Blame.axaml.cs @@ -145,7 +145,7 @@ namespace SourceGit.Views protected override void OnPointerMoved(PointerEventArgs e) { base.OnPointerMoved(e); - + var view = TextView; if (!e.Handled && view is { VisualLinesValid: true }) {