From 31170de0ed083f86a33e533e9ea49f566a9db49f Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Thu, 16 Nov 2017 16:19:31 -0600 Subject: [PATCH] Add XF Entry --- Ooui.Forms/Renderers/EntryRenderer.cs | 167 ++++++++++++++++++++++++++ Ooui/Input.cs | 6 + 2 files changed, 173 insertions(+) create mode 100644 Ooui.Forms/Renderers/EntryRenderer.cs diff --git a/Ooui.Forms/Renderers/EntryRenderer.cs b/Ooui.Forms/Renderers/EntryRenderer.cs new file mode 100644 index 0000000..d48f78e --- /dev/null +++ b/Ooui.Forms/Renderers/EntryRenderer.cs @@ -0,0 +1,167 @@ +using System; +using System.ComponentModel; +using System.Diagnostics; +using Ooui.Forms.Extensions; +using Xamarin.Forms; + +namespace Ooui.Forms.Renderers +{ + public class EntryRenderer : ViewRenderer + { + Ooui.Color _defaultTextColor; + bool _disposed; + + static Size initialSize = Size.Zero; + + public EntryRenderer () + { + } + + public override SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint) + { + return base.GetDesiredSize (widthConstraint, heightConstraint); + } + + IElementController ElementController => Element as IElementController; + + protected override void Dispose (bool disposing) + { + if (_disposed) + return; + + _disposed = true; + + if (disposing) { + if (Control != null) { + //Control.Inputted -= OnEditingBegan; + Control.Inputted -= OnEditingChanged; + Control.Changed -= OnEditingEnded; + } + } + + base.Dispose (disposing); + } + + protected override void OnElementChanged (ElementChangedEventArgs e) + { + base.OnElementChanged (e); + + if (e.NewElement == null) + return; + + if (Control == null) { + var textField = new Ooui.Input (InputType.Text); + SetNativeControl (textField); + + _defaultTextColor = Colors.Black; + + textField.Inputted += OnEditingChanged; + + //textField.EditingDidBegin += OnEditingBegan; + textField.Changed += OnEditingEnded; + } + + UpdatePlaceholder (); + UpdatePassword (); + UpdateText (); + UpdateColor (); + UpdateFont (); + UpdateKeyboard (); + UpdateAlignment (); + } + + protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == Entry.PlaceholderProperty.PropertyName || e.PropertyName == Entry.PlaceholderColorProperty.PropertyName) + UpdatePlaceholder (); + else if (e.PropertyName == Entry.IsPasswordProperty.PropertyName) + UpdatePassword (); + else if (e.PropertyName == Entry.TextProperty.PropertyName) + UpdateText (); + else if (e.PropertyName == Entry.TextColorProperty.PropertyName) + UpdateColor (); + else if (e.PropertyName == Xamarin.Forms.InputView.KeyboardProperty.PropertyName) + UpdateKeyboard (); + else if (e.PropertyName == Entry.HorizontalTextAlignmentProperty.PropertyName) + UpdateAlignment (); + else if (e.PropertyName == Entry.FontAttributesProperty.PropertyName) + UpdateFont (); + else if (e.PropertyName == Entry.FontFamilyProperty.PropertyName) + UpdateFont (); + else if (e.PropertyName == Entry.FontSizeProperty.PropertyName) + UpdateFont (); + else if (e.PropertyName == VisualElement.IsEnabledProperty.PropertyName) { + UpdateColor (); + UpdatePlaceholder (); + } + + base.OnElementPropertyChanged (sender, e); + } + + void OnEditingBegan (object sender, EventArgs e) + { + ElementController.SetValueFromRenderer (VisualElement.IsFocusedPropertyKey, true); + } + + void OnEditingChanged (object sender, EventArgs eventArgs) + { + ElementController.SetValueFromRenderer (Entry.TextProperty, Control.Value); + } + + void OnEditingEnded (object sender, EventArgs e) + { + // Typing aid changes don't always raise EditingChanged event + if (Control.Text != Element.Text) { + ElementController.SetValueFromRenderer (Entry.TextProperty, Control.Text); + } + + ElementController.SetValueFromRenderer (VisualElement.IsFocusedPropertyKey, false); + } + + void UpdateAlignment () + { + Control.Style.TextAlign = Element.HorizontalTextAlignment.ToOouiTextAlign (); + } + + void UpdateColor () + { + var textColor = Element.TextColor; + + if (textColor.IsDefault || !Element.IsEnabled) + Control.Style.Color = _defaultTextColor; + else + Control.Style.Color = textColor.ToOouiColor (); + } + + void UpdateFont () + { + if (initialSize == Size.Zero) { + var testString = "Tj"; + initialSize = testString.MeasureSize (Control.Style); + } + + Element.SetStyleFont (Control.Style); + } + + void UpdateKeyboard () + { + } + + void UpdatePassword () + { + Control.Type = Element.IsPassword ? InputType.Password : InputType.Text; + } + + void UpdatePlaceholder () + { + Control.Placeholder = Element.Placeholder; + } + + void UpdateText () + { + // ReSharper disable once RedundantCheckBeforeAssignment + if (Control.Text != Element.Text) + Control.Text = Element.Text; + } + } +} diff --git a/Ooui/Input.cs b/Ooui/Input.cs index e24abe9..9a2d2f2 100644 --- a/Ooui/Input.cs +++ b/Ooui/Input.cs @@ -40,6 +40,12 @@ namespace Ooui remove => RemoveEventListener ("input", value); } + string placeholder = ""; + public string Placeholder { + get => placeholder; + set => SetProperty (ref placeholder, value, "placeholder"); + } + bool isChecked = false; public bool IsChecked { get => isChecked;