|
| 1 | +using System; |
| 2 | +using Android.Views; |
| 3 | +using Plugin.MaterialDesignControls; |
| 4 | +using Xamarin.Forms; |
| 5 | +using Xamarin.Forms.Platform.Android; |
| 6 | +using View = Android.Views.View; |
| 7 | +using TouchAndPressEffect = Plugin.MaterialDesignControls.Android.TouchAndPressEffect; |
| 8 | + |
| 9 | +[assembly: ResolutionGroupName(Plugin.MaterialDesignControls.TouchAndPressEffect.EffectIdPrefix)] |
| 10 | +[assembly: ExportEffect(typeof(TouchAndPressEffect), nameof(TouchAndPressEffect))] |
| 11 | + |
| 12 | +namespace Plugin.MaterialDesignControls.Android |
| 13 | +{ |
| 14 | + public class TouchAndPressEffect : PlatformEffect |
| 15 | + { |
| 16 | + private View _view; |
| 17 | + private ITouchAndPressEffectConsumer _touchAndPressEffectConsumer; |
| 18 | + |
| 19 | + protected override void OnAttached() |
| 20 | + { |
| 21 | + _view = Control ?? Container; |
| 22 | + |
| 23 | + if (_view != null && Element is ITouchAndPressEffectConsumer touchAndPressEffectConsumer) |
| 24 | + { |
| 25 | + _view.Touch += OnViewOnTouch; |
| 26 | + _touchAndPressEffectConsumer = touchAndPressEffectConsumer; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + protected override void OnDetached() |
| 31 | + { |
| 32 | + if (_view != null) |
| 33 | + { |
| 34 | + _view.Touch -= OnViewOnTouch; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private void OnViewOnTouch(object sender, View.TouchEventArgs e) |
| 39 | + { |
| 40 | + switch (e.Event.ActionMasked) |
| 41 | + { |
| 42 | + case MotionEventActions.ButtonPress: |
| 43 | + _touchAndPressEffectConsumer?.ConsumeEvent(EventType.Pressing); |
| 44 | + break; |
| 45 | + case MotionEventActions.ButtonRelease: |
| 46 | + _touchAndPressEffectConsumer?.ConsumeEvent(EventType.Released); |
| 47 | + break; |
| 48 | + case MotionEventActions.Cancel: |
| 49 | + _touchAndPressEffectConsumer?.ConsumeEvent(EventType.Cancelled); |
| 50 | + break; |
| 51 | + case MotionEventActions.Down: |
| 52 | + _touchAndPressEffectConsumer?.ConsumeEvent(EventType.Pressing); |
| 53 | + break; |
| 54 | + case MotionEventActions.HoverEnter: |
| 55 | + break; |
| 56 | + case MotionEventActions.HoverExit: |
| 57 | + break; |
| 58 | + case MotionEventActions.HoverMove: |
| 59 | + break; |
| 60 | + case MotionEventActions.Mask: |
| 61 | + break; |
| 62 | + case MotionEventActions.Move: |
| 63 | + break; |
| 64 | + case MotionEventActions.Outside: |
| 65 | + break; |
| 66 | + case MotionEventActions.Pointer1Down: |
| 67 | + _touchAndPressEffectConsumer?.ConsumeEvent(EventType.Pressing); |
| 68 | + break; |
| 69 | + case MotionEventActions.Pointer1Up: |
| 70 | + _touchAndPressEffectConsumer?.ConsumeEvent(EventType.Released); |
| 71 | + break; |
| 72 | + case MotionEventActions.Pointer2Down: |
| 73 | + break; |
| 74 | + case MotionEventActions.Pointer2Up: |
| 75 | + break; |
| 76 | + case MotionEventActions.Pointer3Down: |
| 77 | + break; |
| 78 | + case MotionEventActions.Pointer3Up: |
| 79 | + break; |
| 80 | + case MotionEventActions.PointerIdMask: |
| 81 | + break; |
| 82 | + case MotionEventActions.PointerIdShift: |
| 83 | + break; |
| 84 | + case MotionEventActions.Up: |
| 85 | + _touchAndPressEffectConsumer?.ConsumeEvent(EventType.Released); |
| 86 | + break; |
| 87 | + default: |
| 88 | + throw new ArgumentOutOfRangeException(); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments