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

186 lines
6.1 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
{
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-12-11 04:23:19 +00:00
var text = Element.Text;
if (text == null || text.Length == 0) {
text = Element.Placeholder;
}
if (text == null || text.Length == 0) {
text = " ";
2017-12-11 04:23:19 +00:00
}
var size = text.MeasureSize (Element.FontFamily, Element.FontSize, Element.FontAttributes, widthConstraint, heightConstraint);
2018-04-16 02:06:31 +00:00
var vpadding = 16;
var hpadding = 32;
size = new Size (size.Width + hpadding, size.Height + vpadding);
2017-11-26 16:12:28 +00:00
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) {
2018-04-19 01:03:02 +00:00
if (Element != null) {
Element.FocusChangeRequested -= Element_FocusChangeRequested;
}
2017-11-16 22:19:31 +00:00
if (Control != null) {
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);
2018-04-19 01:03:02 +00:00
if (e.OldElement != null) {
e.OldElement.FocusChangeRequested -= Element_FocusChangeRequested;
}
2017-11-16 22:19:31 +00:00
if (e.NewElement == null)
return;
2018-04-19 01:03:02 +00:00
e.NewElement.FocusChangeRequested += Element_FocusChangeRequested;
2017-11-16 22:19:31 +00:00
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";
textField.Input += OnEditingChanged;
textField.Change += OnEditingEnded;
2017-11-16 22:19:31 +00:00
}
UpdatePlaceholder ();
UpdatePassword ();
UpdateText ();
UpdateColor ();
UpdateFont ();
UpdateKeyboard ();
UpdateAlignment ();
}
2018-04-19 01:03:02 +00:00
void Element_FocusChangeRequested (object sender, VisualElement.FocusRequestArgs e)
{
if (e.Focus && Control != null) {
Control?.Focus ();
e.Result = true;
}
}
2017-11-16 22:19:31 +00:00
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
2017-12-11 04:28:05 +00:00
if (Control.Value != Element.Text) {
ElementController.SetValueFromRenderer (Entry.TextProperty, Control.Value);
2017-11-16 22:19:31 +00:00
}
ElementController.SetValueFromRenderer (VisualElement.IsFocusedPropertyKey, false);
}
void UpdateAlignment ()
{
Control.Style.TextAlign = Element.HorizontalTextAlignment.ToOouiTextAlign ();
}
void UpdateColor ()
{
2018-04-26 05:23:26 +00:00
Control.Style.Color = Element.TextColor.ToOouiColor (OouiTheme.TextColor);
2017-11-16 22:19:31 +00:00
}
void UpdateFont ()
{
if (initialSize == Size.Zero) {
var testString = "Tj";
initialSize = testString.MeasureSize (Control.Style, double.PositiveInfinity, double.PositiveInfinity);
2017-11-16 22:19:31 +00:00
}
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
}
}
}