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
{
///
/// 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 line wrap option.
///
/// The line break mode.
public LineBreakMode LineBreakMode
{
get
{
return _span.LineBreakMode;
}
set
{
if (value != _span.LineBreakMode)
{
_span.LineBreakMode = value;
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
{
get
{
return _span.VerticalTextAlignment;
}
set
{
if (value != _span.VerticalTextAlignment)
{
_span.VerticalTextAlignment = value;
ApplyTextAndStyle();
}
}
}
///
/// 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 rawSize = Native.TextHelper.GetRawTextBlockSize(this);
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);
rawSize.Height += verticalPadding;
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.
rawSize.Width += 1;
formattedSize.Width += 1;
if (rawSize.Width > availableWidth)
{
return new ESize()
{
Width = formattedSize.Width,
Height = Math.Min(formattedSize.Height, Math.Max(rawSize.Height, availableHeight)),
};
}
else
{
return formattedSize;
}
}
void ApplyTextAndStyle()
{
SetInternalTextAndStyle(_span.GetDecoratedText(), _span.GetStyle());
}
void SetInternalTextAndStyle(string formattedText, string textStyle)
{
string emission = "elm,state,text,visible";
if (string.IsNullOrEmpty(formattedText))
{
formattedText = null;
textStyle = null;
emission = "elm,state,text,hidden";
}
base.Text = formattedText;
var textblock = EdjeObject["elm.text"];
if (textblock != null)
{
textblock.TextStyle = textStyle;
}
EdjeObject.EmitSignal(emission, "elm");
}
}
}