summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/SearchBarRenderer.cs
blob: 798a0497e9b0443917eba5432d791e1c07638db7 (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
using System;
using EColor = ElmSharp.Color;

namespace Xamarin.Forms.Platform.Tizen
{
	public class SearchBarRenderer : ViewRenderer<SearchBar, Native.SearchBar>
	{
		//TODO need to add internationalization support
		const string DefaultPlaceholderText = "Search";

		static readonly EColor s_defaultCancelButtonColor = EColor.Aqua;

		//TODO: read default platform color
		static readonly EColor s_defaultPlaceholderColor = EColor.Gray;
		static readonly EColor s_defaultTextColor = EColor.Black;
		/// <summary>
		/// Creates a new instance of the <see cref="Xamarin.Forms.Platform.Tizen.SearchBarRenderer"/> class.
		/// Registers handlers for various properties of the SearchBar widget.
		/// </summary>
		public SearchBarRenderer()
		{
			RegisterPropertyHandler(SearchBar.CancelButtonColorProperty, CancelButtonColorPropertyHandler);
			RegisterPropertyHandler(SearchBar.FontAttributesProperty, FontAttributesPropertyHandler);
			RegisterPropertyHandler(SearchBar.FontFamilyProperty, FontFamilyPropertyHandler);
			RegisterPropertyHandler(SearchBar.FontSizeProperty, FontSizePropertyHandler);
			RegisterPropertyHandler(SearchBar.HorizontalTextAlignmentProperty, HorizontalTextAlignmentPropertyHandler);
			RegisterPropertyHandler(SearchBar.PlaceholderProperty, PlaceholderPropertyHandler);
			RegisterPropertyHandler(SearchBar.PlaceholderColorProperty, PlaceholderColorPropertyHandler);
			RegisterPropertyHandler(SearchBar.TextProperty, TextPropertyHandler);
			RegisterPropertyHandler(SearchBar.TextColorProperty, TextColorPropertyHandler);
		}

		/// <summary>
		/// A method called whenever the associated element has changed.
		/// </summary>
		protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
		{
			if (Control == null)
			{
				var searchBar = new Native.SearchBar(Forms.Context.MainWindow);
				SetNativeControl(searchBar);
			}

			if (e.OldElement != null)
			{
				Control.TextChanged -= SearchBarTextChangedHandler;
				Control.SearchButtonPressed -= SearchButtonPressedHandler;
			}

			if (e.NewElement != null)
			{
				Control.TextChanged += SearchBarTextChangedHandler;
				Control.SearchButtonPressed += SearchButtonPressedHandler;
			}

			base.OnElementChanged(e);
		}

		protected override Size MinimumSize()
		{
			return new Size(250, 120);
		}

		/// <summary>
		/// Called upon changing of Xamarin widget's cancel button color property.
		/// Converts current Color to ElmSharp.Color instance and sets it in the underlying Xamarin.Forms.Platform.Tizen.Native widget.
		/// </summary>
		void CancelButtonColorPropertyHandler()
		{
			Control.CancelButtonColor = Element.CancelButtonColor.IsDefault ? s_defaultCancelButtonColor : Element.CancelButtonColor.ToNative();
		}

		/// <summary>
		/// Called upon changing of Xamarin widget's font attributes property.
		/// Converts current FontAttributes to ElmSharp.FontAttributes instance
		/// and sets it in the underlying Xamarin.Forms.Platform.Tizen.Native.SearchBar widget.
		/// </summary>
		void FontAttributesPropertyHandler()
		{
			Control.FontAttributes = Element.FontAttributes;
		}

		/// <summary>
		/// Called upon changing of Xamarin widget's font family property.
		/// Sets current value of FontFamily property to the underlying Xamarin.Forms.Platform.Tizen.Native.SearchBar widget.
		/// </summary>
		void FontFamilyPropertyHandler()
		{
			Control.FontFamily = Element.FontFamily;
		}

		/// <summary>
		/// Called upon changing of Xamarin widget's font size property.
		/// Sets current value of FontSize property to the underlying Xamarin.Forms.Platform.Tizen.Native.SearchBar widget.
		/// </summary>
		void FontSizePropertyHandler()
		{
			Control.FontSize = Element.FontSize;
		}

		/// <summary>
		/// Called upon changing of Xamarin widget's horizontal text alignment property.
		/// Converts current HorizontalTextAlignment property's value to Xamarin.Forms.Platform.Tizen.Native.TextAlignment instance
		/// and sets it in the underlying Xamarin.Forms.Platform.Tizen.Native.SearchBar widget.
		/// </summary>
		void HorizontalTextAlignmentPropertyHandler()
		{
			Control.HorizontalTextAlignment = Element.HorizontalTextAlignment.ToNative();
		}

		/// <summary>
		/// Called upon changing of Xamarin widget's placeholder color property.
		/// Converts current PlaceholderColor property value to ElmSharp.Color instance
		/// and sets it in the underlying Xamarin.Forms.Platform.Tizen.Native.SearchBar widget.
		/// </summary>
		void PlaceholderColorPropertyHandler()
		{
			Control.PlaceholderColor = Element.PlaceholderColor.IsDefault ? s_defaultPlaceholderColor : Element.PlaceholderColor.ToNative();
		}

		/// <summary>
		/// Called upon changing of Xamarin widget's placeholder text property.
		/// </summary>
		void PlaceholderPropertyHandler()
		{
			Control.Placeholder = Element.Placeholder == null ? DefaultPlaceholderText : Element.Placeholder;
		}

		/// <summary>
		/// Called on every change of underlying SearchBar's Text property.
		/// Rewrites current underlying Xamarin.Forms.Platform.Tizen.Native.SearchBar's Text contents to its Xamarin counterpart.
		/// </summary>
		/// <param name="sender">Sender.</param>
		void SearchBarTextChangedHandler(object sender, EventArgs e)
		{
			Element.Text = Control.Text;
		}

		/// <summary>
		/// Called when the user clicks the Search button.
		/// </summary>
		/// <param name="sender">Sender.</param>
		/// <param name="e">Event arguments.</param>
		void SearchButtonPressedHandler(object sender, EventArgs e)
		{
			(Element as ISearchBarController).OnSearchButtonPressed();
		}

		/// <summary>
		/// Called upon changing of Xamarin widget's text color property.
		/// Converts current TextColor property value to ElmSharp.Color instance
		/// and sets it in the underlying Xamarin.Forms.Platform.Tizen.Native.SearchBar widget.
		/// </summary>
		void TextColorPropertyHandler()
		{
			Control.TextColor = Element.TextColor.IsDefault ? s_defaultTextColor : Element.TextColor.ToNative();
		}

		/// <summary>
		/// Called upon changing of Xamarin widget's text property.
		/// </summary>
		void TextPropertyHandler()
		{
			Control.Text = Element.Text;
		}
	}
}