forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathRCTViewComponentView.mm
More file actions
2248 lines (1957 loc) · 78.4 KB
/
RCTViewComponentView.mm
File metadata and controls
2248 lines (1957 loc) · 78.4 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
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) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTViewComponentView.h"
#import "RCTViewAccessibilityElement.h"
#import <CoreGraphics/CoreGraphics.h>
#import <QuartzCore/QuartzCore.h>
#import <objc/runtime.h>
#import <ranges>
#if TARGET_OS_OSX // [macOS
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
#endif // macOS]
#import <React/RCTAssert.h>
#import <React/RCTBorderDrawing.h>
#import <React/RCTBoxShadow.h>
#import <React/RCTConversions.h>
#import <React/RCTLinearGradient.h>
#import <React/RCTLocalizedString.h>
#import <React/RCTRadialGradient.h>
#import <react/featureflags/ReactNativeFeatureFlags.h>
#import <react/renderer/components/view/ViewComponentDescriptor.h>
#import <react/renderer/components/view/ViewEventEmitter.h>
#import <react/renderer/components/view/ViewProps.h>
#import <react/renderer/components/view/accessibilityPropsConversions.h>
#import <react/renderer/graphics/BlendMode.h>
#ifdef RCT_DYNAMIC_FRAMEWORKS
#import <React/RCTComponentViewFactory.h>
#endif
#if TARGET_OS_OSX // [macOS
#import <React/RCTCursor.h>
#import <React/RCTViewKeyboardEvent.h>
#import <React/RCTUtils.h>
#endif // macOS]
using namespace facebook::react;
const CGFloat BACKGROUND_COLOR_ZPOSITION = -1024.0f;
@implementation RCTViewComponentView {
RCTPlatformColor *_backgroundColor; // [macOS]
CALayer *_backgroundColorLayer;
__weak CALayer *_borderLayer;
CALayer *_outlineLayer;
NSMutableArray<CALayer *> *_boxShadowLayers;
CALayer *_filterLayer;
NSMutableArray<CALayer *> *_backgroundImageLayers;
BOOL _needsInvalidateLayer;
BOOL _isJSResponder;
BOOL _removeClippedSubviews;
#if TARGET_OS_OSX // [macOS
BOOL _hasMouseOver;
BOOL _hasClipViewBoundsObserver;
NSTrackingArea *_trackingArea;
BOOL _allowsVibrancy;
#endif // macOS]
NSMutableArray<RCTUIView *> *_reactSubviews; // [macOS]
NSSet<NSString *> *_Nullable _propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN;
RCTPlatformView *_containerView; // [macOS]
BOOL _useCustomContainerView;
NSMutableSet<NSString *> *_accessibilityOrderNativeIDs;
NSMutableArray<NSObject *> *_accessibilityElements;
RCTViewAccessibilityElement *_axElementDescribingSelf;
}
#ifdef RCT_DYNAMIC_FRAMEWORKS
+ (void)load
{
[RCTComponentViewFactory.currentComponentViewFactory registerComponentViewClass:self];
}
#endif
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
_props = ViewShadowNode::defaultSharedProps();
_reactSubviews = [NSMutableArray new];
#if !TARGET_OS_OSX // [macOS]
self.multipleTouchEnabled = YES;
#endif // [macOS]
_useCustomContainerView = NO;
_removeClippedSubviews = NO;
#if TARGET_OS_OSX // [macOS
_allowsVibrancy = NO;
self.mouseDownCanMoveWindow = YES;
#endif // macOS]
}
return self;
}
- (facebook::react::Props::Shared)props
{
return _props;
}
#if !TARGET_OS_OSX // [macOS]
- (void)setContentView:(RCTUIView *)contentView // [macOS]
#else // [macOS
- (void)setContentView:(RCTPlatformView *)contentView // [macOS]
#endif // macOS]
{
if (_contentView) {
[_contentView removeFromSuperview];
}
_contentView = contentView;
if (_contentView) {
[self.currentContainerView addSubview:_contentView];
_contentView.frame = RCTCGRectFromRect(_layoutMetrics.getContentFrame());
#if TARGET_OS_OSX // [macOS
_contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
#endif // macOS]
[self addSubview:_contentView];
}
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
if (UIEdgeInsetsEqualToEdgeInsets(self.hitTestEdgeInsets, UIEdgeInsetsZero)) {
return [super pointInside:point withEvent:event];
}
CGRect hitFrame = UIEdgeInsetsInsetRect(self.bounds, self.hitTestEdgeInsets);
return CGRectContainsPoint(hitFrame, point);
}
- (RCTPlatformColor *)backgroundColor // [macOS]
{
return _backgroundColor;
}
- (void)setBackgroundColor:(RCTPlatformColor *)backgroundColor // [macOS]
{
_backgroundColor = backgroundColor;
}
#if TARGET_OS_OSX // [macOS
- (void)resetCursorRects
{
[self discardCursorRects];
if (_props->cursor != Cursor::Auto)
{
NSCursor *cursor = NSCursorFromRCTCursor(RCTCursorFromCursor(_props->cursor));
[self addCursorRect:self.bounds cursor:cursor];
}
}
- (BOOL)allowsVibrancy
{
return _allowsVibrancy;
}
#endif // macOS]
#if !TARGET_OS_OSX
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
{
[super traitCollectionDidChange:previousTraitCollection];
if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {
[self invalidateLayer];
}
}
#else // [macOS
- (void)viewDidChangeEffectiveAppearance
{
[super viewDidChangeEffectiveAppearance];
[self invalidateLayer];
}
#endif // macOS]
#pragma mark - RCTComponentViewProtocol
+ (ComponentDescriptorProvider)componentDescriptorProvider
{
RCTAssert(
self == [RCTViewComponentView class],
@"`+[RCTComponentViewProtocol componentDescriptorProvider]` must be implemented for all subclasses (and `%@` particularly).",
NSStringFromClass([self class]));
return concreteComponentDescriptorProvider<ViewComponentDescriptor>();
}
- (void)mountChildComponentView:(RCTUIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index // [macOS]
{
RCTAssert(
childComponentView.superview == nil,
@"Attempt to mount already mounted component view. (parent: %@, child: %@, index: %@, existing parent: %@)",
self,
childComponentView,
@(index),
@([childComponentView.superview tag]));
if (_removeClippedSubviews) {
[_reactSubviews insertObject:childComponentView atIndex:index];
} else {
[self.currentContainerView insertSubview:childComponentView atIndex:index];
}
}
- (void)unmountChildComponentView:(RCTUIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index // [macOS]
{
if (_removeClippedSubviews) {
[_reactSubviews removeObjectAtIndex:index];
} else {
RCTAssert(
childComponentView.superview == self.currentContainerView,
@"Attempt to unmount a view which is mounted inside different view. (parent: %@, child: %@, index: %@)",
self,
childComponentView,
@(index));
RCTAssert(
(self.currentContainerView.subviews.count > index) &&
[self.currentContainerView.subviews objectAtIndex:index] == childComponentView,
@"Attempt to unmount a view which has a different index. (parent: %@, child: %@, index: %@, actual index: %@, tag at index: %@)",
self,
childComponentView,
@(index),
@([self.currentContainerView.subviews indexOfObject:childComponentView]),
@([[self.currentContainerView.subviews objectAtIndex:index] tag]));
}
[childComponentView removeFromSuperview];
}
- (void)updateClippedSubviewsWithClipRect:(CGRect)clipRect relativeToView:(RCTUIView *)clipView // [macOS]
{
if (!_removeClippedSubviews) {
// Use default behavior if unmounting is disabled
return [super updateClippedSubviewsWithClipRect:clipRect relativeToView:clipView];
}
if (_reactSubviews.count == 0) {
// Do nothing if we have no subviews
return;
}
if (CGSizeEqualToSize(self.bounds.size, CGSizeZero)) {
// Do nothing if layout hasn't happened yet
return;
}
// Convert clipping rect to local coordinates
clipRect = [clipView convertRect:clipRect toView:self];
// Mount / unmount views
for (RCTUIView *view in _reactSubviews) { // [macOS]
if (CGRectIntersectsRect(clipRect, view.frame)) {
// View is at least partially visible, so remount it if unmounted
[self.currentContainerView addSubview:view];
// View is visible, update clipped subviews
[view updateClippedSubviewsWithClipRect:clipRect relativeToView:self];
} else if (view.superview) {
// View is completely outside the clipRect, so unmount it
[view removeFromSuperview];
}
}
}
- (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &)oldProps
{
RCTAssert(props, @"`props` must not be `null`.");
#ifndef NS_BLOCK_ASSERTIONS
auto propsRawPtr = _props.get();
RCTAssert(
propsRawPtr &&
([self class] == [RCTViewComponentView class] ||
typeid(*propsRawPtr).hash_code() != typeid(const ViewProps).hash_code()),
@"`RCTViewComponentView` subclasses (and `%@` particularly) must setup `_props`"
" instance variable with a default value in the constructor.",
NSStringFromClass([self class]));
#endif
const auto &oldViewProps = static_cast<const ViewProps &>(*_props);
const auto &newViewProps = static_cast<const ViewProps &>(*props);
BOOL needsInvalidateLayer = NO;
// `opacity`
if (oldViewProps.opacity != newViewProps.opacity &&
![_propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN containsObject:@"opacity"]) {
self.layer.opacity = (float)newViewProps.opacity;
needsInvalidateLayer = YES;
}
// Disable `removeClippedSubviews` when Fabric View Culling is enabled.
if (!ReactNativeFeatureFlags::enableViewCulling()) {
if (oldViewProps.removeClippedSubviews != newViewProps.removeClippedSubviews) {
_removeClippedSubviews = newViewProps.removeClippedSubviews;
if (_removeClippedSubviews && self.currentContainerView.subviews.count > 0) {
_reactSubviews = [NSMutableArray arrayWithArray:self.currentContainerView.subviews];
}
}
}
// `backgroundColor`
if (oldViewProps.backgroundColor != newViewProps.backgroundColor) {
self.backgroundColor = RCTUIColorFromSharedColor(newViewProps.backgroundColor); // [macOS]
needsInvalidateLayer = YES;
}
// `shadowColor`
if (oldViewProps.shadowColor != newViewProps.shadowColor) {
RCTPlatformColor *shadowColor = RCTUIColorFromSharedColor(newViewProps.shadowColor); // [macOS]
self.layer.shadowColor = shadowColor.CGColor;
needsInvalidateLayer = YES;
}
// `shadowOffset`
if (oldViewProps.shadowOffset != newViewProps.shadowOffset) {
self.layer.shadowOffset = RCTCGSizeFromSize(newViewProps.shadowOffset);
needsInvalidateLayer = YES;
}
// `shadowOpacity`
if (oldViewProps.shadowOpacity != newViewProps.shadowOpacity) {
self.layer.shadowOpacity = (float)newViewProps.shadowOpacity;
needsInvalidateLayer = YES;
}
// `shadowRadius`
if (oldViewProps.shadowRadius != newViewProps.shadowRadius) {
self.layer.shadowRadius = (CGFloat)newViewProps.shadowRadius;
needsInvalidateLayer = YES;
}
// `backfaceVisibility`
if (oldViewProps.backfaceVisibility != newViewProps.backfaceVisibility) {
self.layer.doubleSided = newViewProps.backfaceVisibility == BackfaceVisibility::Visible;
}
// `cursor`
if (oldViewProps.cursor != newViewProps.cursor) {
needsInvalidateLayer = YES;
}
// `shouldRasterize`
if (oldViewProps.shouldRasterize != newViewProps.shouldRasterize) {
self.layer.shouldRasterize = newViewProps.shouldRasterize;
#if !TARGET_OS_OSX // [macOS]
self.layer.rasterizationScale = newViewProps.shouldRasterize ? self.traitCollection.displayScale : 1.0;
#else // [macOS
self.layer.rasterizationScale = 1.0;
#endif // macOS]
}
// `pointerEvents`
if (oldViewProps.pointerEvents != newViewProps.pointerEvents) {
self.userInteractionEnabled = newViewProps.pointerEvents != PointerEventsMode::None;
}
// `transform`
if ((oldViewProps.transform != newViewProps.transform ||
oldViewProps.transformOrigin != newViewProps.transformOrigin) &&
![_propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN containsObject:@"transform"]) {
auto newTransform = newViewProps.resolveTransform(_layoutMetrics);
CATransform3D caTransform = RCTCATransform3DFromTransformMatrix(newTransform);
#if !TARGET_OS_OSX // [macOS]
self.layer.transform = caTransform;
#else // [macOS
self.transform3D = caTransform;
#endif // macOS]
// Enable edge antialiasing in rotation, skew, or perspective transforms
self.layer.allowsEdgeAntialiasing = caTransform.m12 != 0.0f || caTransform.m21 != 0.0f || caTransform.m34 != 0.0f;
}
// `hitSlop`
if (oldViewProps.hitSlop != newViewProps.hitSlop) {
self.hitTestEdgeInsets = {
-newViewProps.hitSlop.top,
-newViewProps.hitSlop.left,
-newViewProps.hitSlop.bottom,
-newViewProps.hitSlop.right};
}
// `overflow`
if (oldViewProps.getClipsContentToBounds() != newViewProps.getClipsContentToBounds()) {
self.currentContainerView.clipsToBounds = newViewProps.getClipsContentToBounds();
needsInvalidateLayer = YES;
}
// `border`
if (oldViewProps.borderStyles != newViewProps.borderStyles || oldViewProps.borderRadii != newViewProps.borderRadii ||
oldViewProps.borderColors != newViewProps.borderColors) {
needsInvalidateLayer = YES;
}
// `outline`
if (oldViewProps.outlineStyle != newViewProps.outlineStyle ||
oldViewProps.outlineColor != newViewProps.outlineColor ||
oldViewProps.outlineOffset != newViewProps.outlineOffset ||
oldViewProps.outlineWidth != newViewProps.outlineWidth) {
needsInvalidateLayer = YES;
}
// `nativeId`
if (oldViewProps.nativeId != newViewProps.nativeId) {
self.nativeId = RCTNSStringFromStringNilIfEmpty(newViewProps.nativeId);
}
// `accessible`
if (oldViewProps.accessible != newViewProps.accessible) {
#if !TARGET_OS_OSX // [macOS]
self.accessibilityElement.isAccessibilityElement = newViewProps.accessible;
#else // [macOS
self.accessibilityElement.accessibilityElement = newViewProps.accessible;
#endif // macOS]
}
// `accessibilityLabel`
if (oldViewProps.accessibilityLabel != newViewProps.accessibilityLabel) {
self.accessibilityElement.accessibilityLabel = RCTNSStringFromStringNilIfEmpty(newViewProps.accessibilityLabel);
}
#if !TARGET_OS_OSX // [macOS]
// `accessibilityLanguage`
if (oldViewProps.accessibilityLanguage != newViewProps.accessibilityLanguage) {
self.accessibilityElement.accessibilityLanguage =
RCTNSStringFromStringNilIfEmpty(newViewProps.accessibilityLanguage);
}
#endif // [macOS]
// `accessibilityHint`
if (oldViewProps.accessibilityHint != newViewProps.accessibilityHint) {
#if !TARGET_OS_OSX // [macOS]
self.accessibilityElement.accessibilityHint = RCTNSStringFromStringNilIfEmpty(newViewProps.accessibilityHint);
#else // [macOS
self.accessibilityElement.accessibilityHelp = RCTNSStringFromStringNilIfEmpty(newViewProps.accessibilityHint);
#endif // macOS]
}
#if !TARGET_OS_OSX // [macOS]
// `accessibilityViewIsModal`
if (oldViewProps.accessibilityViewIsModal != newViewProps.accessibilityViewIsModal) {
self.accessibilityElement.accessibilityViewIsModal = newViewProps.accessibilityViewIsModal;
}
// `accessibilityElementsHidden`
if (oldViewProps.accessibilityElementsHidden != newViewProps.accessibilityElementsHidden) {
self.accessibilityElement.accessibilityElementsHidden = newViewProps.accessibilityElementsHidden;
}
// `accessibilityShowsLargeContentViewer`
if (oldViewProps.accessibilityShowsLargeContentViewer != newViewProps.accessibilityShowsLargeContentViewer) {
if (@available(iOS 13.0, *)) {
if (newViewProps.accessibilityShowsLargeContentViewer) {
self.showsLargeContentViewer = YES;
UILargeContentViewerInteraction *interaction = [[UILargeContentViewerInteraction alloc] init];
[self addInteraction:interaction];
} else {
self.showsLargeContentViewer = NO;
}
}
}
// `accessibilityLargeContentTitle`
if (oldViewProps.accessibilityLargeContentTitle != newViewProps.accessibilityLargeContentTitle) {
if (@available(iOS 13.0, *)) {
self.largeContentTitle = RCTNSStringFromStringNilIfEmpty(newViewProps.accessibilityLargeContentTitle);
}
}
#endif // [macOS]
// `accessibilityOrder`
if (oldViewProps.accessibilityOrder != newViewProps.accessibilityOrder &&
ReactNativeFeatureFlags::enableAccessibilityOrder()) {
// Creating a set since a lot of logic requires lookups in here. However,
// we still need to preserve the orginal order. So just read from props
// if need to access that
_accessibilityOrderNativeIDs = [NSMutableSet new];
for (const std::string &childId : newViewProps.accessibilityOrder) {
[_accessibilityOrderNativeIDs addObject:RCTNSStringFromString(childId)];
}
_accessibilityElements = [NSMutableArray new];
}
// `accessibilityTraits`
if (oldViewProps.accessibilityTraits != newViewProps.accessibilityTraits) {
#if !TARGET_OS_OSX // [macOS]
self.accessibilityElement.accessibilityTraits =
RCTUIAccessibilityTraitsFromAccessibilityTraits(newViewProps.accessibilityTraits);
#else // [macOS
// On macOS, accessibilityElement returns self (NSView*) which doesn't have accessibilityTraits.
// Set role directly on self based on traits.
RCTUIAccessibilityTraits traits = RCTUIAccessibilityTraitsFromAccessibilityTraits(newViewProps.accessibilityTraits);
self.accessibilityRole = RCTAccessibilityRoleFromTraits(traits);
self.accessibilityEnabled = (traits & RCTUIAccessibilityTraitNotEnabled) == 0;
#endif // macOS]
}
#if !TARGET_OS_OSX // [macOS]
// `accessibilityState`
if (oldViewProps.accessibilityState != newViewProps.accessibilityState) {
self.accessibilityTraits &= ~(UIAccessibilityTraitNotEnabled | UIAccessibilityTraitSelected);
const auto accessibilityState = newViewProps.accessibilityState.value_or(AccessibilityState{});
if (accessibilityState.selected) {
self.accessibilityTraits |= UIAccessibilityTraitSelected;
}
if (accessibilityState.disabled) {
self.accessibilityTraits |= UIAccessibilityTraitNotEnabled;
}
}
// `accessibilityIgnoresInvertColors`
if (oldViewProps.accessibilityIgnoresInvertColors != newViewProps.accessibilityIgnoresInvertColors) {
self.accessibilityIgnoresInvertColors = newViewProps.accessibilityIgnoresInvertColors;
}
#endif // [macOS]
// `accessibilityValue`
if (oldViewProps.accessibilityValue != newViewProps.accessibilityValue) {
if (newViewProps.accessibilityValue.text.has_value()) {
self.accessibilityElement.accessibilityValue =
RCTNSStringFromStringNilIfEmpty(newViewProps.accessibilityValue.text.value());
} else if (
newViewProps.accessibilityValue.now.has_value() && newViewProps.accessibilityValue.min.has_value() &&
newViewProps.accessibilityValue.max.has_value()) {
CGFloat val = (CGFloat)(newViewProps.accessibilityValue.now.value()) /
(newViewProps.accessibilityValue.max.value() - newViewProps.accessibilityValue.min.value());
self.accessibilityElement.accessibilityValue =
[NSNumberFormatter localizedStringFromNumber:@(val) numberStyle:NSNumberFormatterPercentStyle];
;
} else {
self.accessibilityElement.accessibilityValue = nil;
}
}
#if !TARGET_OS_OSX // [macOS]
if (oldViewProps.accessibilityRespondsToUserInteraction != newViewProps.accessibilityRespondsToUserInteraction) {
self.accessibilityElement.accessibilityRespondsToUserInteraction =
newViewProps.accessibilityRespondsToUserInteraction;
}
#endif // [macOS]
// `testId`
if (oldViewProps.testId != newViewProps.testId) {
SEL setAccessibilityIdentifierSelector = @selector(setAccessibilityIdentifier:);
NSString *identifier = RCTNSStringFromString(newViewProps.testId);
if ([self.accessibilityElement respondsToSelector:setAccessibilityIdentifierSelector]) {
RCTPlatformView *accessibilityView = (RCTPlatformView *)self.accessibilityElement; // [macOS]
accessibilityView.accessibilityIdentifier = identifier;
} else {
self.accessibilityIdentifier = identifier;
}
}
// `filter`
if (oldViewProps.filter != newViewProps.filter) {
needsInvalidateLayer = YES;
}
// `mixBlendMode`
if (oldViewProps.mixBlendMode != newViewProps.mixBlendMode) {
switch (newViewProps.mixBlendMode) {
case BlendMode::Multiply:
self.layer.compositingFilter = @"multiplyBlendMode";
break;
case BlendMode::Screen:
self.layer.compositingFilter = @"screenBlendMode";
break;
case BlendMode::Overlay:
self.layer.compositingFilter = @"overlayBlendMode";
break;
case BlendMode::Darken:
self.layer.compositingFilter = @"darkenBlendMode";
break;
case BlendMode::Lighten:
self.layer.compositingFilter = @"lightenBlendMode";
break;
case BlendMode::ColorDodge:
self.layer.compositingFilter = @"colorDodgeBlendMode";
break;
case BlendMode::ColorBurn:
self.layer.compositingFilter = @"colorBurnBlendMode";
break;
case BlendMode::HardLight:
self.layer.compositingFilter = @"hardLightBlendMode";
break;
case BlendMode::SoftLight:
self.layer.compositingFilter = @"softLightBlendMode";
break;
case BlendMode::Difference:
self.layer.compositingFilter = @"differenceBlendMode";
break;
case BlendMode::Exclusion:
self.layer.compositingFilter = @"exclusionBlendMode";
break;
case BlendMode::Hue:
self.layer.compositingFilter = @"hueBlendMode";
break;
case BlendMode::Saturation:
self.layer.compositingFilter = @"saturationBlendMode";
break;
case BlendMode::Color:
self.layer.compositingFilter = @"colorBlendMode";
break;
case BlendMode::Luminosity:
self.layer.compositingFilter = @"luminosityBlendMode";
break;
case BlendMode::Normal:
self.layer.compositingFilter = nil;
break;
}
}
// `linearGradient`
if (oldViewProps.backgroundImage != newViewProps.backgroundImage) {
needsInvalidateLayer = YES;
}
// `boxShadow`
if (oldViewProps.boxShadow != newViewProps.boxShadow) {
needsInvalidateLayer = YES;
}
#if TARGET_OS_OSX // [macOS
// `acceptsFirstMouse`
if (oldViewProps.acceptsFirstMouse != newViewProps.acceptsFirstMouse) {
self.acceptsFirstMouse = newViewProps.acceptsFirstMouse;
}
// `mouseDownCanMoveWindow`
if (oldViewProps.mouseDownCanMoveWindow != newViewProps.mouseDownCanMoveWindow) {
self.mouseDownCanMoveWindow = newViewProps.mouseDownCanMoveWindow;
}
// `allowsVibrancy`
if (oldViewProps.allowsVibrancy != newViewProps.allowsVibrancy) {
_allowsVibrancy = newViewProps.allowsVibrancy;
}
// `draggedTypes`
if (oldViewProps.draggedTypes != newViewProps.draggedTypes) {
if (!oldViewProps.draggedTypes.empty()) {
[self unregisterDraggedTypes];
}
if (!newViewProps.draggedTypes.empty()) {
NSMutableArray<NSPasteboardType> *pasteboardTypes = [NSMutableArray arrayWithCapacity:newViewProps.draggedTypes.size()];
for (const auto &draggedType : newViewProps.draggedTypes) {
if (draggedType == "fileUrl") {
[pasteboardTypes addObject:NSFilenamesPboardType];
} else if (draggedType == "image") {
[pasteboardTypes addObject:NSPasteboardTypePNG];
[pasteboardTypes addObject:NSPasteboardTypeTIFF];
} else if (draggedType == "string") {
[pasteboardTypes addObject:NSPasteboardTypeString];
}
}
[self registerForDraggedTypes:pasteboardTypes];
}
}
// `tooltip`
if (oldViewProps.tooltip != newViewProps.tooltip) {
if (newViewProps.tooltip.has_value()) {
self.toolTip = RCTNSStringFromStringNilIfEmpty(newViewProps.tooltip.value());
} else {
self.toolTip = nil;
}
}
#endif // macOS]
_needsInvalidateLayer = _needsInvalidateLayer || needsInvalidateLayer;
_props = std::static_pointer_cast<const ViewProps>(props);
}
- (void)updateEventEmitter:(const EventEmitter::Shared &)eventEmitter
{
assert(std::dynamic_pointer_cast<const ViewEventEmitter>(eventEmitter));
_eventEmitter = std::static_pointer_cast<const ViewEventEmitter>(eventEmitter);
}
- (void)updateLayoutMetrics:(const LayoutMetrics &)layoutMetrics
oldLayoutMetrics:(const LayoutMetrics &)oldLayoutMetrics
{
// Using stored `_layoutMetrics` as `oldLayoutMetrics` here to avoid
// re-applying individual sub-values which weren't changed.
[super updateLayoutMetrics:layoutMetrics oldLayoutMetrics:_layoutMetrics];
_layoutMetrics = layoutMetrics;
_needsInvalidateLayer = YES;
_borderLayer.frame = self.layer.bounds;
if (_contentView) {
_contentView.frame = RCTCGRectFromRect(_layoutMetrics.getContentFrame());
}
if (_containerView) {
_containerView.frame = CGRectMake(0, 0, self.layer.bounds.size.width, self.layer.bounds.size.height);
}
if (_backgroundColorLayer) {
_backgroundColorLayer.frame = CGRectMake(0, 0, self.layer.bounds.size.width, self.layer.bounds.size.height);
}
if ((_props->transformOrigin.isSet() || _props->transform.operations.size() > 0) &&
layoutMetrics.frame.size != oldLayoutMetrics.frame.size) {
auto newTransform = _props->resolveTransform(layoutMetrics);
#if !TARGET_OS_OSX // [macOS]
self.layer.transform = RCTCATransform3DFromTransformMatrix(newTransform);
#else // [macOS
self.transform3D = RCTCATransform3DFromTransformMatrix(newTransform);
#endif // macOS]
}
}
- (BOOL)isJSResponder
{
return _isJSResponder;
}
- (void)setIsJSResponder:(BOOL)isJSResponder
{
_isJSResponder = isJSResponder;
}
- (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask
{
[super finalizeUpdates:updateMask];
_useCustomContainerView = [self styleWouldClipOverflowInk];
if (!_needsInvalidateLayer) {
return;
}
_needsInvalidateLayer = NO;
[self invalidateLayer];
#if TARGET_OS_OSX // [macOS
[self updateTrackingAreas];
[self updateClipViewBoundsObserverIfNeeded];
#endif // macOS]
}
- (void)prepareForRecycle
{
[super prepareForRecycle];
// If view was managed by animated, its props need to align with UIView's properties.
const auto &props = static_cast<const ViewProps &>(*_props);
if ([_propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN containsObject:@"transform"]) {
self.layer.transform = RCTCATransform3DFromTransformMatrix(props.transform);
}
if ([_propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN containsObject:@"opacity"]) {
self.layer.opacity = (float)props.opacity;
}
_propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN = nil;
_eventEmitter.reset();
_isJSResponder = NO;
_removeClippedSubviews = NO;
_reactSubviews = [NSMutableArray new];
_accessibilityElements = [NSMutableArray new];
#if TARGET_OS_OSX // [macOS
_allowsVibrancy = NO;
self.acceptsFirstMouse = NO;
self.mouseDownCanMoveWindow = YES;
#endif // macOS]
}
- (void)setPropKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN:(NSSet<NSString *> *_Nullable)props
{
_propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN = props;
}
- (NSSet<NSString *> *_Nullable)propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN
{
return _propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN;
}
- (RCTPlatformView *)betterHitTest:(CGPoint)point withEvent:(UIEvent *)event // [macOS]
{
// This is a classic textbook implementation of `hitTest:` with a couple of improvements:
// * It does not stop algorithm if some touch is outside the view
// which does not have `clipToBounds` enabled.
// * Taking `layer.zIndex` field into an account is not required because
// lists of `ShadowView`s are already sorted based on `zIndex` prop.
#if !TARGET_OS_OSX // [macOS]
if (!self.userInteractionEnabled || self.hidden || self.alpha < 0.01) {
#else // [macOS
if (!self.userInteractionEnabled || self.hidden || self.alphaValue < 0.01 ) {
#endif // macOS]
return nil;
}
BOOL isPointInside = [self pointInside:point withEvent:event];
BOOL clipsToBounds = false;
clipsToBounds = clipsToBounds || _layoutMetrics.overflowInset == EdgeInsets{};
if (clipsToBounds && !isPointInside) {
return nil;
}
for (RCTPlatformView *subview in [self.subviews reverseObjectEnumerator]) { // [macOS]
RCTPlatformView *hitView = RCTUIViewHitTestWithEvent(subview, point, self, event); // [macOS]
if (hitView) {
return hitView;
}
}
return isPointInside ? self : nil;
}
- (RCTPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event // [macOS]
{
switch (_props->pointerEvents) {
case PointerEventsMode::Auto:
return [self betterHitTest:point withEvent:event];
case PointerEventsMode::None:
return nil;
case PointerEventsMode::BoxOnly:
return [self pointInside:point withEvent:event] ? self : nil;
case PointerEventsMode::BoxNone:
RCTPlatformView *view = [self betterHitTest:point withEvent:event]; // [macOS]
return view != self ? view : nil;
}
}
static RCTCornerRadii RCTCornerRadiiFromBorderRadii(BorderRadii borderRadii)
{
return RCTCornerRadii{
.topLeftHorizontal = (CGFloat)borderRadii.topLeft.horizontal,
.topLeftVertical = (CGFloat)borderRadii.topLeft.vertical,
.topRightHorizontal = (CGFloat)borderRadii.topRight.horizontal,
.topRightVertical = (CGFloat)borderRadii.topRight.vertical,
.bottomLeftHorizontal = (CGFloat)borderRadii.bottomLeft.horizontal,
.bottomLeftVertical = (CGFloat)borderRadii.bottomLeft.vertical,
.bottomRightHorizontal = (CGFloat)borderRadii.bottomRight.horizontal,
.bottomRightVertical = (CGFloat)borderRadii.bottomRight.vertical};
}
static RCTCornerRadii
RCTCreateOutlineCornerRadiiFromBorderRadii(const BorderRadii &borderRadii, CGFloat outlineWidth, CGFloat outlineOffset)
{
return RCTCornerRadii{
borderRadii.topLeft.horizontal != 0 ? borderRadii.topLeft.horizontal + outlineWidth + outlineOffset : 0,
borderRadii.topLeft.vertical != 0 ? borderRadii.topLeft.vertical + outlineWidth + outlineOffset : 0,
borderRadii.topRight.horizontal != 0 ? borderRadii.topRight.horizontal + outlineWidth + outlineOffset : 0,
borderRadii.topRight.vertical != 0 ? borderRadii.topRight.vertical + outlineWidth + outlineOffset : 0,
borderRadii.bottomLeft.horizontal != 0 ? borderRadii.bottomLeft.horizontal + outlineWidth + outlineOffset : 0,
borderRadii.bottomLeft.vertical != 0 ? borderRadii.bottomLeft.vertical + outlineWidth + outlineOffset : 0,
borderRadii.bottomRight.horizontal != 0 ? borderRadii.bottomRight.horizontal + outlineWidth + outlineOffset : 0,
borderRadii.bottomRight.vertical != 0 ? borderRadii.bottomRight.vertical + outlineWidth + outlineOffset : 0};
}
// To be used for CSS properties like `border` and `outline`.
static void RCTAddContourEffectToLayer(
CALayer *layer,
const RCTCornerRadii &cornerRadii,
const RCTBorderColors &contourColors,
const UIEdgeInsets &contourInsets,
const RCTBorderStyle &contourStyle)
{
RCTUIImage *image = RCTGetBorderImage( // [macOS]
contourStyle, layer.bounds.size, cornerRadii, contourInsets, contourColors, [RCTPlatformColor clearColor], NO); // [macOS]
if (image == nil) {
layer.contents = nil;
} else {
CGSize imageSize = image.size;
UIEdgeInsets imageCapInsets = image.capInsets;
CGRect contentsCenter = CGRect{
CGPoint{imageCapInsets.left / imageSize.width, imageCapInsets.top / imageSize.height},
CGSize{(CGFloat)1.0 / imageSize.width, (CGFloat)1.0 / imageSize.height}};
layer.contents = (id)image.CGImage;
layer.contentsScale = image.scale;
BOOL isResizable = !UIEdgeInsetsEqualToEdgeInsets(image.capInsets, UIEdgeInsetsZero);
if (isResizable) {
layer.contentsCenter = contentsCenter;
} else {
layer.contentsCenter = CGRect{CGPoint{0.0, 0.0}, CGSize{1.0, 1.0}};
}
}
// If mutations are applied inside of Animation block, it may cause layer to be animated.
// To stop that, imperatively remove all animations from layer.
[layer removeAllAnimations];
}
static RCTBorderColors RCTCreateRCTBorderColorsFromBorderColors(BorderColors borderColors)
{
return RCTBorderColors{
.top = RCTUIColorFromSharedColor(borderColors.top),
.left = RCTUIColorFromSharedColor(borderColors.left),
.bottom = RCTUIColorFromSharedColor(borderColors.bottom),
.right = RCTUIColorFromSharedColor(borderColors.right)};
}
static CALayerCornerCurve CornerCurveFromBorderCurve(BorderCurve borderCurve)
{
// The constants are available only starting from iOS 13
// CALayerCornerCurve is a typealias on NSString *
switch (borderCurve) {
case BorderCurve::Continuous:
return @"continuous"; // kCACornerCurveContinuous;
case BorderCurve::Circular:
return @"circular"; // kCACornerCurveCircular;
}
}
static RCTBorderStyle RCTBorderStyleFromBorderStyle(BorderStyle borderStyle)
{
switch (borderStyle) {
case BorderStyle::Solid:
return RCTBorderStyleSolid;
case BorderStyle::Dotted:
return RCTBorderStyleDotted;
case BorderStyle::Dashed:
return RCTBorderStyleDashed;
}
}
#if TARGET_OS_OSX // [macOS
static RCTCursor RCTCursorFromCursor(Cursor cursor)
{
switch (cursor) {
case Cursor::Auto:
return RCTCursorAuto;
case Cursor::Alias:
return RCTCursorAlias;
case Cursor::AllScroll:
return RCTCursorAllScroll;
case Cursor::Cell:
return RCTCursorCell;
case Cursor::ColResize:
return RCTCursorColResize;
case Cursor::ContextMenu:
return RCTCursorContextMenu;
case Cursor::Copy:
return RCTCursorCopy;
case Cursor::Crosshair:
return RCTCursorCrosshair;
case Cursor::Default:
return RCTCursorDefault;
case Cursor::EResize:
return RCTCursorEResize;
case Cursor::EWResize:
return RCTCursorEWResize;
case Cursor::Grab:
return RCTCursorGrab;
case Cursor::Grabbing:
return RCTCursorGrabbing;
case Cursor::Help:
return RCTCursorHelp;
case Cursor::Move:
return RCTCursorMove;
case Cursor::NEResize:
return RCTCursorNEResize;
case Cursor::NESWResize:
return RCTCursorNESWResize;
case Cursor::NResize:
return RCTCursorNResize;
case Cursor::NSResize:
return RCTCursorNSResize;
case Cursor::NWResize:
return RCTCursorNWResize;
case Cursor::NWSEResize:
return RCTCursorNWSEResize;
case Cursor::NoDrop:
return RCTCursorNoDrop;
case Cursor::None:
return RCTCursorNone;
case Cursor::NotAllowed:
return RCTCursorNotAllowed;
case Cursor::Pointer:
return RCTCursorPointer;
case Cursor::Progress:
return RCTCursorProgress;
case Cursor::RowResize:
return RCTCursorRowResize;
case Cursor::SResize:
return RCTCursorSResize;
case Cursor::SEResize:
return RCTCursorSEResize;
case Cursor::SWResize:
return RCTCursorSWResize;
case Cursor::Text:
return RCTCursorText;
case Cursor::Url:
return RCTCursorUrl;
case Cursor::VerticalText:
return RCTCursorVerticalText;
case Cursor::WResize:
return RCTCursorWResize;
case Cursor::Wait: