-
Notifications
You must be signed in to change notification settings - Fork 115
/
UIElement.cs
1840 lines (1594 loc) · 71.5 KB
/
UIElement.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
/*===================================================================================
*
* Copyright (c) Userware/OpenSilver.net
*
* This file is part of the OpenSilver Runtime (https://opensilver.net), which is
* licensed under the MIT license: https://opensource.org/licenses/MIT
*
* As stated in the MIT license, "the above copyright notice and this permission
* notice shall be included in all copies or substantial portions of the Software."
*
\*====================================================================================*/
using CSHTML5.Internal;
using OpenSilver.Internal;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Media.Effects;
using System.Diagnostics;
using System.ComponentModel;
#if MIGRATION
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
#else
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.Foundation;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
#endif
#if MIGRATION
namespace System.Windows
#else
namespace Windows.UI.Xaml
#endif
{
/// <summary>
/// UIElement is a base class for most of the objects that have visual appearance
/// and can process basic input in a user interface.
/// </summary>
public abstract partial class UIElement : DependencyObject
{
static UIElement()
{
#if MIGRATION
MouseMoveEvent = new RoutedEvent(nameof(MouseMove), RoutingStrategy.Bubble, typeof(MouseEventHandler), typeof(UIElement));
MouseLeftButtonDownEvent = new RoutedEvent(nameof(MouseLeftButtonDown), RoutingStrategy.Bubble, typeof(MouseButtonEventHandler), typeof(UIElement));
MouseRightButtonDownEvent = new RoutedEvent(nameof(MouseRightButtonDown), RoutingStrategy.Bubble, typeof(MouseButtonEventHandler), typeof(UIElement));
MouseWheelEvent = new RoutedEvent(nameof(MouseWheel), RoutingStrategy.Bubble, typeof(MouseWheelEventHandler), typeof(UIElement));
MouseLeftButtonUpEvent = new RoutedEvent(nameof(MouseLeftButtonUp), RoutingStrategy.Bubble, typeof(MouseButtonEventHandler), typeof(UIElement));
MouseEnterEvent = new RoutedEvent(nameof(MouseEnter), RoutingStrategy.Direct, typeof(MouseEventHandler), typeof(UIElement));
MouseLeaveEvent = new RoutedEvent(nameof(MouseLeave), RoutingStrategy.Direct, typeof(MouseEventHandler), typeof(UIElement));
TextInputEvent = new RoutedEvent(nameof(TextInput), RoutingStrategy.Bubble, typeof(TextCompositionEventHandler), typeof(UIElement));
TextInputStartEvent = new RoutedEvent(nameof(TextInputStart), RoutingStrategy.Bubble, typeof(TextCompositionEventHandler), typeof(UIElement));
TextInputUpdateEvent = new RoutedEvent(nameof(TextInputUpdate), RoutingStrategy.Bubble, typeof(TextCompositionEventHandler), typeof(UIElement));
TappedEvent = new RoutedEvent(nameof(Tapped), RoutingStrategy.Bubble, typeof(TappedEventHandler), typeof(UIElement));
MouseRightButtonUpEvent = new RoutedEvent(nameof(MouseRightButtonUp), RoutingStrategy.Bubble, typeof(MouseButtonEventHandler), typeof(UIElement));
KeyDownEvent = new RoutedEvent(nameof(KeyDown), RoutingStrategy.Bubble, typeof(KeyEventHandler), typeof(UIElement));
KeyUpEvent = new RoutedEvent(nameof(KeyUp), RoutingStrategy.Bubble, typeof(KeyEventHandler), typeof(UIElement));
GotFocusEvent = new RoutedEvent(nameof(GotFocus), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UIElement));
LostFocusEvent = new RoutedEvent(nameof(LostFocus), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UIElement));
#else
PointerMovedEvent = new RoutedEvent(nameof(PointerMoved), RoutingStrategy.Bubble, typeof(PointerEventHandler), typeof(UIElement));
PointerPressedEvent = new RoutedEvent(nameof(PointerPressed), RoutingStrategy.Bubble, typeof(PointerEventHandler), typeof(UIElement));
PointerWheelChangedEvent = new RoutedEvent(nameof(PointerWheelChanged), RoutingStrategy.Bubble, typeof(PointerEventHandler), typeof(UIElement));
PointerReleasedEvent = new RoutedEvent(nameof(PointerReleased), RoutingStrategy.Bubble, typeof(PointerEventHandler), typeof(UIElement));
PointerEnteredEvent = new RoutedEvent(nameof(PointerEntered), RoutingStrategy.Direct, typeof(PointerEventHandler), typeof(UIElement));
PointerExitedEvent = new RoutedEvent(nameof(PointerExited), RoutingStrategy.Direct, typeof(PointerEventHandler), typeof(UIElement));
TextInputEvent = new RoutedEvent(nameof(TextInput), RoutingStrategy.Bubble, typeof(TextCompositionEventHandler), typeof(UIElement));
TextInputStartEvent = new RoutedEvent(nameof(TextInputStart), RoutingStrategy.Bubble, typeof(TextCompositionEventHandler), typeof(UIElement));
TextInputUpdateEvent = new RoutedEvent(nameof(TextInputUpdate), RoutingStrategy.Bubble, typeof(TextCompositionEventHandler), typeof(UIElement));
TappedEvent = new RoutedEvent(nameof(Tapped), RoutingStrategy.Bubble, typeof(TappedEventHandler), typeof(UIElement));
RightTappedEvent = new RoutedEvent(nameof(RightTapped), RoutingStrategy.Bubble, typeof(RightTappedEventHandler), typeof(UIElement));
KeyDownEvent = new RoutedEvent(nameof(KeyDown), RoutingStrategy.Bubble, typeof(KeyEventHandler), typeof(UIElement));
KeyUpEvent = new RoutedEvent(nameof(KeyUp), RoutingStrategy.Bubble, typeof(KeyEventHandler), typeof(UIElement));
GotFocusEvent = new RoutedEvent(nameof(GotFocus), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UIElement));
LostFocusEvent = new RoutedEvent(nameof(LostFocus), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(UIElement));
#endif
RegisterEvents(typeof(UIElement));
}
internal bool IsConnectedToLiveTree { get; set; }
#region Visual Parent
private DependencyObject _parent;
/// <summary>
/// Returns the parent of this UIElement.
/// </summary>
internal DependencyObject INTERNAL_VisualParent
{
get
{
return _parent;
}
}
#endregion Visual Parent
#region Visual Children
/// <summary>
/// Derived class must implement to support UIElement children. The method must return
/// the child at the specified index. Index must be between 0 and GetVisualChildrenCount-1.
///
/// By default a UIElement does not have any children.
///
/// Remark:
/// Need to lock down Visual tree during the callbacks.
/// During this virtual call it is not valid to modify the Visual tree.
/// </summary>
internal virtual UIElement GetVisualChild(int index)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
/// <summary>
/// Derived classes override this property to enable the UIElement code to enumerate
/// the UIElement children. Derived classes need to return the number of children
/// from this method.
///
/// By default a UIElement does not have any children.
///
/// Remark: During this virtual method the Visual tree must not be modified.
/// </summary>
internal virtual int VisualChildrenCount
{
get { return 0; }
}
/// <Summary>
/// Flag to check if this visual has any children
/// </Summary>
internal bool HasVisualChildren { get; private set; }
// Are we in the process of iterating the visual children.
// This flag is set during a descendents walk, for property invalidation.
internal bool IsVisualChildrenIterationInProgress { get; set; }
/// <summary>
/// AttachChild
///
/// Derived classes must call this method to notify the UIElement layer that a new
/// child appeard in the children collection. The UIElement layer will then call the GetVisualChild
/// method to find out where the child was added.
/// </summary>
internal void AddVisualChild(UIElement child)
{
if (child == null)
{
return;
}
if (child._parent != null)
{
throw new ArgumentException("Must disconnect specified child from current parent UIElement before attaching to new parent UIElement.");
}
HasVisualChildren = true;
// Set the parent pointer.
child._parent = this;
child.OnVisualParentChanged(null);
}
/// <summary>
/// DisconnectChild
///
/// Derived classes must call this method to notify the UIElement layer that a
/// child was removed from the children collection. The UIElement layer will then call
/// GetChildren to find out which child has been removed.
/// </summary>
internal void RemoveVisualChild(UIElement child)
{
if (child == null || child._parent == null)
{
return;
}
if (child._parent != this)
{
throw new ArgumentException("Specified UIElement is not a child of this UIElement.");
}
if (VisualChildrenCount == 0)
{
HasVisualChildren = false;
}
// Set the parent pointer to null.
child._parent = null;
child.OnVisualParentChanged(this);
}
/// <summary>
/// OnVisualParentChanged is called when the parent of the UIElement is changed.
/// </summary>
/// <param name="oldParent">Old parent or null if the UIElement did not have a parent before.</param>
internal virtual void OnVisualParentChanged(DependencyObject oldParent)
{
// Synchronize ForceInherit properties
if (_parent != null)
{
SynchronizeForceInheritProperties(this, _parent);
}
else
{
if (oldParent != null)
{
SynchronizeForceInheritProperties(this, oldParent);
}
}
}
#endregion Visual Children
internal virtual Size MeasureCore()
{
return new Size(0, 0);
}
internal Window INTERNAL_ParentWindow { get; set; } // This is a reference to the window where this control is presented. It is useful for example to know where to display the popups. //todo-perfs: replace all these properties with fields?
// This is the main DIV of the HTML representation of the control
#if CSHTML5NETSTANDARD
internal object INTERNAL_OuterDomElement { get; set; }
#else
internal dynamic INTERNAL_OuterDomElement { get; set; }
#endif
internal object INTERNAL_InnerDomElement { get; set; } // This is used to add visual children to the DOM (optionally wrapped into additional code, c.f. "INTERNAL_VisualChildInformation")
internal object INTERNAL_AdditionalOutsideDivForMargins { get; set; } // This is used to define the margins and to remove the div used for the margins when we remove this element.
internal object INTERNAL_InnerDivOfTheChildWrapperOfTheParentIfAny { get; set; } // This is non-null only if the parent has a "ChildWrapper", that is, a DIV that it creates for each of its children. If it is the case, we store the "inner div" of that child wrapper. It is useful for alignment purposes (cf. alignment methods in the FrameworkElement class).
internal object INTERNAL_OptionalSpecifyDomElementConcernedByIsEnabled { get; set; } // This is optional. When set, it means that the "FrameworkElement.ManageIsEnabled" method sets the "disabled" property on this specified DOM element rather than on the INTERNAL_OuterDomElement. An example is the "CheckBox", which specifies a different DOM element for its "disabled" state.
internal object INTERNAL_OptionalSpecifyDomElementConcernedByMinMaxHeightAndWidth { get; set; } // This is optional. When set, it means that the "FrameworkElement.MinHeight" and "MinWidth" properties are applied on this specified DOM element rather than on the INTERNAL_OuterDomElement. An example is the "TextBox", for which applying MinHeight to the outer DOM does not make the inner DOM bigger.
internal INTERNAL_CellDefinition INTERNAL_SpanParentCell = null; //this is used to know where we put the element when in a cell of a grid that is overlapped (due to the span or presence of another element that was put there previously), which causes it to be "sucked" into the basic cell of the previousl "placed" child.
internal string INTERNAL_HtmlRepresentation { get; set; } // This can be used instead of overriding the "CreateDomElement" method to specify the appearance of the control.
internal Dictionary<UIElement, INTERNAL_VisualChildInformation> INTERNAL_VisualChildrenInformation { get; set; } //todo-performance: verify that JavaScript output is a performant dictionary too, otherwise change structure.
internal bool INTERNAL_RenderTransformOriginHasBeenApplied = false; // This is useful to ensure that the default RenderTransformOrigin is (0,0) like in normal XAML, instead of (0.5,0.5) like in CSS.
//Note: the two following fields are only used in the PointerRoutedEventArgs class to determine how many clicks have been made on this UIElement in a short amount of time.
internal int INTERNAL_clickCount; //this is used in the PointerPressed event to fill the ClickCount Property.
internal int INTERNAL_lastClickDate; //this is used in the PointerPressed event to fill the ClickCount Property.
public string XamlSourcePath; //this is used by the Simulator to tell where this control is defined. It is non-null only on root elements, that is, elements which class has "InitializeComponent" method. This member is public because it needs to be accessible via reflection.
internal bool _isLoaded;
internal bool INTERNAL_EnableProgressiveLoading;
internal Action INTERNAL_DeferredRenderingWhenControlBecomesVisible;
internal Action INTERNAL_DeferredLoadingWhenControlBecomesVisible;
/// <summary>
/// Dictionary that helps link the validationErrors to the BindingExpressions for managing the errors.
/// </summary>
internal Dictionary<BindingExpression, ValidationError> INTERNAL_ValidationErrorsDictionary;
public Size RenderSize { get { return VisualBounds.Size; } }
public Size DesiredSize { get; private set; }
internal Rect VisualBounds { get; set; }
internal bool IsMeasureValid { get; private set; }
internal bool IsArrangeValid { get; private set; }
internal bool IsRendered { get; private set; }
internal bool isFirstRendering;
internal Rect RenderedVisualBounds { get; private set; }
internal Rect PreviousFinalRect { get; private set; }
internal Size PreviousAvailableSize { get; private set; }
private Size previousDesiredSize;
private Size layoutMeasuredSize;
private Size layoutLastSize;
private bool layoutProcessing;
private int visualLevel;
internal int VisualLevel
{
get
{
if (visualLevel == -1)
{
visualLevel = (VisualTreeHelper.GetParent(this) as UIElement)?.VisualLevel + 1 ?? 0;
}
return visualLevel;
}
}
internal bool IsCustomLayoutRoot
{
get
{
FrameworkElement parent = this as FrameworkElement;
if (parent.CustomLayout == false)
return false;
FrameworkElement layoutRoot = null;
while (parent != null)
{
if (parent.CustomLayout)
layoutRoot = parent;
parent = VisualTreeHelper.GetParent(parent) as FrameworkElement;
}
if (layoutRoot == null)
return false;
return layoutRoot == this;
}
}
internal bool IsUnderCustomLayout
{
get
{
FrameworkElement parent = VisualTreeHelper.GetParent(this) as FrameworkElement;
while (parent != null)
{
if (parent.CustomLayout)
return true;
parent = VisualTreeHelper.GetParent(parent) as FrameworkElement;
}
return false;
}
}
public UIElement()
{
DesiredSize = new Size();
PreviousFinalRect = Rect.Empty;
PreviousAvailableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
previousDesiredSize = Size.Empty;
layoutMeasuredSize = Size.Empty;
layoutLastSize = Size.Empty;
layoutProcessing = false;
IsMeasureValid = false;
IsArrangeValid = false;
IsRendered = false;
isFirstRendering = false;
visualLevel = -1;
}
internal virtual object GetDomElementToSetContentString()
{
return INTERNAL_InnerDomElement;
}
#region ClipToBounds
/// <summary>
/// Gets or sets a value indicating whether to clip the content of this element
/// (or content coming from the child elements of this element) to fit into the
/// size of the containing element. This is a dependency property.
/// </summary>
public bool ClipToBounds
{
get { return (bool)GetValue(ClipToBoundsProperty); }
set { SetValue(ClipToBoundsProperty, value); }
}
/// <summary>
/// Identifies the <see cref="UIElement.ClipToBounds"/> dependency
/// property.
/// </summary>
public static readonly DependencyProperty ClipToBoundsProperty =
DependencyProperty.Register(
nameof(ClipToBounds),
typeof(bool),
typeof(UIElement),
new PropertyMetadata(false)
{
MethodToUpdateDom = ClipToBounds_MethodToUpdateDom,
});
private static void ClipToBounds_MethodToUpdateDom(DependencyObject d, object newValue)
{
//todo: doesn't work with vertical overflow if the display is table or table-cell
UIElement uiElement = (UIElement)d;
var outerDomElement = uiElement.INTERNAL_OuterDomElement;
var style = INTERNAL_HtmlDomManager.GetDomElementStyleForModification(outerDomElement);
if ((bool)newValue)
{
style.overflow = "hidden"; //todo: ? if this is a ScrollViewer do not do anything?
style.tableLayout = "fixed"; //Note: this might break stuff somewhere else
}
else
{
style.overflow = "auto";
style.tableLayout = "auto";
}
}
#endregion
#region Clip
public Geometry Clip
{
get { return (Geometry)GetValue(ClipProperty); }
set { SetValue(ClipProperty, value); }
}
public static readonly DependencyProperty ClipProperty =
DependencyProperty.Register("Clip",
typeof(Geometry),
typeof(UIElement),
new PropertyMetadata(null)
{
MethodToUpdateDom = Clip_MethodToUpdateDom,
});
private static void Clip_MethodToUpdateDom(DependencyObject d, object newValue)
{
if (newValue == null)
{
UIElement uiElement = (UIElement)d;
var outerDomElement = uiElement.INTERNAL_OuterDomElement;
var style = INTERNAL_HtmlDomManager.GetDomElementStyleForModification(outerDomElement);
style.clip = "";
return;
}
// Only RectangleGeometry is supported for now
if (newValue is RectangleGeometry)
{
UIElement uiElement = (UIElement)d;
var outerDomElement = uiElement.INTERNAL_OuterDomElement;
var style = INTERNAL_HtmlDomManager.GetDomElementStyleForModification(outerDomElement);
RectangleGeometry val = (RectangleGeometry)newValue;
// CSS rect property has the following format - rect(<top>, <right>, <bottom>, <left>)
double top = val.Rect.Y;
double right = val.Rect.Width + val.Rect.X;
double bottom = val.Rect.Height + val.Rect.Y;
double left = val.Rect.X;
string rect = "rect(" + top.ToInvariantString() + "px, " + right.ToInvariantString() + "px" + ", " + bottom.ToInvariantString() + "px, " + left.ToInvariantString() + "px)";
style.clip = rect;
}
else
{
Debug.WriteLine("Only RectangleGeometry is supported for now.");
}
}
#endregion
/// <summary>
/// When overriden, creates the dom elements designed to represent an instance of an UIElement and defines the place where its child(ren) will be added.
/// </summary>
/// <param name="parentRef">The parent of the UIElement</param>
/// <param name="domElementWhereToPlaceChildren">The dom element where the UIElement's children will be added.</param>
/// <returns>The "root" dom element of the UIElement.</returns>
public abstract object CreateDomElement(object parentRef, out object domElementWhereToPlaceChildren);
/// <summary>
/// When overriden, creates a dom wrapper for each child that is added to the UIElement.
/// </summary>
/// <param name="parentRef"></param>
/// <param name="domElementWhereToPlaceChild"></param>
/// <param name="index">The index for the position in which to add the child.</param>
/// <returns></returns>
public virtual object CreateDomChildWrapper(object parentRef, out object domElementWhereToPlaceChild, int index = -1)
{
// This method is optional (cf. documentation in "INTERNAL_VisualChildInformation" class). It should return null if not used.
domElementWhereToPlaceChild = null;
return null;
}
public virtual object GetDomElementWhereToPlaceChild(UIElement child) // Note: if overridden, it supercedes the "INTERNAL_InnerDomElement" property.
{
return null;
}
public object GetChildsWrapper(UIElement child)
{
if (INTERNAL_VisualTreeManager.IsElementInVisualTree(this) && INTERNAL_VisualTreeManager.IsElementInVisualTree(child))
{
return INTERNAL_VisualChildrenInformation[child].INTERNAL_OptionalChildWrapper_OuterDomElement;
}
else
{
return null;
}
}
#region Effect
// todo: we may add the support for multiple effects on the same
// UIElement since it is possible in html (but not in wpf). If we
// try to, it will require some changes in the Effects already
// implemented and some work to make it work properly in the
// simulator.
public Effect Effect
{
get { return (Effect)GetValue(EffectProperty); }
set { SetValue(EffectProperty, value); }
}
/// <summary>
/// Identifies the <see cref="UIElement.Effect"/> dependency
/// property.
/// </summary>
public static readonly DependencyProperty EffectProperty =
DependencyProperty.Register(
nameof(Effect),
typeof(Effect),
typeof(UIElement),
new PropertyMetadata(null, Effect_Changed)
{
CallPropertyChangedWhenLoadedIntoVisualTree = WhenToCallPropertyChangedEnum.IfPropertyIsSet
});
private static void Effect_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UIElement element = (UIElement)d;
if (e.OldValue != null)
{
((Effect)e.OldValue).SetParentUIElement(null);
}
if (e.NewValue != null)
{
((Effect)e.NewValue).SetParentUIElement(element);
}
}
#endregion
#region RenderTransform and RenderTransformOrigin
/// <summary>
/// Gets or sets transform information that affects the rendering position of
/// a UIElement.
/// </summary>
public Transform RenderTransform
{
get { return (Transform)GetValue(RenderTransformProperty) ?? new MatrixTransform(); }
set { SetValue(RenderTransformProperty, value); }
}
/// <summary>
/// Identifies the <see cref="UIElement.RenderTransform"/> dependency
/// property.
/// </summary>
public static readonly DependencyProperty RenderTransformProperty =
DependencyProperty.Register(
nameof(RenderTransform),
typeof(Transform),
typeof(UIElement),
new PropertyMetadata(null, RenderTransform_Changed)
{
CallPropertyChangedWhenLoadedIntoVisualTree = WhenToCallPropertyChangedEnum.IfPropertyIsSet
});
private static void RenderTransform_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var uiElement = (UIElement)d;
Transform newValue = (Transform)e.NewValue;
if (e.OldValue != null)
{
((Transform)e.OldValue).INTERNAL_UnapplyTransform();
((Transform)e.OldValue).INTERNAL_parent = null;
}
if (INTERNAL_VisualTreeManager.IsElementInVisualTree(uiElement))
{
if (newValue != null)
{
newValue.INTERNAL_parent = uiElement;
newValue.INTERNAL_ApplyTransform();
// Ensure that the default RenderTransformOrigin is (0,0) like in normal XAML, instead of (0.5,0.5) like in CSS:
if (!uiElement.INTERNAL_RenderTransformOriginHasBeenApplied)
ApplyRenderTransformOrigin(uiElement, new Point(0d, 0d));
}
else
{
var domStyle = INTERNAL_HtmlDomManager.GetFrameworkElementOuterStyleForModification(uiElement);
try
{
domStyle.transform = "";
}
catch
{
}
}
}
}
public Point RenderTransformOrigin
{
get { return (Point)GetValue(RenderTransformOriginProperty); }
set { SetValue(RenderTransformOriginProperty, value); }
}
/// <summary>
/// Identifies the <see cref="UIElement.RenderTransformOrigin"/>
/// dependency property.
/// </summary>
public static readonly DependencyProperty RenderTransformOriginProperty =
DependencyProperty.Register(
nameof(RenderTransformOrigin),
typeof(Point),
typeof(UIElement),
new PropertyMetadata(new Point(0d, 0d), RenderTransformOrigin_Changed)
{
CallPropertyChangedWhenLoadedIntoVisualTree = WhenToCallPropertyChangedEnum.IfPropertyIsSet
});
private static void RenderTransformOrigin_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var uiElement = (UIElement)d;
if (INTERNAL_VisualTreeManager.IsElementInVisualTree(uiElement))
{
ApplyRenderTransformOrigin(uiElement, (Point)e.NewValue);
}
}
private static void ApplyRenderTransformOrigin(UIElement uiElement, Point newValue)
{
var domStyle = INTERNAL_HtmlDomManager.GetFrameworkElementOuterStyleForModification(uiElement);
string transformOriginValue = $"{(newValue.X * 100).ToString(CultureInfo.InvariantCulture)}% {(newValue.Y * 100).ToString(CultureInfo.InvariantCulture)}%";
try
{
domStyle.transformOrigin = transformOriginValue;
}
catch
{
}
uiElement.INTERNAL_RenderTransformOriginHasBeenApplied = true;
}
#endregion
#region UseLayoutRounding
/// <summary>
/// Gets or sets a value that determines whether rendering for the object and
/// its visual subtree should use rounding behavior that aligns rendering to
/// whole pixels.
/// </summary>
public bool UseLayoutRounding
{
get { return (bool)GetValue(UseLayoutRoundingProperty); }
set { SetValue(UseLayoutRoundingProperty, value); }
}
/// <summary>
/// Identifies the <see cref="UIElement.UseLayoutRounding"/> dependency
/// property.
/// </summary>
public static readonly DependencyProperty UseLayoutRoundingProperty =
DependencyProperty.Register(
nameof(UseLayoutRounding),
typeof(bool),
typeof(UIElement),
new PropertyMetadata(false));
//-------------------------------------------------------------------
// NOTE: The "UseLayoutRounding" is currently not supported, but we
// provide it anyway because it's a pain for end-users to remove the
// option in all their XAML elements and the benefit of not including
// it is not significant.
//-------------------------------------------------------------------
#endregion
#region Visibility
/// <summary>
/// Gets or sets the visibility of a UIElement. A UIElement that is not visible
/// is not rendered and does not communicate its desired size to layout.
/// </summary>
public Visibility Visibility
{
get { return (Visibility)GetValue(VisibilityProperty); }
set { SetValue(VisibilityProperty, value); }
}
/// <summary>
/// Identifies the <see cref="UIElement.Visibility"/> dependency
/// property.
/// </summary>
public static readonly DependencyProperty VisibilityProperty =
DependencyProperty.Register(
nameof(Visibility),
typeof(Visibility),
typeof(UIElement),
new PropertyMetadata(Visibility.Visible, Visibility_Changed));
private string _previousValueOfDisplayCssProperty = "block";
private static void Visibility_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UIElement uie = (UIElement)d;
Visibility newValue = (Visibility)e.NewValue;
// Finish loading the element if it was not loaded yet because it was Collapsed (and optimization was enabled in the Settings):
if (uie.INTERNAL_DeferredLoadingWhenControlBecomesVisible != null
&& newValue != Visibility.Collapsed)
{
Action deferredLoadingWhenControlBecomesVisible = uie.INTERNAL_DeferredLoadingWhenControlBecomesVisible;
uie.INTERNAL_DeferredLoadingWhenControlBecomesVisible = null;
deferredLoadingWhenControlBecomesVisible();
}
// Make the CSS changes required to apply the visibility at the DOM level:
INTERNAL_ApplyVisibility(uie, newValue);
// The IsVisible property depends on this property.
uie.UpdateIsVisible();
}
internal static void INTERNAL_ApplyVisibility(UIElement uiElement, Visibility newValue)
{
// Set the CSS to make the DOM element visible/collapsed:
if (INTERNAL_VisualTreeManager.IsElementInVisualTree(uiElement))
{
// Get a reference to the most outer DOM element to show/hide:
object mostOuterDomElement = null;
if (VisualTreeHelper.GetParent(uiElement) is UIElement parent &&
parent.INTERNAL_VisualChildrenInformation != null &&
parent.INTERNAL_VisualChildrenInformation.ContainsKey(uiElement))
{
mostOuterDomElement = parent.INTERNAL_VisualChildrenInformation[uiElement].INTERNAL_OptionalChildWrapper_OuterDomElement;
}
if (mostOuterDomElement == null)
{
mostOuterDomElement = uiElement.INTERNAL_AdditionalOutsideDivForMargins ?? uiElement.INTERNAL_OuterDomElement;
}
var style = INTERNAL_HtmlDomManager.GetDomElementStyleForModification(mostOuterDomElement);
// Apply the visibility:
if (newValue == Visibility.Collapsed)
{
// Remember the current value of the CSS property "display" so that we can later revert to it:
string previousValueOfDisplayCssProperty = style.display;
if (previousValueOfDisplayCssProperty != "none")
uiElement._previousValueOfDisplayCssProperty = previousValueOfDisplayCssProperty;
// Hide the DOM element (or its wrapper if any):
style.display = "none";
}
else
{
// Show the DOM element (or its wrapper if any) by reverting the CSS property "display" to its previous value:
if (style.display == "none")
style.display = uiElement._previousValueOfDisplayCssProperty;
// The alignment was not calculated when the object was hidden, so we need to calculate it now:
if (uiElement is FrameworkElement && uiElement.INTERNAL_VisualParent != null) // Note: The "INTERNAL_VisualParent" can be "null" for example if we are changing the visibility of a "PopupRoot" control.
{
FrameworkElement.INTERNAL_ApplyHorizontalAlignmentAndWidth((FrameworkElement)uiElement, ((FrameworkElement)uiElement).HorizontalAlignment); //todo-perfs: only call the relevant portion of the code?
FrameworkElement.INTERNAL_ApplyVerticalAlignmentAndHeight((FrameworkElement)uiElement, ((FrameworkElement)uiElement).VerticalAlignment); //todo-perfs: only call the relevant portion of the code?
}
}
INTERNAL_WorkaroundIE11IssuesWithScrollViewerInsideGrid.RefreshLayoutIfIE();
}
}
#endregion
#region IsVisible
/// <summary>
/// A property indicating if this element is visible or not.
/// </summary>
public bool IsVisible => (bool)GetValue(IsVisibleProperty);
/// <summary>
/// Identifies the <see cref="IsVisible"/> dependency property.
/// </summary>
private static readonly DependencyProperty IsVisibleProperty =
DependencyProperty.Register(
nameof(IsVisible),
typeof(bool),
typeof(UIElement),
new PropertyMetadata(false, OnIsVisibleChanged, CoerceIsVisible));
private static void OnIsVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
UIElement uie = (UIElement)d;
if ((bool)e.NewValue)
{
Action deferredLoadingWhenControlBecomesVisible = uie.INTERNAL_DeferredRenderingWhenControlBecomesVisible;
if (deferredLoadingWhenControlBecomesVisible != null)
{
uie.INTERNAL_DeferredRenderingWhenControlBecomesVisible = null;
deferredLoadingWhenControlBecomesVisible();
}
}
// Invalidate the children so that they will inherit the new value.
uie.InvalidateForceInheritPropertyOnChildren(e.Property);
uie.IsVisibleChanged?.Invoke(d, e);
uie.InvalidateParentMeasure();
uie.InvalidateParentArrange();
}
private static object CoerceIsVisible(DependencyObject d, object baseValue)
{
UIElement uie = (UIElement)d;
// IsVisible is a read-only property. It derives its "base" value
// from the Visibility property.
bool isVisible = uie.Visibility == Visibility.Visible;
// We must be false if our parent is false, but we can be
// either true or false if our parent is true.
//
// Another way of saying this is that we can only be true
// if our parent is true, but we can always be false.
if (isVisible)
{
bool constraintAllowsVisible = false;
// Our parent can constrain us.
if (VisualTreeHelper.GetParent(uie) is UIElement parent)
{
constraintAllowsVisible = parent.IsVisible;
}
else
{
if (VisualTreeHelper.IsRoot(uie))
{
constraintAllowsVisible = true;
}
}
if (!constraintAllowsVisible)
{
isVisible = false;
}
}
return isVisible;
}
public event DependencyPropertyChangedEventHandler IsVisibleChanged;
internal void UpdateIsVisible()
{
CoerceValue(IsVisibleProperty);
}
#endregion
#region Opacity
/// <summary>
/// Gets or sets the degree of the object's opacity.
/// A value between 0 and 1.0 that declares the opacity factor, with 1.0 meaning
/// full opacity and 0 meaning transparent. The default value is 1.0.
/// </summary>
public double Opacity
{
get { return (double)GetValue(OpacityProperty); }
set { SetValue(OpacityProperty, value); }
}
/// <summary>
/// Identifies the <see cref="UIElement.Opacity"/> dependency
/// property.
/// </summary>
public static readonly DependencyProperty OpacityProperty =
DependencyProperty.Register(
nameof(Opacity),
typeof(double),
typeof(UIElement),
new PropertyMetadata(1.0)
{
GetCSSEquivalent = (instance) => new CSSEquivalent
{
// Note: We multiply by 1000 and then divide by 1000 so as to only keep 3 decimals at the most.
Value = (inst, value) => (Math.Floor(Convert.ToDouble(value) * 1000) / 1000).ToInvariantString(),
Name = new List<string> { "opacity" },
ApplyAlsoWhenThereIsAControlTemplate = true,
}
});
#endregion
#region IsHitTestVisible
/// <summary>
/// Gets or sets whether the contained area of this UIElement can return true
/// values for hit testing.
/// </summary>
public bool IsHitTestVisible
{
get { return (bool)GetValue(IsHitTestVisibleProperty); }
set { SetValue(IsHitTestVisibleProperty, value); }
}
/// <summary>
/// Identifies the <see cref="UIElement.IsHitTestVisible"/> dependency
/// property.
/// </summary>
public static readonly DependencyProperty IsHitTestVisibleProperty =
DependencyProperty.Register(
nameof(IsHitTestVisible),
typeof(bool),
typeof(UIElement),
new PropertyMetadata(true, OnIsHitTestVisiblePropertyChanged, CoerceIsHitTestVisibleProperty)
{
MethodToUpdateDom = (d, e) =>
{
SetPointerEvents((UIElement)d);
},
});
private static void OnIsHitTestVisiblePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// Invalidate the children so that they will inherit the new value.
((UIElement)d).InvalidateForceInheritPropertyOnChildren(e.Property);
}
private static object CoerceIsHitTestVisibleProperty(DependencyObject d, object baseValue)
{
UIElement uie = (UIElement)d;
if (!(baseValue is bool)) //todo: this is a temporary workaround - cf. comment in "CoerceIsEnabledProperty"
return true;
// We must be false if our parent is false, but we can be
// either true or false if our parent is true.
//
// Another way of saying this is that we can only be true
// if our parent is true, but we can always be false.
if ((bool)baseValue)
{
DependencyObject parent = VisualTreeHelper.GetParent(uie);
if (parent == null || (bool)parent.GetValue(IsHitTestVisibleProperty))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
#endregion
#region pointer-events
internal static bool EnablePointerEventsBase(UIElement uie)
{
return (bool)uie.GetValue(FrameworkElement.IsEnabledProperty) &&
uie.IsHitTestVisible;
}
/// <summary>
/// Fetches the value that pointer-events (css) should be coerced to.
/// </summary>
internal virtual bool EnablePointerEventsCore
{
get
{
return false;
}
}
internal bool EnablePointerEvents
{