This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathVisualTreeExtensions.cs
More file actions
71 lines (64 loc) · 2.78 KB
/
Copy pathVisualTreeExtensions.cs
File metadata and controls
71 lines (64 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Media;
namespace GitHub.UI
{
public static class VisualTreeExtensions
{
/// <summary>
/// Retrieves all the visual children of a framework element.
/// </summary>
/// <param name="parent">The parent framework element.</param>
/// <returns>The visual children of the framework element.</returns>
internal static IEnumerable<DependencyObject> GetVisualChildren(this DependencyObject parent)
{
Debug.Assert(parent != null, "The parent cannot be null.");
int childCount = VisualTreeHelper.GetChildrenCount(parent);
for (int counter = 0; counter < childCount; counter++)
{
yield return VisualTreeHelper.GetChild(parent, counter);
}
}
/// <summary>
/// Retrieves all the logical children of a framework element using a
/// breadth-first search. A visual element is assumed to be a logical
/// child of another visual element if they are in the same namescope.
/// For performance reasons this method manually manages the queue
/// instead of using recursion.
/// </summary>
/// <remarks>
/// License for this method.
///
/// (c) Copyright Microsoft Corporation.
/// This source is subject to the Microsoft Public License (Ms-PL).
/// Please see http://go.microsoft.com/fwlink/?LinkID=131993] for details.
/// All other rights reserved.
/// </remarks>
/// <param name="parent">The parent framework element.</param>
/// <returns>The logical children of the framework element.</returns>
internal static IEnumerable<FrameworkElement> GetLogicalChildrenBreadthFirst(this FrameworkElement parent)
{
Debug.Assert(parent != null, "The parent cannot be null.");
var queue = new Queue<FrameworkElement>(parent.GetVisualChildren().OfType<FrameworkElement>());
while (queue.Count > 0)
{
var element = queue.Dequeue();
yield return element;
foreach (var visualChild in element.GetVisualChildren().OfType<FrameworkElement>())
{
queue.Enqueue(visualChild);
}
}
}
internal static Window GetActiveWindow(this Application application)
{
var windows = application.Windows;
if (windows.Count == 0) return null;
return windows.Count == 1
? windows[0] // Optimization. I think this is the common case for us.
: windows.Cast<Window>().FirstOrDefault(x => x.IsActive);
}
}
}