using System;
using System.Collections.Generic;
using System.Linq;
using Avalonia.Media;
using Avalonia.VisualTree;
#nullable enable
namespace Avalonia.Input
{
///
/// Defines extensions for the interface.
///
public static class InputExtensions
{
private static readonly Func s_hitTestDelegate = IsHitTestVisible;
private static readonly Func s_hitTestEnabledOnlyDelegate = IsHitTestVisible_EnabledOnly;
///
/// Returns the active input elements at a point on an .
///
/// The element to test.
/// The point on .
/// Whether to only return elements for which is true.
///
/// The active input elements found at the point, ordered topmost first.
///
public static IEnumerable GetInputElementsAt(this IInputElement element, Point p, bool enabledElementsOnly = true)
{
element = element ?? throw new ArgumentNullException(nameof(element));
return (element as Visual)?.GetVisualsAt(p, enabledElementsOnly ? s_hitTestEnabledOnlyDelegate : s_hitTestDelegate).Cast() ??
Enumerable.Empty();
}
///
public static IEnumerable GetInputElementsAt(this IInputElement element, Point p) => GetInputElementsAt(element, p, true);
///
/// Returns the active input elements intersecting a geometry on an .
///
/// The element to test.
/// The geometry on .
/// Whether to only return elements for which is true.
///
/// The active input elements found intersecting the geometry, with intersection details, ordered topmost first.
///
public static IEnumerable GetInputElementsAt(this IInputElement element, Geometry geometry, bool enabledElementsOnly = true)
{
element = element ?? throw new ArgumentNullException(nameof(element));
return (element as Visual)?.GetVisualsAt(geometry, enabledElementsOnly ? s_hitTestEnabledOnlyDelegate : s_hitTestDelegate)
.Where(x => x.VisualHit is IInputElement)
.Select(x => new GeometryHitTestResult(x.VisualHit, x.IntersectionResult)) ??
Enumerable.Empty();
}
///
/// Returns the topmost active input element at a point on an .
///
/// The element to test.
/// The point on .
/// Whether to only return elements for which is true.
/// The topmost at the specified position.
public static IInputElement? InputHitTest(this IInputElement element, Point p, bool enabledElementsOnly = true)
{
element = element ?? throw new ArgumentNullException(nameof(element));
return (element as Visual)?.GetVisualAt(p, enabledElementsOnly ? s_hitTestEnabledOnlyDelegate : s_hitTestDelegate) as IInputElement;
}
///
public static IInputElement? InputHitTest(this IInputElement element, Point p) => InputHitTest(element, p, true);
///
/// Returns the topmost active input element intersecting a geometry on an .
///
/// The element to test.
/// The geometry on .
/// Whether to only return elements for which is true.
/// A containing the topmost intersecting the specified geometry and intersection details, or null if no element intersects.
public static GeometryHitTestResult? InputHitTest(this IInputElement element, Geometry geometry, bool enabledElementsOnly = true)
{
element = element ?? throw new ArgumentNullException(nameof(element));
var hitTest = (element as Visual)?.GetVisualAt(geometry, enabledElementsOnly ? s_hitTestEnabledOnlyDelegate : s_hitTestDelegate);
return hitTest?.VisualHit is IInputElement inputElement ? hitTest : null;
}
///
/// Returns the topmost active input element at a point on an .
///
/// The element to test.
/// The point on .
///
/// A filter predicate. If the predicate returns false then the visual and all its
/// children will be excluded from the results.
///
/// Whether to only return elements for which is true.
/// The topmost at the specified position.
public static IInputElement? InputHitTest(
this IInputElement element,
Point p,
Func filter,
bool enabledElementsOnly = true)
{
element = element ?? throw new ArgumentNullException(nameof(element));
filter = filter ?? throw new ArgumentNullException(nameof(filter));
var hitTestDelegate = enabledElementsOnly ? s_hitTestEnabledOnlyDelegate : s_hitTestDelegate;
return (element as Visual)?.GetVisualAt(p, x => hitTestDelegate(x) && filter(x)) as IInputElement;
}
///
public static IInputElement? InputHitTest(this IInputElement element, Point p, Func filter) => InputHitTest(element, p, filter, true);
///
/// Returns the topmost active input element intersecting a geometry on an .
///
/// The element to test.
/// The geometry on .
///
/// A filter predicate. If the predicate returns false then the visual and all its
/// children will be excluded from the results.
///
/// Whether to only return elements for which is true.
/// A containing the topmost intersecting the specified geometry and intersection details, or null if no element intersects.
public static IInputElement? InputHitTest(
this IInputElement element,
Geometry geometry,
Func filter,
bool enabledElementsOnly = true)
{
element = element ?? throw new ArgumentNullException(nameof(element));
filter = filter ?? throw new ArgumentNullException(nameof(filter));
var hitTestDelegate = enabledElementsOnly ? s_hitTestEnabledOnlyDelegate : s_hitTestDelegate;
return (element as Visual)?.GetVisualAt(geometry, x => hitTestDelegate(x) && filter(x)) as IInputElement;
}
private static bool IsHitTestVisible(Visual visual) => visual is { IsVisible: true, IsAttachedToVisualTree: true } and IInputElement { IsHitTestVisible: true };
private static bool IsHitTestVisible_EnabledOnly(Visual visual) => IsHitTestVisible(visual) && visual is IInputElement { IsEffectivelyEnabled: true };
}
}