From 6c19e729a30de1b0fca548f2c4bab3a6eeeab98d Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sat, 30 May 2020 09:53:11 +0200 Subject: [PATCH] FIx bringing an item into view. --- .../Repeater/ItemsRepeater.cs | 6 +++ .../Repeater/ViewportManager.cs | 40 ++++++++++++++----- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/Avalonia.Controls/Repeater/ItemsRepeater.cs b/src/Avalonia.Controls/Repeater/ItemsRepeater.cs index 2c870b4efd..0ba3c3c50e 100644 --- a/src/Avalonia.Controls/Repeater/ItemsRepeater.cs +++ b/src/Avalonia.Controls/Repeater/ItemsRepeater.cs @@ -80,6 +80,7 @@ namespace Avalonia.Controls static ItemsRepeater() { ClipToBoundsProperty.OverrideDefaultValue(true); + RequestBringIntoViewEvent.AddClassHandler((x, e) => x.OnRequestBringIntoView(e)); } /// @@ -743,6 +744,11 @@ namespace Avalonia.Controls } } + private void OnRequestBringIntoView(RequestBringIntoViewEventArgs e) + { + _viewportManager.OnBringIntoViewRequested(e); + } + private void InvalidateMeasureForLayout(object sender, EventArgs e) => InvalidateMeasure(); private void InvalidateArrangeForLayout(object sender, EventArgs e) => InvalidateArrange(); diff --git a/src/Avalonia.Controls/Repeater/ViewportManager.cs b/src/Avalonia.Controls/Repeater/ViewportManager.cs index 6f0e792aa5..c62d447c95 100644 --- a/src/Avalonia.Controls/Repeater/ViewportManager.cs +++ b/src/Avalonia.Controls/Repeater/ViewportManager.cs @@ -306,8 +306,11 @@ namespace Avalonia.Controls public void OnMakeAnchor(IControl anchor, bool isAnchorOutsideRealizedRange) { - _makeAnchorElement = anchor; - _isAnchorOutsideRealizedRange = isAnchorOutsideRealizedRange; + if (_makeAnchorElement != anchor) + { + _makeAnchorElement = anchor; + _isAnchorOutsideRealizedRange = isAnchorOutsideRealizedRange; + } } public void OnBringIntoViewRequested(RequestBringIntoViewEventArgs args) @@ -334,13 +337,12 @@ namespace Avalonia.Controls ////} } - // Register to rendering event to go back to how things were before where any child can be the anchor. - _isBringIntoViewInProgress = true; - ////if (!m_renderingToken) - ////{ - //// winrt::Windows::UI::Xaml::Media::CompositionTarget compositionTarget{ nullptr }; - //// m_renderingToken = compositionTarget.Rendering(winrt::auto_revoke, { this, &ViewportManagerWithPlatformFeatures::OnCompositionTargetRendering }); - ////} + // Register action to go back to how things were before where any child can be the anchor. + if (!_isBringIntoViewInProgress) + { + _isBringIntoViewInProgress = true; + Dispatcher.UIThread.Post(OnCompositionTargetRendering); + } } } @@ -362,6 +364,26 @@ namespace Avalonia.Controls return targetChild; } + private void OnCompositionTargetRendering() + { + _isBringIntoViewInProgress = false; + _makeAnchorElement = null; + + // Now that the item has been brought into view, we can let the anchor provider pick a new anchor. + ////foreach (var child in _owner.Children) + ////{ + //// if (!child.CanBeScrollAnchor) + //// { + //// var info = ItemsRepeater.GetVirtualizationInfo(child); + + //// if (info.IsRealized && info.IsHeldByLayout) + //// { + //// child.CanBeScrollAnchor = true; + //// } + //// } + ////} + } + public void ResetScrollers() { _scroller = null;