summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/ButtonRenderer.cs
blob: 456bb0d5a4f65cb823d20f6a80d12ed440a751e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using EColor = ElmSharp.Color;
using ESize = ElmSharp.Size;
using Specific = Xamarin.Forms.PlatformConfiguration.TizenSpecific.VisualElement;

namespace Xamarin.Forms.Platform.Tizen
{
	public class ButtonRenderer : ViewRenderer<Button, Native.Button>
	{
		static readonly EColor s_defaultTextColor = EColor.White;

		public ButtonRenderer()
		{
			RegisterPropertyHandler(Button.TextProperty, UpdateText);
			RegisterPropertyHandler(Button.FontFamilyProperty, UpdateText);
			RegisterPropertyHandler(Button.FontSizeProperty, UpdateText);
			RegisterPropertyHandler(Button.FontAttributesProperty, UpdateText);
			RegisterPropertyHandler(Button.TextColorProperty, UpdateTextColor);
			RegisterPropertyHandler(Button.ImageProperty, UpdateBitmap);
			RegisterPropertyHandler(Button.BorderColorProperty, UpdateBorder);
			RegisterPropertyHandler(Button.BorderRadiusProperty, UpdateBorder);
			RegisterPropertyHandler(Button.BorderWidthProperty, UpdateBorder);
		}

		protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
		{
			if (Control == null)
			{
				var button = new Native.Button(Forms.Context.MainWindow)
				{
					PropagateEvents = false,
				};
				SetNativeControl(button);
			}

			if (e.OldElement != null)
			{
				Control.Clicked -= ButtonClickedHandler;
			}

			if (e.NewElement != null)
			{
				Control.Clicked += ButtonClickedHandler;
			}
			base.OnElementChanged(e);
		}

		protected override Size MinimumSize()
		{
			return new ESize(Control.MinimumWidth, Control.MinimumHeight).ToDP();
		}

		void ButtonClickedHandler(object sender, EventArgs e)
		{
			IButtonController btn = Element as IButtonController;
			if (btn != null)
			{
				btn.SendClicked();
			}
		}

		void UpdateText()
		{
			Control.Text = Element.Text ?? "";
			Control.FontSize = Element.FontSize;
			Control.FontAttributes = Element.FontAttributes;
			Control.FontFamily = Element.FontFamily;
		}

		void UpdateTextColor()
		{
			Control.TextColor = Element.TextColor.IsDefault ? s_defaultTextColor : Element.TextColor.ToNative();
		}

		void UpdateBitmap()
		{
			if (!string.IsNullOrEmpty(Element.Image))
			{
				Control.Image = new Native.Image(Control);
				var task = Control.Image.LoadFromImageSourceAsync(Element.Image);
			}
			else
			{
				Control.Image = null;
			}
		}

		protected override void UpdateThemeStyle()
		{
			Control.UpdateStyle(Specific.GetStyle(Element));
			((IVisualElementController)Element).NativeSizeChanged();
		}

		void UpdateBorder()
		{
			/* The simpler way is to create some specialized theme for button in
			 * tizen-theme
			 */
			// TODO: implement border handling
		}
	}
}