From 1a8f2f7f354084f130bcad5d7e393b4434e34fc4 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Fri, 18 Dec 2020 20:56:58 -0500 Subject: [PATCH] Add tapped event args --- src/Avalonia.Input/Gestures.cs | 20 +++++---- src/Avalonia.Input/InputElement.cs | 8 ++-- src/Avalonia.Input/TappedGestureEventArgs.cs | 44 ++++++++++++++++++++ 3 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 src/Avalonia.Input/TappedGestureEventArgs.cs diff --git a/src/Avalonia.Input/Gestures.cs b/src/Avalonia.Input/Gestures.cs index 1be2595ebe..8d7a02f12e 100644 --- a/src/Avalonia.Input/Gestures.cs +++ b/src/Avalonia.Input/Gestures.cs @@ -6,17 +6,17 @@ namespace Avalonia.Input { public static class Gestures { - public static readonly RoutedEvent TappedEvent = RoutedEvent.Register( + public static readonly RoutedEvent TappedEvent = RoutedEvent.Register( "Tapped", RoutingStrategies.Bubble, typeof(Gestures)); - public static readonly RoutedEvent DoubleTappedEvent = RoutedEvent.Register( + public static readonly RoutedEvent DoubleTappedEvent = RoutedEvent.Register( "DoubleTapped", RoutingStrategies.Bubble, typeof(Gestures)); - public static readonly RoutedEvent RightTappedEvent = RoutedEvent.Register( + public static readonly RoutedEvent RightTappedEvent = RoutedEvent.Register( "RightTapped", RoutingStrategies.Bubble, typeof(Gestures)); @@ -24,7 +24,7 @@ namespace Avalonia.Input public static readonly RoutedEvent ScrollGestureEvent = RoutedEvent.Register( "ScrollGesture", RoutingStrategies.Bubble, typeof(Gestures)); - + public static readonly RoutedEvent ScrollGestureEndedEvent = RoutedEvent.Register( "ScrollGestureEnded", RoutingStrategies.Bubble, typeof(Gestures)); @@ -89,7 +89,7 @@ namespace Avalonia.Input { if (s_lastPress.TryGetTarget(out var target) && target == e.Source) { - e.Source.RaiseEvent(new RoutedEventArgs(DoubleTappedEvent)); + e.Source.RaiseEvent(new DoubleTappedEventArgs(e)); } } } @@ -105,8 +105,14 @@ namespace Avalonia.Input { if (e.InitialPressMouseButton == MouseButton.Left || e.InitialPressMouseButton == MouseButton.Right) { - var et = e.InitialPressMouseButton != MouseButton.Right ? TappedEvent : RightTappedEvent; - e.Source.RaiseEvent(new RoutedEventArgs(et)); + if (e.InitialPressMouseButton != MouseButton.Right) + { + e.Source.RaiseEvent(new RightTappedEventArgs(e)); + } + else + { + e.Source.RaiseEvent(new TappedEventArgs(e)); + } } } } diff --git a/src/Avalonia.Input/InputElement.cs b/src/Avalonia.Input/InputElement.cs index f3996cea76..e0594b5ce6 100644 --- a/src/Avalonia.Input/InputElement.cs +++ b/src/Avalonia.Input/InputElement.cs @@ -176,12 +176,12 @@ namespace Avalonia.Input /// /// Defines the event. /// - public static readonly RoutedEvent TappedEvent = Gestures.TappedEvent; + public static readonly RoutedEvent TappedEvent = Gestures.TappedEvent; /// /// Defines the event. /// - public static readonly RoutedEvent DoubleTappedEvent = Gestures.DoubleTappedEvent; + public static readonly RoutedEvent DoubleTappedEvent = Gestures.DoubleTappedEvent; private bool _isEffectivelyEnabled = true; private bool _isFocused; @@ -346,7 +346,7 @@ namespace Avalonia.Input /// /// Occurs when a tap gesture occurs on the control. /// - public event EventHandler Tapped + public event EventHandler Tapped { add { AddHandler(TappedEvent, value); } remove { RemoveHandler(TappedEvent, value); } @@ -355,7 +355,7 @@ namespace Avalonia.Input /// /// Occurs when a double-tap gesture occurs on the control. /// - public event EventHandler DoubleTapped + public event EventHandler DoubleTapped { add { AddHandler(DoubleTappedEvent, value); } remove { RemoveHandler(DoubleTappedEvent, value); } diff --git a/src/Avalonia.Input/TappedGestureEventArgs.cs b/src/Avalonia.Input/TappedGestureEventArgs.cs new file mode 100644 index 0000000000..bed76f5abd --- /dev/null +++ b/src/Avalonia.Input/TappedGestureEventArgs.cs @@ -0,0 +1,44 @@ +using Avalonia.Interactivity; +using Avalonia.VisualTree; + +namespace Avalonia.Input +{ + public class DoubleTappedEventArgs : RoutedEventArgs + { + private readonly PointerEventArgs lastPointerEventArgs; + + public DoubleTappedEventArgs(PointerEventArgs lastPointerEventArgs) + : base(Gestures.DoubleTappedEvent) + { + this.lastPointerEventArgs = lastPointerEventArgs; + } + + public Point GetPosition(IVisual? relativeTo) => lastPointerEventArgs.GetPosition(relativeTo); + } + + public class TappedEventArgs : RoutedEventArgs + { + private readonly PointerEventArgs lastPointerEventArgs; + + public TappedEventArgs(PointerEventArgs lastPointerEventArgs) + : base(Gestures.DoubleTappedEvent) + { + this.lastPointerEventArgs = lastPointerEventArgs; + } + + public Point GetPosition(IVisual? relativeTo) => lastPointerEventArgs.GetPosition(relativeTo); + } + + public class RightTappedEventArgs : RoutedEventArgs + { + private readonly PointerEventArgs lastPointerEventArgs; + + public RightTappedEventArgs(PointerEventArgs lastPointerEventArgs) + : base(Gestures.DoubleTappedEvent) + { + this.lastPointerEventArgs = lastPointerEventArgs; + } + + public Point GetPosition(IVisual? relativeTo) => lastPointerEventArgs.GetPosition(relativeTo); + } +}