2017-12-28 20:01:00 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using Ooui.Forms.Extensions;
|
|
|
|
|
using Xamarin.Forms;
|
|
|
|
|
|
|
|
|
|
namespace Ooui.Forms.Renderers
|
|
|
|
|
{
|
|
|
|
|
public class LinkLabelRenderer : ViewRenderer<LinkLabel, Anchor>
|
|
|
|
|
{
|
|
|
|
|
SizeRequest _perfectSize;
|
|
|
|
|
|
|
|
|
|
bool _perfectSizeValid;
|
|
|
|
|
|
|
|
|
|
public override SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint)
|
|
|
|
|
{
|
|
|
|
|
if (!_perfectSizeValid) {
|
2018-02-03 01:15:38 +00:00
|
|
|
|
var size = Element.Text.MeasureSize (Element.FontFamily, Element.FontSize, Element.FontAttributes, double.PositiveInfinity, double.PositiveInfinity);
|
2017-12-28 20:01:00 +00:00
|
|
|
|
size.Width = Math.Ceiling (size.Width);
|
2018-02-03 01:15:38 +00:00
|
|
|
|
size.Height = Math.Ceiling (size.Height);
|
|
|
|
|
_perfectSize = new SizeRequest (size, new Size (Element.FontSize, Element.FontSize));
|
2017-12-28 20:01:00 +00:00
|
|
|
|
_perfectSizeValid = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var widthFits = widthConstraint >= _perfectSize.Request.Width;
|
|
|
|
|
var heightFits = heightConstraint >= _perfectSize.Request.Height;
|
|
|
|
|
|
|
|
|
|
if (widthFits && heightFits)
|
|
|
|
|
return _perfectSize;
|
|
|
|
|
|
2018-02-03 01:15:38 +00:00
|
|
|
|
var resultRequestSize = Element.Text.MeasureSize (Element.FontFamily, Element.FontSize, Element.FontAttributes, widthConstraint, heightConstraint);
|
|
|
|
|
var result = new SizeRequest (resultRequestSize, resultRequestSize);
|
2017-12-28 20:01:00 +00:00
|
|
|
|
var tinyWidth = Math.Min (10, result.Request.Width);
|
|
|
|
|
result.Minimum = new Size (tinyWidth, result.Request.Height);
|
|
|
|
|
|
|
|
|
|
if (widthFits || Element.LineBreakMode == LineBreakMode.NoWrap)
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
bool containerIsNotInfinitelyWide = !double.IsInfinity (widthConstraint);
|
|
|
|
|
|
|
|
|
|
if (containerIsNotInfinitelyWide) {
|
|
|
|
|
bool textCouldHaveWrapped = Element.LineBreakMode == LineBreakMode.WordWrap || Element.LineBreakMode == LineBreakMode.CharacterWrap;
|
|
|
|
|
bool textExceedsContainer = result.Request.Width > widthConstraint;
|
|
|
|
|
|
|
|
|
|
if (textExceedsContainer || textCouldHaveWrapped) {
|
|
|
|
|
var expandedWidth = Math.Max (tinyWidth, widthConstraint);
|
|
|
|
|
result.Request = new Size (expandedWidth, result.Request.Height);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnElementChanged (ElementChangedEventArgs<Ooui.Forms.LinkLabel> e)
|
|
|
|
|
{
|
|
|
|
|
if (e.NewElement != null) {
|
|
|
|
|
if (Control == null) {
|
|
|
|
|
SetNativeControl (new Anchor ());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UpdateHRef ();
|
2018-02-02 06:52:39 +00:00
|
|
|
|
UpdateTarget ();
|
2017-12-28 20:01:00 +00:00
|
|
|
|
|
|
|
|
|
UpdateText ();
|
|
|
|
|
UpdateTextColor ();
|
|
|
|
|
UpdateFont ();
|
|
|
|
|
|
|
|
|
|
UpdateLineBreakMode ();
|
|
|
|
|
UpdateAlignment ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.OnElementChanged (e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnElementPropertyChanged (sender, e);
|
|
|
|
|
|
|
|
|
|
if (Control == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (e.PropertyName == Ooui.Forms.LinkLabel.HRefProperty.PropertyName)
|
|
|
|
|
UpdateHRef ();
|
2018-02-02 17:20:24 +00:00
|
|
|
|
if (e.PropertyName == Ooui.Forms.LinkLabel.TargetProperty.PropertyName)
|
|
|
|
|
UpdateTarget ();
|
2017-12-28 20:01:00 +00:00
|
|
|
|
else if (e.PropertyName == Xamarin.Forms.Label.HorizontalTextAlignmentProperty.PropertyName)
|
|
|
|
|
UpdateAlignment ();
|
|
|
|
|
else if (e.PropertyName == Xamarin.Forms.Label.VerticalTextAlignmentProperty.PropertyName)
|
|
|
|
|
UpdateAlignment ();
|
|
|
|
|
else if (e.PropertyName == Xamarin.Forms.Label.TextColorProperty.PropertyName)
|
|
|
|
|
UpdateTextColor ();
|
|
|
|
|
else if (e.PropertyName == Xamarin.Forms.Label.FontProperty.PropertyName)
|
|
|
|
|
UpdateFont ();
|
|
|
|
|
else if (e.PropertyName == Xamarin.Forms.Label.TextProperty.PropertyName)
|
|
|
|
|
UpdateText ();
|
|
|
|
|
else if (e.PropertyName == Xamarin.Forms.Label.FormattedTextProperty.PropertyName)
|
|
|
|
|
UpdateText ();
|
|
|
|
|
else if (e.PropertyName == Xamarin.Forms.Label.LineBreakModeProperty.PropertyName)
|
|
|
|
|
UpdateLineBreakMode ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void SetBackgroundColor (Xamarin.Forms.Color color)
|
|
|
|
|
{
|
2018-04-26 05:23:26 +00:00
|
|
|
|
Style.BackgroundColor = color.ToOouiColor (Colors.Clear);
|
2017-12-28 20:01:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateHRef ()
|
|
|
|
|
{
|
|
|
|
|
Control.HRef = Element.HRef;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-02 06:52:39 +00:00
|
|
|
|
void UpdateTarget ()
|
|
|
|
|
{
|
2018-02-02 17:20:24 +00:00
|
|
|
|
Control.Target = Element.Target;
|
2018-02-02 06:52:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-28 20:01:00 +00:00
|
|
|
|
void UpdateAlignment ()
|
|
|
|
|
{
|
|
|
|
|
this.Style.Display = "table";
|
|
|
|
|
Control.Style.Display = "table-cell";
|
|
|
|
|
this.Style.TextAlign = Element.HorizontalTextAlignment.ToOouiTextAlign ();
|
|
|
|
|
Control.Style.VerticalAlign = Element.VerticalTextAlignment.ToOouiVerticalAlign ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateLineBreakMode ()
|
|
|
|
|
{
|
|
|
|
|
_perfectSizeValid = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isTextFormatted;
|
|
|
|
|
void UpdateText ()
|
|
|
|
|
{
|
|
|
|
|
_perfectSizeValid = false;
|
|
|
|
|
|
2020-03-09 01:01:51 +00:00
|
|
|
|
if (Element.FormattedText != null) {
|
|
|
|
|
Control.Text = Element.Text;
|
2017-12-28 20:01:00 +00:00
|
|
|
|
isTextFormatted = true;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2020-03-09 01:01:51 +00:00
|
|
|
|
Control.Text = Element.Text;
|
2017-12-28 20:01:00 +00:00
|
|
|
|
isTextFormatted = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateFont ()
|
|
|
|
|
{
|
|
|
|
|
if (isTextFormatted)
|
|
|
|
|
return;
|
|
|
|
|
_perfectSizeValid = false;
|
|
|
|
|
|
|
|
|
|
Element.SetStyleFont (Element.FontFamily, Element.FontSize, Element.FontAttributes, Control.Style);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UpdateTextColor ()
|
|
|
|
|
{
|
|
|
|
|
if (isTextFormatted)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_perfectSizeValid = false;
|
|
|
|
|
|
|
|
|
|
var textColor = Element.TextColor;
|
|
|
|
|
if (textColor.IsDefault) {
|
|
|
|
|
Control.Style.Color = null;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Control.Style.Color = textColor.ToOouiColor (Xamarin.Forms.Color.Black);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|