2017-04-06 16:19:52 +03:00
using System ;
using System.ComponentModel ;
using Android.Views ;
2017-08-10 20:35:27 +03:00
using Xamarin.Forms.Internals ;
2017-04-06 16:19:52 +03:00
using AView = Android . Views . View ;
namespace Xamarin.Forms.Platform.Android.FastRenderers
{
// TODO hartez 2017/03/03 14:11:17 It's weird that this class is called VisualElementRenderer but it doesn't implement that interface. The name should probably be different.
2017-08-10 21:08:18 +03:00
internal sealed class VisualElementRenderer : IDisposable , IEffectControlProvider
2017-04-06 16:19:52 +03:00
{
bool _disposed ;
IVisualElementRenderer _renderer ;
readonly GestureManager _gestureManager ;
2017-11-09 17:31:15 +03:00
readonly AutomationPropertiesProvider _automationPropertiesProvider ;
2017-04-06 16:19:52 +03:00
readonly EffectControlProvider _effectControlProvider ;
public VisualElementRenderer ( IVisualElementRenderer renderer )
{
_renderer = renderer ;
_renderer . ElementPropertyChanged + = OnElementPropertyChanged ;
_renderer . ElementChanged + = OnElementChanged ;
_gestureManager = new GestureManager ( _renderer ) ;
2017-11-09 17:31:15 +03:00
_automationPropertiesProvider = new AutomationPropertiesProvider ( _renderer ) ;
2017-08-10 20:35:27 +03:00
2017-04-06 16:19:52 +03:00
_effectControlProvider = new EffectControlProvider ( _renderer ? . View ) ;
}
VisualElement Element = > _renderer ? . Element ;
2017-11-09 17:31:15 +03:00
2017-04-06 16:19:52 +03:00
AView Control = > _renderer ? . View ;
void IEffectControlProvider . RegisterEffect ( Effect effect )
{
_effectControlProvider . RegisterEffect ( effect ) ;
}
public void UpdateBackgroundColor ( Color ? color = null )
2017-04-28 12:27:15 +03:00
{
if ( _disposed | | Element = = null | | Control = = null )
2017-04-06 16:19:52 +03:00
return ;
Control . SetBackgroundColor ( ( color ? ? Element . BackgroundColor ) . ToAndroid ( ) ) ;
}
2017-11-09 17:31:15 +03:00
void UpdateFlowDirection ( )
{
if ( _disposed )
return ;
Control . UpdateFlowDirection ( Element ) ;
}
public bool OnTouchEvent ( MotionEvent e )
2017-04-06 16:19:52 +03:00
{
2017-09-15 21:49:19 +03:00
return _gestureManager . OnTouchEvent ( e ) ;
2017-04-06 16:19:52 +03:00
}
public void Dispose ( )
{
Dispose ( true ) ;
GC . SuppressFinalize ( this ) ;
}
2017-08-10 21:08:18 +03:00
void Dispose ( bool disposing )
2017-04-06 16:19:52 +03:00
{
if ( _disposed )
return ;
_disposed = true ;
if ( disposing )
{
_gestureManager ? . Dispose ( ) ;
2017-11-09 17:31:15 +03:00
_automationPropertiesProvider ? . Dispose ( ) ;
2017-04-06 16:19:52 +03:00
if ( _renderer ! = null )
{
_renderer . ElementChanged - = OnElementChanged ;
_renderer . ElementPropertyChanged - = OnElementPropertyChanged ;
_renderer = null ;
}
}
}
void OnElementChanged ( object sender , VisualElementChangedEventArgs e )
{
if ( e . OldElement ! = null )
{
e . OldElement . PropertyChanged - = OnElementPropertyChanged ;
}
if ( e . NewElement ! = null )
{
e . NewElement . PropertyChanged + = OnElementPropertyChanged ;
UpdateBackgroundColor ( ) ;
2017-11-09 17:31:15 +03:00
UpdateFlowDirection ( ) ;
2017-04-06 16:19:52 +03:00
}
2017-08-10 20:35:27 +03:00
EffectUtilities . RegisterEffectControlProvider ( this , e . OldElement , e . NewElement ) ;
2017-04-06 16:19:52 +03:00
}
void OnElementPropertyChanged ( object sender , PropertyChangedEventArgs e )
{
if ( e . PropertyName = = VisualElement . BackgroundColorProperty . PropertyName )
UpdateBackgroundColor ( ) ;
2017-11-09 17:31:15 +03:00
else if ( e . PropertyName = = VisualElement . FlowDirectionProperty . PropertyName )
UpdateFlowDirection ( ) ;
2017-04-06 16:19:52 +03:00
}
}
2017-04-28 12:27:15 +03:00
}