Skip to content

Commit 4b5d65c

Browse files
committed
refactor: rewrite Models.CommitGraph.PathHelper (#528)
1 parent fe015f9 commit 4b5d65c

File tree

1 file changed

+66
-25
lines changed

1 file changed

+66
-25
lines changed

src/Models/CommitGraph.cs

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class PathHelper
2020
public bool IsMerged;
2121
public double LastX;
2222
public double LastY;
23-
public double EndY;
2423
public Path Path;
2524

2625
public PathHelper(string next, bool isMerged, int color, Point start)
@@ -29,7 +28,6 @@ public PathHelper(string next, bool isMerged, int color, Point start)
2928
IsMerged = isMerged;
3029
LastX = start.X;
3130
LastY = start.Y;
32-
EndY = LastY;
3331

3432
Path = new Path();
3533
Path.Color = color;
@@ -42,51 +40,94 @@ public PathHelper(string next, bool isMerged, int color, Point start, Point to)
4240
IsMerged = isMerged;
4341
LastX = to.X;
4442
LastY = to.Y;
45-
EndY = LastY;
4643

4744
Path = new Path();
4845
Path.Color = color;
4946
Path.Points.Add(start);
5047
Path.Points.Add(to);
5148
}
5249

53-
public void Add(double x, double y, double halfHeight, bool isEnd = false)
50+
/// <summary>
51+
/// A path that just passed this row.
52+
/// </summary>
53+
/// <param name="x"></param>
54+
/// <param name="y"></param>
55+
/// <param name="halfHeight"></param>
56+
public void Pass(double x, double y, double halfHeight)
5457
{
5558
if (x > LastX)
5659
{
57-
Add(new Point(LastX, LastY));
58-
Add(new Point(x, y - halfHeight));
59-
if (isEnd)
60-
Add(new Point(x, y));
60+
Add(LastX, LastY);
61+
Add(x, y - halfHeight);
6162
}
6263
else if (x < LastX)
6364
{
64-
var testY = LastY + halfHeight;
65-
if (y > testY)
66-
Add(new Point(LastX, testY));
65+
Add(LastX, y - halfHeight);
66+
y += halfHeight;
67+
Add(x, y);
68+
}
6769

68-
if (!isEnd)
69-
y += halfHeight;
70+
LastX = x;
71+
LastY = y;
72+
}
7073

71-
Add(new Point(x, y));
74+
/// <summary>
75+
/// A path that has commit in this row but not ended
76+
/// </summary>
77+
/// <param name="x"></param>
78+
/// <param name="y"></param>
79+
/// <param name="halfHeight"></param>
80+
public void Goto(double x, double y, double halfHeight)
81+
{
82+
if (x > LastX)
83+
{
84+
Add(LastX, LastY);
85+
Add(x, y - halfHeight);
7286
}
73-
else if (isEnd)
87+
else if (x < LastX)
7488
{
75-
Add(new Point(x, y));
89+
Add(LastX, y - halfHeight);
90+
Add(x, y);
7691
}
7792

7893
LastX = x;
7994
LastY = y;
8095
}
8196

82-
private void Add(Point p)
97+
/// <summary>
98+
/// A path that has commit in this row and end.
99+
/// </summary>
100+
/// <param name="x"></param>
101+
/// <param name="y"></param>
102+
/// <param name="halfHeight"></param>
103+
public void End(double x, double y, double halfHeight)
104+
{
105+
if (x > LastX)
106+
{
107+
Add(LastX, LastY);
108+
Add(x, y - halfHeight);
109+
}
110+
else if (x < LastX)
111+
{
112+
Add(LastX, y - halfHeight);
113+
}
114+
115+
Add(x, y);
116+
117+
LastX = x;
118+
LastY = y;
119+
}
120+
121+
private void Add(double x, double y)
83122
{
84-
if (EndY < p.Y)
123+
if (_endY < y)
85124
{
86-
Path.Points.Add(p);
87-
EndY = p.Y;
125+
Path.Points.Add(new Point(x, y));
126+
_endY = y;
88127
}
89128
}
129+
130+
private double _endY = 0;
90131
}
91132

92133
public class Link
@@ -173,17 +214,17 @@ public static CommitGraph Parse(List<Commit> commits, bool firstParentOnlyEnable
173214
if (commit.Parents.Count > 0)
174215
{
175216
major.Next = commit.Parents[0];
176-
major.Add(offsetX, offsetY, HALF_HEIGHT);
217+
major.Goto(offsetX, offsetY, HALF_HEIGHT);
177218
}
178219
else
179220
{
180-
major.Add(offsetX, offsetY, HALF_HEIGHT, true);
221+
major.End(offsetX, offsetY, HALF_HEIGHT);
181222
ended.Add(l);
182223
}
183224
}
184225
else
185226
{
186-
l.Add(major.LastX, offsetY, HALF_HEIGHT, true);
227+
l.End(major.LastX, offsetY, HALF_HEIGHT);
187228
ended.Add(l);
188229
}
189230

@@ -193,7 +234,7 @@ public static CommitGraph Parse(List<Commit> commits, bool firstParentOnlyEnable
193234
else
194235
{
195236
offsetX += UNIT_WIDTH;
196-
l.Add(offsetX, offsetY, HALF_HEIGHT);
237+
l.Pass(offsetX, offsetY, HALF_HEIGHT);
197238
}
198239
}
199240

@@ -278,7 +319,7 @@ public static CommitGraph Parse(List<Commit> commits, bool firstParentOnlyEnable
278319
if (path.Path.Points.Count == 1 && Math.Abs(path.Path.Points[0].Y - endY) < 0.0001)
279320
continue;
280321

281-
path.Add((i + 0.5) * UNIT_WIDTH + H_MARGIN, endY + HALF_HEIGHT, HALF_HEIGHT, true);
322+
path.End((i + 0.5) * UNIT_WIDTH + H_MARGIN, endY + HALF_HEIGHT, HALF_HEIGHT);
282323
}
283324
unsolved.Clear();
284325

0 commit comments

Comments
 (0)