summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WinRT.Phone/SearchBox.xaml.cs
blob: 4cc8ac1c700160387836add9bb32268ceafd5338 (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
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Xamarin.Forms.Platform.WinRT
{
	public class SearchBoxQuerySubmittedEventArgs
		: EventArgs
	{
	}

	public class SearchBoxQueryChangedEventArgs
		: EventArgs
	{
		public SearchBoxQueryChangedEventArgs (string query)
		{
			QueryText = query;
		}

		public string QueryText
		{
			get;
			private set;
		}
	}

	public delegate void QueryChangedEventHandler (SearchBox search, SearchBoxQueryChangedEventArgs args);
	public delegate void QuerySubmittedEventHandler (SearchBox search, SearchBoxQuerySubmittedEventArgs args);

	public sealed partial class SearchBox
	{
		public SearchBox ()
		{
			InitializeComponent ();

			IsEnabledChanged += OnIsEnabledChanged;
		}

		public event QuerySubmittedEventHandler QuerySubmitted;
		public event QueryChangedEventHandler QueryChanged;

		public static readonly DependencyProperty QueryTextProperty = DependencyProperty.Register (
			"QueryText", typeof(string), typeof(SearchBox), new PropertyMetadata (null, OnQueryTextChanged));

		public string QueryText
		{
			get { return (string)GetValue (QueryTextProperty); }
			set { SetValue (QueryTextProperty, value); }
		}

		public static readonly DependencyProperty PlaceholderTextProperty = DependencyProperty.Register (
			"PlaceholderText", typeof(string), typeof(SearchBox), new PropertyMetadata (null, OnPlaceholderChanged));

		public string PlaceholderText
		{
			get { return (string)GetValue (PlaceholderTextProperty); }
			set { SetValue (PlaceholderTextProperty, value); }
		}

		public static readonly DependencyProperty HorizontalTextAlignmentProperty = DependencyProperty.Register (
			"HorizontalTextAlignment", typeof(string), typeof(SearchBox), new PropertyMetadata (null, OnAlignmentChanged));

		public TextAlignment HorizontalTextAlignment
		{
			get { return (TextAlignment)GetValue (HorizontalTextAlignmentProperty); }
			set { SetValue (HorizontalTextAlignmentProperty, value); }
		}

		protected override void OnApplyTemplate ()
		{
			base.OnApplyTemplate ();

			GoToNormal ();

			_searchTextBox = (TextBox)GetTemplateChild ("SearchTextBox");

			((Windows.UI.Xaml.Controls.Button) GetTemplateChild ("SearchButton")).Click += OnSearchButtonClicked;

			UpdatePlaceholder ();
			UpdateAlignment ();
		}

		protected override void OnGotFocus (RoutedEventArgs e)
		{
			base.OnGotFocus (e);

			VisualStateManager.GoToState (this, "Focused", true);
		}

		protected override void OnLostFocus (RoutedEventArgs e)
		{
			base.OnLostFocus (e);

			GoToNormal ();
		}

		void OnSearchButtonClicked (object sender, RoutedEventArgs e)
		{
			var querySubmitted = QuerySubmitted;
			if (querySubmitted != null)
				querySubmitted (this, new SearchBoxQuerySubmittedEventArgs());
		}

		TextBox _searchTextBox;

		void GoToNormal ()
		{
			VisualStateManager.GoToState (this, (IsEnabled) ? "Normal" : "Disabled", false);
		}

		void UpdatePlaceholder ()
		{
			if (_searchTextBox == null)
				return;

			_searchTextBox.PlaceholderText = PlaceholderText;
		}

		void OnIsEnabledChanged (object sender, DependencyPropertyChangedEventArgs e)
		{
			string state = "Normal";
			if (!(bool) e.NewValue)
				state = "Disabled";
			else if (FocusState != FocusState.Unfocused)
				state = "Focused";

			VisualStateManager.GoToState (this, state, true);
		}

		static void OnQueryTextChanged (DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var search = (SearchBox) d;
			var changed = search.QueryChanged;
			if (changed != null)
				changed (search, new SearchBoxQueryChangedEventArgs ((string) e.NewValue));
		}

		static void OnPlaceholderChanged (DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			((SearchBox) d).UpdatePlaceholder ();
		}

		static void OnAlignmentChanged (DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			((SearchBox) d).UpdateAlignment ();
		}

		void UpdateAlignment ()
		{
			if (_searchTextBox == null) {
				return;
			}

			_searchTextBox.TextAlignment = HorizontalTextAlignment.ToNativeTextAlignment();
		}
	}
}