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

133 lines
4.0 KiB
C#
Raw Normal View History

2017-11-26 19:30:19 +00:00
using System;
using System.ComponentModel;
using Xamarin.Forms;
using Ooui.Forms.Extensions;
namespace Ooui.Forms.Renderers
{
public class EditorRenderer : ViewRenderer<Editor, TextArea>
{
bool _disposed;
IEditorController ElementController => Element;
2017-12-10 07:22:39 +00:00
public override SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint)
{
var size = new Size (160, 100);
return new SizeRequest (size, size);
}
2017-11-26 19:30:19 +00:00
protected override void Dispose (bool disposing)
{
if (_disposed)
return;
_disposed = true;
if (disposing) {
if (Control != null) {
Control.Input -= HandleChanged;
2017-11-26 19:30:19 +00:00
//Control.Started -= OnStarted;
Control.Change -= OnEnded;
2017-11-26 19:30:19 +00:00
}
}
base.Dispose (disposing);
}
protected override void OnElementChanged (ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged (e);
if (e.NewElement == null)
return;
if (Control == null) {
2018-04-16 01:09:09 +00:00
var textArea = new TextArea {
2017-11-26 19:30:19 +00:00
ClassName = "form-control"
2018-04-16 01:09:09 +00:00
};
textArea.Style["resize"] = "none";
SetNativeControl (textArea);
2017-11-26 19:30:19 +00:00
Control.Input += HandleChanged;
2017-11-26 19:30:19 +00:00
//Control.Started += OnStarted;
Control.Change += OnEnded;
2017-11-26 19:30:19 +00:00
}
UpdateText ();
UpdateFont ();
UpdateTextColor ();
UpdateKeyboard ();
UpdateEditable ();
UpdateTextAlignment ();
}
protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged (sender, e);
if (e.PropertyName == Editor.TextProperty.PropertyName)
UpdateText ();
else if (e.PropertyName == Xamarin.Forms.InputView.KeyboardProperty.PropertyName)
UpdateKeyboard ();
else if (e.PropertyName == VisualElement.IsEnabledProperty.PropertyName)
UpdateEditable ();
else if (e.PropertyName == Editor.TextColorProperty.PropertyName)
UpdateTextColor ();
else if (e.PropertyName == Editor.FontAttributesProperty.PropertyName)
UpdateFont ();
else if (e.PropertyName == Editor.FontFamilyProperty.PropertyName)
UpdateFont ();
else if (e.PropertyName == Editor.FontSizeProperty.PropertyName)
UpdateFont ();
}
void HandleChanged (object sender, EventArgs e)
{
2017-11-26 20:18:33 +00:00
ElementController.SetValueFromRenderer (Editor.TextProperty, Control.Value);
2017-11-26 19:30:19 +00:00
}
void OnEnded (object sender, EventArgs eventArgs)
{
2017-11-26 20:18:33 +00:00
if (Control.Value != Element.Text)
ElementController.SetValueFromRenderer (Editor.TextProperty, Control.Value);
2017-11-26 19:30:19 +00:00
Element.SetValue (VisualElement.IsFocusedPropertyKey, false);
ElementController.SendCompleted ();
}
void OnStarted (object sender, EventArgs eventArgs)
{
ElementController.SetValueFromRenderer (VisualElement.IsFocusedPropertyKey, true);
}
void UpdateEditable ()
{
Control.IsDisabled = !Element.IsEnabled;
}
void UpdateFont ()
{
Element.SetStyleFont (Element.FontFamily, Element.FontSize, Element.FontAttributes, Control.Style);
}
void UpdateKeyboard ()
{
}
void UpdateText ()
{
2017-11-26 20:18:33 +00:00
if (Control.Value != Element.Text)
Control.Value = Element.Text;
2017-11-26 19:30:19 +00:00
}
void UpdateTextAlignment ()
{
}
void UpdateTextColor ()
{
2018-04-26 05:23:26 +00:00
Control.Style.Color = Element.TextColor.ToOouiColor (OouiTheme.TextColor);
2017-11-26 19:30:19 +00:00
}
}
}