Ooui-tws-port/Ooui.Forms/Renderers/ButtonRenderer.cs

161 lines
5.5 KiB
C#
Raw Normal View History

2017-11-09 06:21:05 +00:00
using System;
2017-11-10 00:09:48 +00:00
using System.ComponentModel;
using System.Diagnostics;
using Ooui.Forms.Extensions;
2017-11-09 06:21:05 +00:00
using Xamarin.Forms;
namespace Ooui.Forms.Renderers
{
2017-11-09 21:03:56 +00:00
public class ButtonRenderer : ViewRenderer<Xamarin.Forms.Button, Ooui.Button>
{
2017-11-10 00:09:48 +00:00
Ooui.Color _buttonTextColorDefaultDisabled;
Ooui.Color _buttonTextColorDefaultHighlighted;
Ooui.Color _buttonTextColorDefaultNormal;
2017-11-26 15:57:32 +00:00
public override SizeRequest GetDesiredSize (double widthConstraint, double heightConstraint)
{
var size = Element.Text.MeasureSize (Element.FontFamily, Element.FontSize, Element.FontAttributes, widthConstraint, heightConstraint);
2018-04-16 02:06:31 +00:00
size = new Size (size.Width + 32, size.Height + 16);
2017-11-26 15:57:32 +00:00
return new SizeRequest (size, size);
}
2017-11-10 00:09:48 +00:00
protected override void Dispose (bool disposing)
{
if (Control != null) {
Control.Click -= OnButtonTouchUpInside;
2017-11-10 00:09:48 +00:00
}
base.Dispose (disposing);
}
protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.Button> e)
{
base.OnElementChanged (e);
if (e.NewElement != null) {
if (Control == null) {
SetNativeControl (new Ooui.Button ());
Debug.Assert (Control != null, "Control != null");
2017-11-26 15:57:32 +00:00
Control.ClassName = "btn btn-primary";
2017-11-10 00:09:48 +00:00
_buttonTextColorDefaultNormal = Ooui.Colors.Black;
_buttonTextColorDefaultHighlighted = Ooui.Colors.Black;
_buttonTextColorDefaultDisabled = Ooui.Colors.Black;
Control.Click += OnButtonTouchUpInside;
2017-11-10 00:09:48 +00:00
}
UpdateText ();
UpdateFont ();
UpdateBorder ();
UpdateImage ();
UpdateTextColor ();
}
}
protected override void OnElementPropertyChanged (object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged (sender, e);
if (e.PropertyName == Xamarin.Forms.Button.TextProperty.PropertyName)
UpdateText ();
else if (e.PropertyName == Xamarin.Forms.Button.TextColorProperty.PropertyName)
UpdateTextColor ();
else if (e.PropertyName == Xamarin.Forms.Button.FontProperty.PropertyName)
UpdateFont ();
else if (e.PropertyName == Xamarin.Forms.Button.BorderWidthProperty.PropertyName || e.PropertyName == Xamarin.Forms.Button.CornerRadiusProperty.PropertyName || e.PropertyName == Xamarin.Forms.Button.BorderColorProperty.PropertyName)
2017-11-10 00:09:48 +00:00
UpdateBorder ();
2020-03-09 01:01:51 +00:00
else if (e.PropertyName == Xamarin.Forms.Button.ImageSourceProperty.PropertyName)
2017-11-10 00:09:48 +00:00
UpdateImage ();
}
void OnButtonTouchUpInside (object sender, EventArgs eventArgs)
{
((IButtonController)Element)?.SendPressed ();
((IButtonController)Element)?.SendReleased ();
((IButtonController)Element)?.SendClicked ();
}
void UpdateBorder ()
{
var uiButton = Control;
var button = Element;
if (button.BorderColor != Xamarin.Forms.Color.Default)
2018-04-26 05:23:26 +00:00
uiButton.Style.BorderColor = button.BorderColor.ToOouiColor (OouiTheme.ButtonBorderColor);
2017-11-10 01:50:19 +00:00
else
uiButton.Style.BorderColor = null;
2017-11-10 00:09:48 +00:00
2017-11-10 01:50:19 +00:00
float bw = Math.Max (0f, (float)button.BorderWidth);
if (bw > 0) {
uiButton.Style.BorderWidth = bw + "px";
}
else {
uiButton.Style.BorderWidth = null;
}
var br = button.CornerRadius;
2017-11-10 01:50:19 +00:00
if (br > 0 && (bw > 0 || br != 5)) { // 5 is the default
uiButton.Style.BorderRadius = br + "px";
}
else {
uiButton.Style.BorderRadius = null;
}
2017-11-10 00:09:48 +00:00
}
void UpdateFont ()
{
2017-11-26 15:57:32 +00:00
Element.SetStyleFont (Element.FontFamily, Element.FontSize, Element.FontAttributes, Control.Style);
2017-11-10 00:09:48 +00:00
}
async void UpdateImage ()
2017-11-10 00:09:48 +00:00
{
IImageSourceHandler handler;
2020-03-09 01:01:51 +00:00
ImageSource source = Element.ImageSource;
if (source != null &&
(handler = Xamarin.Forms.Internals.Registrar.Registered.GetHandler<IImageSourceHandler>(source.GetType())) != null)
{
string uiimage;
try
{
uiimage = await handler.LoadImageAsync(source, scale: 1.0f);
}
catch (OperationCanceledException)
{
uiimage = null;
}
var Button = Control;
if (Button != null && uiimage != null)
{
Button.Style.BackgroundImage = uiimage;
Button.Style.BackgroundPosition = "center";
}
}
else
{
Control.Style.BackgroundImage = null;
Control.Style.BackgroundPosition = null;
}
((IVisualElementController)Element).NativeSizeChanged ();
2017-11-10 00:09:48 +00:00
}
void UpdateText ()
{
var newText = Element.Text;
if (Control.Text != newText) {
Control.Text = Element.Text;
}
}
void UpdateTextColor ()
2017-11-09 21:03:56 +00:00
{
2018-04-26 05:23:26 +00:00
Control.Style.Color = Element.TextColor.ToOouiColor (OouiTheme.ButtonTextColor);
2017-11-09 21:03:56 +00:00
}
}
2017-11-09 06:21:05 +00:00
}