Ooui-tws-port/Ooui.Forms/VisualElementRenderer.cs

193 lines
6.2 KiB
C#
Raw Normal View History

2017-11-09 07:57:04 +00:00
using System;
2017-11-09 09:38:19 +00:00
using System.Collections.Generic;
using System.ComponentModel;
using Ooui.Forms.Extensions;
2017-11-09 07:57:04 +00:00
using Xamarin.Forms;
namespace Ooui.Forms
{
2017-11-09 22:17:36 +00:00
[Flags]
public enum VisualElementRendererFlags
{
Disposed = 1 << 0,
AutoTrack = 1 << 1,
AutoPackage = 1 << 2
}
2017-11-09 21:03:56 +00:00
public class VisualElementRenderer<TElement> : Ooui.Element, IVisualElementRenderer where TElement : VisualElement
{
bool disposedValue = false; // To detect redundant calls
2017-11-10 00:09:48 +00:00
readonly Color _defaultColor = Colors.Clear;
2017-11-09 09:38:19 +00:00
2017-11-09 21:03:56 +00:00
readonly PropertyChangedEventHandler _propertyChangedHandler;
public TElement Element { get; private set; }
2017-11-09 09:38:19 +00:00
2017-11-09 21:03:56 +00:00
VisualElement IVisualElementRenderer.Element => Element;
public Element NativeView => this;
event EventHandler<VisualElementChangedEventArgs> IVisualElementRenderer.ElementChanged {
add { _elementChangedHandlers.Add (value); }
remove { _elementChangedHandlers.Remove (value); }
}
readonly List<EventHandler<VisualElementChangedEventArgs>> _elementChangedHandlers =
new List<EventHandler<VisualElementChangedEventArgs>> ();
2017-11-09 22:17:36 +00:00
VisualElementRendererFlags _flags = VisualElementRendererFlags.AutoPackage | VisualElementRendererFlags.AutoTrack;
2017-12-10 04:39:10 +00:00
EventTracker _events;
2017-11-09 22:17:36 +00:00
VisualElementPackager _packager;
VisualElementTracker _tracker;
protected bool AutoPackage {
get { return (_flags & VisualElementRendererFlags.AutoPackage) != 0; }
set {
if (value)
_flags |= VisualElementRendererFlags.AutoPackage;
else
_flags &= ~VisualElementRendererFlags.AutoPackage;
}
}
protected bool AutoTrack {
get { return (_flags & VisualElementRendererFlags.AutoTrack) != 0; }
set {
if (value)
_flags |= VisualElementRendererFlags.AutoTrack;
else
_flags &= ~VisualElementRendererFlags.AutoTrack;
}
}
public VisualElementRenderer (string tagName = "div") : base (tagName)
2017-11-09 21:03:56 +00:00
{
_propertyChangedHandler = OnElementPropertyChanged;
}
protected virtual void OnElementChanged (ElementChangedEventArgs<TElement> e)
{
var args = new VisualElementChangedEventArgs (e.OldElement, e.NewElement);
for (int i = 0; i < _elementChangedHandlers.Count; i++) {
_elementChangedHandlers[i] (this, args);
}
var changed = ElementChanged;
if (changed != null)
changed (this, e);
}
public event EventHandler<ElementChangedEventArgs<TElement>> ElementChanged;
void IVisualElementRenderer.SetElement (VisualElement element)
{
SetElement ((TElement)element);
}
public void SetElement (TElement element)
{
var oldElement = Element;
Element = element;
if (oldElement != null)
oldElement.PropertyChanged -= _propertyChangedHandler;
if (element != null) {
if (element.BackgroundColor != Xamarin.Forms.Color.Default || (oldElement != null && element.BackgroundColor != oldElement.BackgroundColor))
SetBackgroundColor (element.BackgroundColor);
if (_tracker == null) {
_tracker = new VisualElementTracker (this);
_tracker.NativeControlUpdated += (sender, e) => UpdateNativeWidget ();
}
2017-11-09 22:17:36 +00:00
if (AutoPackage && _packager == null) {
_packager = new VisualElementPackager (this);
_packager.Load ();
}
2017-11-09 21:03:56 +00:00
2017-12-10 04:39:10 +00:00
if (AutoTrack && _events == null) {
_events = new EventTracker (this);
_events.LoadEvents (this);
}
2017-11-09 21:03:56 +00:00
element.PropertyChanged += _propertyChangedHandler;
}
OnElementChanged (new ElementChangedEventArgs<TElement> (oldElement, element));
if (element != null)
SendVisualElementInitialized (element, this);
if (Element != null && !string.IsNullOrEmpty (Element.AutomationId))
SetAutomationId (Element.AutomationId);
}
public void SetElementSize (Size size)
{
Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion (Element, new Rectangle (Element.X, Element.Y, size.Width, size.Height));
}
2017-11-10 02:12:02 +00:00
public virtual void SetControlSize (Size size)
{
}
2017-11-09 21:03:56 +00:00
protected virtual void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName) {
SetBackgroundColor (Element.BackgroundColor);
}
else if (e.PropertyName == Layout.IsClippedToBoundsProperty.PropertyName) {
//UpdateClipToBounds ();
}
}
protected virtual void OnRegisterEffect (PlatformEffect effect)
{
//effect.Container = this;
}
protected virtual void SetAutomationId (string id)
{
}
protected virtual void SetBackgroundColor (Xamarin.Forms.Color color)
{
if (color == Xamarin.Forms.Color.Default)
Style.BackgroundColor = _defaultColor;
else
Style.BackgroundColor = color.ToOouiColor ();
}
protected virtual void UpdateNativeWidget ()
{
}
protected virtual void SendVisualElementInitialized (VisualElement element, Element nativeView)
{
element.SendViewInitialized (nativeView);
}
2017-11-10 00:09:48 +00:00
public virtual SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint)
{
return NativeView.GetSizeRequest (widthConstraint, heightConstraint);
}
2017-11-09 21:03:56 +00:00
protected virtual void Dispose (bool disposing)
{
if (!disposedValue) {
if (disposing) {
}
disposedValue = true;
}
}
public void Dispose ()
{
Dispose (true);
}
}
2017-11-09 07:57:04 +00:00
}