From 01ed685b59357b79844e1a4156cc1139eacb6873 Mon Sep 17 00:00:00 2001 From: leo Date: Sat, 19 Dec 2020 21:35:29 +0800 Subject: [PATCH] optimize: rename CommitGraphHelper to CommitGraphData; improve checking visibility of lines/links/dots in commit graph --- src/Helpers/CommitGraph.cs | 67 +++++++++++++++++++------------------- src/UI/Histories.xaml | 2 +- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/Helpers/CommitGraph.cs b/src/Helpers/CommitGraph.cs index 4acc3ed7..3820daa6 100644 --- a/src/Helpers/CommitGraph.cs +++ b/src/Helpers/CommitGraph.cs @@ -9,13 +9,12 @@ namespace SourceGit.Helpers { /// /// Tools to parse commit graph. /// - public class CommitGraphMaker { + public class CommitGraphData { /// - /// Sizes + /// Unit lengths for commit graph /// public static readonly double UNIT_WIDTH = 12; public static readonly double HALF_WIDTH = 6; - public static readonly double DOUBLE_WIDTH = 24; public static readonly double UNIT_HEIGHT = 24; public static readonly double HALF_HEIGHT = 12; @@ -34,9 +33,9 @@ namespace SourceGit.Helpers { }; /// - /// Helpers to draw lines. + /// Data to draw lines. /// - public class LineHelper { + public class Line { private double lastX = 0; private double lastY = 0; @@ -72,7 +71,7 @@ namespace SourceGit.Helpers { /// Is merged in tree /// Color index /// Start point - public LineHelper(string nextCommitId, bool isMerged, int colorIdx, Point startPoint) { + public Line(string nextCommitId, bool isMerged, int colorIdx, Point startPoint) { Next = nextCommitId; IsMerged = isMerged; Points = new List() { startPoint }; @@ -128,7 +127,7 @@ namespace SourceGit.Helpers { /// /// Independent lines in graph /// - public List Lines { get; set; } = new List(); + public List Lines { get; set; } = new List(); /// /// Short links. @@ -145,18 +144,18 @@ namespace SourceGit.Helpers { /// /// /// - public static CommitGraphMaker Parse(List commits) { - CommitGraphMaker maker = new CommitGraphMaker(); + public static CommitGraphData Parse(List commits) { + CommitGraphData data = new CommitGraphData(); - List unsolved = new List(); - List ended = new List(); - Dictionary currentMap = new Dictionary(); + List unsolved = new List(); + List ended = new List(); + Dictionary currentMap = new Dictionary(); double offsetY = -HALF_HEIGHT; int colorIdx = 0; for (int i = 0; i < commits.Count; i++) { Git.Commit commit = commits[i]; - LineHelper major = null; + Line major = null; bool isMerged = commit.IsHEAD || commit.IsMerged; int oldCount = unsolved.Count; @@ -195,7 +194,7 @@ namespace SourceGit.Helpers { // 处理本提交为非当前分支HEAD的情况(创建新依赖线路) if (major == null && commit.Parents.Count > 0) { offsetX += UNIT_WIDTH; - major = new LineHelper(commit.Parents[0], isMerged, colorIdx, new Point(offsetX, offsetY)); + major = new Line(commit.Parents[0], isMerged, colorIdx, new Point(offsetX, offsetY)); unsolved.Add(major); colorIdx++; } @@ -206,9 +205,9 @@ namespace SourceGit.Helpers { major.IsMerged = isMerged; position.X = major.HorizontalOffset; position.Y = offsetY; - maker.Dots.Add(new Dot() { Center = position, Color = major.Brush }); + data.Dots.Add(new Dot() { Center = position, Color = major.Brush }); } else { - maker.Dots.Add(new Dot() { Center = position, Color = Brushes.Orange }); + data.Dots.Add(new Dot() { Center = position, Color = Brushes.Orange }); } // 处理本提交的其他依赖 @@ -222,10 +221,10 @@ namespace SourceGit.Helpers { link.End = new Point(l.HorizontalOffset, offsetY + HALF_HEIGHT); link.Control = new Point(link.End.X, link.Start.Y); link.Brush = l.Brush; - maker.Links.Add(link); + data.Links.Add(link); } else { offsetX += UNIT_WIDTH; - unsolved.Add(new LineHelper(commit.Parents[j], isMerged, colorIdx, position)); + unsolved.Add(new Line(commit.Parents[j], isMerged, colorIdx, position)); colorIdx++; } } @@ -233,7 +232,7 @@ namespace SourceGit.Helpers { // 处理已终止的线 foreach (var l in ended) { l.AddPoint(position.X, position.Y, true); - maker.Lines.Add(l); + data.Lines.Add(l); unsolved.Remove(l); } @@ -254,12 +253,12 @@ namespace SourceGit.Helpers { if (path.Points.Count == 1 && path.Points[0].Y == endY) continue; path.AddPoint((i + 0.5) * UNIT_WIDTH, endY, true); - maker.Lines.Add(path); + data.Lines.Add(path); } unsolved.Clear(); - maker.Lines.Sort((l, h) => l.Points[0].Y.CompareTo(h.Points[0].Y)); - return maker; + data.Lines.Sort((l, h) => l.Points[0].Y.CompareTo(h.Points[0].Y)); + return data; } } @@ -268,7 +267,7 @@ namespace SourceGit.Helpers { /// public class CommitGraph : FrameworkElement { private double offsetY; - private CommitGraphMaker maker; + private CommitGraphData data; public CommitGraph() { Clear(); @@ -276,21 +275,21 @@ namespace SourceGit.Helpers { public void Clear() { offsetY = 0; - maker = null; + data = null; } public void SetCommits(List commits) { - maker = CommitGraphMaker.Parse(commits); + data = CommitGraphData.Parse(commits); Dispatcher.Invoke(() => InvalidateVisual()); } public void SetOffset(double y) { - offsetY = y * CommitGraphMaker.UNIT_HEIGHT; + offsetY = y * CommitGraphData.UNIT_HEIGHT; InvalidateVisual(); } protected override void OnRender(DrawingContext dc) { - if (maker == null) return; + if (data == null) return; var startY = offsetY; var endY = offsetY + ActualHeight; @@ -298,12 +297,12 @@ namespace SourceGit.Helpers { dc.PushTransform(new TranslateTransform(0, -offsetY)); // Draw all visible lines. - foreach (var path in maker.Lines) { + foreach (var path in data.Lines) { var last = path.Points[0]; var size = path.Points.Count; - if (last.Y > endY) break; if (path.Points[size - 1].Y < startY) continue; + if (last.Y > endY) break; var geo = new StreamGeometry(); var pen = new Pen(path.Brush, 2); @@ -325,7 +324,7 @@ namespace SourceGit.Helpers { geoCtx.QuadraticBezierTo(new Point(cur.X, last.Y), cur, true, false); } else if (cur.X < last.X) { if (i < size - 1) { - cur.Y += CommitGraphMaker.HALF_HEIGHT; + cur.Y += CommitGraphData.HALF_HEIGHT; var midY = (last.Y + cur.Y) / 2; var midX = (last.X + cur.X) / 2; @@ -351,9 +350,9 @@ namespace SourceGit.Helpers { } // Draw short links - foreach (var link in maker.Links) { - if (link.Start.Y > endY) break; + foreach (var link in data.Links) { if (link.End.Y < startY) continue; + if (link.Start.Y > endY) break; var geo = new StreamGeometry(); var pen = new Pen(link.Brush, 2); @@ -368,9 +367,9 @@ namespace SourceGit.Helpers { } // Draw visible points - foreach (var dot in maker.Dots) { - if (dot.Center.Y > endY) break; + foreach (var dot in data.Dots) { if (dot.Center.Y < startY) continue; + if (dot.Center.Y > endY) break; dc.DrawEllipse(dot.Color, null, dot.Center, 3, 3); } diff --git a/src/UI/Histories.xaml b/src/UI/Histories.xaml index 2719c7c9..9c027f10 100644 --- a/src/UI/Histories.xaml +++ b/src/UI/Histories.xaml @@ -53,7 +53,7 @@