Skip to content

Commit

Permalink
- cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Jun 29, 2021
1 parent 36a3dff commit ac59312
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public override void Draw(Canvas canvas)

public override bool OnTouchEvent(MotionEvent e)
{
if (_visualElementRenderer.OnTouchEvent(e) || base.OnTouchEvent(e))
if (base.OnTouchEvent(e))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected virtual void OnElementChanged(ElementChangedEventArgs<Image> e)

public override bool OnTouchEvent(MotionEvent e)
{
if (_visualElementRenderer.OnTouchEvent(e) || base.OnTouchEvent(e))
if (base.OnTouchEvent(e))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ protected override void Dispose(bool disposing)

public override bool OnTouchEvent(MotionEvent e)
{
if (_visualElementRenderer.OnTouchEvent(e) || base.OnTouchEvent(e))
if (base.OnTouchEvent(e))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public sealed class VisualElementRenderer : IDisposable, IEffectControlProvider,
bool _disposed;

IVisualElementRenderer _renderer;
readonly GestureManager _gestureManager;
readonly AutomationPropertiesProvider _automationPropertiesProvider;
readonly EffectControlProvider _effectControlProvider;

Expand All @@ -21,7 +20,6 @@ public VisualElementRenderer(IVisualElementRenderer renderer)
_renderer = renderer;
_renderer.ElementPropertyChanged += OnElementPropertyChanged;
_renderer.ElementChanged += OnElementChanged;
_gestureManager = new GestureManager(renderer?.Element?.Handler);
_automationPropertiesProvider = new AutomationPropertiesProvider(_renderer);

_effectControlProvider = new EffectControlProvider(_renderer?.View);
Expand All @@ -46,11 +44,6 @@ void UpdateFlowDirection()
Control.UpdateFlowDirection(Element);
}

public bool OnTouchEvent(MotionEvent e)
{
return _gestureManager.OnTouchEvent(e);
}

public void Dispose()
{
Dispose(true);
Expand Down Expand Up @@ -80,7 +73,6 @@ void Dispose(bool disposing)
_renderer = null;
}

_gestureManager?.Dispose();
_automationPropertiesProvider?.Dispose();
}
}
Expand All @@ -102,7 +94,6 @@ void OnElementChanged(object sender, VisualElementChangedEventArgs e)

EffectUtilities.RegisterEffectControlProvider(this, e.OldElement, e.NewElement);

_gestureManager.OnElementChanged(e);
Performance.Stop(reference);
}

Expand All @@ -126,8 +117,6 @@ void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
UpdateIsEnabled();
}

_gestureManager.OnElementPropertyChanged(e);
}
}
}
15 changes: 0 additions & 15 deletions src/Compatibility/Core/src/Android/VisualElementRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,8 @@ public abstract class VisualElementRenderer<TElement> : FormsViewGroup, IVisualE
VisualElementPackager _packager;
PropertyChangedEventHandler _propertyChangeHandler;

GestureManager _gestureManager;

protected VisualElementRenderer(Context context) : base(context)
{
_gestureManager = new GestureManager(null);
}

public override bool OnTouchEvent(MotionEvent e)
{
return _gestureManager.OnTouchEvent(e) || base.OnTouchEvent(e);
}

public override bool OnInterceptTouchEvent(MotionEvent ev)
Expand Down Expand Up @@ -302,12 +294,6 @@ protected override void Dispose(bool disposing)
_packager = null;
}

if (_gestureManager != null)
{
_gestureManager.Dispose();
_gestureManager = null;
}

if (ManageNativeControlLifetime)
{
while (ChildCount > 0)
Expand Down Expand Up @@ -345,7 +331,6 @@ protected virtual Size MinimumSize()
protected virtual void OnElementChanged(ElementChangedEventArgs<TElement> e)
{
var args = new VisualElementChangedEventArgs(e.OldElement, e.NewElement);
_gestureManager.OnElementChanged(args);

// The list of event handlers can be changed inside the handlers. (ex.: are used CompressedLayout)
// To avoid an exception, a copy of the handlers is called.
Expand Down
29 changes: 0 additions & 29 deletions src/Controls/src/Core/HandlerImpl/View.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,15 @@ public partial class View
private protected override void OnAttachedHandlerCore()
{
base.OnAttachedHandlerCore();

_gestureManager?.Dispose();
_gestureManager = new GestureManager(Handler);
_gestureRecognizers.CollectionChanged += OnGestureRecognizersCollectionChanged;

#if __ANDROID__
var nativeview = (Handler as INativeViewHandler)?.NativeView;
if (nativeview != null)
{
nativeview.Touch += Nativeview_Touch;
}
#endif
}

#if __ANDROID__
private void Nativeview_Touch(object? sender, Android.Views.View.TouchEventArgs e)
{
if (e.Event != null)
_gestureManager?.OnTouchEvent(e.Event);
}
#endif

private protected override void OnDetachingHandlerCore()
{
base.OnDetachingHandlerCore();
_gestureManager?.Dispose();
_gestureManager = null;
_gestureRecognizers.CollectionChanged -= OnGestureRecognizersCollectionChanged;
}


void OnGestureRecognizersCollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
}

void SetupGestures()
{

}
}
}
136 changes: 58 additions & 78 deletions src/Controls/src/Core/Platform/Android/GestureManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Android.Content;
using Android.Views;
using AndroidX.Core.View;
Expand All @@ -32,19 +30,11 @@ class GestureManager : IDisposable

public GestureManager(IViewHandler handler)
{
Console.WriteLine("taxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxestaxes");
_handler = handler;
_tapAndPanAndSwipeDetector = new Lazy<TapAndPanGestureDetector>(InitializeTapAndPanAndSwipeDetector);
_scaleDetector = new Lazy<ScaleGestureDetector>(InitializeScaleDetector);
_dragAndDropGestureHandler = new Lazy<DragAndDropGestureHandler>(InitializeDragAndDropHandler);
UpdateDragAndDrop();
UpdateInputTransparent();
UpdateIsEnabled();

if (View?.GestureRecognizers is INotifyCollectionChanged incc)
{
incc.CollectionChanged += GestureCollectionChanged;
}
SetupElement(null, Element);
}

public bool OnTouchEvent(MotionEvent e)
Expand Down Expand Up @@ -76,56 +66,6 @@ public bool OnTouchEvent(MotionEvent e)
return eventConsumed;
}

public class TapAndPanGestureDetector : GestureDetector
{
InnerGestureListener? _listener;
public TapAndPanGestureDetector(Context context, InnerGestureListener listener) : base(context, listener)
{
_listener = listener;
UpdateLongPressSettings();
}

public void UpdateLongPressSettings()
{
if (_listener == null)
return;

// Right now this just disables long press, since we don't support a long press gesture
// in Forms. If we ever do, we'll need to selectively enable it, probably by hooking into the
// InnerGestureListener and listening for the addition of any long press gesture recognizers.
// (since a long press will prevent a pan gesture from starting, we can't just leave support for it
// on by default).
// Also, since the property is virtual we shouldn't just set it from the constructor.

IsLongpressEnabled = _listener.EnableLongPressGestures;
}

public override bool OnTouchEvent(MotionEvent? ev)
{
if (base.OnTouchEvent(ev))
return true;

if (_listener != null && ev?.Action == MotionEventActions.Up)
_listener.EndScrolling();

return false;
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

if (disposing)
{
if (_listener != null)
{
_listener.Dispose();
_listener = null;
}
}
}
}

public void Dispose()
{
Dispose(true);
Expand Down Expand Up @@ -192,40 +132,87 @@ ScaleGestureDetector InitializeScaleDetector()

bool ViewHasPinchGestures()
{
return View != null && View.GestureRecognizers.OfType<PinchGestureRecognizer>().Any();
if (View == null)
return false;

int count = View.GestureRecognizers.Count;
for (var i = 0; i < count; i++)
{
if (View.GestureRecognizers[i] is PinchGestureRecognizer)
return true;
}

return false;
}

void SetupGestures()
{
if (View == null)
return;

AView? nativeView = (_handler?.NativeView as AView);

if (nativeView == null)
return;

if (View.GestureRecognizers.Count == 0)
{
nativeView.Touch -= OnNativeViewTouched;

}
else
{
nativeView.Touch += OnNativeViewTouched;
}
}

void OnNativeViewTouched(object? sender, Android.Views.View.TouchEventArgs e)
{
if (e.Event != null)
OnTouchEvent(e.Event);
}

internal void OnElementChanged(VisualElementChangedEventArgs e)
void SetupElement(VisualElement? oldElement, VisualElement? newElement)
{
_handler = null;

if (e.OldElement != null)
if (oldElement != null)
{
if (e.OldElement is View ov &&
if (oldElement is View ov &&
ov.GestureRecognizers is INotifyCollectionChanged incc)
{
incc.CollectionChanged -= GestureCollectionChanged;
}

oldElement.PropertyChanged -= OnElementPropertyChanged;
}

if (e.NewElement != null)
if (newElement != null)
{
_handler = e.NewElement.Handler;
if (e.NewElement is View ov &&
_handler = newElement.Handler;
if (newElement is View ov &&
ov.GestureRecognizers is INotifyCollectionChanged incc)
{
incc.CollectionChanged += GestureCollectionChanged;
}

newElement.PropertyChanged += OnElementPropertyChanged;
}

UpdateInputTransparent();
UpdateIsEnabled();
UpdateDragAndDrop();
SetupGestures();
}

internal void OnElementChanged(VisualElementChangedEventArgs e)
{
SetupElement(e.OldElement, e.NewElement);
}

void GestureCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
UpdateDragAndDrop();
SetupGestures();

if (_tapAndPanAndSwipeDetector.IsValueCreated)
_tapAndPanAndSwipeDetector.Value.UpdateLongPressSettings();
Expand All @@ -237,7 +224,7 @@ void UpdateDragAndDrop()
_dragAndDropGestureHandler.Value.SetupHandlerForDrop();
}

internal void OnElementPropertyChanged(PropertyChangedEventArgs e)
void OnElementPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == VisualElement.InputTransparentProperty.PropertyName)
UpdateInputTransparent();
Expand All @@ -256,14 +243,7 @@ protected void Dispose(bool disposing)

if (disposing)
{
if (Element != null)
{
if (Element is View ov &&
ov.GestureRecognizers is INotifyCollectionChanged incc)
{
incc.CollectionChanged -= GestureCollectionChanged;
}
}
SetupElement(Element, null);

if (_tapAndPanAndSwipeDetector.IsValueCreated)
{
Expand Down
Loading

0 comments on commit ac59312

Please sign in to comment.