From ef352984f92e1eec2e8e29e98d73f9d8dcf3c46d Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 19 Feb 2024 15:30:10 +0800 Subject: [PATCH] refactor: remove DataGrid patch and use original API to get display offset --- src/Avalonia.Controls.DataGrid.patch | 81 -------------------------- src/Views/About.axaml | 2 +- src/Views/AssumeUnchangedManager.axaml | 2 +- src/Views/CaptionButtonsMacOS.axaml | 6 +- src/Views/Histories.axaml | 36 ++++++------ src/Views/Histories.axaml.cs | 37 ++++++++---- src/Views/Preference.axaml | 2 +- 7 files changed, 51 insertions(+), 115 deletions(-) delete mode 100644 src/Avalonia.Controls.DataGrid.patch diff --git a/src/Avalonia.Controls.DataGrid.patch b/src/Avalonia.Controls.DataGrid.patch deleted file mode 100644 index 415c537f..00000000 --- a/src/Avalonia.Controls.DataGrid.patch +++ /dev/null @@ -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 DisplayRegionChangedEvent = RoutedEvent.Register( -+ nameof(DisplayRegionChanged), -+ RoutingStrategies.Bubble); -+ -+ public event EventHandler DisplayRegionChanged -+ { -+ add => AddHandler(DisplayRegionChangedEvent, value); -+ remove => RemoveHandler(DisplayRegionChangedEvent, value); -+ } -+ - static DataGrid() - { - AffectsMeasure( -@@ -2428,6 +2438,11 @@ protected virtual void OnUnloadingRow(DataGridRowEventArgs e) - } - } - -+ protected virtual void OnDisplayRegionChanged() -+ { -+ RaiseEvent(new RoutedEventArgs(DisplayRegionChangedEvent)); -+ } -+ - /// - /// Comparator class so we can sort list by the display index - /// -@@ -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); -+ } - } - } diff --git a/src/Views/About.axaml b/src/Views/About.axaml index cb2090a0..7bf8d601 100644 --- a/src/Views/About.axaml +++ b/src/Views/About.axaml @@ -26,7 +26,7 @@ diff --git a/src/Views/AssumeUnchangedManager.axaml b/src/Views/AssumeUnchangedManager.axaml index 6e58abeb..3e535966 100644 --- a/src/Views/AssumeUnchangedManager.axaml +++ b/src/Views/AssumeUnchangedManager.axaml @@ -28,7 +28,7 @@ diff --git a/src/Views/CaptionButtonsMacOS.axaml b/src/Views/CaptionButtonsMacOS.axaml index cc9976e1..642c6262 100644 --- a/src/Views/CaptionButtonsMacOS.axaml +++ b/src/Views/CaptionButtonsMacOS.axaml @@ -12,19 +12,19 @@ diff --git a/src/Views/Histories.axaml b/src/Views/Histories.axaml index fa88baaf..7ab7d994 100644 --- a/src/Views/Histories.axaml +++ b/src/Views/Histories.axaml @@ -13,22 +13,22 @@ UseHorizontal="{Binding Source={x:Static vm:Preference.Instance}, Path=UseTwoColumnsLayoutInHistories}"> + 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"> @@ -133,8 +133,8 @@ + HorizontalAlignment="Stretch" VerticalAlignment="Stretch" + Background="{DynamicResource Brush.Border0}"/> diff --git a/src/Views/Histories.axaml.cs b/src/Views/Histories.axaml.cs index d77aecf0..0cb5d8fa 100644 --- a/src/Views/Histories.axaml.cs +++ b/src/Views/Histories.axaml.cs @@ -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(BindingDataGridProperty, GraphProperty); + AffectsRender(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(); + 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) { diff --git a/src/Views/Preference.axaml b/src/Views/Preference.axaml index 39869e74..8e711da8 100644 --- a/src/Views/Preference.axaml +++ b/src/Views/Preference.axaml @@ -34,7 +34,7 @@