-using System;
using System.Collections.Generic;
-using System.Collections.ObjectModel;
-using System.Collections.Specialized;
using System.Linq;
using ElmSharp;
using EGestureType = ElmSharp.GestureLayer.GestureType;
{
internal class GestureDetector
{
- readonly IDictionary<EGestureType, List<GestureHandler>> _handlerCache = new Dictionary<EGestureType, List<GestureHandler>>();
+ readonly IDictionary<EGestureType, List<GestureHandler>> _handlerCache;
readonly IVisualElementRenderer _renderer;
GestureLayer _gestureLayer;
public GestureDetector(IVisualElementRenderer renderer)
{
+ _handlerCache = new Dictionary<EGestureType, List<GestureHandler>>();
_renderer = renderer;
_isEnabled = View.IsEnabled;
_inputTransparent = View.InputTransparent;
-
- (View.GestureRecognizers as ObservableCollection<IGestureRecognizer>).CollectionChanged += OnGestureRecognizerCollectionChanged;
-
- if (View.GestureRecognizers.Count > 0)
- {
- CreateGestureLayer();
- AddGestures(View.GestureRecognizers);
- }
}
public void Clear()
_handlerCache.Clear();
}
+ public void AddGestures(IEnumerable<IGestureRecognizer> recognizers)
+ {
+ if (_gestureLayer == null)
+ {
+ CreateGestureLayer();
+ }
+
+ foreach (var item in recognizers)
+ AddGesture(item);
+ }
+
+ public void RemoveGestures(IEnumerable<IGestureRecognizer> recognizers)
+ {
+ foreach (var item in recognizers)
+ RemoveGesture(item);
+ }
+
void CreateGestureLayer()
{
_gestureLayer = new GestureLayer(_renderer.NativeView);
}
}
-
- void AddGestures(IEnumerable<IGestureRecognizer> recognizers)
- {
- foreach (var item in recognizers)
- AddGesture(item);
- }
-
- void RemoveGestures(IEnumerable<IGestureRecognizer> recognizers)
- {
- foreach (var item in recognizers)
- RemoveGesture(item);
- }
-
void AddGesture(IGestureRecognizer recognizer)
{
var handler = CreateHandler(recognizer);
}
}
}
-
- void OnGestureRecognizerCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
- {
- // Gestures will be registered/unregistered according to changes in the GestureRecognizers list
- switch (e.Action)
- {
- case NotifyCollectionChangedAction.Add:
- if (_gestureLayer == null)
- CreateGestureLayer();
- AddGestures(e.NewItems.OfType<IGestureRecognizer>());
- break;
-
- case NotifyCollectionChangedAction.Replace:
- RemoveGestures(e.OldItems.OfType<IGestureRecognizer>());
- AddGestures(e.NewItems.OfType<IGestureRecognizer>());
- break;
-
- case NotifyCollectionChangedAction.Remove:
- RemoveGestures(e.OldItems.OfType<IGestureRecognizer>());
- break;
-
- case NotifyCollectionChangedAction.Reset:
- Clear();
- break;
- }
- }
}
}
\ No newline at end of file
-using System;
-using System.ComponentModel;
+using System.Collections.ObjectModel;
+using System.Collections.Specialized;
using System.Diagnostics;
+using System.Linq;
using ElmSharp;
namespace Xamarin.Forms.Platform.Tizen
where TView : View
where TNativeView : EvasObject
{
- readonly Lazy<GestureDetector> _gestureDetector;
+ ObservableCollection<IGestureRecognizer> GestureRecognizers => Element.GestureRecognizers as ObservableCollection<IGestureRecognizer>;
- internal GestureDetector GestureDetector => _gestureDetector.Value;
+ internal GestureDetector GestureDetector { get; private set; }
/// <summary>
- /// Default constructor.
+ /// Native control associated with this renderer.
/// </summary>
- protected ViewRenderer()
+ public TNativeView Control
{
- _gestureDetector = new Lazy<GestureDetector>(() => new GestureDetector(this));
+ get
+ {
+ return (TNativeView)NativeView;
+ }
}
protected override void OnElementChanged(ElementChangedEventArgs<TView> e)
{
base.OnElementChanged(e);
- if (e.OldElement != null && _gestureDetector.IsValueCreated)
+ if (GestureDetector != null)
{
- _gestureDetector.Value.Clear();
+ GestureRecognizers.CollectionChanged -= OnGestureRecognizerCollectionChanged;
+ GestureDetector.Clear();
+ GestureDetector = null;
}
- }
- /// <summary>
- /// Native control associated with this renderer.
- /// </summary>
- public TNativeView Control
- {
- get
+ GestureRecognizers.CollectionChanged += OnGestureRecognizerCollectionChanged;
+ if (Element.GestureRecognizers.Count > 0)
{
- return (TNativeView)NativeView;
+ GestureDetector = new GestureDetector(this);
+ GestureDetector.AddGestures(Element.GestureRecognizers);
}
}
protected override void UpdateIsEnabled(bool initialize)
{
base.UpdateIsEnabled(initialize);
- _gestureDetector.Value.IsEnabled = Element.IsEnabled;
+ if (initialize && Element.IsEnabled)
+ return;
+
+ if (GestureDetector != null)
+ {
+ GestureDetector.IsEnabled = Element.IsEnabled;
+ }
+ }
+
+ void OnGestureRecognizerCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
+ {
+ if (GestureDetector == null)
+ {
+ GestureDetector = new GestureDetector(this);
+ }
+
+ // Gestures will be registered/unregistered according to changes in the GestureRecognizers list
+ switch (e.Action)
+ {
+ case NotifyCollectionChangedAction.Add:
+ GestureDetector.AddGestures(e.NewItems.OfType<IGestureRecognizer>());
+ break;
+
+ case NotifyCollectionChangedAction.Replace:
+ GestureDetector.RemoveGestures(e.OldItems.OfType<IGestureRecognizer>());
+ GestureDetector.AddGestures(e.NewItems.OfType<IGestureRecognizer>());
+ break;
+
+ case NotifyCollectionChangedAction.Remove:
+ GestureDetector.RemoveGestures(e.OldItems.OfType<IGestureRecognizer>());
+ break;
+
+ case NotifyCollectionChangedAction.Reset:
+ GestureDetector.Clear();
+ break;
+ }
}
}
}
\ No newline at end of file