Add support for enabling/disabling touch events:

- switch between gestures and touch events
 - improved performance as we don't need to listen always
This commit is contained in:
Matthew Leibowitz 2017-06-02 17:04:41 -05:00
Родитель 2670f6728b
Коммит bfa980d249
10 изменённых файлов: 120 добавлений и 39 удалений

Просмотреть файл

@ -14,18 +14,22 @@ namespace SkiaSharp.Views.Forms
this.scalePixels = scalePixels;
}
public void Attach(View view)
public void SetEnabled(View view, bool enableTouchEvents)
{
view.Touch += OnTouch;
if (view != null)
{
view.Touch -= OnTouch;
if (enableTouchEvents)
{
view.Touch += OnTouch;
}
}
}
public void Detach(View view)
{
// clean the view
if (view != null)
{
view.Touch -= OnTouch;
}
SetEnabled(view, false);
// remove references
onTouchAction = null;

Просмотреть файл

@ -16,18 +16,25 @@ namespace SkiaSharp.Views.Forms
this.scalePixels = scalePixels;
}
public void Attach(NSView view)
public void SetEnabled(NSView view, bool enableTouchEvents)
{
view.AddGestureRecognizer(this);
if (view != null)
{
if (enableTouchEvents && !view.GestureRecognizers.Contains(this))
{
view.AddGestureRecognizer(this);
}
else if (!enableTouchEvents && view.GestureRecognizers.Contains(this))
{
view.RemoveGestureRecognizer(this);
}
}
}
public void Detach(NSView view)
{
// clean the view
if (view != null)
{
view.RemoveGestureRecognizer(this);
}
SetEnabled(view, false);
// remove references
onTouchAction = null;

Просмотреть файл

@ -51,6 +51,13 @@ namespace SkiaSharp.Views.Forms
#endif
}
#if __IOS__
protected void SetDisablesUserInteraction(bool disablesUserInteraction)
{
touchHandler.DisablesUserInteraction = disablesUserInteraction;
}
#endif
protected override void OnElementChanged(ElementChangedEventArgs<TFormsView> e)
{
if (e.OldElement != null)
@ -71,11 +78,11 @@ namespace SkiaSharp.Views.Forms
{
var view = CreateNativeControl();
view.PaintSurface += OnPaintSurface;
touchHandler.Attach(view);
SetNativeControl(view);
}
// set the initial values
touchHandler.SetEnabled(Control, e.NewElement.EnableTouchEvents);
Control.IgnorePixelScaling = e.NewElement.IgnorePixelScaling;
// subscribe to events from the user
@ -88,7 +95,7 @@ namespace SkiaSharp.Views.Forms
base.OnElementChanged(e);
}
#if __ANDROID__
protected override TNativeView CreateNativeControl()
{
@ -105,10 +112,14 @@ namespace SkiaSharp.Views.Forms
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == nameof(SKFormsView.IgnorePixelScaling))
if (e.PropertyName == SKFormsView.IgnorePixelScalingProperty.PropertyName)
{
Control.IgnorePixelScaling = Element.IgnorePixelScaling;
}
else if (e.PropertyName == SKFormsView.EnableTouchEventsProperty.PropertyName)
{
touchHandler.SetEnabled(Control, Element.EnableTouchEvents);
}
}
protected override void Dispose(bool disposing)

Просмотреть файл

@ -51,6 +51,13 @@ namespace SkiaSharp.Views.Forms
#endif
}
#if __IOS__
protected void SetDisablesUserInteraction(bool disablesUserInteraction)
{
touchHandler.DisablesUserInteraction = disablesUserInteraction;
}
#endif
protected override void OnElementChanged(ElementChangedEventArgs<TFormsView> e)
{
if (e.OldElement != null)
@ -75,10 +82,11 @@ namespace SkiaSharp.Views.Forms
#else
view.PaintSurface += OnPaintSurface;
#endif
touchHandler.Attach(view);
SetNativeControl(view);
}
touchHandler.SetEnabled(Control, e.NewElement.EnableTouchEvents);
// subscribe to events from the user
newController.SurfaceInvalidated += OnSurfaceInvalidated;
newController.GetCanvasSize += OnGetCanvasSize;
@ -111,6 +119,10 @@ namespace SkiaSharp.Views.Forms
{
SetupRenderLoop(false);
}
else if (e.PropertyName == SKFormsView.EnableTouchEventsProperty.PropertyName)
{
touchHandler.SetEnabled(Control, Element.EnableTouchEvents);
}
}
protected override void Dispose(bool disposing)
@ -159,7 +171,7 @@ namespace SkiaSharp.Views.Forms
private void OnPaintSurface(object sender, SKNativePaintGLSurfaceEventArgs e)
{
var controller = Element as ISKGLViewController;
// the control is being repainted, let the user know
controller?.OnPaintSurface(new SKPaintGLSurfaceEventArgs(e.Surface, e.RenderTarget));
}

Просмотреть файл

@ -7,7 +7,10 @@ namespace SkiaSharp.Views.Forms
public class SKCanvasView : View, ISKCanvasViewController
{
public static readonly BindableProperty IgnorePixelScalingProperty =
BindableProperty.Create(nameof(IgnorePixelScaling), typeof(bool), typeof(SKCanvasView), default(bool));
BindableProperty.Create(nameof(IgnorePixelScaling), typeof(bool), typeof(SKCanvasView), false);
public static readonly BindableProperty EnableTouchEventsProperty =
BindableProperty.Create(nameof(EnableTouchEvents), typeof(bool), typeof(SKCanvasView), false);
// the user can subscribe to repaint
public event EventHandler<SKPaintSurfaceEventArgs> PaintSurface;
@ -37,6 +40,12 @@ namespace SkiaSharp.Views.Forms
set { SetValue(IgnorePixelScalingProperty, value); }
}
public bool EnableTouchEvents
{
get { return (bool)GetValue(EnableTouchEventsProperty); }
set { SetValue(EnableTouchEventsProperty, value); }
}
// the user asks to repaint
public void InvalidateSurface()
{

Просмотреть файл

@ -7,7 +7,10 @@ namespace SkiaSharp.Views.Forms
public class SKGLView : View, ISKGLViewController
{
public static readonly BindableProperty HasRenderLoopProperty =
BindableProperty.Create("HasRenderLoop", typeof(bool), typeof(SKGLView), default(bool));
BindableProperty.Create("HasRenderLoop", typeof(bool), typeof(SKGLView), false);
public static readonly BindableProperty EnableTouchEventsProperty =
BindableProperty.Create(nameof(EnableTouchEvents), typeof(bool), typeof(SKCanvasView), false);
public bool HasRenderLoop
{
@ -15,9 +18,15 @@ namespace SkiaSharp.Views.Forms
set { SetValue(HasRenderLoopProperty, value); }
}
public bool EnableTouchEvents
{
get { return (bool)GetValue(EnableTouchEventsProperty); }
set { SetValue(EnableTouchEventsProperty, value); }
}
// the user can subscribe to repaint
public event EventHandler<SKPaintGLSurfaceEventArgs> PaintSurface;
// the user can subscribe to touch events
public event EventHandler<SKTouchEventArgs> Touch;
@ -49,7 +58,7 @@ namespace SkiaSharp.Views.Forms
{
PaintSurface?.Invoke(this, e);
}
// the native view responds to a touch
protected virtual void OnTouch(SKTouchEventArgs e)
{

Просмотреть файл

@ -17,19 +17,8 @@ namespace SkiaSharp.Views.Forms
this.scalePixels = scalePixels;
}
public void Attach(FrameworkElement view)
public void SetEnabled(View view, bool enableTouchEvents)
{
view.PointerEntered += OnPointerEntered;
view.PointerExited += OnPointerExited;
view.PointerPressed += OnPointerPressed;
view.PointerMoved += OnPointerMoved;
view.PointerReleased += OnPointerReleased;
view.PointerCanceled += OnPointerCancelled;
}
public void Detach(FrameworkElement view)
{
// clean the view
if (view != null)
{
view.PointerEntered -= OnPointerEntered;
@ -38,7 +27,22 @@ namespace SkiaSharp.Views.Forms
view.PointerMoved -= OnPointerMoved;
view.PointerReleased -= OnPointerReleased;
view.PointerCanceled -= OnPointerCancelled;
if (enableTouchEvents)
{
view.PointerEntered += OnPointerEntered;
view.PointerExited += OnPointerExited;
view.PointerPressed += OnPointerPressed;
view.PointerMoved += OnPointerMoved;
view.PointerReleased += OnPointerReleased;
view.PointerCanceled += OnPointerCancelled;
}
}
}
public void Detach(FrameworkElement view)
{
// clean the view
SetEnabled(view, false);
// remove references
onTouchAction = null;

Просмотреть файл

@ -9,6 +9,11 @@ namespace SkiaSharp.Views.Forms
{
public class SKCanvasViewRenderer : SKCanvasViewRendererBase<SKFormsView, SKNativeView>
{
public SKCanvasViewRenderer()
{
SetDisablesUserInteraction(true);
}
protected override SKNativeView CreateNativeControl()
{
var view = base.CreateNativeControl();

Просмотреть файл

@ -13,6 +13,11 @@ namespace SkiaSharp.Views.Forms
{
private CADisplayLink displayLink;
public SKGLViewRenderer()
{
SetDisablesUserInteraction(true);
}
protected override SKNativeView CreateNativeControl()
{
var view = base.CreateNativeControl();

Просмотреть файл

@ -14,20 +14,35 @@ namespace SkiaSharp.Views.Forms
{
this.onTouchAction = onTouchAction;
this.scalePixels = scalePixels;
DisablesUserInteraction = false;
}
public void Attach(UIView view)
public bool DisablesUserInteraction { get; set; }
public void SetEnabled(UIView view, bool enableTouchEvents)
{
view.AddGestureRecognizer(this);
if (view != null)
{
if (!view.UserInteractionEnabled || DisablesUserInteraction)
{
view.UserInteractionEnabled = enableTouchEvents;
}
if (enableTouchEvents && view.GestureRecognizers?.Contains(this) != true)
{
view.AddGestureRecognizer(this);
}
else if (!enableTouchEvents && view.GestureRecognizers?.Contains(this) == true)
{
view.RemoveGestureRecognizer(this);
}
}
}
public void Detach(UIView view)
{
// clean the view
if (view != null)
{
view.RemoveGestureRecognizer(this);
}
SetEnabled(view, false);
// remove references
onTouchAction = null;