/
Control.cs
1315 lines (1066 loc) · 43.2 KB
/
Control.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
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using Uno.UI;
using System.Linq;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Windows.UI.Text;
using Microsoft.UI.Xaml.Markup;
using System.ComponentModel;
using System.Reflection;
using Uno.UI.Xaml;
using Windows.Foundation;
using Uno;
using Uno.UI.Xaml.Core;
using Uno.UI.Xaml.Input;
#if __ANDROID__
using View = Android.Views.View;
using ViewGroup = Android.Views.ViewGroup;
using Font = Android.Graphics.Typeface;
using Android.Graphics;
#elif __IOS__
using View = UIKit.UIView;
using ViewGroup = UIKit.UIView;
using Color = UIKit.UIColor;
using Font = UIKit.UIFont;
using UIKit;
#elif __MACOS__
using View = AppKit.NSView;
using ViewGroup = AppKit.NSView;
using Color = AppKit.NSColor;
using Font = AppKit.NSFont;
using AppKit;
#elif UNO_REFERENCE_API || IS_UNIT_TESTS
using View = Microsoft.UI.Xaml.UIElement;
#endif
namespace Microsoft.UI.Xaml.Controls
{
public partial class Control : FrameworkElement
{
private bool _suspendStateChanges;
private View _templatedRoot;
private bool _updateTemplate;
private bool _suppressIsEnabled;
private void InitializeControl()
{
SetDefaultForeground(ForegroundProperty);
SubscribeToOverridenRoutedEvents();
OnIsFocusableChanged();
DefaultStyleKey = typeof(Control);
}
// TODO: Should use DefaultStyleKeyProperty DP
protected object DefaultStyleKey { get; set; }
private protected override bool IsTabStopDefaultValue => true;
protected override bool IsSimpleLayout => true;
internal override bool IsEnabledOverride() => IsEnabled && base.IsEnabledOverride();
internal override void UpdateThemeBindings(Data.ResourceUpdateReason updateReason)
{
base.UpdateThemeBindings(updateReason);
//override the default value from dependency property based on application theme
SetDefaultForeground(ForegroundProperty);
}
private protected override Type GetDefaultStyleKey() => DefaultStyleKey as Type;
protected override void OnBackgroundChanged(DependencyPropertyChangedEventArgs e)
{
// this is defined in the FrameworkElement mixin, and must not be used in Control.
// When setting the background color in a Control, the property is simply used as a placeholder
// for children controls, applied by inheritance.
}
internal virtual void UpdateVisualState(bool useTransitions = true)
{
if (!_suspendStateChanges)
{
ChangeVisualState(useTransitions);
}
}
private protected virtual void ChangeVisualState(bool useTransitions)
{
}
/// <summary>
/// Will be set to Template when it is applied
/// </summary>
private ControlTemplate _controlTemplateUsedLastUpdate;
partial void UnregisterSubView();
partial void RegisterSubView(View child);
#region IsEnabled DependencyProperty
// Note: we keep the event args as a private field for perf consideration: This avoids creating a new instance each time.
// As it's used only internally it's safe to do so.
[ThreadStatic]
private static IsEnabledChangedEventArgs _isEnabledChangedEventArgs;
public event DependencyPropertyChangedEventHandler IsEnabledChanged;
[GeneratedDependencyProperty(DefaultValue = true, ChangedCallback = true, CoerceCallback = true, Options = FrameworkPropertyMetadataOptions.Inherits | FrameworkPropertyMetadataOptions.KeepCoercedWhenEquals)]
public static DependencyProperty IsEnabledProperty { get; } = CreateIsEnabledProperty();
public bool IsEnabled
{
get => GetIsEnabledValue();
set => SetIsEnabledValue(value);
}
private void OnIsEnabledChanged(DependencyPropertyChangedEventArgs args)
{
#if UNO_HAS_MANAGED_POINTERS || __WASM__
UpdateHitTest();
#endif
_isEnabledChangedEventArgs ??= new IsEnabledChangedEventArgs();
_isEnabledChangedEventArgs.SourceEvent = args;
OnIsEnabledChanged(_isEnabledChangedEventArgs);
#if __ANDROID__
var newValue = (bool)args.NewValue;
base.SetNativeIsEnabled(newValue);
this.Enabled = newValue;
#elif __IOS__
UserInteractionEnabled = (bool)args.NewValue;
#elif __MACOS__
// UserInteractionEnabled = (bool)args.NewValue; // UNO-TODO: Set MacOS native equivalent
#endif
IsEnabledChanged?.Invoke(this, args);
// TODO: move focus elsewhere if control.FocusState != FocusState.Unfocused
#if __WASM__
if (FeatureConfiguration.UIElement.AssignDOMXamlProperties)
{
UpdateDOMProperties();
}
#endif
}
#endregion
internal bool IsEnabledSuppressed => _suppressIsEnabled;
/// <summary>
/// Provides the ability to disable <see cref="IsEnabled"/> value changes, e.g. in the context of ICommand CanExecute.
/// </summary>
/// <param name="suppress">If true, <see cref="IsEnabled"/> will always be false</param>
private protected void SuppressIsEnabled(bool suppress)
{
if (_suppressIsEnabled != suppress)
{
_suppressIsEnabled = suppress;
this.CoerceValue(IsEnabledProperty);
}
}
private protected virtual object CoerceIsEnabled(object baseValue, DependencyPropertyValuePrecedences precedence)
{
if (_suppressIsEnabled)
{
return false;
}
// The baseValue hasn't been set inside PropertyDetails yet, so we need to make sure we're not
// reading soon-to-be-outdated values
var parentValue = precedence == DependencyPropertyValuePrecedences.Inheritance ?
baseValue :
this.GetValue(IsEnabledProperty, DependencyPropertyValuePrecedences.Inheritance);
// If the parent is disabled, this control must be disabled as well
if (parentValue is false)
{
return false;
}
// otherwise use the more local value
var (localValue, localPrecedence) = this.GetValueUnderPrecedence(IsEnabledProperty, DependencyPropertyValuePrecedences.Coercion);
if (localPrecedence >= precedence) // > means weaker precedence
{
// The baseValue hasn't been set inside PropertyDetails yet, so we need to make sure we're not
// using the old weaker value when a new stronger value is being set
localValue = baseValue;
}
return localValue;
}
#region Template DependencyProperty
public ControlTemplate Template
{
get { return (ControlTemplate)GetValue(TemplateProperty); }
set { SetValue(TemplateProperty, value); }
}
// Using a DependencyProperty as the backing store for Template. This enables animation, styling, binding, etc...
public static DependencyProperty TemplateProperty { get; } =
DependencyProperty.Register("Template", typeof(ControlTemplate), typeof(Control), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.ValueDoesNotInheritDataContext, (s, e) => ((Control)s)?.OnTemplateChanged(e)));
private void OnTemplateChanged(DependencyPropertyChangedEventArgs e)
{
_updateTemplate = true;
SetUpdateControlTemplate();
}
#endregion
/// <summary>
/// Defines a method that will request the update of the control's template and request layout update.
/// </summary>
/// <param name="forceUpdate">If true, forces an update even if the control has no parent.</param>
internal void SetUpdateControlTemplate(bool forceUpdate = false)
{
if (
!FeatureConfiguration.Control.UseLegacyLazyApplyTemplate
|| forceUpdate
|| this.HasParent()
|| CanCreateTemplateWithoutParent
)
{
UpdateTemplate();
this.InvalidateMeasure();
}
}
/// <summary>
/// Represents the single child that is the result of the control template application.
/// </summary>
internal View TemplatedRoot
{
get { return _templatedRoot; }
set
{
if (_templatedRoot == value)
{
return;
}
CleanupView(_templatedRoot);
UnregisterSubView();
_templatedRoot = value;
if (value != null)
{
if (_templatedRoot is IDependencyObjectStoreProvider provider)
{
provider.Store.SetValue(provider.Store.TemplatedParentProperty, this, DependencyPropertyValuePrecedences.Local);
}
RegisterSubView(value);
if (_templatedRoot != null)
{
RegisterContentTemplateRoot();
if (
#if __CROSSRUNTIME__
!IsLoading &&
#endif
!IsLoaded && FeatureConfiguration.Control.UseDeferredOnApplyTemplate)
{
// It's too soon the call the ".OnApplyTemplate" method: it should be invoked after the "Loading" event.
// Note: we however still allow if already 'IsLoading':
//
// If this child is added to its parent while this parent is 'IsLoading' itself (eg. loading its template),
// the parent will invoke the Loading on this child element (and the PostLoading which will "dequeue" the _applyTemplateShouldBeInvoked),
// which will set the 'IsLoading' flag.
//
// The parent will then apply its own style, which might set/change the template of this element (if data-bound or set using VisualState),
// which would end here and set this _applyTemplateShouldBeInvoked flag (if IsLoaded were not allowed!).
//
// The parent will then invoke the Loading on all its children, but as this child has already been flagged as 'IsLoading',
// it will be ignored and the 'PostLoading' won't be invokes a second time, driving the control to never "dequeue" the _applyTemplateShouldBeInvoked.
_applyTemplateShouldBeInvoked = true;
}
else
{
_applyTemplateShouldBeInvoked = false;
OnApplyTemplate();
}
}
}
}
}
private bool _applyTemplateShouldBeInvoked;
private protected override void OnPostLoading()
{
base.OnPostLoading();
TryCallOnApplyTemplate();
// Update bindings to ensure resources defined
// in visual parents get applied.
this.UpdateResourceBindings();
}
private void TryCallOnApplyTemplate()
{
if (_applyTemplateShouldBeInvoked)
{
_applyTemplateShouldBeInvoked = false;
OnApplyTemplate();
}
}
private void SubscribeToOverridenRoutedEvents()
{
// Overridden Events are registered from constructor to ensure they are
// registered first in event handlers.
// https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.control.onpointerpressed#remarks
var implementedEvents = GetImplementedRoutedEventsForType(GetType());
if (HasFlag(implementedEvents, RoutedEventFlag.PointerPressed))
{
PointerPressed += OnPointerPressedHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.PointerReleased))
{
PointerReleased += OnPointerReleasedHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.PointerMoved))
{
PointerMoved += OnPointerMovedHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.PointerEntered))
{
PointerEntered += OnPointerEnteredHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.PointerExited))
{
PointerExited += OnPointerExitedHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.PointerCanceled))
{
PointerCanceled += OnPointerCanceledHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.PointerCaptureLost))
{
PointerCaptureLost += OnPointerCaptureLostHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.PointerWheelChanged))
{
PointerWheelChanged += OnPointerWheelChangedHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.ManipulationStarting))
{
ManipulationStarting += OnManipulationStartingHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.ManipulationStarted))
{
ManipulationStarted += OnManipulationStartedHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.ManipulationDelta))
{
ManipulationDelta += OnManipulationDeltaHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.ManipulationInertiaStarting))
{
ManipulationInertiaStarting += OnManipulationInertiaStartingHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.ManipulationCompleted))
{
ManipulationCompleted += OnManipulationCompletedHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.Tapped))
{
Tapped += OnTappedHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.DoubleTapped))
{
DoubleTapped += OnDoubleTappedHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.RightTapped))
{
RightTapped += OnRightTappedHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.DragEnter))
{
DragEnter += OnDragEnterHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.DragOver))
{
DragOver += OnDragOverHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.DragLeave))
{
DragLeave += OnDragLeaveHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.Drop))
{
Drop += OnDropHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.Holding))
{
Holding += OnHoldingHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.KeyDown))
{
KeyDown += OnKeyDownHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.KeyUp))
{
KeyUp += OnKeyUpHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.GotFocus))
{
GotFocus += OnGotFocusHandler;
}
if (HasFlag(implementedEvents, RoutedEventFlag.LostFocus))
{
LostFocus += OnLostFocusHandler;
}
bool HasFlag(RoutedEventFlag implementedEvents, RoutedEventFlag flag) => (implementedEvents & flag) != 0;
}
private protected override void OnLoaded()
{
SetUpdateControlTemplate();
base.OnLoaded();
}
protected override Size MeasureOverride(Size availableSize)
=> MeasureFirstChild(availableSize);
protected override Size ArrangeOverride(Size finalSize)
=> ArrangeFirstChild(finalSize);
/// <summary>
/// Loads the relevant control template so that its parts can be referenced.
/// </summary>
/// <returns>A value that indicates whether the visual tree was rebuilt by this call. True if the tree was rebuilt; false if the previous visual tree was retained.</returns>
public bool ApplyTemplate()
{
var currentTemplateRoot = _templatedRoot;
SetUpdateControlTemplate(forceUpdate: true);
// When .ApplyTemplate is called manually, we should not defer the call to OnApplyTemplate
TryCallOnApplyTemplate();
return currentTemplateRoot != _templatedRoot;
}
/// <summary>
/// Applies default Style and implicit/explicit Style if not applied already, and materializes template.
/// </summary>
internal void EnsureTemplate()
{
ApplyStyles();
ApplyTemplate();
}
/// <summary>
/// Finds a realized element in the control template.
/// </summary>
/// <param name="childName">The name of the template part.</param>
/// <returns>The first template part of the specified name; otherwise, null.</returns>
public DependencyObject GetTemplateChild(string childName)
{
return FindNameInScope(TemplatedRoot as IFrameworkElement, childName) as DependencyObject
?? FindName(childName) as DependencyObject;
}
/// <summary>
/// Finds a realized element in the control template of the specified type.
/// </summary>
/// <typeparam name="T">The type of the template part.</typeparam>
/// <param name="childName">The name of the template part.</param>
/// <returns>The first template part of the specified name; otherwise, null.</returns>
internal T GetTemplateChild<T>(string childName) where T : class, DependencyObject
{
return FindNameInScope(TemplatedRoot as IFrameworkElement, childName) as T ?? FindName(childName) as T;
}
private static object FindNameInScope(IFrameworkElement root, string name)
{
return root != null
&& name != null
&& NameScope.GetNameScope(root) is INameScope nameScope
&& nameScope.FindName(name) is DependencyObject element
// Doesn't currently support ElementStub (fallbacks to other FindName implementation)
&& !(element is ElementStub)
? element
: null;
}
private void CleanupView(View view)
{
if (view is IDependencyObjectStoreProvider provider)
{
provider.Store.Parent = null;
provider.Store.ClearValue(provider.Store.TemplatedParentProperty, DependencyPropertyValuePrecedences.Local);
}
}
/// <summary>
/// Allows to bypass the validation of the presence of a parent.
/// </summary>
/// <remarks>
/// Setting to true property allows the ContentControl to create its children even if
/// no parent view has been set. This is used for the ListView control (and other virtualizing controls)
/// to measure items properly. These controls set the size of the view based on the size reported
/// immediately after the BaseAdapter.GetView method returns, but the parent still has not been set.
///
/// The Content control uses this delayed creation as an optimization technique for layout creation, when controls
/// are created but not yet used.
/// </remarks>
protected virtual bool CanCreateTemplateWithoutParent { get; }
protected override void OnVisibilityChanged(Visibility oldValue, Visibility newValue)
{
base.OnVisibilityChanged(oldValue, newValue);
if (oldValue == Visibility.Collapsed && newValue == Visibility.Visible)
{
SetUpdateControlTemplate();
}
OnIsFocusableChanged();
}
private void UpdateTemplate()
{
// If TemplatedRoot is null, it must be updated even if the templates haven't changed
if (TemplatedRoot == null)
{
_controlTemplateUsedLastUpdate = null;
}
if (_updateTemplate && !object.Equals(Template, _controlTemplateUsedLastUpdate))
{
_controlTemplateUsedLastUpdate = Template;
if (Template != null)
{
TemplatedRoot = Template.LoadContentCached();
}
else
{
TemplatedRoot = null;
}
_updateTemplate = false;
}
}
partial void RegisterContentTemplateRoot();
#region Foreground Dependency Property
public
#if __ANDROID__
new
#endif
Brush Foreground
{
get { return (Brush)this.GetValue(ForegroundProperty); }
set { this.SetValue(ForegroundProperty, value); }
}
public static DependencyProperty ForegroundProperty { get; } =
DependencyProperty.Register(
"Foreground",
typeof(Brush),
typeof(Control),
new FrameworkPropertyMetadata(
SolidColorBrushHelper.Black,
FrameworkPropertyMetadataOptions.Inherits,
propertyChangedCallback: (s, e) => ((Control)s)?.OnForegroundColorChanged(e.OldValue as Brush, e.NewValue as Brush)
)
);
#endregion
#region FontWeight
public FontWeight FontWeight
{
get { return (FontWeight)this.GetValue(FontWeightProperty); }
set { this.SetValue(FontWeightProperty, value); }
}
public static DependencyProperty FontWeightProperty { get; } =
DependencyProperty.Register(
"FontWeight",
typeof(FontWeight),
typeof(Control),
new FrameworkPropertyMetadata(
FontWeights.Normal,
FrameworkPropertyMetadataOptions.Inherits,
(s, e) => ((Control)s)?.OnFontWeightChanged((FontWeight)e.OldValue, (FontWeight)e.NewValue)
)
);
#endregion
#region FontSize
public double FontSize
{
get { return (double)this.GetValue(FontSizeProperty); }
set { this.SetValue(FontSizeProperty, value); }
}
public static DependencyProperty FontSizeProperty { get; } =
DependencyProperty.Register(
"FontSize",
typeof(double),
typeof(Control),
new FrameworkPropertyMetadata(
14.0,
FrameworkPropertyMetadataOptions.Inherits,
(s, e) => ((Control)s)?.OnFontSizeChanged((double)e.OldValue, (double)e.NewValue)
)
);
#endregion
#region FontFamily
public FontFamily FontFamily
{
get { return (FontFamily)this.GetValue(FontFamilyProperty); }
set { this.SetValue(FontFamilyProperty, value); }
}
public static DependencyProperty FontFamilyProperty { get; } =
DependencyProperty.Register(
"FontFamily",
typeof(FontFamily),
typeof(Control),
new FrameworkPropertyMetadata(
FontFamily.Default,
FrameworkPropertyMetadataOptions.Inherits,
(s, e) => ((Control)s)?.OnFontFamilyChanged(e.OldValue as FontFamily, e.NewValue as FontFamily)
)
);
#endregion
#region FontStyle
public FontStyle FontStyle
{
get { return (FontStyle)this.GetValue(FontStyleProperty); }
set { this.SetValue(FontStyleProperty, value); }
}
public static DependencyProperty FontStyleProperty { get; } =
DependencyProperty.Register(
"FontStyle",
typeof(FontStyle),
typeof(Control),
new FrameworkPropertyMetadata(
FontStyle.Normal,
FrameworkPropertyMetadataOptions.Inherits,
(s, e) => ((Control)s)?.OnFontStyleChanged((FontStyle)e.OldValue, (FontStyle)e.NewValue)
)
);
#endregion
#region Padding DependencyProperty
public Thickness Padding
{
get { return (Thickness)GetValue(PaddingProperty); }
set { SetValue(PaddingProperty, value); }
}
// Using a DependencyProperty as the backing store for Padding. This enables animation, styling, binding, etc...
public static DependencyProperty PaddingProperty { get; } =
DependencyProperty.Register(
"Padding",
typeof(Thickness),
typeof(Control),
new FrameworkPropertyMetadata(
Thickness.Empty,
FrameworkPropertyMetadataOptions.None,
(s, e) => ((Control)s)?.OnPaddingChanged((Thickness)e.OldValue, (Thickness)e.NewValue)
)
);
#endregion
#region BorderThickness DependencyProperty
public Thickness BorderThickness
{
get { return (Thickness)GetValue(BorderThicknessProperty); }
set { SetValue(BorderThicknessProperty, value); }
}
// Using a DependencyProperty as the backing store for BorderThickness. This enables animation, styling, binding, etc...
public static DependencyProperty BorderThicknessProperty { get; } =
DependencyProperty.Register(
"BorderThickness",
typeof(Thickness),
typeof(Control),
new FrameworkPropertyMetadata(
Thickness.Empty,
FrameworkPropertyMetadataOptions.None,
(s, e) => ((Control)s)?.OnBorderThicknessChanged((Thickness)e.OldValue, (Thickness)e.NewValue)
)
);
#endregion
#region BorderBrush Dependency Property
#if __ANDROID__
//This field is never accessed. It just exists to create a reference, because the DP causes issues with ImageBrush of the backing bitmap being prematurely garbage-collected. (Bug with ConditionalWeakTable? https://bugzilla.xamarin.com/show_bug.cgi?id=21620)
private Brush _borderBrushStrongReference;
#endif
public Brush BorderBrush
{
get { return (Brush)this.GetValue(BorderBrushProperty); }
set
{
this.SetValue(BorderBrushProperty, value);
#if __ANDROID__
_borderBrushStrongReference = value;
#endif
}
}
// Using a DependencyProperty as the backing store for BorderBrush. This enables animation, styling, binding, etc...
public static DependencyProperty BorderBrushProperty { get; } =
DependencyProperty.Register(
"BorderBrush",
typeof(Brush),
typeof(Control),
new FrameworkPropertyMetadata(
SolidColorBrushHelper.Transparent,
FrameworkPropertyMetadataOptions.None,
propertyChangedCallback: (s, e) => ((Control)s).OnBorderBrushChanged((Brush)e.OldValue, (Brush)e.NewValue)
)
);
public CornerRadius CornerRadius
{
get => GetCornerRadiusValue();
set => SetCornerRadiusValue(value);
}
public static CornerRadius GetCornerRadiusDefaultValue() => default(CornerRadius);
[GeneratedDependencyProperty(ChangedCallbackName = nameof(OnCornerRadiousChanged))]
public static DependencyProperty CornerRadiusProperty { get; } = CreateCornerRadiusProperty();
private protected virtual void OnCornerRadiousChanged(DependencyPropertyChangedEventArgs args)
{
}
#endregion
#if HAS_UNO_WINUI
private protected override void OnIsTabStopChanged(bool oldValue, bool newValue)
{
OnIsFocusableChanged();
}
#endif
#region TabNavigation DependencyProperty
public KeyboardNavigationMode TabNavigation
{
get => TabFocusNavigation;
set => TabFocusNavigation = value;
}
public static DependencyProperty TabNavigationProperty => UIElement.TabFocusNavigationProperty;
#endregion
public static bool GetIsTemplateFocusTarget(FrameworkElement element) =>
GetIsTemplateFocusTargetValue(element);
public static void SetIsTemplateFocusTarget(FrameworkElement element, bool value) =>
SetIsTemplateFocusTargetValue(element, value);
[GeneratedDependencyProperty(DefaultValue = false, AttachedBackingFieldOwner = typeof(Control), Attached = true)]
public static DependencyProperty IsTemplateFocusTargetProperty { get; } = CreateIsTemplateFocusTargetProperty();
/// <summary>
/// Get or sets a value that indicates whether focus is constrained
/// within the control boundaries (for game pad/remote interaction).
/// </summary>
public bool IsFocusEngaged
{
get => GetIsFocusEngagedValue();
set => SetIsFocusEngagedValue(value);
}
/// <summary>
/// Identifies the IsFocusEngaged dependency property.
/// </summary>
[GeneratedDependencyProperty(DefaultValue = false, ChangedCallback = true)]
public static DependencyProperty IsFocusEngagedProperty { get; } = CreateIsFocusEngagedProperty();
private void OnIsFocusEngagedChanged(bool oldValue, bool newValue) => SetFocusEngagement();
/// <summary>
/// Get or sets a value that indicates whether focus can be constrained within
/// the control boundaries (for game pad/remote interaction).
/// </summary>
public bool IsFocusEngagementEnabled
{
get => GetIsFocusEngagementEnabledValue();
set => SetIsFocusEngagementEnabledValue(value);
}
/// <summary>
/// Identifies the IsFocusEngagementEnabled dependency property.
/// </summary>
[GeneratedDependencyProperty(DefaultValue = false)]
public static DependencyProperty IsFocusEngagementEnabledProperty { get; } = CreateIsFocusEngagementEnabledProperty();
private void OnIsFocusEngagementEnabledChanged(bool oldValue, bool newValue) => RemoveFocusEngagement();
internal protected override void OnDataContextChanged(DependencyPropertyChangedEventArgs e)
{
base.OnDataContextChanged(e);
OnDataContextChanged();
}
[EditorBrowsable(EditorBrowsableState.Never)]
protected virtual void OnDataContextChanged()
{
}
private protected virtual void OnIsEnabledChanged(IsEnabledChangedEventArgs e)
{
OnIsFocusableChanged();
// Part of logic from MUX Control.cpp Enabled method.
var focusManager = VisualTree.GetFocusManagerForElement(this);
if (focusManager == null)
{
return;
}
// Set focus if this control is the first focusable control in case of
// no having focus yet
if (IsFocusable)
{
if (focusManager.FocusedElement == null)
{
// No focused control here, so try to check the control is
// the first focusable control
var focusable = focusManager.GetFirstFocusableElement();
if (this == focusable)
{
// If we are trying to set focus in a changing focus event handler, we will end up leaving focus on the disabled control.
// As a result, we fail fast here. This is being tracked by Bug 9840123
Focus(FocusState.Programmatic, animateIfBringIntoView: false);
}
}
}
// TODO Uno specific: Adding check for IsEnabledSuppressed here
// as we need to make sure that when a Command is executing, the control
// should not lose focus
bool shouldReevaluateFocus =
!(IsEnabled || IsEnabledSuppressed) && //!pControl->ParserOwnsParent()
!AllowFocusWhenDisabled &&
// We just disabled this control, find if this control
//or one of its children had focus.
FocusProperties.HasFocusedElement(this);
if (shouldReevaluateFocus)
{
// Set the focus on the next focusable control.
focusManager.SetFocusOnNextFocusableElement(focusManager.GetRealFocusStateForFocusedElement(), true);
}
}
partial void OnIsFocusableChanged();
public event TypedEventHandler<Control, FocusDisengagedEventArgs> FocusDisengaged;
public event TypedEventHandler<Control, FocusEngagedEventArgs> FocusEngaged;
protected virtual void OnForegroundColorChanged(Brush oldValue, Brush newValue)
{
OnForegroundColorChangedPartial(oldValue, newValue);
}
partial void OnForegroundColorChangedPartial(Brush oldValue, Brush newValue);
protected virtual void OnFontWeightChanged(FontWeight oldValue, FontWeight newValue)
{
OnFontWeightChangedPartial(oldValue, newValue);
}
partial void OnFontWeightChangedPartial(FontWeight oldValue, FontWeight newValue);
protected virtual void OnFontFamilyChanged(FontFamily oldValue, FontFamily newValue)
{
OnFontFamilyChangedPartial(oldValue, newValue);
}
partial void OnFontFamilyChangedPartial(FontFamily oldValue, FontFamily newValue);
protected virtual void OnFontSizeChanged(double oldValue, double newValue)
{
OnFontSizeChangedPartial(oldValue, newValue);
}
partial void OnFontSizeChangedPartial(double oldValue, double newValue);
protected virtual void OnFontStyleChanged(FontStyle oldValue, FontStyle newValue)
{
OnFontStyleChangedPartial(oldValue, newValue);
}
partial void OnFontStyleChangedPartial(FontStyle oldValue, FontStyle newValue);
protected virtual void OnPaddingChanged(Thickness oldValue, Thickness newValue)
{
OnPaddingChangedPartial(oldValue, newValue);
}
partial void OnPaddingChangedPartial(Thickness oldValue, Thickness newValue);
protected virtual void OnBorderThicknessChanged(Thickness oldValue, Thickness newValue)
{
OnBorderThicknessChangedPartial(oldValue, newValue);
}
partial void OnBorderThicknessChangedPartial(Thickness oldValue, Thickness newValue);
protected virtual void OnBorderBrushChanged(Brush oldValue, Brush newValue)
{
OnBorderBrushChangedPartial(oldValue, newValue);
}
partial void OnBorderBrushChangedPartial(Brush oldValue, Brush newValue);
protected virtual void OnPointerPressed(PointerRoutedEventArgs e) { }
protected virtual void OnPointerReleased(PointerRoutedEventArgs e) { }
protected virtual void OnPointerEntered(PointerRoutedEventArgs e) { }
protected virtual void OnPointerExited(PointerRoutedEventArgs e) { }
protected virtual void OnPointerMoved(PointerRoutedEventArgs e) { }
protected virtual void OnPointerCanceled(PointerRoutedEventArgs e) { }
protected virtual void OnPointerCaptureLost(PointerRoutedEventArgs e) { }
#if !__WASM__
[global::Uno.NotImplemented]
#endif
protected virtual void OnPointerWheelChanged(PointerRoutedEventArgs e) { }
protected virtual void OnManipulationStarting(ManipulationStartingRoutedEventArgs e) { }
protected virtual void OnManipulationStarted(ManipulationStartedRoutedEventArgs e) { }
protected virtual void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e) { }