Added Forms EditorRenderer
This commit is contained in:
parent
7c36801d5e
commit
98efd72b61
|
@ -7,6 +7,7 @@ using Xamarin.Forms.Internals;
|
|||
[assembly: Dependency (typeof (ResourcesProvider))]
|
||||
[assembly: ExportRenderer (typeof (BoxView), typeof (BoxRenderer))]
|
||||
[assembly: ExportRenderer (typeof (Button), typeof (ButtonRenderer))]
|
||||
[assembly: ExportRenderer(typeof(Editor), typeof(EditorRenderer))]
|
||||
[assembly: ExportRenderer (typeof (Entry), typeof (EntryRenderer))]
|
||||
[assembly: ExportRenderer (typeof (Label), typeof (LabelRenderer))]
|
||||
|
||||
|
|
|
@ -0,0 +1,124 @@
|
|||
using Ooui.Forms.Extensions;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace Ooui.Forms.Renderers
|
||||
{
|
||||
public class EditorRenderer : ViewRenderer<Editor, Ooui.TextArea>
|
||||
{
|
||||
private bool _disposed;
|
||||
private Ooui.Color _defaultTextColor;
|
||||
|
||||
static Size initialSize = Size.Zero;
|
||||
|
||||
protected IElementController ElementController => Element as IElementController;
|
||||
|
||||
public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed)
|
||||
return;
|
||||
|
||||
_disposed = true;
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
if (Control != null)
|
||||
{
|
||||
Control.Inputted -= OnEditingChanged;
|
||||
Control.Changed -= OnEditingEnded;
|
||||
}
|
||||
}
|
||||
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
|
||||
{
|
||||
if (e.NewElement == null)
|
||||
return;
|
||||
|
||||
if (Control == null)
|
||||
{
|
||||
SetNativeControl(new Ooui.TextArea());
|
||||
|
||||
_defaultTextColor = Colors.Black;
|
||||
|
||||
Debug.Assert(Control != null, "Control != null");
|
||||
|
||||
Control.Inputted += OnEditingChanged;
|
||||
Control.Changed += OnEditingEnded;
|
||||
}
|
||||
|
||||
UpdateText();
|
||||
UpdateTextColor();
|
||||
UpdateFont();
|
||||
|
||||
base.OnElementChanged(e);
|
||||
}
|
||||
|
||||
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
base.OnElementPropertyChanged(sender, e);
|
||||
|
||||
if (e.PropertyName == Editor.TextProperty.PropertyName)
|
||||
UpdateText();
|
||||
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 UpdateText()
|
||||
{
|
||||
if (Control.Value != Element.Text)
|
||||
Control.Value = Element.Text;
|
||||
}
|
||||
|
||||
void UpdateTextColor()
|
||||
{
|
||||
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(Element.FontFamily, Element.FontSize, Element.FontAttributes, Control.Style);
|
||||
}
|
||||
|
||||
private void OnEditingChanged(object sender, TargetEventArgs e)
|
||||
{
|
||||
ElementController.SetValueFromRenderer(Editor.TextProperty, Control.Value);
|
||||
}
|
||||
|
||||
private void OnEditingEnded(object sender, TargetEventArgs e)
|
||||
{
|
||||
if (Control.Text != Element.Text)
|
||||
{
|
||||
ElementController.SetValueFromRenderer(Editor.TextProperty, Control.Text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
using Ooui;
|
||||
using Xamarin.Forms;
|
||||
|
||||
namespace Samples
|
||||
{
|
||||
public class EditorSample : ISample
|
||||
{
|
||||
public string Title => "Editor Sample";
|
||||
|
||||
public Ooui.Element CreateElement()
|
||||
{
|
||||
var panel = new StackLayout();
|
||||
|
||||
var titleLabel = new Xamarin.Forms.Label
|
||||
{
|
||||
Text = "Editor"
|
||||
};
|
||||
panel.Children.Add(titleLabel);
|
||||
|
||||
var editor = new Editor();
|
||||
panel.Children.Add(editor);
|
||||
|
||||
var labelEditor = new Xamarin.Forms.Label();
|
||||
panel.Children.Add(labelEditor);
|
||||
|
||||
editor.TextChanged += (sender, args) =>
|
||||
{
|
||||
labelEditor.Text = args.NewTextValue;
|
||||
};
|
||||
|
||||
var page = new ContentPage
|
||||
{
|
||||
Content = panel
|
||||
};
|
||||
|
||||
return page.GetOouiElement();
|
||||
}
|
||||
|
||||
public void Publish()
|
||||
{
|
||||
UI.Publish("/editor", CreateElement);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -30,6 +30,7 @@ namespace Samples
|
|||
new DrawSample ().Publish ();
|
||||
new FilesSample ().Publish ();
|
||||
new DisplayAlertSample ().Publish ();
|
||||
new EditorSample().Publish();
|
||||
|
||||
UI.Present ("/display-alert");
|
||||
|
||||
|
|
Loading…
Reference in New Issue