From e36fbc6f22c2c92debd73b20930772704aa666b5 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 15 Jun 2020 10:37:56 +0200 Subject: [PATCH] More anchor validation checks. - Make sure an anchor is a descendent of the `ScrollContentPresenter` when added - Ignore controls that are no longer descendents of the `ScrollContentPresenter` when evaluating --- .../Presenters/ScrollContentPresenter.cs | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs b/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs index 11604992f1..327cfb7736 100644 --- a/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs +++ b/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs @@ -207,6 +207,12 @@ namespace Avalonia.Controls.Presenters /// void IScrollAnchorProvider.RegisterAnchorCandidate(IControl element) { + if (!this.IsVisualAncestorOf(element)) + { + throw new InvalidOperationException( + "An anchor control must be a visual descendent of the ScrollContentPresenter."); + } + _anchorCandidates ??= new List(); _anchorCandidates.Add(element); } @@ -531,14 +537,19 @@ namespace Avalonia.Controls.Presenters private bool GetViewportBounds(IControl element, out Rect bounds) { - // We want the bounds relative to the new Offset, regardless of whether the child - // control has actually been arranged to this offset yet, so translate first to the - // child control and then apply Offset rather than translating directly to this - // control. - var thisBounds = new Rect(Bounds.Size); - var childBounds = TranslateBounds(element, Child); - bounds = new Rect(childBounds.Position - Offset, childBounds.Size); - return bounds.Intersects(thisBounds); + if (TranslateBounds(element, Child, out var childBounds)) + { + // We want the bounds relative to the new Offset, regardless of whether the child + // control has actually been arranged to this offset yet, so translate first to the + // child control and then apply Offset rather than translating directly to this + // control. + var thisBounds = new Rect(Bounds.Size); + bounds = new Rect(childBounds.Position - Offset, childBounds.Size); + return bounds.Intersects(thisBounds); + } + + bounds = default; + return false; } private Rect TranslateBounds(IControl control, IControl to)