-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
EntryRenderer.cs
488 lines (413 loc) · 14.5 KB
/
EntryRenderer.cs
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
using System;
using System.ComponentModel;
using Windows.System;
using Windows.UI.Text;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Xamarin.Forms.Internals;
using Xamarin.Forms.PlatformConfiguration.WindowsSpecific;
using WBrush = Windows.UI.Xaml.Media.Brush;
using Specifics = Xamarin.Forms.PlatformConfiguration.WindowsSpecific.InputView;
namespace Xamarin.Forms.Platform.UWP
{
public class EntryRenderer : ViewRenderer<Entry, FormsTextBox>
{
bool _fontApplied;
WBrush _backgroundColorFocusedDefaultBrush;
WBrush _placeholderDefaultBrush;
WBrush _textDefaultBrush;
WBrush _defaultTextColorFocusBrush;
WBrush _defaultPlaceholderColorFocusBrush;
bool _cursorPositionChangePending;
bool _selectionLengthChangePending;
bool _nativeSelectionIsUpdating;
string _transformedText;
IElementController ElementController => Element as IElementController;
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
if (Control == null)
{
var textBox = new FormsTextBox { Style = Windows.UI.Xaml.Application.Current.Resources["FormsTextBoxStyle"] as Windows.UI.Xaml.Style };
SetNativeControl(textBox);
textBox.TextChanged += OnNativeTextChanged;
textBox.KeyUp += TextBoxOnKeyUp;
textBox.SelectionChanged += SelectionChanged;
textBox.GotFocus += TextBoxGotFocus;
// If the Forms VisualStateManager is in play or the user wants to disable the Forms legacy
// color stuff, then the underlying textbox should just use the Forms VSM states
textBox.UseFormsVsm = e.NewElement.HasVisualStateGroups()
|| !e.NewElement.OnThisPlatform().GetIsLegacyColorModeEnabled();
}
// When we set the control text, it triggers the SelectionChanged event, which updates CursorPosition and SelectionLength;
// These one-time-use variables will let us initialize a CursorPosition and SelectionLength via ctor/xaml/etc.
_cursorPositionChangePending = Element.IsSet(Entry.CursorPositionProperty);
_selectionLengthChangePending = Element.IsSet(Entry.SelectionLengthProperty);
UpdateIsPassword();
UpdateText();
UpdatePlaceholder();
UpdateTextColor();
UpdateFont();
UpdateCharacterSpacing();
UpdateHorizontalTextAlignment();
UpdateVerticalTextAlignment();
UpdatePlaceholderColor();
UpdateMaxLength();
UpdateDetectReadingOrderFromContent();
UpdateReturnType();
UpdateIsReadOnly();
UpdateInputScope();
UpdateClearButtonVisibility();
if (_cursorPositionChangePending)
UpdateCursorPosition();
if (_selectionLengthChangePending)
UpdateSelectionLength();
}
}
void TextBoxGotFocus(object sender, RoutedEventArgs e)
{
if (_cursorPositionChangePending)
UpdateCursorPosition();
if (_selectionLengthChangePending)
UpdateSelectionLength();
SetCursorPositionFromRenderer(Control.SelectionStart);
SetSelectionLengthFromRenderer(Control.SelectionLength);
}
protected override void Dispose(bool disposing)
{
if (disposing && Control != null)
{
Control.TextChanged -= OnNativeTextChanged;
Control.KeyUp -= TextBoxOnKeyUp;
Control.SelectionChanged -= SelectionChanged;
Control.GotFocus -= TextBoxGotFocus;
}
base.Dispose(disposing);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.IsOneOf(Entry.TextProperty, Entry.TextTransformProperty))
UpdateText();
else if (e.PropertyName == Entry.IsPasswordProperty.PropertyName)
UpdateIsPassword();
else if (e.PropertyName == Entry.PlaceholderProperty.PropertyName)
UpdatePlaceholder();
else if (e.PropertyName == Entry.TextColorProperty.PropertyName)
UpdateTextColor();
else if (e.PropertyName == Entry.CharacterSpacingProperty.PropertyName)
{
UpdateCharacterSpacing();
}
else if (e.PropertyName == InputView.KeyboardProperty.PropertyName)
UpdateInputScope();
else if (e.PropertyName == InputView.IsSpellCheckEnabledProperty.PropertyName)
UpdateInputScope();
else if (e.PropertyName == Entry.IsTextPredictionEnabledProperty.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)
UpdateHorizontalTextAlignment();
else if (e.PropertyName == Entry.VerticalTextAlignmentProperty.PropertyName)
UpdateVerticalTextAlignment();
else if (e.PropertyName == Entry.PlaceholderColorProperty.PropertyName)
UpdatePlaceholderColor();
else if (e.PropertyName == VisualElement.FlowDirectionProperty.PropertyName)
UpdateHorizontalTextAlignment();
else if (e.PropertyName == InputView.MaxLengthProperty.PropertyName)
UpdateMaxLength();
else if (e.PropertyName == Specifics.DetectReadingOrderFromContentProperty.PropertyName)
UpdateDetectReadingOrderFromContent();
else if (e.PropertyName == Entry.ReturnTypeProperty.PropertyName)
UpdateReturnType();
else if (e.PropertyName == Entry.CursorPositionProperty.PropertyName)
UpdateCursorPosition();
else if (e.PropertyName == Entry.SelectionLengthProperty.PropertyName)
UpdateSelectionLength();
else if (e.PropertyName == InputView.IsReadOnlyProperty.PropertyName)
UpdateIsReadOnly();
else if (e.PropertyName == Entry.ClearButtonVisibilityProperty.PropertyName)
UpdateClearButtonVisibility();
}
protected override void UpdateBackgroundColor()
{
base.UpdateBackgroundColor();
if (Control == null)
{
return;
}
// By default some platforms have alternate default background colors when focused
BrushHelpers.UpdateColor(Element.BackgroundColor, ref _backgroundColorFocusedDefaultBrush,
() => Control.BackgroundFocusBrush, brush => Control.BackgroundFocusBrush = brush);
}
void OnNativeTextChanged(object sender, Windows.UI.Xaml.Controls.TextChangedEventArgs args)
{
if (Control.Text == _transformedText)
return;
_transformedText = Element.UpdateFormsText(Control.Text, Element.TextTransform);
Element.SetValueCore(Entry.TextProperty, _transformedText);
}
void TextBoxOnKeyUp(object sender, KeyRoutedEventArgs args)
{
if (args?.Key != VirtualKey.Enter)
return;
if (Element.ReturnType == ReturnType.Next)
{
FocusManager.TryMoveFocus(FocusNavigationDirection.Next);
}
else
{
// Hide the soft keyboard; this matches the behavior of Forms on Android/iOS
Windows.UI.ViewManagement.InputPane.GetForCurrentView().TryHide();
}
((IEntryController)Element).SendCompleted();
}
void UpdateHorizontalTextAlignment()
{
Control.TextAlignment = Element.HorizontalTextAlignment.ToNativeTextAlignment(((IVisualElementController)Element).EffectiveFlowDirection);
}
void UpdateVerticalTextAlignment()
{
Control.VerticalContentAlignment = Element.VerticalTextAlignment.ToNativeVerticalAlignment();
}
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)
{
// ReSharper disable AccessToStaticMemberViaDerivedType
// Resharper wants to simplify 'FormsTextBox' to 'Control', but then it'll conflict with the property 'Control'
Control.ClearValue(FormsTextBox.FontStyleProperty);
Control.ClearValue(FormsTextBox.FontSizeProperty);
Control.ClearValue(FormsTextBox.FontFamilyProperty);
Control.ClearValue(FormsTextBox.FontWeightProperty);
Control.ClearValue(FormsTextBox.FontStretchProperty);
// ReSharper restore AccessToStaticMemberViaDerivedType
}
else
{
Control.ApplyFont(entry);
}
_fontApplied = true;
}
void UpdateCharacterSpacing()
{
Control.CharacterSpacing = Element.CharacterSpacing.ToEm();
}
void UpdateClearButtonVisibility()
{
Control.ClearButtonVisible = Element.ClearButtonVisibility == ClearButtonVisibility.WhileEditing;
}
void UpdateInputScope()
{
Entry entry = Element;
if (entry.Keyboard is CustomKeyboard custom)
{
Control.IsTextPredictionEnabled = (custom.Flags & KeyboardFlags.Suggestions) != 0;
Control.IsSpellCheckEnabled = (custom.Flags & KeyboardFlags.Spellcheck) != 0;
}
else
{
if (entry.IsSet(Entry.IsTextPredictionEnabledProperty))
Control.IsTextPredictionEnabled = entry.IsTextPredictionEnabled;
else
Control.ClearValue(TextBox.IsTextPredictionEnabledProperty);
if (entry.IsSet(InputView.IsSpellCheckEnabledProperty))
Control.IsSpellCheckEnabled = entry.IsSpellCheckEnabled;
else
Control.ClearValue(TextBox.IsSpellCheckEnabledProperty);
}
Control.InputScope = entry.Keyboard.ToInputScope();
}
void UpdateIsPassword()
{
Control.IsPassword = Element.IsPassword;
}
void UpdatePlaceholder()
{
Control.PlaceholderText = Element.Placeholder ?? "";
}
void UpdatePlaceholderColor()
{
Color placeholderColor = Element.PlaceholderColor;
BrushHelpers.UpdateColor(placeholderColor, ref _placeholderDefaultBrush,
() => Control.PlaceholderForegroundBrush, brush => Control.PlaceholderForegroundBrush = brush);
BrushHelpers.UpdateColor(placeholderColor, ref _defaultPlaceholderColorFocusBrush,
() => Control.PlaceholderForegroundFocusBrush, brush => Control.PlaceholderForegroundFocusBrush = brush);
}
void UpdateText()
{
Control.Text = _transformedText = Element.UpdateFormsText(Element.Text, Element.TextTransform);
}
void UpdateTextColor()
{
Color textColor = Element.TextColor;
BrushHelpers.UpdateColor(textColor, ref _textDefaultBrush,
() => Control.Foreground, brush => Control.Foreground = brush);
BrushHelpers.UpdateColor(textColor, ref _defaultTextColorFocusBrush,
() => Control.ForegroundFocusBrush, brush => Control.ForegroundFocusBrush = brush);
}
void UpdateMaxLength()
{
Control.MaxLength = Element.MaxLength;
var currentControlText = Control.Text;
if (currentControlText.Length > Element.MaxLength)
Control.Text = currentControlText.Substring(0, Element.MaxLength);
}
void UpdateDetectReadingOrderFromContent()
{
if (Element.IsSet(Specifics.DetectReadingOrderFromContentProperty))
{
if (Element.OnThisPlatform().GetDetectReadingOrderFromContent())
{
Control.TextReadingOrder = TextReadingOrder.DetectFromContent;
}
else
{
Control.TextReadingOrder = TextReadingOrder.UseFlowDirection;
}
}
}
void UpdateReturnType()
{
if (Control == null || Element == null)
return;
Control.InputScope = Element.ReturnType.ToInputScope();
}
void SelectionChanged(object sender, RoutedEventArgs e)
{
if (_nativeSelectionIsUpdating || Control == null || Element == null)
return;
int cursorPosition = Element.CursorPosition;
if (!_cursorPositionChangePending)
{
var start = cursorPosition;
int selectionStart = Control.SelectionStart;
if (selectionStart != start)
SetCursorPositionFromRenderer(selectionStart);
}
if (!_selectionLengthChangePending)
{
int elementSelectionLength = Math.Min(Control.Text.Length - cursorPosition, Element.SelectionLength);
int controlSelectionLength = Control.SelectionLength;
if (controlSelectionLength != elementSelectionLength)
SetSelectionLengthFromRenderer(controlSelectionLength);
}
}
void UpdateSelectionLength()
{
if (_nativeSelectionIsUpdating || Control == null || Element == null)
return;
if (Control.Focus(FocusState.Programmatic))
{
try
{
int selectionLength = 0;
int elemSelectionLength = Element.SelectionLength;
if (Element.IsSet(Entry.SelectionLengthProperty))
selectionLength = Math.Max(0, Math.Min(Control.Text.Length - Element.CursorPosition, elemSelectionLength));
if (elemSelectionLength != selectionLength)
SetSelectionLengthFromRenderer(selectionLength);
Control.SelectionLength = selectionLength;
}
catch (Exception ex)
{
Log.Warning("Entry", $"Failed to set Control.SelectionLength from SelectionLength: {ex}");
}
finally
{
_selectionLengthChangePending = false;
}
}
}
void UpdateCursorPosition()
{
if (_nativeSelectionIsUpdating || Control == null || Element == null)
return;
if (Control.Focus(FocusState.Programmatic))
{
try
{
int start = Control.Text.Length;
int cursorPosition = Element.CursorPosition;
if (Element.IsSet(Entry.CursorPositionProperty))
start = Math.Min(start, cursorPosition);
if (start != cursorPosition)
SetCursorPositionFromRenderer(start);
Control.SelectionStart = start;
// Length is dependent on start, so we'll need to update it
UpdateSelectionLength();
}
catch (Exception ex)
{
Log.Warning("Entry", $"Failed to set Control.SelectionStart from CursorPosition: {ex}");
}
finally
{
_cursorPositionChangePending = false;
}
}
}
void SetCursorPositionFromRenderer(int start)
{
try
{
_nativeSelectionIsUpdating = true;
ElementController?.SetValueFromRenderer(Entry.CursorPositionProperty, start);
}
catch (Exception ex)
{
Log.Warning("Entry", $"Failed to set CursorPosition from renderer: {ex}");
}
finally
{
_nativeSelectionIsUpdating = false;
}
}
void SetSelectionLengthFromRenderer(int selectionLength)
{
try
{
_nativeSelectionIsUpdating = true;
ElementController?.SetValueFromRenderer(Entry.SelectionLengthProperty, selectionLength);
}
catch (Exception ex)
{
Log.Warning("Entry", $"Failed to set SelectionLength from renderer: {ex}");
}
finally
{
_nativeSelectionIsUpdating = false;
}
}
void UpdateIsReadOnly()
{
Control.IsReadOnly = Element.IsReadOnly;
}
public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
{
FormsTextBox child = Control;
if (Children.Count == 0 || child == null)
return new SizeRequest();
var constraint = new Windows.Foundation.Size(widthConstraint, heightConstraint);
child.Measure(constraint);
var result = FormsTextBox.GetCopyOfSize(child, constraint);
return new SizeRequest(result);
}
}
}