|
| 1 | +namespace Sentry.Maui.Internal; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Detects and adds breadcrumbs for any gesture recognizers attached to the visual element |
| 5 | +/// </summary> |
| 6 | +public class MauiGestureRecognizerEventsBinder : IMauiElementEventBinder |
| 7 | +{ |
| 8 | + private static Action<BreadcrumbEvent>? _addBreadcrumb = null!; |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// Searches VisualElement for gesture recognizers to bind to |
| 12 | + /// </summary> |
| 13 | + public void Bind(VisualElement element, Action<BreadcrumbEvent> addBreadcrumb) |
| 14 | + { |
| 15 | + _addBreadcrumb ??= addBreadcrumb; // this is fine... it's the same callback for everyone and it never changes |
| 16 | + TryBind(element, true); |
| 17 | + } |
| 18 | + |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Searches VisualElement for gesture recognizers to unbind from |
| 22 | + /// </summary> |
| 23 | + /// <param name="element"></param> |
| 24 | + public void UnBind(VisualElement element) |
| 25 | + { |
| 26 | + _addBreadcrumb = null; |
| 27 | + TryBind(element, false); |
| 28 | + } |
| 29 | + |
| 30 | + private static void TryBind(VisualElement element, bool bind) |
| 31 | + { |
| 32 | + if (element is IGestureRecognizers recognizers) |
| 33 | + { |
| 34 | + foreach (var recognizer in recognizers.GestureRecognizers) |
| 35 | + { |
| 36 | + SetHooks(recognizer, bind); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + private static void SetHooks(IGestureRecognizer recognizer, bool bind) |
| 43 | + { |
| 44 | + switch (recognizer) |
| 45 | + { |
| 46 | + case TapGestureRecognizer tap: |
| 47 | + tap.Tapped -= OnTapGesture; |
| 48 | + |
| 49 | + if (bind) |
| 50 | + { |
| 51 | + tap.Tapped += OnTapGesture; |
| 52 | + } |
| 53 | + break; |
| 54 | + |
| 55 | + case SwipeGestureRecognizer swipe: |
| 56 | + swipe.Swiped -= OnSwipeGesture; |
| 57 | + |
| 58 | + if (bind) |
| 59 | + { |
| 60 | + swipe.Swiped += OnSwipeGesture; |
| 61 | + } |
| 62 | + break; |
| 63 | + |
| 64 | + case PinchGestureRecognizer pinch: |
| 65 | + pinch.PinchUpdated -= OnPinchGesture; |
| 66 | + |
| 67 | + if (bind) |
| 68 | + { |
| 69 | + pinch.PinchUpdated += OnPinchGesture; |
| 70 | + } |
| 71 | + break; |
| 72 | + |
| 73 | + case DragGestureRecognizer drag: |
| 74 | + drag.DragStarting -= OnDragStartingGesture; |
| 75 | + drag.DropCompleted -= OnDropCompletedGesture; |
| 76 | + |
| 77 | + if (bind) |
| 78 | + { |
| 79 | + drag.DragStarting += OnDragStartingGesture; |
| 80 | + drag.DropCompleted += OnDropCompletedGesture; |
| 81 | + } |
| 82 | + break; |
| 83 | + |
| 84 | + case PanGestureRecognizer pan: |
| 85 | + pan.PanUpdated -= OnPanGesture; |
| 86 | + |
| 87 | + if (bind) |
| 88 | + { |
| 89 | + pan.PanUpdated += OnPanGesture; |
| 90 | + } |
| 91 | + break; |
| 92 | + |
| 93 | + case PointerGestureRecognizer pointer: |
| 94 | + pointer.PointerEntered -= OnPointerEnteredGesture; |
| 95 | + pointer.PointerExited -= OnPointerExitedGesture; |
| 96 | + pointer.PointerMoved -= OnPointerMovedGesture; |
| 97 | + pointer.PointerPressed -= OnPointerPressedGesture; |
| 98 | + pointer.PointerReleased -= OnPointerReleasedGesture; |
| 99 | + |
| 100 | + if (bind) |
| 101 | + { |
| 102 | + pointer.PointerEntered += OnPointerEnteredGesture; |
| 103 | + pointer.PointerExited += OnPointerExitedGesture; |
| 104 | + pointer.PointerMoved += OnPointerMovedGesture; |
| 105 | + pointer.PointerPressed += OnPointerPressedGesture; |
| 106 | + pointer.PointerReleased += OnPointerReleasedGesture; |
| 107 | + } |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private static void OnPointerReleasedGesture(object? sender, PointerEventArgs e) => _addBreadcrumb?.Invoke(new( |
| 113 | + sender, |
| 114 | + nameof(PointerGestureRecognizer.PointerReleased), |
| 115 | + ToPointerData(e) |
| 116 | + )); |
| 117 | + |
| 118 | + private static void OnPointerPressedGesture(object? sender, PointerEventArgs e) => _addBreadcrumb?.Invoke(new( |
| 119 | + sender, |
| 120 | + nameof(PointerGestureRecognizer.PointerPressed), |
| 121 | + ToPointerData(e) |
| 122 | + )); |
| 123 | + |
| 124 | + private static void OnPointerMovedGesture(object? sender, PointerEventArgs e) => _addBreadcrumb?.Invoke(new( |
| 125 | + sender, |
| 126 | + nameof(PointerGestureRecognizer.PointerMoved), |
| 127 | + ToPointerData(e) |
| 128 | + )); |
| 129 | + |
| 130 | + private static void OnPointerExitedGesture(object? sender, PointerEventArgs e) => _addBreadcrumb?.Invoke(new( |
| 131 | + sender, |
| 132 | + nameof(PointerGestureRecognizer.PointerExited), |
| 133 | + ToPointerData(e) |
| 134 | + )); |
| 135 | + |
| 136 | + private static void OnPointerEnteredGesture(object? sender, PointerEventArgs e) => _addBreadcrumb?.Invoke(new( |
| 137 | + sender, |
| 138 | + nameof(PointerGestureRecognizer.PointerEntered), |
| 139 | + ToPointerData(e) |
| 140 | + )); |
| 141 | + |
| 142 | + private static IEnumerable<(string Key, string Value)> ToPointerData(PointerEventArgs e) => |
| 143 | + [ |
| 144 | + #if ANDROID |
| 145 | + ("MotionEventAction", e.PlatformArgs?.MotionEvent.Action.ToString() ?? string.Empty) |
| 146 | + #elif IOS |
| 147 | + ("State", e.PlatformArgs?.GestureRecognizer.State.ToString() ?? string.Empty) |
| 148 | + #endif |
| 149 | + ]; |
| 150 | + |
| 151 | + private static void OnPanGesture(object? sender, PanUpdatedEventArgs e) => _addBreadcrumb?.Invoke(new( |
| 152 | + sender, |
| 153 | + nameof(PanGestureRecognizer.PanUpdated), |
| 154 | + [ |
| 155 | + ("GestureId", e.GestureId.ToString()), |
| 156 | + ("StatusType", e.StatusType.ToString()), |
| 157 | + ("TotalX", e.TotalX.ToString()), |
| 158 | + ("TotalY", e.TotalY.ToString()) |
| 159 | + ] |
| 160 | + )); |
| 161 | + |
| 162 | + private static void OnDropCompletedGesture(object? sender, DropCompletedEventArgs e) => _addBreadcrumb?.Invoke(new( |
| 163 | + sender, |
| 164 | + nameof(DragGestureRecognizer.DropCompleted) |
| 165 | + )); |
| 166 | + |
| 167 | + private static void OnDragStartingGesture(object? sender, DragStartingEventArgs e) => _addBreadcrumb?.Invoke(new( |
| 168 | + sender, |
| 169 | + nameof(DragGestureRecognizer.DragStarting) |
| 170 | + )); |
| 171 | + |
| 172 | + |
| 173 | + private static void OnPinchGesture(object? sender, PinchGestureUpdatedEventArgs e) => _addBreadcrumb?.Invoke(new( |
| 174 | + sender, |
| 175 | + nameof(PinchGestureRecognizer.PinchUpdated), |
| 176 | + [ |
| 177 | + ("GestureStatus", e.Status.ToString()), |
| 178 | + ("Scale", e.Scale.ToString()), |
| 179 | + ("ScaleOrigin", e.ScaleOrigin.ToString()) |
| 180 | + ] |
| 181 | + )); |
| 182 | + |
| 183 | + private static void OnSwipeGesture(object? sender, SwipedEventArgs e) => _addBreadcrumb?.Invoke(new( |
| 184 | + sender, |
| 185 | + nameof(SwipeGestureRecognizer.Swiped), |
| 186 | + [("Direction", e.Direction.ToString())] |
| 187 | + )); |
| 188 | + |
| 189 | + private static void OnTapGesture(object? sender, TappedEventArgs e) => _addBreadcrumb?.Invoke(new( |
| 190 | + sender, |
| 191 | + nameof(TapGestureRecognizer.Tapped), |
| 192 | + [("ButtonMask", e.Buttons.ToString())] |
| 193 | + )); |
| 194 | +} |
0 commit comments