summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/ButtonRenderer.cs
blob: def27a99f3943a76073be8c4256c24514a1228c2 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.ComponentModel;
using EColor = ElmSharp.Color;
using Xamarin.Forms.PlatformConfiguration.TizenSpecific;
using Specific = Xamarin.Forms.PlatformConfiguration.TizenSpecific.Button;

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)
			{
				UpdateStyle();
				Control.Clicked += ButtonClickedHandler;
			}

			base.OnElementChanged(e);
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);

			if (e.PropertyName == Specific.ButtonStyleProperty.PropertyName)
			{
				UpdateStyle();
			}
		}

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

		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;
			}
		}

		void UpdateStyle()
		{
			string style;
			switch (Specific.GetButtonStyle(Element))
			{
				case ButtonStyle.Circle:
					style = "circle";
					break;
				case ButtonStyle.Bottom:
					style = "bottom";
					break;
				default:
					style = "default";
					break;
			}

			Control.UpdateStyle(style);
			((IVisualElementController)Element).NativeSizeChanged();
		}

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