forked from microsoft/WinObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextField.xaml.cpp
More file actions
700 lines (585 loc) · 30.9 KB
/
TextField.xaml.cpp
File metadata and controls
700 lines (585 loc) · 30.9 KB
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
//******************************************************************************
//
// Copyright (c) Microsoft. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//******************************************************************************
// clang-format does not like C++/CX
// clang-format off
#include "pch.h"
#include "TextField.xaml.h"
#include "ObjCXamlControls.h"
#include "Dwrite_3.h"
using namespace Microsoft::WRL;
using namespace Platform;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Text;
using namespace Windows::UI::Xaml::Media;
namespace UIKit {
namespace Xaml {
DependencyProperty^ TextField::s_textProperty = nullptr;
unsigned int TextField::c_borderCornerRadius = 8;
Platform::Boolean TextField::s_dependencyPropertiesRegistered = false;
template <typename T>
static T FindTemplateChild(FrameworkElement^ source, Platform::String^ name) {
T target = nullptr;
if (source) {
unsigned int count = VisualTreeHelper::GetChildrenCount(source);
if (count > 0) {
auto templateRoot = dynamic_cast<FrameworkElement^>(VisualTreeHelper::GetChild(source, 0));
if (templateRoot) {
target = dynamic_cast<T>(templateRoot->FindName(name));
}
}
}
return target;
}
TextField::TextField() {
_RegisterDependencyProperties();
InitializeComponent();
// by default control is enabled
_enabled = true;
// by default control is Non-secure
_secureEntry = false;
// by default no border
_borderStyle = TextBorderStyle::BorderStyleNone;
// by default vertical center aligned
_textVerticalAlignment = Windows::UI::Xaml::VerticalAlignment::Center;
// by default left aligned
_textAlignment = Windows::UI::Xaml::TextAlignment::Left;
// default inputScopeNameValue
_inputScopeNameValue = Input::InputScopeNameValue::Default;
}
void TextField::_SwitchToMode(bool secure) {
if (_backingControl) {
_backingControl->GotFocus -= _gotFocusHandlerRegistrationToken;
_backingControl->LostFocus -= _lostFocusHandlerRegistrationToken;
_backingControl->KeyDown -= _keydownHandlerRegistrationToken;
if (secure) {
// switch to SecureMode, unhook textChanged event
auto textBox = dynamic_cast<Controls::TextBox^>(_backingControl);
if (textBox) {
textBox->TextChanged -= _textChangedHandlerRegistrationToken;
}
} else {
// switch to Non-SecureMode, unhook passwordChanged event
auto passwordBox = dynamic_cast<Controls::PasswordBox^>(_backingControl);
if (passwordBox) {
PasswordBox->PasswordChanged -= _textChangedHandlerRegistrationToken;
}
}
_textChangedHandlerRegistrationToken = {0};
_gotFocusHandlerRegistrationToken = {0};
_lostFocusHandlerRegistrationToken = {0};
_keydownHandlerRegistrationToken = {0};
// removing backing control from the visual
unsigned int index = 0;
if (Children->IndexOf(_backingControl, &index)) {
Children->RemoveAt(index);
}
_backingControl = nullptr;
}
if (secure) {
// switching to secure mode, create passowordbox as backingControl
_backingControl = ref new Controls::PasswordBox();
} else {
// switch to Non-secure mode, create a textbox as backingControl
_backingControl = ref new Controls::TextBox();
}
_backingControl->Loaded += ref new Windows::UI::Xaml::RoutedEventHandler(this, &UIKit::Xaml::TextField::OnBackingControlLoaded);
_gotFocusHandlerRegistrationToken = _backingControl->GotFocus += ref new Windows::UI::Xaml::RoutedEventHandler(this, &TextField::OnGotFocus);
_lostFocusHandlerRegistrationToken = _backingControl->LostFocus += ref new Windows::UI::Xaml::RoutedEventHandler(this, &UIKit::Xaml::TextField::OnLostFocus);
_keydownHandlerRegistrationToken = _backingControl->KeyDown += ref new Windows::UI::Xaml::Input::KeyEventHandler(this, &UIKit::Xaml::TextField::OnKeyDown);
if (secure) {
auto passwordBox = safe_cast<Controls::PasswordBox^>(_backingControl);
_textChangedHandlerRegistrationToken = passwordBox->PasswordChanged += ref new Windows::UI::Xaml::RoutedEventHandler(this, &TextField::OnPasswordChanged);
passwordBox->InputScope = _FindBestFitInputScope();
} else {
auto textBox = safe_cast<Controls::TextBox^>(_backingControl);
_textChangedHandlerRegistrationToken = textBox->TextChanged += ref new Windows::UI::Xaml::Controls::TextChangedEventHandler(this, &TextField::OnTextChanged);
textBox->InputScope = _FindBestFitInputScope();
}
// adding the new backing control to visual tree
Children->Append(_backingControl);
}
Windows::UI::Xaml::Input::InputScope^ TextField::_FindBestFitInputScope() {
Input::InputScopeNameValue targetInputScopeNameValue = _inputScopeNameValue;
if (_secureEntry) {
if (_inputScopeNameValue != Input::InputScopeNameValue::Password && _inputScopeNameValue != Input::InputScopeNameValue::NumericPin) {
// passwordbox only allows these two inputScope, overwrite with password if inputScope isn't those two
targetInputScopeNameValue = Input::InputScopeNameValue::Password;
}
} else {
if (_inputScopeNameValue == Input::InputScopeNameValue::Password) {
// textbox does not like Password inputScope, overwrite with default
targetInputScopeNameValue = Input::InputScopeNameValue::Default;
}
}
auto scopeName = ref new Windows::UI::Xaml::Input::InputScopeName();
scopeName->NameValue = targetInputScopeNameValue;
auto inputScope = ref new Windows::UI::Xaml::Input::InputScope();
inputScope->Names->Append(scopeName);
return inputScope;
}
void TextField::OnBackingControlLoaded(Platform::Object ^sender, Windows::UI::Xaml::RoutedEventArgs ^e)
{
if (_secureEntry) {
// two way bind passwordbox's password property to TextField's Text Property
auto passwordBox = safe_cast<Controls::PasswordBox^>(_backingControl);
Windows::UI::Xaml::Data::Binding^ binding = ref new Windows::UI::Xaml::Data::Binding();
binding->Mode = Windows::UI::Xaml::Data::BindingMode::TwoWay;
binding->ElementName = "Root";
binding->Path = ref new Windows::UI::Xaml::PropertyPath("TextField_Text");
binding->UpdateSourceTrigger = Windows::UI::Xaml::Data::UpdateSourceTrigger::PropertyChanged;
passwordBox->SetBinding(Controls::PasswordBox::PasswordProperty, binding);
// one way binding background to inner passwordbox background
binding = ref new Windows::UI::Xaml::Data::Binding();
binding->Mode = Windows::UI::Xaml::Data::BindingMode::OneWay;
binding->ElementName = "Root";
binding->Path = ref new Windows::UI::Xaml::PropertyPath("Background");
passwordBox->SetBinding(Controls::PasswordBox::BackgroundProperty, binding);
passwordBox->PlaceholderText = _placeholder;
passwordBox->InputScope = _FindBestFitInputScope();
} else {
// two way bind textbox's text property to TextField's Text Property
auto textBox = safe_cast<Controls::TextBox^>(_backingControl);
Windows::UI::Xaml::Data::Binding^ binding = ref new Windows::UI::Xaml::Data::Binding();
binding->ElementName = "Root";
binding->Path = ref new Windows::UI::Xaml::PropertyPath("TextField_Text");
binding->Mode = Windows::UI::Xaml::Data::BindingMode::TwoWay;
binding->UpdateSourceTrigger = Windows::UI::Xaml::Data::UpdateSourceTrigger::PropertyChanged;
textBox->SetBinding(Controls::TextBox::TextProperty, binding);
// one way binding background to inner textbox background
binding = ref new Windows::UI::Xaml::Data::Binding();
binding->Mode = Windows::UI::Xaml::Data::BindingMode::OneWay;
binding->ElementName = "Root";
binding->Path = ref new Windows::UI::Xaml::PropertyPath("Background");
textBox->SetBinding(Controls::TextBox::BackgroundProperty, binding);
textBox->PlaceholderText = _placeholder;
textBox->TextAlignment = _textAlignment;
textBox->InputScope = _FindBestFitInputScope();
}
_backingControl->IsEnabled = _enabled;
_backingControl->Foreground = _foreground;
_SetTextVerticalAlignment();
_SetBorderStyle();
}
// Hook pointer events
void TextField::RegisterEventsHandlers(
const Microsoft::WRL::ComPtr<ABI::Windows::UI::Xaml::IRoutedEventHandler>& textChangedHandler,
const Microsoft::WRL::ComPtr<ABI::Windows::UI::Xaml::IRoutedEventHandler>& gotFocusHandler,
const Microsoft::WRL::ComPtr<ABI::Windows::UI::Xaml::IRoutedEventHandler>& lostFocusHanlder,
const Microsoft::WRL::ComPtr<ABI::Windows::UI::Xaml::IRoutedEventHandler>& enterKeyDownHandler) {
_textChangedHandler = std::move(textChangedHandler);
_gotFocusHandler = std::move(gotFocusHandler);
_lostFocusHanlder = std::move(lostFocusHanlder);
_enterKeyDownHandler = std::move(enterKeyDownHandler);
}
void TextField::UnregisterEventHandlers() {
_textChangedHandler = nullptr;
_gotFocusHandler = nullptr;
_lostFocusHanlder = nullptr;
_enterKeyDownHandler = nullptr;
}
void TextField::_RegisterDependencyProperties() {
if (!s_dependencyPropertiesRegistered) {
s_dependencyPropertiesRegistered = true;
// TODO: These Dependency Properties should be attached to TextField instead of FrameworkElement once #2607 is fixed
s_textProperty = DependencyProperty::RegisterAttached("TextField_Text",
Platform::String::typeid,
FrameworkElement::typeid,
nullptr);
}
}
// Accessor for our Layer content
Image^ TextField::LayerContent::get() {
return nullptr;
}
// Accessor for our Layer content
bool TextField::HasLayerContent::get() {
return false;
}
// Accessor for our SublayerCanvas
Canvas^ TextField::SublayerCanvas::get() {
return nullptr;
}
// Accessor for the LayerProperty that manages the BorderBrush of this TextField
Private::CoreAnimation::LayerProperty^ TextField::GetBorderBrushProperty() {
return nullptr;
}
// Accessor for the LayerProperty that manages the BorderThickness of this TextField
Private::CoreAnimation::LayerProperty^ TextField::GetBorderThicknessProperty() {
return nullptr;
}
TextBox^ TextField::TextBox::get() {
return dynamic_cast<Windows::UI::Xaml::Controls::TextBox^>(_backingControl);
}
PasswordBox^ TextField::PasswordBox::get() {
return dynamic_cast<Windows::UI::Xaml::Controls::PasswordBox^>(_backingControl);
}
void TextField::KillFocus() {
// TODO: this is still a hack to kill the focus on this control
// Will switch to the Windows API when it is available.
if (_backingControl) {
_backingControl->IsEnabled = false;
_backingControl->IsEnabled = true;
}
}
bool TextField::BecomeFirstResponder() {
if (_backingControl) {
return _backingControl->Focus(FocusState::Programmatic);
}
return false;
}
void TextField::OnTextChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::TextChangedEventArgs^ e)
{
if (_textChangedHandler) {
_textChangedHandler->Invoke(
InspectableFromObject(this).Get(),
ObjectToType<ABI::Windows::UI::Xaml::IRoutedEventArgs>(e).Get());
}
}
void TextField::OnPasswordChanged(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
if (_textChangedHandler) {
_textChangedHandler->Invoke(
InspectableFromObject(this).Get(),
ObjectToType<ABI::Windows::UI::Xaml::IRoutedEventArgs>(e).Get());
}
}
void TextField::OnGotFocus(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
if (_gotFocusHandler) {
_gotFocusHandler->Invoke(
InspectableFromObject(this).Get(),
ObjectToType<ABI::Windows::UI::Xaml::IRoutedEventArgs>(e).Get());
}
}
void TextField::OnLostFocus(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
if (_lostFocusHanlder) {
_lostFocusHanlder->Invoke(
InspectableFromObject(this).Get(),
ObjectToType<ABI::Windows::UI::Xaml::IRoutedEventArgs>(e).Get());
}
}
void TextField::OnKeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
if (_enterKeyDownHandler) {
if (e->Key == Windows::System::VirtualKey::Enter) {
_enterKeyDownHandler->Invoke(
InspectableFromObject(this).Get(),
ObjectToType<ABI::Windows::UI::Xaml::IRoutedEventArgs>(e).Get());
}
}
}
void TextField::ApplyFont(Platform::String^ fontFamily, FontStretch strech, FontStyle fontStyle, double fontSize, FontWeight fontWeight) {
if (_backingControl) {
_backingControl->FontFamily = ref new FontFamily(fontFamily);
_backingControl->FontStretch = strech;
_backingControl->FontStyle = fontStyle;
_backingControl->FontSize = fontSize;
_backingControl->FontWeight = fontWeight;
}
}
void TextField::_SetTextVerticalAlignment() {
// Set vertical aligment of both content and placeholder to the same value
// In TextBox/PasswordBox control templates, both have ContentElement and PlaceholderTextContentPresenter
// to hold normal text/password content and placeholder content
// TextBox control template can be found at https://msdn.microsoft.com/en-us/library/windows/apps/mt299154.aspx
// PasswordBox control template can be found at https://msdn.microsoft.com/en-us/library/dd334412(v=vs.95).aspx
auto target = FindTemplateChild<FrameworkElement^>(_backingControl, "ContentElement");
if (target) {
target->VerticalAlignment = VerticalTextAlignment;
}
target = FindTemplateChild<FrameworkElement^>(_backingControl, "PlaceholderTextContentPresenter");
if (target) {
target->VerticalAlignment = VerticalTextAlignment;
}
}
void TextField::_SetBorderStyle() {
if (_backingControl) {
switch (BorderStyle) {
case TextBorderStyle::BorderStyleNone:
_backingControl->BorderThickness = ThicknessHelper::FromUniformLength(0);
break;
case TextBorderStyle::BorderStyleLine:
_backingControl->BorderThickness = ThicknessHelper::FromUniformLength(1);
break;
case TextBorderStyle::UITextBorderStyleBezel:
UNIMPLEMENTED_WITH_MSG("UITextBorderStyleBezel not yet supported; treated as no border.");
_backingControl->BorderThickness = ThicknessHelper::FromUniformLength(0);
break;
case TextBorderStyle::UITextBorderStyleRoundedRect:
UNIMPLEMENTED_WITH_MSG("UITextBorderStyleBezel not yet supported; treated as no border.");
_backingControl->BorderThickness = ThicknessHelper::FromUniformLength(0);
break;
}
}
}
void TextField::_SetPlaceHolder() {
if (_backingControl) {
if (_secureEntry) {
auto passwordBox = safe_cast<Controls::PasswordBox^>(_backingControl);
passwordBox->PlaceholderText = _placeholder;
} else {
auto textBox = safe_cast<Controls::TextBox^>(_backingControl);
textBox->PlaceholderText = _placeholder;
}
}
}
void TextField::_SetInputScope() {
if (_backingControl) {
if (_secureEntry) {
auto passwordBox = safe_cast<Controls::PasswordBox^>(_backingControl);
passwordBox->InputScope = _FindBestFitInputScope();
}
else {
auto textBox = safe_cast<Controls::TextBox^>(_backingControl);
textBox->InputScope = _FindBestFitInputScope();
}
}
}
void TextField::_SetEnabled() {
if (_backingControl) {
_backingControl->IsEnabled = _enabled;
}
}
void TextField::_SetForeground() {
if (_backingControl) {
_backingControl->Foreground = _foreground;
}
}
void TextField::_SetBackground() {
if (_backingControl) {
_backingControl->Background = Background;
}
}
void TextField::_SetTextAlignment() {
if (_backingControl) {
if (!_secureEntry) {
auto textBox = safe_cast<Controls::TextBox^>(_backingControl);
textBox->TextAlignment = _textAlignment;
} else {
UNIMPLEMENTED_WITH_MSG("XAML passwordbox currently does not allow set TextAlignment properties");
}
}
}
} /* Xaml*/
} /* UIKit*/
////////////////////////////////////////////////////////////////////////////////////
// ObjectiveC Interop
////////////////////////////////////////////////////////////////////////////////////
// Returns a UIKit::Xaml::TextField as an IInspectable
UIKIT_XAML_EXPORT void XamlCreateTextField(IInspectable** created) {
ComPtr<IInspectable> inspectable = InspectableFromObject(ref new UIKit::Xaml::TextField());
*created = inspectable.Detach();
}
// Returns a UIKit::Xaml::TextField's backing Canvas as an IInspectable
UIKIT_XAML_EXPORT IInspectable* XamlGetTextFieldSubLayerCanvas(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
return InspectableFromObject(textField->SublayerCanvas).Detach();
}
// Retrieves the UIKit::Xaml::TextField's backing TextBox as an IInspectable
UIKIT_XAML_EXPORT IInspectable* XamlGetTextFieldTextBox(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
return InspectableFromObject(textField->TextBox).Detach();
}
// Retrieves the UIKit::Xaml::TextField's backing PasswordBox as an IInspectable
UIKIT_XAML_EXPORT IInspectable* XamlGetTextFieldPasswordBox(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
return InspectableFromObject(textField->PasswordBox).Detach();
}
// Set the UIKit::Xaml::TextField's secureTextEntry property
UIKIT_XAML_EXPORT void XamlSetTextFieldSecureTextEntryValue(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField, bool secureTextEntry) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
textField->SecureEntry = secureTextEntry;
}
// Get the UIKit::Xaml::TextField's secureTextEntry property
UIKIT_XAML_EXPORT bool XamlGetTextFieldSecureTextEntryValue(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
return textField->SecureEntry;
}
// Set the UIKit::Xaml::TextField's Text property
UIKIT_XAML_EXPORT void XamlSetTextFieldText(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField, const std::wstring& text) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
textField->TextField_Text = Platform::StringReference(text.c_str());
if (!textField->SecureEntry) {
// this is to set the selection to the end of of text so that
// to ensure when user tap into TextBox, the caret is at
// the end of the TextBox.
textField->TextBox->SelectionStart = textField->TextField_Text->Length();
textField->TextBox->SelectionLength = 0;
} else {
UNIMPLEMENTED_WITH_MSG("XAML passwordbox currently does not allow set selection programmatically");
}
}
// Get the UIKit::Xaml::TextField's Text property
UIKIT_XAML_EXPORT IInspectable* XamlGetTextFieldText(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
Platform::Object^ propVal = Windows::Foundation::PropertyValue::CreateString(textField->TextField_Text);
return InspectableFromObject(propVal).Detach();
}
// Set the UIKit::Xaml::TextField's PlaceHolder property
UIKIT_XAML_EXPORT void XamlSetTextFieldPlaceholder(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField, const std::wstring& text) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
textField->Placeholder = Platform::StringReference(text.c_str());
}
// Get the UIKit::Xaml::TextField's PlaceHolder property
UIKIT_XAML_EXPORT IInspectable* XamlGetTextFieldPlaceholder(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
Platform::Object^ propVal = Windows::Foundation::PropertyValue::CreateString(textField->Placeholder);
return InspectableFromObject(propVal).Detach();
}
// Set the UIKit::Xaml::TextField's InputScope property
UIKIT_XAML_EXPORT void XamlSetTextFieldInputScope(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField, int inputScopeNameValue) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
auto inputScopeNameValueToUse = static_cast<Input::InputScopeNameValue>(inputScopeNameValue);
textField->InputScopeNameValue = inputScopeNameValueToUse;
}
// Set the UIKit::Xaml::TextField's Enabled property
UIKIT_XAML_EXPORT void XamlSetTextFieldEnabled(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField, bool enabled) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
textField->Enabled = enabled;
}
// Get the UIKit::Xaml::TextField's Enabled property
UIKIT_XAML_EXPORT bool XamlGetTextFieldEnabled(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
return textField->Enabled;
}
// kill the focus if TextFiled has the editing focus
UIKIT_XAML_EXPORT void XamlTextFieldKillFocus(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
textField->KillFocus();
}
// Set the UIKit::Xaml::TextField's textColor property
UIKIT_XAML_EXPORT void XamlSetTextFieldTextColor(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField, const Microsoft::WRL::ComPtr<IInspectable>& textCOlor) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
auto textColorBrush = safe_cast<Media::SolidColorBrush^>(reinterpret_cast<Platform::Object^>(textCOlor.Get()));
textField->Foreground = textColorBrush;
}
// Get the UIKit::Xaml::TextField's Forground property
UIKIT_XAML_EXPORT IInspectable* XamlGetTextFieldTextColor(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
return InspectableFromObject(textField->Foreground).Detach();
}
// Set the UIKit::Xaml::TextField's Background property
UIKIT_XAML_EXPORT void XamlSetTextFieldBackgroundColor(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField, const Microsoft::WRL::ComPtr<IInspectable>& backgroundColorBrush) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
auto background = safe_cast<Media::SolidColorBrush^>(reinterpret_cast<Platform::Object^>(backgroundColorBrush.Get()));
textField->Background = background;
}
// Get the UIKit::Xaml::TextField's Background property
UIKIT_XAML_EXPORT IInspectable* XamlGetTextFieldBackgroundColor(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
// try convert the background (Brush) to SolidColorBrush to retrieve the background color
auto backgroundColorBrush = dynamic_cast<Media::SolidColorBrush^>(textField->Background);
if (backgroundColorBrush) {
return InspectableFromObject(backgroundColorBrush).Detach();
}
return nullptr;
}
// Set the UIKit::Xaml::TextField's BackgroundImage property
UIKIT_XAML_EXPORT void XamlSetTextFieldBackgroundImage(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField, const Microsoft::WRL::ComPtr<IInspectable>& backgroundImageBrush) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
auto background = safe_cast<Media::ImageBrush^>(reinterpret_cast<Platform::Object^>(backgroundImageBrush.Get()));
textField->Background = background;
}
// Set the UIKit::Xaml::TextField's TextAlignment property
UIKIT_XAML_EXPORT void XamlSetTextFieldTextAlignment(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField, int textAlignment) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
textField->TextAlignment = static_cast<Windows::UI::Xaml::TextAlignment>(textAlignment);
}
// Get the UIKit::Xaml::TextField's TextAlignment property
UIKIT_XAML_EXPORT int XamlGetTextFieldTextAlignment(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
return static_cast<int>(textField->TextAlignment);
}
// Get the UIKit::Xaml::TextField's Editing property
UIKIT_XAML_EXPORT Platform::Boolean XamlGetTextFieldEditing(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
return textField->Editing;
}
// Get the UIKit::Xaml::TextField's Editing property
UIKIT_XAML_EXPORT Platform::Boolean XamlTextFieldBecomeFirstResponder(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
return textField->BecomeFirstResponder();
}
UIKIT_XAML_EXPORT void XamlTextFieldRegisterEventHandlers(
const ComPtr<IInspectable>& inspectableTextField,
const ComPtr<IInspectable>& textChangedHandler,
const ComPtr<IInspectable>& gotFocusHandler,
const ComPtr<IInspectable>& lostFocusHanlder,
const ComPtr<IInspectable>& enterKeyDownHandler) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
textField->RegisterEventsHandlers(InspectableToType<ABI::Windows::UI::Xaml::IRoutedEventHandler>(textChangedHandler),
InspectableToType<ABI::Windows::UI::Xaml::IRoutedEventHandler>(gotFocusHandler),
InspectableToType<ABI::Windows::UI::Xaml::IRoutedEventHandler>(lostFocusHanlder),
InspectableToType<ABI::Windows::UI::Xaml::IRoutedEventHandler>(enterKeyDownHandler));
}
UIKIT_XAML_EXPORT void XamlTextFieldUnregisterEventHandlers(const ComPtr<IInspectable>& inspectableTextField) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
textField->UnregisterEventHandlers();
}
// Set the UIKit::Xaml::TextField's TextAlignment property
UIKIT_XAML_EXPORT void XamlSetTextFieldVerticalTextAlignment(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField, int verticalTextAlignment) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
textField->VerticalTextAlignment = static_cast<Windows::UI::Xaml::VerticalAlignment>(verticalTextAlignment);
}
// Get the UIKit::Xaml::TextField's TextAlignment property
UIKIT_XAML_EXPORT int XamlGetTextFieldVerticalTextAlignment(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
return static_cast<int>(textField->VerticalTextAlignment);
}
// Set the UIKit::Xaml::TextField's Border property
UIKIT_XAML_EXPORT void XamlSetTextFieldBorderStyle(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField, int borderStyle) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
textField->BorderStyle = static_cast<UIKit::Xaml::TextBorderStyle>(borderStyle);
}
// Get the UIKit::Xaml::TextField's Border property
UIKIT_XAML_EXPORT int XamlGetTextFieldBorderStyle(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
return static_cast<int>(textField->BorderStyle);
}
FontWeight ConvertToFontWeight(DWRITE_FONT_WEIGHT fontWeight) {
switch (fontWeight) {
case DWRITE_FONT_WEIGHT_THIN:
return FontWeights::Thin;
case DWRITE_FONT_WEIGHT_ULTRA_LIGHT:
return FontWeights::ExtraLight;
case DWRITE_FONT_WEIGHT_LIGHT:
return FontWeights::Light;
case DWRITE_FONT_WEIGHT_SEMI_LIGHT:
return FontWeights::SemiLight;
case DWRITE_FONT_WEIGHT_REGULAR:
return FontWeights::Normal;
case DWRITE_FONT_WEIGHT_MEDIUM:
return FontWeights::Medium;
case DWRITE_FONT_WEIGHT_SEMI_BOLD:
return FontWeights::SemiBold;
case DWRITE_FONT_WEIGHT_BOLD:
return FontWeights::Bold;
case DWRITE_FONT_WEIGHT_ULTRA_BOLD:
return FontWeights::ExtraBold;
case DWRITE_FONT_WEIGHT_HEAVY:
return FontWeights::Black;
case DWRITE_FONT_WEIGHT_ULTRA_BLACK:
return FontWeights::ExtraBlack;
}
return FontWeights::Normal;
}
// ApplyFont
UIKIT_XAML_EXPORT void XamlTextFieldApplyFont(const Microsoft::WRL::ComPtr<IInspectable>& inspectableTextField, const std::wstring& fontFamilyname, int fontStrech, int fontStyle, double fontSize, int fontWeight) {
auto textField = safe_cast<UIKit::Xaml::TextField^>(reinterpret_cast<Platform::Object^>(inspectableTextField.Get()));
textField->ApplyFont(Platform::StringReference(fontFamilyname.c_str()), safe_cast<FontStretch>(fontStrech), safe_cast<FontStyle>(fontStyle), fontSize, ConvertToFontWeight(safe_cast<DWRITE_FONT_WEIGHT>(fontWeight)));
}