refactor<Histories>: remove DataGrid patch and use original API to get display offset

This commit is contained in:
leo 2024-02-19 15:30:10 +08:00
parent ba9f775bb8
commit ef352984f9
7 changed files with 51 additions and 115 deletions

View file

@ -1,81 +0,0 @@
diff --git a/src/Avalonia.Controls.DataGrid/DataGrid.cs b/src/Avalonia.Controls.DataGrid/DataGrid.cs
index e4573c3759a8a1eeece45c8bacb4fa853201f8e7..aa29066173e03092b61477985aed73beb08ec8fc 100644
--- a/src/Avalonia.Controls.DataGrid/DataGrid.cs
+++ b/src/Avalonia.Controls.DataGrid/DataGrid.cs
@@ -716,6 +716,16 @@ public DataGridRowDetailsVisibilityMode RowDetailsVisibilityMode
set { SetValue(RowDetailsVisibilityModeProperty, value); }
}
+ public static readonly RoutedEvent<RoutedEventArgs> DisplayRegionChangedEvent = RoutedEvent.Register<DataGrid, RoutedEventArgs>(
+ nameof(DisplayRegionChanged),
+ RoutingStrategies.Bubble);
+
+ public event EventHandler<RoutedEventArgs> DisplayRegionChanged
+ {
+ add => AddHandler(DisplayRegionChangedEvent, value);
+ remove => RemoveHandler(DisplayRegionChangedEvent, value);
+ }
+
static DataGrid()
{
AffectsMeasure<DataGrid>(
@@ -2428,6 +2438,11 @@ protected virtual void OnUnloadingRow(DataGridRowEventArgs e)
}
}
+ protected virtual void OnDisplayRegionChanged()
+ {
+ RaiseEvent(new RoutedEventArgs(DisplayRegionChangedEvent));
+ }
+
/// <summary>
/// Comparator class so we can sort list by the display index
/// </summary>
@@ -3879,6 +3894,7 @@ private void InvalidateColumnHeadersMeasure()
{
EnsureColumnHeadersVisibility();
_columnHeadersPresenter.InvalidateMeasure();
+ OnDisplayRegionChanged();
}
}
@@ -3903,6 +3919,8 @@ private void InvalidateRowsMeasure(bool invalidateIndividualElements)
element.InvalidateMeasure();
}
}
+
+ OnDisplayRegionChanged();
}
}
@@ -6211,5 +6229,30 @@ protected virtual void OnAutoGeneratingColumn(DataGridAutoGeneratingColumnEventA
{
AutoGeneratingColumn?.Invoke(this, e);
}
+
+ public Vector GetDisplayOffset()
+ {
+ // Has bug when using arrow keys via keyboard.
+ // return new Vector(_horizontalOffset, _verticalOffset);
+
+ double startX = 0;
+ double startY = 0;
+
+ foreach (var child in _rowsPresenter.Children)
+ {
+ var row = child as DataGridRow;
+ if (row.Slot >= 0 && row.Bounds.Top <= 0 && row.Bounds.Top > -RowHeight)
+ {
+ var testY = RowHeight * row.Index - row.Bounds.Top;
+ if (startY < testY)
+ {
+ startY = testY;
+ startX = row.Bounds.Left;
+ }
+ }
+ }
+
+ return new Vector(startX, startY);
+ }
}
}

View file

@ -26,7 +26,7 @@
<Button Classes="caption_button_macos" Click="CloseWindow"> <Button Classes="caption_button_macos" Click="CloseWindow">
<Grid> <Grid>
<Ellipse Fill="{DynamicResource Brush.MacOS.Close}"/> <Ellipse Fill="{DynamicResource Brush.MacOS.Close}"/>
<Path Height="7" Width="7" Stretch="Fill" Fill="#804040" Data="{StaticResource Icons.MacOS.Close}"/> <Path Height="6" Width="6" Stretch="Fill" Fill="#404040" Stroke="#404040" StrokeThickness="1" Data="{StaticResource Icons.Window.Close}"/>
</Grid> </Grid>
</Button> </Button>
</Grid> </Grid>

View file

@ -28,7 +28,7 @@
<Button Classes="caption_button_macos" Click="CloseWindow"> <Button Classes="caption_button_macos" Click="CloseWindow">
<Grid> <Grid>
<Ellipse Fill="{DynamicResource Brush.MacOS.Close}"/> <Ellipse Fill="{DynamicResource Brush.MacOS.Close}"/>
<Path Height="7" Width="7" Stretch="Fill" Fill="#804040" Data="{StaticResource Icons.MacOS.Close}"/> <Path Height="6" Width="6" Stretch="Fill" Fill="#404040" Stroke="#404040" StrokeThickness="1" Data="{StaticResource Icons.Window.Close}"/>
</Grid> </Grid>
</Button> </Button>
</Grid> </Grid>

View file

@ -12,19 +12,19 @@
<Button Classes="caption_button_macos" Click="CloseWindow" Margin="10,0,0,0"> <Button Classes="caption_button_macos" Click="CloseWindow" Margin="10,0,0,0">
<Grid> <Grid>
<Ellipse Fill="{DynamicResource Brush.MacOS.Close}"/> <Ellipse Fill="{DynamicResource Brush.MacOS.Close}"/>
<Path Height="7" Width="7" Stretch="Fill" Fill="#804040" Data="{StaticResource Icons.MacOS.Close}"/> <Path Height="6" Width="6" Stretch="Fill" Fill="#404040" Stroke="#404040" StrokeThickness="1" Data="{StaticResource Icons.Window.Close}"/>
</Grid> </Grid>
</Button> </Button>
<Button Classes="caption_button_macos" Click="MinimizeWindow"> <Button Classes="caption_button_macos" Click="MinimizeWindow">
<Grid> <Grid>
<Ellipse Fill="{DynamicResource Brush.MacOS.Minimize}"/> <Ellipse Fill="{DynamicResource Brush.MacOS.Minimize}"/>
<Path Height="2" Width="8" Stretch="Fill" Fill="#606060" Data="{StaticResource Icons.MacOS.Minimize}"/> <Path Height="2" Width="8" Stretch="Fill" Fill="#404040" Data="{StaticResource Icons.MacOS.Minimize}"/>
</Grid> </Grid>
</Button> </Button>
<Button Classes="caption_button_macos" Click="MaximizeOrRestoreWindow"> <Button Classes="caption_button_macos" Click="MaximizeOrRestoreWindow">
<Grid> <Grid>
<Ellipse Fill="{DynamicResource Brush.MacOS.Maximize}"/> <Ellipse Fill="{DynamicResource Brush.MacOS.Maximize}"/>
<Path Height="8" Width="8" Stretch="Fill" Fill="#606060" Data="{StaticResource Icons.MacOS.Maximize}}"/> <Path Height="6" Width="6" Stretch="Fill" Fill="#404040" Data="{StaticResource Icons.MacOS.Maximize}"/>
</Grid> </Grid>
</Button> </Button>
</StackPanel> </StackPanel>

View file

@ -26,7 +26,7 @@
RowHeight="{Binding DataGridRowHeight}" RowHeight="{Binding DataGridRowHeight}"
HorizontalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
DisplayRegionChanged="OnCommitDataGridDisplayRegionChanged" LayoutUpdated="OnCommitDataGridLayoutUpdated"
SelectionChanged="OnCommitDataGridSelectionChanged" SelectionChanged="OnCommitDataGridSelectionChanged"
ContextRequested="OnCommitDataGridContextRequested"> ContextRequested="OnCommitDataGridContextRequested">
<DataGrid.Columns> <DataGrid.Columns>

View file

@ -1,7 +1,9 @@
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.VisualTree;
using System; using System;
namespace SourceGit.Views { namespace SourceGit.Views {
@ -77,7 +79,7 @@ namespace SourceGit.Views {
} }
static CommitGraph() { static CommitGraph() {
AffectsMeasure<CommitGraph>(BindingDataGridProperty, GraphProperty); AffectsRender<CommitGraph>(BindingDataGridProperty, GraphProperty);
} }
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) {
@ -91,16 +93,31 @@ namespace SourceGit.Views {
public override void Render(DrawingContext context) { public override void Render(DrawingContext context) {
base.Render(context); base.Render(context);
if (Graph == null || BindingDataGrid == null) return; var graph = Graph;
var grid = BindingDataGrid;
if (graph == null || grid == null) return;
var rowsPresenter = grid.FindDescendantOfType<DataGridRowsPresenter>();
if (rowsPresenter == null) return;
// Find the content display offset Y of binding DataGrid.
double rowHeight = grid.RowHeight;
double startY = 0;
foreach (var child in rowsPresenter.Children) {
var row = child as DataGridRow;
if (row.IsVisible && row.Bounds.Top <= 0 && row.Bounds.Top > -rowHeight) {
var test = rowHeight * row.GetIndex() - row.Bounds.Top;
if (startY < test) startY = test;
}
}
// Apply scroll offset. // Apply scroll offset.
var offset = BindingDataGrid.GetDisplayOffset(); context.PushClip(new Rect(Bounds.Left, Bounds.Top, grid.Columns[0].ActualWidth, Bounds.Height));
context.PushClip(new Rect(Bounds.Left, Bounds.Top, BindingDataGrid.Columns[0].ActualWidth, Bounds.Height)); context.PushTransform(Matrix.CreateTranslation(0, -startY));
context.PushTransform(Matrix.CreateTranslation(0, -offset.Y));
// Calculate bounds. // Calculate bounds.
var top = offset.Y; var top = startY;
var bottom = offset.Y + BindingDataGrid.Bounds.Height + BindingDataGrid.RowHeight * 2; var bottom = startY + grid.Bounds.Height + rowHeight * 2;
// Draw all curves // Draw all curves
DrawCurves(context, top, bottom); DrawCurves(context, top, bottom);
@ -110,7 +127,7 @@ namespace SourceGit.Views {
if (App.Current.TryGetResource("Brush.Contents", App.Current.ActualThemeVariant, out object res) && res is SolidColorBrush) { if (App.Current.TryGetResource("Brush.Contents", App.Current.ActualThemeVariant, out object res) && res is SolidColorBrush) {
dotFill = res as SolidColorBrush; dotFill = res as SolidColorBrush;
} }
foreach (var dot in Graph.Dots) { foreach (var dot in graph.Dots) {
if (dot.Center.Y < top) continue; if (dot.Center.Y < top) continue;
if (dot.Center.Y > bottom) break; if (dot.Center.Y > bottom) break;
@ -196,8 +213,8 @@ namespace SourceGit.Views {
GC.Collect(); GC.Collect();
} }
private void OnCommitDataGridDisplayRegionChanged(object sender, RoutedEventArgs e) { private void OnCommitDataGridLayoutUpdated(object sender, EventArgs e) {
commitGraph.InvalidateMeasure(); commitGraph.InvalidateVisual();
} }
private void OnCommitDataGridSelectionChanged(object sender, SelectionChangedEventArgs e) { private void OnCommitDataGridSelectionChanged(object sender, SelectionChangedEventArgs e) {

View file

@ -34,7 +34,7 @@
<Button Classes="caption_button_macos" Click="CloseWindow"> <Button Classes="caption_button_macos" Click="CloseWindow">
<Grid> <Grid>
<Ellipse Fill="{DynamicResource Brush.MacOS.Close}"/> <Ellipse Fill="{DynamicResource Brush.MacOS.Close}"/>
<Path Height="7" Width="7" Stretch="Fill" Fill="#804040" Data="{StaticResource Icons.MacOS.Close}"/> <Path Height="6" Width="6" Stretch="Fill" Fill="#404040" Stroke="#404040" StrokeThickness="1" Data="{StaticResource Icons.Window.Close}"/>
</Grid> </Grid>
</Button> </Button>
</Grid> </Grid>