summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Native/SearchBar.cs
blob: 42ada16eec5814bb3fb7f273d78f1533691ef360 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
using System;
using ElmSharp;
using EColor = ElmSharp.Color;
using ESize = ElmSharp.Size;
using ERect = ElmSharp.Rect;
using ERectangle = ElmSharp.Rectangle;

namespace Xamarin.Forms.Platform.Tizen.Native
{
	/// <summary>
	/// Provides implementation of the search bar widget.
	/// </summary>
	public class SearchBar : Canvas, IMeasurable
	{
		/// <summary>
		/// The height of the background of the search bar.
		/// </summary>
		const int BackgroundHeight = 120;

		/// <summary>
		/// The style of the cancel button.
		/// </summary>
		const string CancelButtonLayoutStyle = "editfield_clear";

		/// <summary>
		/// The horizontal padding of the cancel button.
		/// </summary>
		const int CancelButtonPaddingHorizontal = 17;

		/// <summary>
		/// The size of the cancel button.
		/// </summary>
		const int CancelButtonSize = 80;

		/// <summary>
		/// The height of the entry.
		/// </summary>
		const int EntryHeight = 54;

		/// <summary>
		/// The horizontal padding of the entry.
		/// </summary>
		const int EntryPaddingHorizontal = 42;

		/// <summary>
		/// The vertical padding of the entry.
		/// </summary>
		const int EntryPaddingVertical = 33;

		/// <summary>
		/// The height of the rectangle used to draw underline effect.
		/// </summary>
		const int RectangleHeight = 2;

		/// <summary>
		/// The bottom padding of the rectangle used to draw underline effect.
		/// </summary>
		const int RectanglePaddingBottom = 20;

		/// <summary>
		/// The horizontal padding of the rectangle used to draw underline effect.
		/// </summary>
		const int RectanglePaddingHorizontal = 32;

		/// <summary>
		/// The top padding of the rectangle used to draw underline effect.
		/// </summary>
		const int RectanglePaddingTop = 11;

		//TODO: read default platform color

		/// <summary>
		/// The color of the underline rectangle.
		/// </summary>
		static readonly EColor s_underlineColor = EColor.Aqua;

		/// <summary>
		/// The dimmed color of the underline rectangle.
		/// </summary>
		static readonly EColor s_underlineDimColor = EColor.Gray;

		/// <summary>
		/// The cancel button.
		/// </summary>
		Button _cancelButton;

		/// <summary>
		/// The text entry.
		/// </summary>
		Entry _entry;

		/// <summary>
		/// The underline rectangle.
		/// </summary>
		ERectangle _underlineRectangle;

		/// <summary>
		/// Initializes a new instance of the <see cref="Xamarin.Forms.Platform.Tizen.Native.SearchBar"/> class.
		/// </summary>
		/// <param name="parent">Parent evas object.</param>
		public SearchBar(EvasObject parent) : base(parent)
		{
			_entry = new Entry(parent)
			{
				IsSingleLine = true,
			};
			_entry.SetInputPanelReturnKeyType(InputPanelReturnKeyType.Search);
			_entry.TextChanged += EntryTextChanged;
			_entry.Activated += EntryActivated;
			_entry.Focused += EntryFocused;
			_entry.Unfocused += EntryUnfocused;
			_entry.Show();

			_cancelButton = new Button(parent);
			_cancelButton.Style = CancelButtonLayoutStyle;
			_cancelButton.Clicked += CancelButtonClicked;

			_underlineRectangle = new ERectangle(parent)
			{
				Color = IsEnabled ? s_underlineColor : s_underlineDimColor,
			};
			_underlineRectangle.Show();

			Children.Add(_entry);
			Children.Add(_cancelButton);
			Children.Add(_underlineRectangle);

			Show();

			this.LayoutUpdated += SearchBarLayoutUpdated;
		}

		/// <summary>
		/// Occurs when the search button on the keyboard is pressed.
		/// </summary>
		public event EventHandler SearchButtonPressed;

		/// <summary>
		/// Occurs when the entry's text has changed.
		/// </summary>
		public event EventHandler<TextChangedEventArgs> TextChanged;

		/// <summary>
		/// Gets or sets the color of the cancel button.
		/// </summary>
		/// <value>Color of the cancel button.</value>
		public EColor CancelButtonColor
		{
			get
			{
				return _cancelButton.Color;
			}

			set
			{
				if (!_cancelButton.Color.Equals(value))
				{
					_cancelButton.Color = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the font attributes of the search bar's entry.
		/// </summary>
		/// <value>The font attributes.</value>
		public FontAttributes FontAttributes
		{
			get
			{
				return _entry.FontAttributes;
			}

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

		/// <summary>
		/// Gets or sets the font family of the search bar's entry.
		/// </summary>
		/// <value>The font family.</value>
		public string FontFamily
		{
			get
			{
				return _entry.FontFamily;
			}

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

		/// <summary>
		/// Gets or sets the size of the font of the search bar's entry.
		/// </summary>
		/// <value>The size of the font.</value>
		public double FontSize
		{
			get
			{
				return _entry.FontSize;
			}

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

		/// <summary>
		/// Gets or sets the horizontal text alignment of the search bar's entry.
		/// </summary>
		/// <value>The horizontal text alignment.</value>
		public TextAlignment HorizontalTextAlignment
		{
			get
			{
				return _entry.HorizontalTextAlignment;
			}

			set
			{
				if (value != _entry.HorizontalTextAlignment)
				{
					_entry.HorizontalTextAlignment = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the placeholder of the search bar's entry.
		/// </summary>
		/// <value>The placeholder.</value>
		public string Placeholder
		{
			get
			{
				return _entry.Placeholder;
			}

			set
			{
				if (value != _entry.Placeholder)
				{
					_entry.Placeholder = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the color of the placeholder.
		/// </summary>
		/// <value>The color of the placeholder.</value>
		public EColor PlaceholderColor
		{
			get
			{
				return _entry.PlaceholderColor;
			}

			set
			{
				if (!_entry.PlaceholderColor.Equals(value))
				{
					_entry.PlaceholderColor = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the text of the search bar's entry.
		/// </summary>
		/// <value>The text.</value>
		public override string Text
		{
			get
			{
				return _entry.Text;
			}

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

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

			set
			{
				if (!_entry.TextColor.Equals(value))
				{
					_entry.TextColor = value;
				}
			}
		}

		/// <summary>
		/// Implementation of the IMeasurable.Measure() method.
		/// </summary>
		public ESize Measure(int availableWidth, int availableHeight)
		{
			ESize entrySize = _entry.Measure(availableWidth, availableHeight);
			int width = entrySize.Width + (CancelButtonPaddingHorizontal * 2) + CancelButtonSize;
			return new ESize(width, BackgroundHeight);
		}

		/// <summary>
		/// Handles the event triggered by the cancel button being clicked.
		/// </summary>
		/// <param name="sender">Sender of the event.</param>
		/// <param name="e">Event arguments, ignored.</param>
		void CancelButtonClicked(object sender, EventArgs e)
		{
			_entry.Text = string.Empty;
			_cancelButton.Hide();
		}

		/// <summary>
		/// Handles the event triggered by clicking the search button on the keyboard.
		/// </summary>
		/// <param name="sender">Sender of the event.</param>
		/// <param name="e">Event arguments, ignored.</param>
		void EntryActivated(object sender, EventArgs e)
		{
			SearchButtonPressed?.Invoke(this, EventArgs.Empty);
		}

		/// <summary>
		/// Handles the event triggered by entry gaining the focus.
		/// </summary>
		/// <param name="sender">Sender of the event.</param>
		/// <param name="e">Event arguments, ignored.</param>
		void EntryFocused(object sender, EventArgs e)
		{
			_underlineRectangle.Color = s_underlineColor;
		}

		/// <summary>
		/// Handles the event triggered by entry's text being changed.
		/// </summary>
		/// <param name="sender">Sender of the event.</param>
		/// <param name="e">Event arguments.</param>
		void EntryTextChanged(object sender, TextChangedEventArgs e)
		{
			if (string.IsNullOrEmpty(e.NewTextValue))
			{
				_cancelButton.Hide();
			}
			else if (!_cancelButton.IsVisible)
			{
				_cancelButton.Show();
			}
			TextChanged?.Invoke(this, e);
		}

		/// <summary>
		/// Handles the event triggered by entry losing the focus.
		/// </summary>
		/// <param name="sender">Sender of the event.</param>
		/// <param name="e">Event arguments, ignored.</param>
		void EntryUnfocused(object sender, EventArgs e)
		{
			_underlineRectangle.Color = s_underlineDimColor;
		}

		/// <summary>
		/// Handles the event triggered by search bar's layout being changed.
		/// </summary>
		/// <remarks>
		/// Updates the geometry of the widgets comprising the search bar.
		/// </remarks>
		/// <param name="sender">Sender of the event.</param>
		/// <param name="e">Event arguments.</param>
		void SearchBarLayoutUpdated(object sender, LayoutEventArgs e)
		{
			_underlineRectangle.Geometry = new ERect(e.Geometry.Left + RectanglePaddingHorizontal,
				e.Geometry.Top + EntryPaddingVertical + EntryHeight + RectanglePaddingTop,
				e.Geometry.Width - (RectanglePaddingHorizontal * 2),
				RectangleHeight);

			_entry.Geometry = new ERect(e.Geometry.Left + EntryPaddingHorizontal,
				e.Geometry.Top + EntryPaddingVertical,
				e.Geometry.Width - (EntryPaddingHorizontal + (CancelButtonPaddingHorizontal * 2) + CancelButtonSize),
				EntryHeight);

			_cancelButton.Geometry = new ERect(e.Geometry.Right - CancelButtonSize - CancelButtonPaddingHorizontal,
				e.Geometry.Top + RectanglePaddingBottom,
				CancelButtonSize,
				CancelButtonSize);
		}
	}
}