1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00
Artemis/src/Artemis.UI.Shared/Utilities/HitTestUtilities.cs
2021-09-04 20:52:03 +02:00

38 lines
1.4 KiB
C#

using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
namespace Artemis.UI.Shared
{
/// <summary>
/// Provides utilities for running hit tests on visual elements
/// </summary>
public static class HitTestUtilities
{
/// <summary>
/// Runs a hit test on children of the container within the rectangle matching all elements that have a data context of
/// T
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="container"></param>
/// <param name="rectangleGeometry"></param>
/// <returns></returns>
public static List<T> GetHitViewModels<T>(Visual container, RectangleGeometry rectangleGeometry)
{
List<T> result = new();
GeometryHitTestParameters hitTestParams = new(rectangleGeometry);
HitTestResultBehavior ResultCallback(HitTestResult r) => HitTestResultBehavior.Continue;
HitTestFilterBehavior FilterCallback(DependencyObject e)
{
if (e is FrameworkElement fe && fe.DataContext is T context && !result.Contains(context))
result.Add(context);
return HitTestFilterBehavior.Continue;
}
VisualTreeHelper.HitTest(container, FilterCallback, ResultCallback, hitTestParams);
return result;
}
}
}