Skip to content

Commit

Permalink
prevent multiple enumerations
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Miller committed Jan 18, 2024
1 parent 8821d48 commit 9ba2780
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Core/src/Core/Extensions/VisualTreeElementExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Maui.Graphics;
Expand Down Expand Up @@ -145,11 +145,18 @@ static List<IVisualTreeElement> GetVisualTreeElementsWindowsInternal(IVisualTree

if (uiElement != null)
{
var uniqueElements = findChildren(uiElement).Distinct();
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);
var uniqueElements = findChildren(uiElement).ToHashSet();

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()))
.ToList();

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));
visualElements.AddRange(testList.Cast<IVisualTreeElement>());
}

visualElements.Reverse();
Expand Down

0 comments on commit 9ba2780

Please sign in to comment.