summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Native/Button.cs
blob: c6638662602bda27848fc05c62d5b32365c7e119 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
using System;
using ElmSharp;
using EButton = ElmSharp.Button;
using ESize = ElmSharp.Size;
using EColor = ElmSharp.Color;

namespace Xamarin.Forms.Platform.Tizen.Native
{
	/// <summary>
	/// Extends the EButton control, providing basic formatting features,
	/// i.e. font color, size, additional image.
	/// </summary>
	public class Button : EButton, IMeasurable
	{
		/// <summary>
		/// Holds the formatted text of the button.
		/// </summary>
		readonly Span _span = new Span();

		/// <summary>
		/// The internal padding of the button, helps to determine the size.
		/// </summary>
		ESize _internalPadding;

		/// <summary>
		/// Optional image, if set will be drawn on the button.
		/// </summary>
		Image _image;

		/// <summary>
		/// Initializes a new instance of the <see cref="Xamarin.Forms.Platform.Tizen.Native.Button"/> class.
		/// </summary>
		/// <param name="parent">Parent evas object.</param>
		public Button(EvasObject parent) : base(parent)
		{
		}

		/// <summary>
		/// Gets or sets the button's text.
		/// </summary>
		/// <value>The text.</value>
		public override string Text
		{
			get
			{
				return _span.Text;
			}

			set
			{
				if (value != _span.Text)
				{
					_span.Text = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Gets or sets the color of the text.
		/// </summary>
		/// <value>The color of the text.</value>
		public EColor TextColor
		{
			get
			{
				return _span.ForegroundColor;
			}

			set
			{
				if (!_span.ForegroundColor.Equals(value))
				{
					_span.ForegroundColor = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Gets or sets the color of the text background.
		/// </summary>
		/// <value>The color of the text background.</value>
		public EColor TextBackgroundColor
		{
			get
			{
				return _span.BackgroundColor;
			}

			set
			{
				if (!_span.BackgroundColor.Equals(value))
				{
					_span.BackgroundColor = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Gets or sets the font family.
		/// </summary>
		/// <value>The font family.</value>
		public string FontFamily
		{
			get
			{
				return _span.FontFamily;
			}

			set
			{
				if (value != _span.FontFamily)
				{
					_span.FontFamily = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Gets or sets the font attributes.
		/// </summary>
		/// <value>The font attributes.</value>
		public FontAttributes FontAttributes
		{
			get
			{
				return _span.FontAttributes;
			}

			set
			{
				if (value != _span.FontAttributes)
				{
					_span.FontAttributes = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Gets or sets the size of the font.
		/// </summary>
		/// <value>The size of the font.</value>
		public double FontSize
		{
			get
			{
				return _span.FontSize;
			}

			set
			{
				if (value != _span.FontSize)
				{
					_span.FontSize = value;
					ApplyTextAndStyle();
				}
			}
		}

		/// <summary>
		/// Gets or sets the image to be displayed next to the button's text.
		/// </summary>
		/// <value>The image displayed on the button.</value>
		public Image Image
		{
			get
			{
				return _image;
			}

			set
			{
				if (value != _image)
				{
					ApplyImage(value);
				}
			}
		}

		/// <summary>
		/// Implementation of the IMeasurable.Measure() method.
		/// </summary>
		public ESize Measure(int availableWidth, int availableHeight)
		{
			var size = Geometry;

			// resize the control using the whole available width
			Resize(availableWidth, size.Height);

			// measure the button's text, use it as a hint for the size
			var rawSize = Native.TextHelper.GetRawTextBlockSize(this);
			var formattedSize = Native.TextHelper.GetFormattedTextBlockSize(this);

			// restore the original size
			Resize(size.Width, size.Height);

			var padding = _internalPadding;

			// TODO : If the efl theme for the circle button is modified, it will be deleted.
			if (Style == "circle")
			{
				var circleTextPadding = (EdjeObject["icon_text_padding"]?.Geometry.Height).GetValueOrDefault(0);
				var circleHeight = padding.Height + ((rawSize.Width == 0) ? 0 : circleTextPadding + formattedSize.Height);

				return new ESize
				{
					Width = padding.Width,
					Height = circleHeight
				};
			}

			if (rawSize.Width > availableWidth)
			{
				// if the raw text width is larger than the available width, use
				// either formatted size or internal padding, whichever is bigger
				return new ESize()
				{
					Width = Math.Max(padding.Width, formattedSize.Width),
					Height = Math.Max(padding.Height, Math.Min(formattedSize.Height, Math.Max(rawSize.Height, availableHeight))),
				};
			}
			else
			{
				// otherwise use the formatted size along with padding
				return new ESize()
				{
					Width = padding.Width + formattedSize.Width,
					Height = Math.Max(padding.Height, formattedSize.Height),
				};
			}
		}

		/// <summary>
		/// Applies the button's text and its style.
		/// </summary>
		void ApplyTextAndStyle()
		{
			SetInternalTextAndStyle(_span.GetDecoratedText(), _span.GetStyle());
		}

		/// <summary>
		/// Sets the button's internal text and its style.
		/// </summary>
		/// <param name="formattedText">Formatted text, supports HTML tags.</param>
		/// <param name="textStyle">Style applied to the formattedText.</param>
		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");
		}

		/// <summary>
		/// Applies the image to be displayed on the button. If value is <c>null</c>,
		/// image will be removed.
		/// </summary>
		/// <param name="image">Image to be displayed or null.</param>
		void ApplyImage(Image image)
		{
			_image = image;

			SetInternalImage();
		}

		/// <summary>
		/// Sets the internal image. If value is <c>null</c>, image will be removed.
		/// </summary>
		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();
			}
		}

		/// <summary>
		/// Update the button's style
		/// </summary>
		/// <param name="style">The style of button</param>
		public void UpdateStyle(string style)
		{
			if (Style != style)
			{
				Style = style;

				//TODO : If the efl theme for the circle button is modified, will use MinimumWidth, MinimumHeight to get the size.
				if (Style == "circle")
				{
					var circleSize = (EdjeObject["bg"]?.Geometry.Width).GetValueOrDefault(0);
					_internalPadding = new ESize(circleSize, circleSize);
					_span.HorizontalTextAlignment = TextAlignment.Center;
				}
				else
				{
					_internalPadding = new ESize(MinimumWidth, MinimumHeight);
					_span.HorizontalTextAlignment = TextAlignment.Auto;
				}
				ApplyTextAndStyle();
			}
		}
	}
}