Ooui-tws-port/Ooui.Forms/Renderers/EntryRenderer.cs

170 lines
5.5 KiB
C#
Raw Normal View History

2017-11-16 22:19:31 +00:00
using System;
using System.ComponentModel;
using System.Diagnostics;
using Ooui.Forms.Extensions;
using Xamarin.Forms;
namespace Ooui.Forms.Renderers
{
public class EntryRenderer : ViewRenderer<Entry, Ooui.TextInput>
2017-11-16 22:19:31 +00:00
{
Ooui.Color _defaultTextColor;
bool _disposed;
static Size initialSize = Size.Zero;
2017-11-26 16:12:28 +00:00
IElementController ElementController => Element as IElementController;
2017-11-16 22:19:31 +00:00
public override SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint)
{
2017-11-26 16:12:28 +00:00
var size = Element.Text.MeasureSize (Element.FontFamily, Element.FontSize, Element.FontAttributes);
size = new Size (size.Width, size.Height * 1.428 + 14);
return new SizeRequest (size, size);
2017-11-16 22:19:31 +00:00
}
protected override void Dispose (bool disposing)
{
if (_disposed)
return;
_disposed = true;
if (disposing) {
if (Control != null) {
//Control.Inputted -= OnEditingBegan;
Control.Input -= OnEditingChanged;
Control.Change -= OnEditingEnded;
2017-11-16 22:19:31 +00:00
}
}
base.Dispose (disposing);
}
protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged (e);
if (e.NewElement == null)
return;
if (Control == null) {
var textField = new Ooui.TextInput ();
2017-11-16 22:19:31 +00:00
SetNativeControl (textField);
2017-11-26 16:12:28 +00:00
Debug.Assert (Control != null, "Control != null");
textField.ClassName = "form-control";
2017-11-16 22:19:31 +00:00
_defaultTextColor = Colors.Black;
textField.Input += OnEditingChanged;
2017-11-16 22:19:31 +00:00
//textField.EditingDidBegin += OnEditingBegan;
textField.Change += OnEditingEnded;
2017-11-16 22:19:31 +00:00
}
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);
}
2017-11-26 15:57:32 +00:00
Element.SetStyleFont (Element.FontFamily, Element.FontSize, Element.FontAttributes, Control.Style);
2017-11-16 22:19:31 +00:00
}
void UpdateKeyboard ()
{
}
void UpdatePassword ()
{
Control.Type = Element.IsPassword ? InputType.Password : InputType.Text;
}
void UpdatePlaceholder ()
{
2017-11-26 16:12:28 +00:00
Control.Placeholder = Element.Placeholder ?? "";
2017-11-16 22:19:31 +00:00
}
void UpdateText ()
{
// ReSharper disable once RedundantCheckBeforeAssignment
2017-11-26 16:12:28 +00:00
if (Control.Value != Element.Text)
Control.Value = Element.Text;
2017-11-16 22:19:31 +00:00
}
}
}