Skip to content

Commit 8600ff4

Browse files
committed
refactor: only redraw commit graph when it is necessary
Signed-off-by: leo <[email protected]>
1 parent 09fadea commit 8600ff4

File tree

4 files changed

+83
-45
lines changed

4 files changed

+83
-45
lines changed

src/Models/CommitGraph.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
namespace SourceGit.Models
88
{
9+
public record CommitGraphLayout(double startY, double clipWidth, double rowHeight)
10+
{
11+
public double StartY { get; set; } = startY;
12+
public double ClipWidth { get; set; } = clipWidth;
13+
public double RowHeight { get; set; } = rowHeight;
14+
}
15+
916
public class CommitGraph
1017
{
1118
public static List<Pen> Pens { get; } = [];

src/Views/CommitGraph.cs

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using Avalonia;
22
using Avalonia.Controls;
3-
using Avalonia.Controls.Primitives;
43
using Avalonia.Media;
5-
using Avalonia.VisualTree;
64

75
namespace SourceGit.Views
86
{
@@ -35,57 +33,39 @@ public bool OnlyHighlightCurrentBranch
3533
set => SetValue(OnlyHighlightCurrentBranchProperty, value);
3634
}
3735

36+
public static readonly StyledProperty<Models.CommitGraphLayout> LayoutProperty =
37+
AvaloniaProperty.Register<CommitGraph, Models.CommitGraphLayout>(nameof(Layout));
38+
39+
public Models.CommitGraphLayout Layout
40+
{
41+
get => GetValue(LayoutProperty);
42+
set => SetValue(LayoutProperty, value);
43+
}
44+
3845
static CommitGraph()
3946
{
40-
AffectsRender<CommitGraph>(GraphProperty, DotBrushProperty, OnlyHighlightCurrentBranchProperty);
47+
AffectsRender<CommitGraph>(
48+
GraphProperty,
49+
DotBrushProperty,
50+
OnlyHighlightCurrentBranchProperty,
51+
LayoutProperty);
4152
}
4253

4354
public override void Render(DrawingContext context)
4455
{
4556
base.Render(context);
4657

47-
var graph = Graph;
48-
if (graph == null)
49-
return;
50-
51-
var histories = this.FindAncestorOfType<Histories>();
52-
if (histories == null)
53-
return;
54-
55-
var grid = histories.CommitListContainer;
56-
if (grid == null)
57-
return;
58-
59-
var rowsPresenter = grid.FindDescendantOfType<DataGridRowsPresenter>();
60-
if (rowsPresenter == null)
58+
if (Graph is not { } graph || Layout is not { } layout)
6159
return;
6260

63-
double rowHeight = grid.RowHeight;
64-
double startY = 0;
65-
foreach (var child in rowsPresenter.Children)
66-
{
67-
var row = child as DataGridRow;
68-
if (row.IsVisible)
69-
{
70-
if (rowHeight != row.Bounds.Height)
71-
rowHeight = row.Bounds.Height;
72-
73-
if (row.Bounds.Top <= 0 && row.Bounds.Top > -rowHeight)
74-
{
75-
var test = rowHeight * row.Index - row.Bounds.Top;
76-
if (startY < test)
77-
startY = test;
78-
}
79-
}
80-
}
81-
82-
var headersHeight = grid.ColumnHeaderHeight;
83-
var width = histories.CommitListContainer.Columns[0].ActualWidth;
84-
var height = Bounds.Height - headersHeight;
85-
var endY = startY + height + 28;
61+
var startY = layout.StartY;
62+
var clipWidth = layout.ClipWidth;
63+
var clipHeight = Bounds.Height;
64+
var rowHeight = layout.RowHeight;
65+
var endY = startY + clipHeight + 28;
8666

87-
using (context.PushClip(new Rect(0, headersHeight, width, height)))
88-
using (context.PushTransform(Matrix.CreateTranslation(0, headersHeight - startY)))
67+
using (context.PushClip(new Rect(0, 0, clipWidth, clipHeight)))
68+
using (context.PushTransform(Matrix.CreateTranslation(0, -startY)))
8969
{
9070
DrawCurves(context, graph, startY, endY, rowHeight);
9171
DrawAnchors(context, graph, startY, endY, rowHeight);

src/Views/Histories.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@
226226
</DataGrid>
227227

228228
<v:CommitGraph x:Name="CommitGraph"
229+
Margin="0,24,0,0"
229230
Graph="{Binding Graph}"
230231
DotBrush="{DynamicResource Brush.Contents}"
231232
OnlyHighlightCurrentBranch="{Binding $parent[v:Histories].OnlyHighlightCurrentBranch}"

src/Views/Histories.axaml.cs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Avalonia;
55
using Avalonia.Collections;
66
using Avalonia.Controls;
7+
using Avalonia.Controls.Primitives;
78
using Avalonia.Input;
89
using Avalonia.Interactivity;
910
using Avalonia.VisualTree;
@@ -137,14 +138,59 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
137138

138139
private void OnCommitListLoaded(object sender, RoutedEventArgs e)
139140
{
140-
if (CommitListContainer is { SelectedItems.Count: 1 } dataGrid)
141+
var dataGrid = CommitListContainer;
142+
if (dataGrid.SelectedItems.Count == 1)
143+
{
141144
dataGrid.ScrollIntoView(dataGrid.SelectedItem, null);
145+
}
146+
else
147+
{
148+
var rowsPresenter = dataGrid.FindDescendantOfType<DataGridRowsPresenter>();
149+
if (rowsPresenter is { Children: { Count: > 0 } rows })
150+
CommitGraph.Layout = new(0, dataGrid.Columns[0].ActualWidth, rows[0].Bounds.Height);
151+
}
142152
}
143153

144154
private void OnCommitListLayoutUpdated(object _1, EventArgs _2)
145155
{
146-
if (IsLoaded)
147-
CommitGraph.InvalidateVisual();
156+
if (!IsLoaded)
157+
return;
158+
159+
var dataGrid = CommitListContainer;
160+
var rowsPresenter = dataGrid.FindDescendantOfType<DataGridRowsPresenter>();
161+
if (rowsPresenter == null)
162+
return;
163+
164+
double rowHeight = dataGrid.RowHeight;
165+
double startY = 0;
166+
foreach (var child in rowsPresenter.Children)
167+
{
168+
var row = child as DataGridRow;
169+
if (row.IsVisible)
170+
{
171+
if (rowHeight != row.Bounds.Height)
172+
rowHeight = row.Bounds.Height;
173+
174+
if (row.Bounds.Top <= 0 && row.Bounds.Top > -rowHeight)
175+
{
176+
var test = rowHeight * row.Index - row.Bounds.Top;
177+
if (startY < test)
178+
startY = test;
179+
}
180+
}
181+
}
182+
183+
var clipWidth = dataGrid.Columns[0].ActualWidth;
184+
if (_lastGraphStartY != startY ||
185+
_lastGraphClipWidth != clipWidth ||
186+
_lastGraphRowHeight != rowHeight)
187+
{
188+
_lastGraphStartY = startY;
189+
_lastGraphClipWidth = clipWidth;
190+
_lastGraphRowHeight = rowHeight;
191+
192+
CommitGraph.Layout = new(startY, clipWidth, rowHeight);
193+
}
148194
}
149195

150196
private void OnCommitListSelectionChanged(object _, SelectionChangedEventArgs e)
@@ -228,5 +274,9 @@ sender is DataGrid grid &&
228274
histories.CheckoutBranchByCommit(c);
229275
}
230276
}
277+
278+
private double _lastGraphStartY = 0;
279+
private double _lastGraphClipWidth = 0;
280+
private double _lastGraphRowHeight = 0;
231281
}
232282
}

0 commit comments

Comments
 (0)