summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WP8/EntryRenderer.cs
blob: 00fdbb4a265aa79563e641ad04332366cd32fe21 (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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using static System.String;
using WControl = System.Windows.Controls.Control;

namespace Xamarin.Forms.Platform.WinPhone
{
	internal static class KeyboardExtensions
	{
		public static InputScope ToInputScope(this Keyboard self)
		{
			var result = new InputScope();
			var name = new InputScopeName();
			if (self == Keyboard.Default)
				name.NameValue = InputScopeNameValue.Default;
			else if (self == Keyboard.Chat)
				name.NameValue = InputScopeNameValue.Chat;
			else if (self == Keyboard.Email)
				name.NameValue = InputScopeNameValue.EmailNameOrAddress;
			else if (self == Keyboard.Numeric)
				name.NameValue = InputScopeNameValue.Number;
			else if (self == Keyboard.Telephone)
				name.NameValue = InputScopeNameValue.TelephoneNumber;
			else if (self == Keyboard.Text)
				name.NameValue = InputScopeNameValue.Text;
			else if (self == Keyboard.Url)
				name.NameValue = InputScopeNameValue.Url;
			else if (self is CustomKeyboard)
			{
				var custom = (CustomKeyboard)self;
				bool capitalizedSentenceEnabled = (custom.Flags & KeyboardFlags.CapitalizeSentence) == KeyboardFlags.CapitalizeSentence;
				bool spellcheckEnabled = (custom.Flags & KeyboardFlags.Spellcheck) == KeyboardFlags.Spellcheck;
				bool suggestionsEnabled = (custom.Flags & KeyboardFlags.Suggestions) == KeyboardFlags.Suggestions;

				if (!capitalizedSentenceEnabled && !spellcheckEnabled && !suggestionsEnabled)
					name.NameValue = InputScopeNameValue.Default;
				if (!capitalizedSentenceEnabled && !spellcheckEnabled && suggestionsEnabled)
					name.NameValue = InputScopeNameValue.Search;
				if (!capitalizedSentenceEnabled && spellcheckEnabled && !suggestionsEnabled)
				{
					Debug.WriteLine("Keyboard: Suggestions cannot be disabled in Windows Phone if spellcheck is enabled");
					name.NameValue = InputScopeNameValue.Search;
				}
				if (!capitalizedSentenceEnabled && spellcheckEnabled && suggestionsEnabled)
					name.NameValue = InputScopeNameValue.Search;
				if (capitalizedSentenceEnabled && !spellcheckEnabled && !suggestionsEnabled)
				{
					Debug.WriteLine("Keyboard: Suggestions cannot be disabled in Windows Phone if auto Capitalization is enabled");
					name.NameValue = InputScopeNameValue.Chat;
				}
				if (capitalizedSentenceEnabled && !spellcheckEnabled && suggestionsEnabled)
					name.NameValue = InputScopeNameValue.Chat;
				if (capitalizedSentenceEnabled && spellcheckEnabled && !suggestionsEnabled)
				{
					Debug.WriteLine("Keyboard: Suggestions cannot be disabled in Windows Phone if spellcheck is enabled");
					name.NameValue = InputScopeNameValue.Text;
				}
				if (capitalizedSentenceEnabled && spellcheckEnabled && suggestionsEnabled)
					name.NameValue = InputScopeNameValue.Text;
			}
			else
			{
				// Should never happens
				name.NameValue = InputScopeNameValue.Default;
			}
			result.Names.Add(name);
			return result;
		}
	}

	public class EntryRenderer : ViewRenderer<Entry, FormsPhoneTextBox>
	{
		bool _fontApplied;
		bool _ignoreTextChange;
		Brush _placeholderDefaultBrush;
		Brush _textDefaultBrush;

		public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
		{
			if (Children.Count == 0)
				return new SizeRequest();

			var constraint = new System.Windows.Size(widthConstraint, heightConstraint);

			FormsPhoneTextBox child = Control;

			double oldWidth = child.Width;
			double oldHeight = child.Height;

			child.Height = double.NaN;
			child.Width = double.NaN;

			child.Measure(constraint);
			var result = new Size(Math.Ceiling(child.DesiredSize.Width), Math.Ceiling(child.DesiredSize.Height));

			child.Width = oldWidth;
			child.Height = oldHeight;

			return new SizeRequest(result);
		}

		protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
		{
			base.OnElementChanged(e);

			var textBox = new FormsPhoneTextBox();

			SetNativeControl(textBox);

			UpdateInputScope();
			UpdateIsPassword();
			UpdateText();
			UpdatePlaceholder();
			UpdateColor();
			UpdateFont();
			UpdateAlignment();
			UpdatePlaceholderColor();
			UpdateIsEnabled();

			Control.LostFocus += OnTextBoxUnfocused;
			Control.TextChanged += TextBoxOnTextChanged;
			Control.KeyUp += TextBoxOnKeyUp;
		}

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

			if (e.PropertyName == Entry.TextProperty.PropertyName)
				UpdateText();
			else if (e.PropertyName == Entry.PlaceholderProperty.PropertyName)
				UpdatePlaceholder();
			else if (e.PropertyName == Entry.IsPasswordProperty.PropertyName)
				UpdateIsPassword();
			else if (e.PropertyName == VisualElement.IsEnabledProperty.PropertyName)
				UpdateIsEnabled();
			else if (e.PropertyName == Entry.TextColorProperty.PropertyName)
				UpdateColor();
			else if (e.PropertyName == InputView.KeyboardProperty.PropertyName)
				UpdateInputScope();
			else if (e.PropertyName == Entry.FontAttributesProperty.PropertyName)
				UpdateFont();
			else if (e.PropertyName == Entry.FontFamilyProperty.PropertyName)
				UpdateFont();
			else if (e.PropertyName == Entry.FontSizeProperty.PropertyName)
				UpdateFont();
			else if (e.PropertyName == Entry.HorizontalTextAlignmentProperty.PropertyName)
				UpdateAlignment();
			else if (e.PropertyName == Entry.PlaceholderColorProperty.PropertyName)
				UpdatePlaceholderColor();
		}

		protected override void UpdateBackgroundColor()
		{
			Control.Background = Element.BackgroundColor.IsDefault ? (Brush)System.Windows.Application.Current.Resources["PhoneTextBoxBrush"] : Element.BackgroundColor.ToBrush();
		}

		internal override void OnModelFocusChangeRequested(object sender, VisualElement.FocusRequestArgs args)
		{
			if (args.Focus)
				args.Result = Control.Focus();
			else
			{
				UnfocusControl(Control);
				args.Result = true;
			}
		}

		void OnTextBoxUnfocused(object sender, RoutedEventArgs e)
		{
			if (Element.TextColor.IsDefault)
				return;

			if (!IsNullOrEmpty(Element.Text))
				Control.Foreground = Element.TextColor.ToBrush();
		}

		void TextBoxOnKeyUp(object sender, KeyEventArgs keyEventArgs)
		{
			if (keyEventArgs.Key == Key.Enter)
				((IEntryController)Element).SendCompleted();
		}

		void TextBoxOnTextChanged(object sender, System.Windows.Controls.TextChangedEventArgs textChangedEventArgs)
		{
			// Signal to the UpdateText method that the change to TextProperty doesn't need to update the control
			// This prevents the cursor position from getting lost
			_ignoreTextChange = true;
			((IElementController)Element).SetValueFromRenderer(Entry.TextProperty, Control.Text);

			// If an Entry.TextChanged handler modified the value of the Entry's text, the values could now be 
			// out-of-sync; re-sync them and force the TextBox cursor to the end of the text
			string entryText = Element.Text;
			if (Control.Text != entryText)
			{
				Control.Text = entryText;
				Control.SelectionStart = Control.Text.Length;
			}

			_ignoreTextChange = false;
		}

		void UpdateAlignment()
		{
			if (Control == null)
				return;

			Control.TextAlignment = Element.HorizontalTextAlignment.ToNativeTextAlignment();
		}

		void UpdateColor()
		{
			if (Control == null)
				return;

			BrushHelpers.UpdateColor(Element.TextColor, ref _textDefaultBrush,
				() => Control.Foreground, brush => Control.Foreground = brush);

			// Force the PhoneTextBox control to do some internal bookkeeping
			// so the colors change immediately and remain changed when the control gets focus
			Control.OnApplyTemplate();
		}

		void UpdateFont()
		{
			if (Control == null)
				return;

			Entry entry = Element;

			if (entry == null)
				return;

			bool entryIsDefault = entry.FontFamily == null && entry.FontSize == Device.GetNamedSize(NamedSize.Default, typeof(Entry), true) && entry.FontAttributes == FontAttributes.None;

			if (entryIsDefault && !_fontApplied)
				return;

			if (entryIsDefault)
			{
				Control.ClearValue(WControl.FontStyleProperty);
				Control.ClearValue(WControl.FontSizeProperty);
				Control.ClearValue(WControl.FontFamilyProperty);
				Control.ClearValue(WControl.FontWeightProperty);
				Control.ClearValue(WControl.FontStretchProperty);
			}
			else
				Control.ApplyFont(entry);

			_fontApplied = true;
		}

		void UpdateInputScope()
		{
			Control.InputScope = Element.Keyboard.ToInputScope();
		}

		void UpdateIsEnabled()
		{
			Control.IsEnabled = Element.IsEnabled;
		}

		void UpdateIsPassword()
		{
			Control.IsPassword = Element.IsPassword;
		}

		void UpdatePlaceholder()
		{
			Control.Hint = Element.Placeholder ?? "";
		}

		void UpdatePlaceholderColor()
		{
			BrushHelpers.UpdateColor(Element.PlaceholderColor, ref _placeholderDefaultBrush, 
				() => Control.PlaceholderForegroundBrush, brush => Control.PlaceholderForegroundBrush = brush);
		}

		void UpdateText()
		{
			// If the text property has changed because TextBoxOnTextChanged called SetValueFromRenderer,
			// we don't want to re-update the text and reset the cursor position
			if (_ignoreTextChange)
				return;

			if (Control.Text == Element.Text)
				return;

			Control.Text = Element.Text ?? "";
			Control.Select(Control.Text.Length, 0);
		}
	}
}