Skip to content

Commit 832fcd7

Browse files
committed
fix: offset of commit graph does not look quite right (#1287)
This is because that when using `VirtualizingStackPanel`, the `Bounds.Height` of `ListBoxItem` may not be the same with its `Height` setted in axaml. Signed-off-by: leo <[email protected]>
1 parent 6df38ad commit 832fcd7

File tree

3 files changed

+52
-30
lines changed

3 files changed

+52
-30
lines changed

src/Models/CommitGraph.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public static CommitGraph Parse(List<Commit> commits, bool firstParentOnlyEnable
6464
{
6565
const double unitWidth = 12;
6666
const double halfWidth = 6;
67-
const double unitHeight = 28;
68-
const double halfHeight = 14;
67+
const double unitHeight = 1;
68+
const double halfHeight = 0.5;
6969

7070
var temp = new CommitGraph();
7171
var unsolved = new List<PathHelper>();

src/Views/CommitGraph.cs

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,33 @@ public override void Render(DrawingContext context)
5656
return;
5757

5858
// Calculate drawing area.
59-
double width = Bounds.Width - 273 - histories.AuthorNameColumnWidth.Value;
60-
double height = Bounds.Height;
61-
double startY = list.Scroll?.Offset.Y ?? 0;
62-
double endY = startY + height + 28;
59+
var width = Bounds.Width - 273 - histories.AuthorNameColumnWidth.Value;
60+
var height = Bounds.Height;
61+
62+
// Calculate row height
63+
var container = list.ItemsPanelRoot as VirtualizingStackPanel;
64+
if (container == null)
65+
return;
66+
67+
var item = list.ContainerFromIndex(container.FirstRealizedIndex);
68+
if (item == null)
69+
return;
70+
71+
var rowHeight = item.Bounds.Height;
72+
var startY = container.FirstRealizedIndex * rowHeight - item.TranslatePoint(new Point(0, 0), list).Value!.Y;
73+
var endY = startY + height + 28;
6374

6475
// Apply scroll offset and clip.
6576
using (context.PushClip(new Rect(0, 0, width, height)))
6677
using (context.PushTransform(Matrix.CreateTranslation(0, -startY)))
6778
{
6879
// Draw contents
69-
DrawCurves(context, graph, startY, endY);
70-
DrawAnchors(context, graph, startY, endY);
80+
DrawCurves(context, graph, startY, endY, rowHeight);
81+
DrawAnchors(context, graph, startY, endY, rowHeight);
7182
}
7283
}
7384

74-
private void DrawCurves(DrawingContext context, Models.CommitGraph graph, double top, double bottom)
85+
private void DrawCurves(DrawingContext context, Models.CommitGraph graph, double top, double bottom, double rowHeight)
7586
{
7687
var grayedPen = new Pen(new SolidColorBrush(Colors.Gray, 0.4), Models.CommitGraph.Pens[0].Thickness);
7788
var onlyHighlightCurrentBranch = OnlyHighlightCurrentBranch;
@@ -82,16 +93,20 @@ private void DrawCurves(DrawingContext context, Models.CommitGraph graph, double
8293
{
8394
if (link.IsMerged)
8495
continue;
85-
if (link.End.Y < top)
96+
97+
var startY = link.Start.Y * rowHeight;
98+
var endY = link.End.Y * rowHeight;
99+
100+
if (endY < top)
86101
continue;
87-
if (link.Start.Y > bottom)
102+
if (startY > bottom)
88103
break;
89104

90105
var geo = new StreamGeometry();
91106
using (var ctx = geo.Open())
92107
{
93-
ctx.BeginFigure(link.Start, false);
94-
ctx.QuadraticBezierTo(link.Control, link.End);
108+
ctx.BeginFigure(new Point(link.Start.X, startY), false);
109+
ctx.QuadraticBezierTo(new Point(link.Control.X, link.Control.Y * rowHeight), new Point(link.End.X, endY));
95110
}
96111

97112
context.DrawGeometry(null, grayedPen, geo);
@@ -100,10 +115,11 @@ private void DrawCurves(DrawingContext context, Models.CommitGraph graph, double
100115

101116
foreach (var line in graph.Paths)
102117
{
103-
var last = line.Points[0];
118+
var last = new Point(line.Points[0].X, line.Points[0].Y * rowHeight);
104119
var size = line.Points.Count;
120+
var endY = line.Points[size - 1].Y * rowHeight;
105121

106-
if (line.Points[size - 1].Y < top)
122+
if (endY < top)
107123
continue;
108124
if (last.Y > bottom)
109125
break;
@@ -117,7 +133,7 @@ private void DrawCurves(DrawingContext context, Models.CommitGraph graph, double
117133
var ended = false;
118134
for (int i = 1; i < size; i++)
119135
{
120-
var cur = line.Points[i];
136+
var cur = new Point(line.Points[i].X, line.Points[i].Y * rowHeight);
121137
if (cur.Y < top)
122138
{
123139
last = cur;
@@ -173,23 +189,27 @@ private void DrawCurves(DrawingContext context, Models.CommitGraph graph, double
173189
{
174190
if (onlyHighlightCurrentBranch && !link.IsMerged)
175191
continue;
176-
if (link.End.Y < top)
192+
193+
var startY = link.Start.Y * rowHeight;
194+
var endY = link.End.Y * rowHeight;
195+
196+
if (endY < top)
177197
continue;
178-
if (link.Start.Y > bottom)
198+
if (startY > bottom)
179199
break;
180200

181201
var geo = new StreamGeometry();
182202
using (var ctx = geo.Open())
183203
{
184-
ctx.BeginFigure(link.Start, false);
185-
ctx.QuadraticBezierTo(link.Control, link.End);
204+
ctx.BeginFigure(new Point(link.Start.X, startY), false);
205+
ctx.QuadraticBezierTo(new Point(link.Control.X, link.Control.Y * rowHeight), new Point(link.End.X, endY));
186206
}
187207

188208
context.DrawGeometry(null, Models.CommitGraph.Pens[link.Color], geo);
189209
}
190210
}
191211

192-
private void DrawAnchors(DrawingContext context, Models.CommitGraph graph, double top, double bottom)
212+
private void DrawAnchors(DrawingContext context, Models.CommitGraph graph, double top, double bottom, double rowHeight)
193213
{
194214
var dotFill = DotBrush;
195215
var dotFillPen = new Pen(dotFill, 2);
@@ -198,9 +218,11 @@ private void DrawAnchors(DrawingContext context, Models.CommitGraph graph, doubl
198218

199219
foreach (var dot in graph.Dots)
200220
{
201-
if (dot.Center.Y < top)
221+
var center = new Point(dot.Center.X, dot.Center.Y * rowHeight);
222+
223+
if (center.Y < top)
202224
continue;
203-
if (dot.Center.Y > bottom)
225+
if (center.Y > bottom)
204226
break;
205227

206228
var pen = Models.CommitGraph.Pens[dot.Color];
@@ -210,16 +232,16 @@ private void DrawAnchors(DrawingContext context, Models.CommitGraph graph, doubl
210232
switch (dot.Type)
211233
{
212234
case Models.CommitGraph.DotType.Head:
213-
context.DrawEllipse(dotFill, pen, dot.Center, 6, 6);
214-
context.DrawEllipse(pen.Brush, null, dot.Center, 3, 3);
235+
context.DrawEllipse(dotFill, pen, center, 6, 6);
236+
context.DrawEllipse(pen.Brush, null, center, 3, 3);
215237
break;
216238
case Models.CommitGraph.DotType.Merge:
217-
context.DrawEllipse(pen.Brush, null, dot.Center, 6, 6);
218-
context.DrawLine(dotFillPen, new Point(dot.Center.X, dot.Center.Y - 3), new Point(dot.Center.X, dot.Center.Y + 3));
219-
context.DrawLine(dotFillPen, new Point(dot.Center.X - 3, dot.Center.Y), new Point(dot.Center.X + 3, dot.Center.Y));
239+
context.DrawEllipse(pen.Brush, null, center, 6, 6);
240+
context.DrawLine(dotFillPen, new Point(center.X, center.Y - 3), new Point(center.X, center.Y + 3));
241+
context.DrawLine(dotFillPen, new Point(center.X - 3, center.Y), new Point(center.X + 3, center.Y));
220242
break;
221243
default:
222-
context.DrawEllipse(dotFill, pen, dot.Center, 3, 3);
244+
context.DrawEllipse(dotFill, pen, center, 3, 3);
223245
break;
224246
}
225247
}

src/Views/Histories.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
<Style Selector="ListBoxItem">
7777
<Setter Property="Margin" Value="0"/>
7878
<Setter Property="Padding" Value="0"/>
79-
<Setter Property="Height" Value="28"/>
79+
<Setter Property="Height" Value="26"/>
8080
<Setter Property="Template">
8181
<ControlTemplate>
8282
<Grid>

0 commit comments

Comments
 (0)