Skip to content

Commit

Permalink
Filter GetVisualTreeDescendants to not check elements with null handl…
Browse files Browse the repository at this point in the history
…ers (#18539)

* Filter GetVisualTreeDescendants to not check elements with null handlers

* Add border

* Update test

* Add Handler
  • Loading branch information
drasticactions committed Nov 7, 2023
1 parent 06ae915 commit bb90908
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Expand Up @@ -9,6 +9,8 @@
using System.Collections.Generic;
using ContentView = Microsoft.Maui.Controls.ContentView;
using Microsoft.Maui.Controls.Handlers.Items;
using Microsoft.Maui.Controls.Shapes;

#if ANDROID || IOS || MACCATALYST
using ShellHandler = Microsoft.Maui.Controls.Handlers.Compatibility.ShellRenderer;
#endif
Expand Down Expand Up @@ -37,6 +39,7 @@ void SetupBuilder()
handlers.AddHandler<NestingView, NestingViewHandler>();
handlers.AddHandler<ContentView, ContentViewHandler>();
handlers.AddHandler<CollectionView, CollectionViewHandler>();
handlers.AddHandler<Border, BorderHandler>();
});
});
}
Expand All @@ -45,11 +48,15 @@ void SetupBuilder()
public async Task GetVisualTreeElements()
{
SetupBuilder();

var border = new Border() { WidthRequest = 50, HeightRequest = 50, StrokeShape = new RoundRectangle() { CornerRadius = 5 } };
var label = new Label() { Text = "Find Me" };

var page = new ContentPage() { Title = "Title Page" };
page.Content = new VerticalStackLayout()
{
label
label,
border
};

var rootPage = await InvokeOnMainThreadAsync(() =>
Expand All @@ -58,6 +65,8 @@ public async Task GetVisualTreeElements()

await CreateHandlerAndAddToWindow<IWindowHandler>(rootPage, async handler =>
{
await OnFrameSetToNotEmpty(rootPage);
await OnFrameSetToNotEmpty(border);
await OnFrameSetToNotEmpty(label);
var locationOnScreen = label.GetLocationOnScreen().Value;
var labelFrame = label.Frame;
Expand Down
4 changes: 2 additions & 2 deletions src/Core/src/Core/Extensions/VisualTreeElementExtensions.cs
Expand Up @@ -146,7 +146,7 @@ static List<IVisualTreeElement> GetVisualTreeElementsWindowsInternal(IVisualTree
if (uiElement != null)
{
var uniqueElements = findChildren(uiElement).Distinct();
var viewTree = visualElement.GetVisualTreeDescendants().Where(n => n is IView).Select(n => new Tuple<IView, object?>((IView)n, ((IView)n).ToPlatform()));
var viewTree = visualElement.GetVisualTreeDescendants().Where(n => n is IView view && view.Handler is not null).Select(n => new Tuple<IView, object?>((IView)n, ((IView)n).ToPlatform()));
var testList = viewTree.Where(n => uniqueElements.Contains(n.Item2)).Select(n => n.Item1);
if (testList != null && testList.Any())
visualElements.AddRange(testList.Select(n => (IVisualTreeElement)n));
Expand Down Expand Up @@ -299,7 +299,7 @@ static List<IVisualTreeElement> GetVisualTreeElementsInternal(IVisualTreeElement

static void Impl(IVisualTreeElement visualElement, Predicate<Rect> intersectElementBounds, List<IVisualTreeElement> elements)
{
if (visualElement is IView view)
if (visualElement is IView view && view.Handler is not null)
{
Rect bounds = view.GetBoundingBox();
if (intersectElementBounds(bounds))
Expand Down

0 comments on commit bb90908

Please sign in to comment.