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">
<Grid>
<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>
</Button>
</Grid>

View file

@ -28,7 +28,7 @@
<Button Classes="caption_button_macos" Click="CloseWindow">
<Grid>
<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>
</Button>
</Grid>

View file

@ -12,19 +12,19 @@
<Button Classes="caption_button_macos" Click="CloseWindow" Margin="10,0,0,0">
<Grid>
<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>
</Button>
<Button Classes="caption_button_macos" Click="MinimizeWindow">
<Grid>
<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>
</Button>
<Button Classes="caption_button_macos" Click="MaximizeOrRestoreWindow">
<Grid>
<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>
</Button>
</StackPanel>

View file

@ -13,22 +13,22 @@
UseHorizontal="{Binding Source={x:Static vm:Preference.Instance}, Path=UseTwoColumnsLayoutInHistories}">
<Grid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3">
<DataGrid x:Name="commitDataGrid"
Background="{DynamicResource Brush.Contents}"
ItemsSource="{Binding Commits}"
SelectionMode="Extended"
SelectedItem="{Binding AutoSelectedCommit, Mode=OneWay}"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserSortColumns="False"
IsReadOnly="True"
HeadersVisibility="None"
Focusable="False"
RowHeight="{Binding DataGridRowHeight}"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto"
DisplayRegionChanged="OnCommitDataGridDisplayRegionChanged"
SelectionChanged="OnCommitDataGridSelectionChanged"
ContextRequested="OnCommitDataGridContextRequested">
Background="{DynamicResource Brush.Contents}"
ItemsSource="{Binding Commits}"
SelectionMode="Extended"
SelectedItem="{Binding AutoSelectedCommit, Mode=OneWay}"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserSortColumns="False"
IsReadOnly="True"
HeadersVisibility="None"
Focusable="False"
RowHeight="{Binding DataGridRowHeight}"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto"
LayoutUpdated="OnCommitDataGridLayoutUpdated"
SelectionChanged="OnCommitDataGridSelectionChanged"
ContextRequested="OnCommitDataGridContextRequested">
<DataGrid.Columns>
<DataGridTemplateColumn Width="*" Header="GRAPH">
<DataGridTemplateColumn.CellTemplate>
@ -133,8 +133,8 @@
<GridSplitter Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3"
MinWidth="1" MinHeight="1"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Background="{DynamicResource Brush.Border0}"/>
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Background="{DynamicResource Brush.Border0}"/>
<Grid Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3">
<Border Background="{DynamicResource Brush.Window}">

View file

@ -1,7 +1,9 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.VisualTree;
using System;
namespace SourceGit.Views {
@ -77,7 +79,7 @@ namespace SourceGit.Views {
}
static CommitGraph() {
AffectsMeasure<CommitGraph>(BindingDataGridProperty, GraphProperty);
AffectsRender<CommitGraph>(BindingDataGridProperty, GraphProperty);
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) {
@ -91,16 +93,31 @@ namespace SourceGit.Views {
public override void Render(DrawingContext 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.
var offset = BindingDataGrid.GetDisplayOffset();
context.PushClip(new Rect(Bounds.Left, Bounds.Top, BindingDataGrid.Columns[0].ActualWidth, Bounds.Height));
context.PushTransform(Matrix.CreateTranslation(0, -offset.Y));
context.PushClip(new Rect(Bounds.Left, Bounds.Top, grid.Columns[0].ActualWidth, Bounds.Height));
context.PushTransform(Matrix.CreateTranslation(0, -startY));
// Calculate bounds.
var top = offset.Y;
var bottom = offset.Y + BindingDataGrid.Bounds.Height + BindingDataGrid.RowHeight * 2;
var top = startY;
var bottom = startY + grid.Bounds.Height + rowHeight * 2;
// Draw all curves
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) {
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 > bottom) break;
@ -196,8 +213,8 @@ namespace SourceGit.Views {
GC.Collect();
}
private void OnCommitDataGridDisplayRegionChanged(object sender, RoutedEventArgs e) {
commitGraph.InvalidateMeasure();
private void OnCommitDataGridLayoutUpdated(object sender, EventArgs e) {
commitGraph.InvalidateVisual();
}
private void OnCommitDataGridSelectionChanged(object sender, SelectionChangedEventArgs e) {

View file

@ -34,7 +34,7 @@
<Button Classes="caption_button_macos" Click="CloseWindow">
<Grid>
<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>
</Button>
</Grid>