using System;
using ElmSharp;
using ELabel = ElmSharp.Label;
using EColor = ElmSharp.Color;
using ESize = ElmSharp.Size;
namespace Xamarin.Forms.Platform.Tizen.Native
{
///
/// The Label class extends ElmSharp.Label to be better suited for Xamarin renderers.
/// Mainly the formatted text support.
///
public class Label : ELabel, ITextable, IMeasurable, IBatchable
{
///
/// The _span holds the content of the label.
///
readonly Span _span = new Span();
///
/// Initializes a new instance of the class.
///
/// Parent evas object.
public Label(EvasObject parent) : base(parent)
{
}
///
/// Get or sets the formatted text.
///
/// Setting FormattedText changes the value of the Text property.
/// The formatted text.
public FormattedString FormattedText
{
get
{
return _span.FormattedText;
}
set
{
if (value != _span.FormattedText)
{
_span.FormattedText = value;
ApplyTextAndStyle();
}
}
}
///
/// Gets or sets the text.
///
/// Setting Text overwrites the value of the FormattedText property too.
/// The content of the label.
public override string Text
{
get
{
return _span.Text;
}
set
{
if (value != _span.Text)
{
_span.Text = value;
ApplyTextAndStyle();
}
}
}
///
/// Gets or sets the color of the formatted text.
///
/// The color of the text.
public EColor TextColor
{
get
{
return _span.ForegroundColor;
}
set
{
if (!_span.ForegroundColor.Equals(value))
{
_span.ForegroundColor = value;
ApplyTextAndStyle();
}
}
}
///
/// Gets or sets the background color for the text.
///
/// The color of the label's background.
public EColor TextBackgroundColor
{
get
{
return _span.BackgroundColor;
}
set
{
if (!_span.BackgroundColor.Equals(value))
{
_span.BackgroundColor = value;
ApplyTextAndStyle();
}
}
}
///
/// Gets or sets the font family for the text.
///
/// The font family.
public string FontFamily
{
get
{
return _span.FontFamily;
}
set
{
if (value != _span.FontFamily)
{
_span.FontFamily = value;
ApplyTextAndStyle();
}
}
}
///
/// Gets or sets the font attributes.
///
/// The font attributes.
public FontAttributes FontAttributes
{
get
{
return _span.FontAttributes;
}
set
{
if (value != _span.FontAttributes)
{
_span.FontAttributes = value;
ApplyTextAndStyle();
}
}
}
///
/// Gets or sets the font size for the text.
///
/// The size of the font.
public double FontSize
{
get
{
return _span.FontSize;
}
set
{
if (value != _span.FontSize)
{
_span.FontSize = value;
ApplyTextAndStyle();
}
}
}
///
/// Gets or sets the font weight for the text.
///
/// The weight of the font.
public string FontWeight
{
get
{
return _span.FontWeight;
}
set
{
if (value != _span.FontWeight)
{
_span.FontWeight = value;
ApplyTextAndStyle();
}
}
}
///
/// Gets or sets the line wrap option.
///
/// The line break mode.
public LineBreakMode LineBreakMode
{
get
{
return _span.LineBreakMode;
}
set
{
if (value != _span.LineBreakMode)
{
var previousMode = _span.LineBreakMode;
_span.LineBreakMode = value;
switch(value)
{
case LineBreakMode.NoWrap:
LineWrapType = WrapType.None;
IsEllipsis = false;
break;
case LineBreakMode.CharacterWrap:
LineWrapType = WrapType.Char;
IsEllipsis = false;
break;
case LineBreakMode.WordWrap:
LineWrapType = WrapType.Word;
IsEllipsis = false;
break;
case LineBreakMode.MixedWrap:
LineWrapType = WrapType.Mixed;
IsEllipsis = false;
break;
default:
LineWrapType = WrapType.None;
IsEllipsis = true;
break;
}
ApplyTextAndStyle();
}
}
}
///
/// Gets or sets the horizontal text alignment.
///
/// The horizontal text alignment.
public TextAlignment HorizontalTextAlignment
{
get
{
return _span.HorizontalTextAlignment;
}
set
{
if (value != _span.HorizontalTextAlignment)
{
_span.HorizontalTextAlignment = value;
ApplyTextAndStyle();
}
}
}
///
/// Gets or sets the vertical text alignment.
///
/// The vertical text alignment.
public TextAlignment VerticalTextAlignment
{
// README: It only work on Tizen 4.0
get
{
double valign = GetVerticalTextAlignment("elm.text");
if (valign == 0.0)
{
return TextAlignment.Start;
}
else if (valign == 0.5)
{
return TextAlignment.Center;
}
else if (valign == 1.0)
{
return TextAlignment.End;
}
else
{
return TextAlignment.Auto;
}
}
set
{
double valign = 0;
switch (value)
{
case TextAlignment.Auto:
valign = -1;
break;
case TextAlignment.None:
case TextAlignment.Start:
valign = 0;
break;
case TextAlignment.Center:
valign = 0.5;
break;
case TextAlignment.End:
valign = 1.0;
break;
}
SetVerticalTextAlignment("elm.text", valign);
}
}
///
/// Gets or sets the value that indicates whether the text is underlined.
///
/// true if the text is underlined.
public bool Underline
{
get
{
return _span.Underline;
}
set
{
if (value != _span.Underline)
{
_span.Underline = value;
ApplyTextAndStyle();
}
}
}
///
/// Gets or sets the value that indicates whether the text is striked out.
///
/// true if the text is striked out.
public bool Strikethrough
{
get
{
return _span.Strikethrough;
}
set
{
if (value != _span.Strikethrough)
{
_span.Strikethrough = value;
ApplyTextAndStyle();
}
}
}
///
/// Implements to provide a desired size of the label.
///
/// Available width.
/// Available height.
/// Size of the control that fits the available area.
public ESize Measure(int availableWidth, int availableHeight)
{
var size = Geometry;
Resize(availableWidth, size.Height);
var formattedSize = Native.TextHelper.GetFormattedTextBlockSize(this);
Resize(size.Width, size.Height);
// Set bottom padding for lower case letters that have segments below the bottom line of text (g, j, p, q, y).
var verticalPadding = (int)Math.Ceiling(0.05 * FontSize);
formattedSize.Height += verticalPadding;
// This is the EFL team's guide.
// For wrap to work properly, the label must be 1 pixel larger than the size of the formatted text.
formattedSize.Width += 1;
return formattedSize;
}
void IBatchable.OnBatchCommitted()
{
ApplyTextAndStyle();
}
void ApplyTextAndStyle()
{
if (!this.IsBatched())
{
SetInternalTextAndStyle(_span.GetDecoratedText(), _span.GetStyle());
}
}
void SetInternalTextAndStyle(string formattedText, string textStyle)
{
base.Text = formattedText;
TextStyle = textStyle;
}
}
}