-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathCommitGraph.cs
More file actions
246 lines (208 loc) · 9.26 KB
/
CommitGraph.cs
File metadata and controls
246 lines (208 loc) · 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
namespace SourceGit.Views
{
public class CommitGraph : Control
{
public static readonly StyledProperty<Models.CommitGraph> GraphProperty =
AvaloniaProperty.Register<CommitGraph, Models.CommitGraph>(nameof(Graph));
public Models.CommitGraph Graph
{
get => GetValue(GraphProperty);
set => SetValue(GraphProperty, value);
}
public static readonly StyledProperty<IBrush> DotBrushProperty =
AvaloniaProperty.Register<CommitGraph, IBrush>(nameof(DotBrush), Brushes.Transparent);
public IBrush DotBrush
{
get => GetValue(DotBrushProperty);
set => SetValue(DotBrushProperty, value);
}
public static readonly StyledProperty<bool> OnlyHighlightCurrentBranchProperty =
AvaloniaProperty.Register<CommitGraph, bool>(nameof(OnlyHighlightCurrentBranch), true);
public bool OnlyHighlightCurrentBranch
{
get => GetValue(OnlyHighlightCurrentBranchProperty);
set => SetValue(OnlyHighlightCurrentBranchProperty, value);
}
public static readonly StyledProperty<Models.CommitGraphLayout> LayoutProperty =
AvaloniaProperty.Register<CommitGraph, Models.CommitGraphLayout>(nameof(Layout));
public Models.CommitGraphLayout Layout
{
get => GetValue(LayoutProperty);
set => SetValue(LayoutProperty, value);
}
static CommitGraph()
{
AffectsRender<CommitGraph>(
GraphProperty,
DotBrushProperty,
OnlyHighlightCurrentBranchProperty,
LayoutProperty);
}
public override void Render(DrawingContext context)
{
base.Render(context);
if (Graph is not { } graph || Layout is not { } layout)
return;
var startY = layout.StartY;
var clipWidth = layout.ClipWidth;
var clipHeight = Bounds.Height;
var rowHeight = layout.RowHeight;
var endY = startY + clipHeight + 28;
using (context.PushClip(new Rect(0, 0, clipWidth, clipHeight)))
using (context.PushTransform(Matrix.CreateTranslation(0, -startY)))
{
DrawCurves(context, graph, startY, endY, rowHeight);
DrawAnchors(context, graph, startY, endY, rowHeight);
}
}
private void DrawCurves(DrawingContext context, Models.CommitGraph graph, double top, double bottom, double rowHeight)
{
var grayedPen = new Pen(new SolidColorBrush(Colors.Gray, 0.4), Models.CommitGraph.Pens[0].Thickness);
var onlyHighlightCurrentBranch = OnlyHighlightCurrentBranch;
if (onlyHighlightCurrentBranch)
{
foreach (var link in graph.Links)
{
if (link.IsMerged)
continue;
var startY = link.Start.Y * rowHeight;
var endY = link.End.Y * rowHeight;
if (endY < top)
continue;
if (startY > bottom)
break;
var geo = new StreamGeometry();
using (var ctx = geo.Open())
{
ctx.BeginFigure(new Point(link.Start.X, startY), false);
ctx.QuadraticBezierTo(new Point(link.Control.X, link.Control.Y * rowHeight), new Point(link.End.X, endY));
}
context.DrawGeometry(null, grayedPen, geo);
}
}
foreach (var line in graph.Paths)
{
var last = new Point(line.Points[0].X, line.Points[0].Y * rowHeight);
var size = line.Points.Count;
var endY = line.Points[size - 1].Y * rowHeight;
if (endY < top)
continue;
if (last.Y > bottom)
break;
var geo = new StreamGeometry();
var pen = Models.CommitGraph.Pens[line.Color];
using (var ctx = geo.Open())
{
var started = false;
var ended = false;
for (int i = 1; i < size; i++)
{
var cur = new Point(line.Points[i].X, line.Points[i].Y * rowHeight);
if (cur.Y < top)
{
last = cur;
continue;
}
if (!started)
{
ctx.BeginFigure(last, false);
started = true;
}
if (cur.Y > bottom)
{
cur = new Point(cur.X, bottom);
ended = true;
}
if (cur.X > last.X)
{
ctx.QuadraticBezierTo(new Point(cur.X, last.Y), cur);
}
else if (cur.X < last.X)
{
if (i < size - 1)
{
var midY = (last.Y + cur.Y) / 2;
ctx.CubicBezierTo(new Point(last.X, midY + 4), new Point(cur.X, midY - 4), cur);
}
else
{
ctx.QuadraticBezierTo(new Point(last.X, cur.Y), cur);
}
}
else
{
ctx.LineTo(cur);
}
if (ended)
break;
last = cur;
}
}
if (!line.IsMerged && onlyHighlightCurrentBranch)
context.DrawGeometry(null, grayedPen, geo);
else
context.DrawGeometry(null, pen, geo);
}
foreach (var link in graph.Links)
{
if (onlyHighlightCurrentBranch && !link.IsMerged)
continue;
var startY = link.Start.Y * rowHeight;
var endY = link.End.Y * rowHeight;
if (endY < top)
continue;
if (startY > bottom)
break;
var geo = new StreamGeometry();
using (var ctx = geo.Open())
{
ctx.BeginFigure(new Point(link.Start.X, startY), false);
ctx.QuadraticBezierTo(new Point(link.Control.X, link.Control.Y * rowHeight), new Point(link.End.X, endY));
}
context.DrawGeometry(null, Models.CommitGraph.Pens[link.Color], geo);
}
}
private void DrawAnchors(DrawingContext context, Models.CommitGraph graph, double top, double bottom, double rowHeight)
{
var dotFill = DotBrush;
var dotFillPen = new Pen(dotFill, 2);
var grayedPen = new Pen(Brushes.Gray, Models.CommitGraph.Pens[0].Thickness);
var onlyHighlightCurrentBranch = OnlyHighlightCurrentBranch;
foreach (var dot in graph.Dots)
{
var center = new Point(dot.Center.X, dot.Center.Y * rowHeight);
if (center.Y < top)
continue;
if (center.Y > bottom)
break;
var pen = Models.CommitGraph.Pens[dot.Color];
if (!dot.IsMerged && onlyHighlightCurrentBranch)
pen = grayedPen;
switch (dot.Type)
{
case Models.CommitGraph.DotType.Head:
context.DrawEllipse(dotFill, pen, center, 6, 6);
context.DrawEllipse(pen.Brush, null, center, 3, 3);
break;
case Models.CommitGraph.DotType.Merge:
context.DrawEllipse(pen.Brush, null, center, 6, 6);
context.DrawLine(dotFillPen, new Point(center.X, center.Y - 3), new Point(center.X, center.Y + 3));
context.DrawLine(dotFillPen, new Point(center.X - 3, center.Y), new Point(center.X + 3, center.Y));
break;
case Models.CommitGraph.DotType.Filter:
context.DrawEllipse(pen.Brush, null, center, 7, 7);
context.DrawLine(dotFillPen, new Point(center.X, center.Y - 5), new Point(center.X, center.Y + 5));
context.DrawLine(dotFillPen, new Point(center.X - 4, center.Y - 3), new Point(center.X + 4, center.Y + 3));
context.DrawLine(dotFillPen, new Point(center.X + 4, center.Y - 3), new Point(center.X - 4, center.Y + 3));
break;
default:
context.DrawEllipse(dotFill, pen, center, 3, 3);
break;
}
}
}
}
}