|
| 1 | +using Microsoft.UI.Xaml; |
| 2 | +using Microsoft.UI.Xaml.Media; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | + |
| 6 | +namespace WinUI.TableView.Extensions; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Provides extension methods for traversing the visual tree of <see cref="DependencyObject"/>. |
| 10 | +/// </summary> |
| 11 | +internal static class DependencyObjectExtensions |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Finds the first descendant of the specified type <typeparamref name="T"/> that matches the optional predicate. |
| 15 | + /// </summary> |
| 16 | + /// <typeparam name="T">The type of descendant to find.</typeparam> |
| 17 | + /// <param name="element">The root element to search from.</param> |
| 18 | + /// <param name="predicate">An optional predicate to filter descendants.</param> |
| 19 | + /// <returns>The first matching descendant, or <c>null</c> if none found.</returns> |
| 20 | + internal static T? FindDescendant<T>(this DependencyObject element, Func<T, bool>? predicate = default) where T : DependencyObject |
| 21 | + { |
| 22 | + foreach (var descendant in element.FindDescendants()) |
| 23 | + { |
| 24 | + if (descendant is T tDescendant && (predicate?.Invoke(tDescendant) ?? true)) |
| 25 | + { |
| 26 | + return tDescendant; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + return default; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Enumerates all descendants of the specified <see cref="DependencyObject"/> in the visual tree. |
| 35 | + /// </summary> |
| 36 | + /// <param name="element">The root element to enumerate from.</param> |
| 37 | + /// <returns>An enumerable of all descendant <see cref="DependencyObject"/>s.</returns> |
| 38 | + internal static IEnumerable<DependencyObject> FindDescendants(this DependencyObject element) |
| 39 | + { |
| 40 | + var childrenCount = VisualTreeHelper.GetChildrenCount(element); |
| 41 | + |
| 42 | + for (var i = 0; i < childrenCount; i++) |
| 43 | + { |
| 44 | + var child = VisualTreeHelper.GetChild(element, i); |
| 45 | + |
| 46 | + yield return child; |
| 47 | + |
| 48 | + foreach (var childOfChild in FindDescendants(child)) |
| 49 | + { |
| 50 | + yield return childOfChild; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Finds the first ascendant of the specified type <typeparamref name="T"/>. |
| 57 | + /// </summary> |
| 58 | + /// <typeparam name="T">The type of ascendant to find.</typeparam> |
| 59 | + /// <param name="element">The element to start searching from.</param> |
| 60 | + /// <returns>The first matching ascendant, or <c>null</c> if none found.</returns> |
| 61 | + internal static T? FindAscendant<T>(this DependencyObject element) where T : DependencyObject |
| 62 | + { |
| 63 | + foreach (var ascendant in element.FindAscendants()) |
| 64 | + { |
| 65 | + if (ascendant is T tAscendant) |
| 66 | + { |
| 67 | + return tAscendant; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return default; |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Enumerates all ascendants of the specified <see cref="DependencyObject"/> in the visual tree. |
| 76 | + /// </summary> |
| 77 | + /// <param name="element">The element to start enumerating from.</param> |
| 78 | + /// <returns>An enumerable of all ascendant <see cref="DependencyObject"/>s.</returns> |
| 79 | + internal static IEnumerable<DependencyObject> FindAscendants(this DependencyObject element) |
| 80 | + { |
| 81 | + while (true) |
| 82 | + { |
| 83 | + var parent = VisualTreeHelper.GetParent(element); |
| 84 | + |
| 85 | + if (parent is null) |
| 86 | + { |
| 87 | + yield break; |
| 88 | + } |
| 89 | + |
| 90 | + yield return parent; |
| 91 | + |
| 92 | + element = parent; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments