diff options
Diffstat (limited to 'Xamarin.Forms.Platform.iOS/ViewRenderer.cs')
-rw-r--r-- | Xamarin.Forms.Platform.iOS/ViewRenderer.cs | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.iOS/ViewRenderer.cs b/Xamarin.Forms.Platform.iOS/ViewRenderer.cs new file mode 100644 index 00000000..5b3be1c3 --- /dev/null +++ b/Xamarin.Forms.Platform.iOS/ViewRenderer.cs @@ -0,0 +1,151 @@ +using System; +using System.Drawing; +using System.ComponentModel; +#if __UNIFIED__ +using UIKit; +#else +using MonoTouch.UIKit; +#endif +#if __UNIFIED__ +using RectangleF = CoreGraphics.CGRect; +using SizeF = CoreGraphics.CGSize; +using PointF = CoreGraphics.CGPoint; + +#else +using nfloat=System.Single; +using nint=System.Int32; +using nuint=System.UInt32; +#endif + +namespace Xamarin.Forms.Platform.iOS +{ + public abstract class ViewRenderer : ViewRenderer<View, UIView> + { + } + + public abstract class ViewRenderer<TView, TNativeView> : VisualElementRenderer<TView> where TView : View where TNativeView : UIView + { + UIColor _defaultColor; + + public TNativeView Control { get; private set; } + + public override void LayoutSubviews() + { + base.LayoutSubviews(); + if (Control != null) + Control.Frame = new RectangleF(0, 0, (nfloat)Element.Width, (nfloat)Element.Height); + } + + public override SizeF SizeThatFits(SizeF size) + { + return Control.SizeThatFits(size); + } + + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + + if (disposing && Control != null) + { + Control.Dispose(); + Control = null; + } + } + + protected override void OnElementChanged(ElementChangedEventArgs<TView> e) + { + base.OnElementChanged(e); + + if (e.OldElement != null) + e.OldElement.FocusChangeRequested -= ViewOnFocusChangeRequested; + + if (e.NewElement != null) + { + if (Control != null && e.OldElement != null && e.OldElement.BackgroundColor != e.NewElement.BackgroundColor || e.NewElement.BackgroundColor != Color.Default) + SetBackgroundColor(e.NewElement.BackgroundColor); + + e.NewElement.FocusChangeRequested += ViewOnFocusChangeRequested; + } + + UpdateIsEnabled(); + } + + protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (Control != null) + { + if (e.PropertyName == VisualElement.IsEnabledProperty.PropertyName) + UpdateIsEnabled(); + else if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName) + SetBackgroundColor(Element.BackgroundColor); + } + + base.OnElementPropertyChanged(sender, e); + } + + protected override void OnRegisterEffect(PlatformEffect effect) + { + base.OnRegisterEffect(effect); + effect.Control = Control; + } + + protected override void SetAutomationId(string id) + { + if (Control == null) + base.SetAutomationId(id); + else + { + AccessibilityIdentifier = id + "_Container"; + Control.AccessibilityIdentifier = id; + } + } + + protected override void SetBackgroundColor(Color color) + { + if (Control == null) + return; + + if (color == Color.Default) + Control.BackgroundColor = _defaultColor; + else + Control.BackgroundColor = color.ToUIColor(); + } + + protected void SetNativeControl(TNativeView uiview) + { + _defaultColor = uiview.BackgroundColor; + Control = uiview; + + if (Element.BackgroundColor != Color.Default) + SetBackgroundColor(Element.BackgroundColor); + + UpdateIsEnabled(); + + AddSubview(uiview); + } + + internal override void SendVisualElementInitialized(VisualElement element, UIView nativeView) + { + base.SendVisualElementInitialized(element, Control); + } + + void UpdateIsEnabled() + { + if (Element == null || Control == null) + return; + + var uiControl = Control as UIControl; + if (uiControl == null) + return; + uiControl.Enabled = Element.IsEnabled; + } + + void ViewOnFocusChangeRequested(object sender, VisualElement.FocusRequestArgs focusRequestArgs) + { + if (Control == null) + return; + + focusRequestArgs.Result = focusRequestArgs.Focus ? Control.BecomeFirstResponder() : Control.ResignFirstResponder(); + } + } +}
\ No newline at end of file |