using ElmSharp;
using System;
using EButton = ElmSharp.Button;
using EColor = ElmSharp.Color;
using ESize = ElmSharp.Size;
namespace Xamarin.Forms.Platform.Tizen.Native
{
///
/// Extends the EButton control, providing basic formatting features,
/// i.e. font color, size, additional image.
///
public class Button : EButton, IMeasurable
{
///
/// Holds the formatted text of the button.
///
readonly Span _span = new Span();
///
/// Optional image, if set will be drawn on the button.
///
Image _image;
///
/// Initializes a new instance of the class.
///
/// Parent evas object.
public Button(EvasObject parent) : base(parent)
{
}
///
/// Gets or sets the button's text.
///
/// The text.
public override string Text
{
get
{
return _span.Text;
}
set
{
if (value != _span.Text)
{
_span.Text = value;
ApplyTextAndStyle();
}
}
}
///
/// Gets or sets the color of the 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 color of the text background.
///
/// The color of the text background.
public EColor TextBackgroundColor
{
get
{
return _span.BackgroundColor;
}
set
{
if (!_span.BackgroundColor.Equals(value))
{
_span.BackgroundColor = value;
ApplyTextAndStyle();
}
}
}
///
/// Gets or sets the font family.
///
/// 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 size of the font.
///
/// 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 image to be displayed next to the button's text.
///
/// The image displayed on the button.
public Image Image
{
get
{
return _image;
}
set
{
if (value != _image)
{
ApplyImage(value);
}
}
}
///
/// Implementation of the IMeasurable.Measure() method.
///
public ESize Measure(int availableWidth, int availableHeight)
{
switch (Style)
{
case "bottom":
case "circle":
return new ESize(MinimumWidth, MinimumHeight);
default:
var rawSize = TextHelper.GetRawTextBlockSize(this);
return new ESize(rawSize.Width + MinimumWidth, Math.Max(MinimumHeight, rawSize.Height));
}
}
///
/// Applies the button's text and its style.
///
void ApplyTextAndStyle()
{
SetInternalTextAndStyle(_span.GetDecoratedText(), _span.GetStyle());
}
///
/// Sets the button's internal text and its style.
///
/// Formatted text, supports HTML tags.
/// Style applied to the formattedText.
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");
}
///
/// Applies the image to be displayed on the button. If value is null,
/// image will be removed.
///
/// Image to be displayed or null.
void ApplyImage(Image image)
{
_image = image;
SetInternalImage();
}
///
/// Sets the internal image. If value is null, image will be removed.
///
void SetInternalImage()
{
if (_image == null)
{
SetPartContent("icon", null);
}
else
{
// TODO : Hide() and Show() should be removed later.
// these methods are called to trigger rendering process because of EFL issue.
Hide();
SetPartContent("icon", _image);
Show();
}
}
///
/// Update the button's style
///
/// The style of button
public void UpdateStyle(string style)
{
if (Style != style)
{
Style = style;
if (Style == "circle")
_span.HorizontalTextAlignment = TextAlignment.Center;
else
_span.HorizontalTextAlignment = TextAlignment.Auto;
ApplyTextAndStyle();
}
}
}
}