forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathRCTView.m
1755 lines (1539 loc) · 62.7 KB
/
RCTView.m
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) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTView.h"
#import "RCTAutoInsetsProtocol.h"
#import "RCTBorderDrawing.h"
#import "RCTFocusChangeEvent.h" // TODO(OSS Candidate ISS#2710739)
#import "RCTConvert.h"
#import "RCTI18nUtil.h"
#import "RCTLog.h"
#import "RCTRootContentView.h" // TODO(macOS GH#774)
#import "RCTUtils.h"
#import "UIView+React.h"
#import "RCTViewKeyboardEvent.h"
#if TARGET_OS_OSX // [TODO(macOS GH#774)
#import "RCTTextView.h"
#endif // ]TODO(macOS GH#774)
#if !TARGET_OS_OSX // TODO(macOS GH#774)
UIAccessibilityTraits const SwitchAccessibilityTrait = 0x20000000000001;
#endif // TODO(macOS GH#774)
@implementation RCTPlatformView (RCTViewUnmounting) // TODO(macOS GH#774)
- (void)react_remountAllSubviews
{
// Normal views don't support unmounting, so all
// this does is forward message to our subviews,
// in case any of those do support it
for (RCTUIView *subview in self.subviews) { // TODO(macOS ISS#3536887)
[subview react_remountAllSubviews];
}
}
- (void)react_updateClippedSubviewsWithClipRect:(CGRect)clipRect relativeToView:(RCTPlatformView *)clipView // TODO(macOS GH#774)
{
// Even though we don't support subview unmounting
// we do support clipsToBounds, so if that's enabled
// we'll update the clipping
if (RCTUIViewSetClipsToBounds(self) && self.subviews.count > 0) { // TODO(macOS GH#774) and TODO(macOS ISS#3536887)
clipRect = [clipView convertRect:clipRect toView:self];
clipRect = CGRectIntersection(clipRect, self.bounds);
clipView = self;
}
// Normal views don't support unmounting, so all
// this does is forward message to our subviews,
// in case any of those do support it
for (RCTUIView *subview in self.subviews) { // TODO(macOS ISS#3536887)
[subview react_updateClippedSubviewsWithClipRect:clipRect relativeToView:clipView];
}
}
- (RCTPlatformView *)react_findClipView // TODO(macOS GH#774)
{
RCTPlatformView *testView = self; // TODO(macOS GH#774)
RCTPlatformView *clipView = nil; // TODO(macOS GH#774)
CGRect clipRect = self.bounds;
// We will only look for a clipping view up the view hierarchy until we hit the root view.
while (testView) {
if (RCTUIViewSetClipsToBounds(testView)) { // TODO(macOS GH#774) and TODO(macOS ISS#3536887)
if (clipView) {
CGRect testRect = [clipView convertRect:clipRect toView:testView];
if (!CGRectContainsRect(testView.bounds, testRect)) {
clipView = testView;
clipRect = CGRectIntersection(testView.bounds, testRect);
}
} else {
clipView = testView;
clipRect = [self convertRect:self.bounds toView:clipView];
}
}
if ([testView isReactRootView]) {
break;
}
testView = testView.superview;
}
#if !TARGET_OS_OSX // TODO(macOS GH#774)
return clipView ?: self.window;
#else // [TODO(macOS GH#774)
return clipView ?: self.window.contentView;
#endif // ]TODO(macOS GH#774)
}
@end
static NSString *RCTRecursiveAccessibilityLabel(RCTUIView *view) // TODO(macOS ISS#3536887)
{
NSMutableString *str = [NSMutableString stringWithString:@""];
for (RCTUIView *subview in view.subviews) { // TODO(macOS ISS#3536887)
#if !TARGET_OS_OSX // TODO(macOS GH#774)
NSString *label = subview.accessibilityLabel;
#else // [TODO(macOS GH#774)
NSString *label;
if ([subview isKindOfClass:[RCTTextView class]]) {
// on macOS VoiceOver a text element will always have its accessibilityValue read, but will only read it's accessibilityLabel if it's value is set.
// the macOS RCTTextView accessibilityValue will return its accessibilityLabel if set otherwise return its text.
label = subview.accessibilityValue;
} else {
label = subview.accessibilityLabel;
}
#endif // ]TODO(macOS GH#774)
if (!label) {
label = RCTRecursiveAccessibilityLabel(subview);
}
if (label && label.length > 0) {
if (str.length > 0) {
[str appendString:@" "];
}
[str appendString:label];
}
}
return str.length == 0 ? nil : str;
}
@implementation RCTView {
RCTUIColor *_backgroundColor; // TODO(OSS Candidate ISS#2710739)
RCTEventDispatcher *_eventDispatcher; // TODO(OSS Candidate ISS#2710739)
#if TARGET_OS_OSX // [TODO(macOS GH#774)
NSTrackingArea *_trackingArea;
BOOL _hasMouseOver;
#endif // ]TODO(macOS GH#774)
NSMutableDictionary<NSString *, NSDictionary *> *accessibilityActionsNameMap;
NSMutableDictionary<NSString *, NSDictionary *> *accessibilityActionsLabelMap;
}
// [TODO(OSS Candidate ISS#2710739)
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher
{
if ((self = [self initWithFrame:CGRectZero])) {
_eventDispatcher = eventDispatcher;
}
return self;
}
// ]TODO(OSS Candidate ISS#2710739)
- (instancetype)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
_borderWidth = -1;
_borderTopWidth = -1;
_borderRightWidth = -1;
_borderBottomWidth = -1;
_borderLeftWidth = -1;
_borderStartWidth = -1;
_borderEndWidth = -1;
_borderTopLeftRadius = -1;
_borderTopRightRadius = -1;
_borderTopStartRadius = -1;
_borderTopEndRadius = -1;
_borderBottomLeftRadius = -1;
_borderBottomRightRadius = -1;
_borderBottomStartRadius = -1;
_borderBottomEndRadius = -1;
_borderStyle = RCTBorderStyleSolid;
_hitTestEdgeInsets = UIEdgeInsetsZero;
_backgroundColor = super.backgroundColor;
}
return self;
}
RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : unused)
- (void)setReactLayoutDirection:(UIUserInterfaceLayoutDirection)layoutDirection
{
if (_reactLayoutDirection != layoutDirection) {
_reactLayoutDirection = layoutDirection;
[self.layer setNeedsDisplay];
}
#if !TARGET_OS_OSX // TODO(macOS GH#774)
if ([self respondsToSelector:@selector(setSemanticContentAttribute:)]) {
#pragma clang diagnostic push // TODO(OSS Candidate ISS#2710739)
#pragma clang diagnostic ignored "-Wunguarded-availability" // TODO(OSS Candidate ISS#2710739)
self.semanticContentAttribute = layoutDirection == UIUserInterfaceLayoutDirectionLeftToRight
? UISemanticContentAttributeForceLeftToRight
: UISemanticContentAttributeForceRightToLeft;
#pragma clang diagnostic pop // TODO(OSS Candidate ISS#2710739)
}
#else // [TODO(macOS GH#774)
self.userInterfaceLayoutDirection =
layoutDirection == UIUserInterfaceLayoutDirectionLeftToRight ?
NSUserInterfaceLayoutDirectionLeftToRight :
NSUserInterfaceLayoutDirectionRightToLeft;
#endif // ]TODO(macOS GH#774)
}
#pragma mark - Hit Testing
- (void)setPointerEvents:(RCTPointerEvents)pointerEvents
{
_pointerEvents = pointerEvents;
self.userInteractionEnabled = (pointerEvents != RCTPointerEventsNone);
#if !TARGET_OS_OSX // TODO(macOS GH#774)
if (pointerEvents == RCTPointerEventsBoxNone) {
self.accessibilityViewIsModal = NO;
}
#endif // TODO(macOS GH#774)
}
- (RCTPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event // TODO(macOS GH#774)
{
BOOL canReceiveTouchEvents = ([self isUserInteractionEnabled] && ![self isHidden]);
if (!canReceiveTouchEvents) {
return nil;
}
// `hitSubview` is the topmost subview which was hit. The hit point can
// be outside the bounds of `view` (e.g., if -clipsToBounds is NO).
RCTPlatformView *hitSubview = nil; // TODO(macOS GH#774)
BOOL isPointInside = [self pointInside:point withEvent:event];
BOOL needsHitSubview = !(_pointerEvents == RCTPointerEventsNone || _pointerEvents == RCTPointerEventsBoxOnly);
if (needsHitSubview && (![self clipsToBounds] || isPointInside)) {
// Take z-index into account when calculating the touch target.
NSArray<RCTUIView *> *sortedSubviews = [self reactZIndexSortedSubviews]; // TODO(macOS ISS#3536887)
// The default behaviour of UIKit is that if a view does not contain a point,
// then no subviews will be returned from hit testing, even if they contain
// the hit point. By doing hit testing directly on the subviews, we bypass
// the strict containment policy (i.e., UIKit guarantees that every ancestor
// of the hit view will return YES from -pointInside:withEvent:). See:
// - https://developer.apple.com/library/ios/qa/qa2013/qa1812.html
for (RCTUIView *subview in [sortedSubviews reverseObjectEnumerator]) { // TODO(macOS ISS#3536887)
CGPoint pointForHitTest = CGPointZero; // [TODO(macOS GH#774)
#if TARGET_OS_OSX
if ([subview isKindOfClass:[RCTView class]]) {
pointForHitTest = [subview convertPoint:point fromView:self];
} else {
pointForHitTest = point;
}
#else
pointForHitTest = [subview convertPoint:point fromView:self];
#endif
hitSubview = RCTUIViewHitTestWithEvent(subview, pointForHitTest, event); // ]TODO(macOS GH#774) and TODO(macOS ISS#3536887)
if (hitSubview != nil) {
break;
}
}
}
RCTPlatformView *hitView = (isPointInside ? self : nil); // TODO(macOS GH#774)
switch (_pointerEvents) {
case RCTPointerEventsNone:
return nil;
case RCTPointerEventsUnspecified:
return hitSubview ?: hitView;
case RCTPointerEventsBoxOnly:
return hitView;
case RCTPointerEventsBoxNone:
return hitSubview;
default:
RCTLogError(@"Invalid pointer-events specified %lld on %@", (long long)_pointerEvents, self);
return hitSubview ?: hitView;
}
}
- (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);
}
#pragma mark - Accessibility
- (NSString *)accessibilityLabel
{
NSString *label = super.accessibilityLabel;
if (label) {
return label;
}
#if TARGET_OS_OSX // [TODO(macOS GH#774)
// calling super.accessibilityLabel above on macOS causes the return value of this accessor to be ignored by VoiceOver.
// Calling the super's setAccessibilityLabel with nil ensures that the return value of this accessor is used by VoiceOver.
[super setAccessibilityLabel:nil];
#endif // ]TODO(macOS GH#774)
return RCTRecursiveAccessibilityLabel(self);
}
- (NSArray<UIAccessibilityCustomAction *> *)accessibilityCustomActions
{
if (!self.accessibilityActions.count) {
return nil;
}
accessibilityActionsNameMap = [[NSMutableDictionary alloc] init];
accessibilityActionsLabelMap = [[NSMutableDictionary alloc] init];
NSMutableArray *actions = [NSMutableArray array];
for (NSDictionary *action in self.accessibilityActions) {
if (action[@"name"]) {
accessibilityActionsNameMap[action[@"name"]] = action;
}
if (action[@"label"]) {
accessibilityActionsLabelMap[action[@"label"]] = action;
[actions addObject:[[UIAccessibilityCustomAction alloc]
initWithName:action[@"label"]
target:self
selector:@selector(didActivateAccessibilityCustomAction:)]];
}
}
return [actions copy];
}
- (BOOL)didActivateAccessibilityCustomAction:(UIAccessibilityCustomAction *)action
{
if (!_onAccessibilityAction || !accessibilityActionsLabelMap) {
return NO;
}
// iOS defines the name as the localized label, so use our map to convert this back to the non-localized action name
// when passing to JS. This allows for standard action names across platforms.
NSDictionary *actionObject = accessibilityActionsLabelMap[action.name];
if (actionObject) {
_onAccessibilityAction(@{@"actionName" : actionObject[@"name"], @"actionTarget" : self.reactTag});
}
return YES;
}
#if !TARGET_OS_OSX // TODO(macOS GH#774)
- (NSString *)accessibilityValue
{
static dispatch_once_t onceToken;
static NSDictionary<NSString *, NSString *> *rolesAndStatesDescription = nil;
dispatch_once(&onceToken, ^{
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"AccessibilityResources" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
if (bundle) {
NSURL *url = [bundle URLForResource:@"Localizable" withExtension:@"strings"];
if (@available(iOS 11.0, *)) {
rolesAndStatesDescription = [NSDictionary dictionaryWithContentsOfURL:url error:nil];
} else {
// Fallback on earlier versions
rolesAndStatesDescription = [NSDictionary dictionaryWithContentsOfURL:url];
}
}
if (rolesAndStatesDescription == nil) {
// Falling back to hardcoded English list.
NSLog(@"Cannot load localized accessibility strings.");
rolesAndStatesDescription = @{
@"alert" : @"alert",
@"checkbox" : @"checkbox",
@"combobox" : @"combo box",
@"menu" : @"menu",
@"menubar" : @"menu bar",
@"menuitem" : @"menu item",
@"progressbar" : @"progress bar",
@"radio" : @"radio button",
@"radiogroup" : @"radio group",
@"scrollbar" : @"scroll bar",
@"spinbutton" : @"spin button",
@"switch" : @"switch",
@"tab" : @"tab",
@"tablist" : @"tab list",
@"timer" : @"timer",
@"toolbar" : @"tool bar",
@"checked" : @"checked",
@"unchecked" : @"not checked",
@"busy" : @"busy",
@"expanded" : @"expanded",
@"collapsed" : @"collapsed",
@"mixed" : @"mixed",
};
}
});
// Handle Switch.
if ((self.accessibilityTraits & SwitchAccessibilityTrait) == SwitchAccessibilityTrait) {
for (NSString *state in self.accessibilityState) {
id val = self.accessibilityState[state];
if (!val) {
continue;
}
if ([state isEqualToString:@"checked"] && [val isKindOfClass:[NSNumber class]]) {
return [val boolValue] ? @"1" : @"0";
}
}
}
NSMutableArray *valueComponents = [NSMutableArray new];
NSString *roleDescription = self.accessibilityRoleInternal ? rolesAndStatesDescription[self.accessibilityRoleInternal] : nil; // TODO(OSS Candidate ISS#2710739): renamed prop so it doesn't conflict with -[NSAccessibility accessibilityRole].
if (roleDescription) {
[valueComponents addObject:roleDescription];
}
// Handle states which haven't already been handled in RCTViewManager.
for (NSString *state in self.accessibilityState) {
id val = self.accessibilityState[state];
if (!val) {
continue;
}
if ([state isEqualToString:@"checked"]) {
if ([val isKindOfClass:[NSNumber class]]) {
[valueComponents addObject:rolesAndStatesDescription[[val boolValue] ? @"checked" : @"unchecked"]];
} else if ([val isKindOfClass:[NSString class]] && [val isEqualToString:@"mixed"]) {
[valueComponents addObject:rolesAndStatesDescription[@"mixed"]];
}
}
if ([state isEqualToString:@"expanded"] && [val isKindOfClass:[NSNumber class]]) {
[valueComponents addObject:rolesAndStatesDescription[[val boolValue] ? @"expanded" : @"collapsed"]];
}
if ([state isEqualToString:@"busy"] && [val isKindOfClass:[NSNumber class]] && [val boolValue]) {
[valueComponents addObject:rolesAndStatesDescription[@"busy"]];
}
}
// Handle accessibilityValue.
if (self.accessibilityValueInternal) {
id min = self.accessibilityValueInternal[@"min"];
id now = self.accessibilityValueInternal[@"now"];
id max = self.accessibilityValueInternal[@"max"];
id text = self.accessibilityValueInternal[@"text"];
if (text && [text isKindOfClass:[NSString class]]) {
[valueComponents addObject:text];
} else if (
[min isKindOfClass:[NSNumber class]] && [now isKindOfClass:[NSNumber class]] &&
[max isKindOfClass:[NSNumber class]] && ([min intValue] < [max intValue]) &&
([min intValue] <= [now intValue] && [now intValue] <= [max intValue])) {
int val = ([now intValue] * 100) / ([max intValue] - [min intValue]);
[valueComponents addObject:[NSString stringWithFormat:@"%d percent", val]];
}
}
if (valueComponents.count > 0) {
return [valueComponents componentsJoinedByString:@", "];
}
return nil;
}
#else // [TODO(macOS GH#774)
- (id)accessibilityValue {
id accessibilityValue = nil;
NSAccessibilityRole role = [self accessibilityRole];
if (role == NSAccessibilityCheckBoxRole ||
role == NSAccessibilityRadioButtonRole ||
role == NSAccessibilityDisclosureTriangleRole) {
for (NSString *state in [self accessibilityState]) {
id val = [self accessibilityState][state];
if (val != nil) {
if ([state isEqualToString:@"checked"]) {
if ([val isKindOfClass:[NSNumber class]]) {
accessibilityValue = @([val boolValue]);
} else if ([val isKindOfClass:[NSString class]] && [val isEqualToString:@"mixed"]) {
accessibilityValue = @(2); // undocumented by Apple: @(2) is the accessibilityValue an NSButton has when its state is NSMixedState (-1) and causes VoiceOver to announced "mixed".
}
}
}
}
} else if ([self accessibilityRole] == NSAccessibilityStaticTextRole) {
// On macOS if the role is static text, VoiceOver will only read the text returned by accessibilityValue.
// So return accessibilityLabel which has the logic to return either either the ivar or a computed value of all the children's text.
// If the accessibilityValueInternal "text" is present, it will override this value below.
accessibilityValue = [self accessibilityLabel];
}
// handle accessibilityValue
id accessibilityValueInternal = [self accessibilityValueInternal];
if (accessibilityValueInternal != nil) {
id now = accessibilityValueInternal[@"now"];
id text = accessibilityValueInternal[@"text"];
if (text != nil && [text isKindOfClass:[NSString class]]) {
accessibilityValue = text;
} else if (now != nil && [now isKindOfClass:[NSNumber class]]) {
accessibilityValue = now;
}
}
return accessibilityValue;
}
- (BOOL)isAccessibilitySelectorAllowed:(SEL)selector {
BOOL isAllowed = NO;
if (selector == @selector(isAccessibilityEnabled)) {
if (self.accessibilityState != nil) {
id disabled = self.accessibilityState[@"disabled"];
if ([disabled isKindOfClass:[NSNumber class]]) {
isAllowed = YES;
}
}
} else if (selector == @selector(isAccessibilitySelected)) {
if (self.accessibilityState != nil) {
id selected = self.accessibilityState[@"selected"];
if ([selected isKindOfClass:[NSNumber class]]) {
isAllowed = YES;
}
}
} else if (selector == @selector(isAccessibilityExpanded)) {
if (self.accessibilityState != nil) {
id expanded = self.accessibilityState[@"expanded"];
if ([expanded isKindOfClass:[NSNumber class]]) {
isAllowed = YES;
}
}
} else if (selector == @selector(accessibilityPerformPress)) {
if (_onAccessibilityTap != nil ||
(_onAccessibilityAction != nil && accessibilityActionsNameMap[@"activate"]) ||
_onClick != nil) {
isAllowed = YES;
}
} else if (selector == @selector(accessibilityPerformIncrement)) {
if (_onAccessibilityAction != nil && accessibilityActionsNameMap[@"increment"]) {
isAllowed = YES;
}
} else if (selector == @selector(accessibilityPerformDecrement)) {
if (_onAccessibilityAction != nil && accessibilityActionsNameMap[@"decrement"]) {
isAllowed = YES;
}
#if TARGET_OS_OSX // [TODO(macOS GH#774)
} else if (selector == @selector(accessibilityPerformShowMenu)) {
if (_onAccessibilityAction != nil && accessibilityActionsNameMap[@"showMenu"]) {
isAllowed = YES;
}
#endif // ]TODO(macOS GH#774)
} else {
isAllowed = YES;
}
return isAllowed;
}
// This override currently serves as a workaround to avoid the generic "action 1"
// description for show menu
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
- (NSString *)accessibilityActionDescription:(NSString *)action {
NSString *actionDescription = nil;
if ([action isEqualToString:NSAccessibilityPressAction] || [action isEqualToString:NSAccessibilityShowMenuAction]) {
actionDescription = NSAccessibilityActionDescription(action);
} else {
actionDescription = [super accessibilityActionDescription:action];
}
return actionDescription;
}
#pragma clang dianostic pop
- (BOOL)isAccessibilityEnabled {
BOOL isAccessibilityEnabled = YES;
if (self.accessibilityState != nil) {
id disabled = self.accessibilityState[@"disabled"];
if ([disabled isKindOfClass:[NSNumber class]]) {
isAccessibilityEnabled = [disabled boolValue] ? NO : YES;
}
}
return isAccessibilityEnabled;
}
- (BOOL)isAccessibilitySelected {
BOOL isAccessibilitySelected = NO;
if (self.accessibilityState != nil) {
id selected = self.accessibilityState[@"selected"];
if ([selected isKindOfClass:[NSNumber class]]) {
isAccessibilitySelected = [selected boolValue];
}
}
return isAccessibilitySelected;
}
- (BOOL)isAccessibilityExpanded {
BOOL isAccessibilityExpanded = NO;
if (self.accessibilityState != nil) {
id expanded = self.accessibilityState[@"expanded"];
if ([expanded isKindOfClass:[NSNumber class]]) {
isAccessibilityExpanded = [expanded boolValue];
}
}
return isAccessibilityExpanded;
}
- (id)accessibilityMinValue {
id accessibilityMinValue = nil;
if (self.accessibilityValueInternal != nil) {
id min = self.accessibilityValueInternal[@"min"];
if ([min isKindOfClass:[NSNumber class]]) {
accessibilityMinValue = min;
}
}
return accessibilityMinValue;
}
- (id)accessibilityMaxValue {
id accessibilityMaxValue = nil;
if (self.accessibilityValueInternal != nil) {
id max = self.accessibilityValueInternal[@"max"];
if ([max isKindOfClass:[NSNumber class]]) {
accessibilityMaxValue = max;
}
}
return accessibilityMaxValue;
}
#endif // ]TODO(macOS GH#774)
- (RCTPlatformView *)reactAccessibilityElement // TODO(macOS GH#774)
{
return self;
}
- (BOOL)isAccessibilityElement
{
if (self.reactAccessibilityElement == self) {
return [super isAccessibilityElement];
}
return NO;
}
- (BOOL)performAccessibilityAction:(NSString *)name
{
if (_onAccessibilityAction && accessibilityActionsNameMap[name]) {
_onAccessibilityAction(@{@"actionName" : name, @"actionTarget" : self.reactTag});
return YES;
}
return NO;
}
#if !TARGET_OS_OSX // TODO(macOS GH#774)
- (BOOL)accessibilityActivate
#else // [TODO(macOS GH#774)
- (BOOL)accessibilityPerformPress
#endif // ]TODO(macOS GH#774)
{
#if TARGET_OS_OSX // [TODO(macOS GH#774)
if ([self isAccessibilityEnabled] == NO) {
return NO;
}
#endif // ]TODO(macOS GH#774)
if ([self performAccessibilityAction:@"activate"]) {
return YES;
} else if (_onAccessibilityTap) {
_onAccessibilityTap(nil);
return YES;
#if TARGET_OS_OSX // [TODO(macOS GH#774)
} else if (_onClick != nil) {
// macOS is not simulating a click if there is no onAccessibilityAction like it does on iOS, so we simulate it here.
_onClick(nil);
return YES;
#endif // ]TODO(macOS GH#774)
} else {
return NO;
}
}
#if !TARGET_OS_OSX // TODO(macOS GH#774)
- (BOOL)accessibilityPerformMagicTap
{
if ([self performAccessibilityAction:@"magicTap"]) {
return YES;
} else if (_onMagicTap) {
_onMagicTap(nil);
return YES;
} else {
return NO;
}
}
#endif // TODO(macOS GH#774)
- (BOOL)accessibilityPerformEscape
{
if ([self performAccessibilityAction:@"escape"]) {
return YES;
} else if (_onAccessibilityEscape) {
_onAccessibilityEscape(nil);
return YES;
} else {
return NO;
}
}
#if !TARGET_OS_OSX // TODO(macOS GH#774)
- (void)accessibilityIncrement
{
[self performAccessibilityAction:@"increment"];
}
#else // [TODO(macOS GH#774)
- (BOOL)accessibilityPerformIncrement
{
return [self performAccessibilityAction:@"increment"];
}
#endif // ]TODO(macOS GH#774)
#if !TARGET_OS_OSX // TODO(macOS GH#774)
- (void)accessibilityDecrement
{
[self performAccessibilityAction:@"decrement"];
}
#else // [TODO(macOS GH#774)
- (BOOL)accessibilityPerformDecrement
{
return [self performAccessibilityAction:@"decrement"];
}
#endif // ]TODO(macOS GH#774)
#if TARGET_OS_OSX // TODO(macOS GH#774)
- (BOOL)accessibilityPerformShowMenu
{
return [self performAccessibilityAction:@"showMenu"];
}
#endif // ]TODO(macOS GH#774)
- (NSString *)description
{
NSString *superDescription = super.description;
NSRange semicolonRange = [superDescription rangeOfString:@";"];
if (semicolonRange.location == NSNotFound) { // [TODO(macOS GH#774)
return [[superDescription substringToIndex:superDescription.length - 1] stringByAppendingFormat:@"; reactTag: %@; frame = %@; layer = %@>", self.reactTag, NSStringFromCGRect(self.frame), self.layer];
} else { // ]TODO(macOS GH#774)
NSString *replacement = [NSString stringWithFormat:@"; reactTag: %@;", self.reactTag];
return [superDescription stringByReplacingCharactersInRange:semicolonRange withString:replacement];
} // TODO(macOS GH#774)
}
#if TARGET_OS_OSX // [TODO(macOS GH#774)
- (void)viewDidMoveToWindow
{
// Subscribe to view bounds changed notification so that the view can be notified when a
// scroll event occurs either due to trackpad/gesture based scrolling or a scrollwheel event
// both of which would not cause the mouseExited to be invoked.
if ([self window] == nil) {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSViewBoundsDidChangeNotification
object:nil];
}
else if ([[self enclosingScrollView] contentView] != nil) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(viewBoundsChanged:)
name:NSViewBoundsDidChangeNotification
object:[[self enclosingScrollView] contentView]];
}
[super viewDidMoveToWindow];
}
- (void)viewBoundsChanged:(NSNotification*)__unused inNotif
{
// When an enclosing scrollview is scrolled using the scrollWheel or trackpad,
// the mouseExited: event does not get called on the view where mouseEntered: was previously called.
// This creates an unnatural pairing of mouse enter and exit events and can cause problems.
// We therefore explicitly check for this here and handle them by calling the appropriate callbacks.
if (!_hasMouseOver && self.onMouseEnter)
{
NSPoint locationInWindow = [[self window] mouseLocationOutsideOfEventStream];
NSPoint locationInView = [self convertPoint:locationInWindow fromView:nil];
if (NSPointInRect(locationInView, [self bounds]))
{
_hasMouseOver = YES;
[self sendMouseEventWithBlock:self.onMouseEnter
locationInfo:[self locationInfoFromDraggingLocation:locationInWindow]
modifierFlags:0
additionalData:nil];
}
}
else if (_hasMouseOver && self.onMouseLeave)
{
NSPoint locationInWindow = [[self window] mouseLocationOutsideOfEventStream];
NSPoint locationInView = [self convertPoint:locationInWindow fromView:nil];
if (!NSPointInRect(locationInView, [self bounds]))
{
_hasMouseOver = NO;
[self sendMouseEventWithBlock:self.onMouseLeave
locationInfo:[self locationInfoFromDraggingLocation:locationInWindow]
modifierFlags:0
additionalData:nil];
}
}
}
#endif // ]TODO(macOS GH#774)
#pragma mark - Statics for dealing with layoutGuides
+ (void)autoAdjustInsetsForView:(RCTUIView<RCTAutoInsetsProtocol> *)parentView // TODO(macOS ISS#3536887)
withScrollView:(RCTUIScrollView *)scrollView // TODO(macOS ISS#3536887)
updateOffset:(BOOL)updateOffset
{
UIEdgeInsets baseInset = parentView.contentInset;
CGFloat previousInsetTop = scrollView.contentInset.top;
CGPoint contentOffset = scrollView.contentOffset;
#if !TARGET_OS_OSX // TODO(macOS GH#774)
if (parentView.automaticallyAdjustContentInsets) {
UIEdgeInsets autoInset = [self contentInsetsForView:parentView];
baseInset.top += autoInset.top;
baseInset.bottom += autoInset.bottom;
baseInset.left += autoInset.left;
baseInset.right += autoInset.right;
}
#endif // TODO(macOS GH#774)
scrollView.contentInset = baseInset;
scrollView.scrollIndicatorInsets = baseInset;
if (updateOffset) {
// If we're adjusting the top inset, then let's also adjust the contentOffset so that the view
// elements above the top guide do not cover the content.
// This is generally only needed when your views are initially laid out, for
// manual changes to contentOffset, you can optionally disable this step
CGFloat currentInsetTop = scrollView.contentInset.top;
if (currentInsetTop != previousInsetTop) {
contentOffset.y -= (currentInsetTop - previousInsetTop);
scrollView.contentOffset = contentOffset;
}
}
}
#if !TARGET_OS_OSX // TODO(macOS GH#774)
+ (UIEdgeInsets)contentInsetsForView:(UIView *)view
{
while (view) {
UIViewController *controller = view.reactViewController;
if (controller) {
return controller.view.safeAreaInsets;
}
view = view.superview;
}
return UIEdgeInsetsZero;
}
#endif // TODO(macOS GH#774)
#pragma mark - View Unmounting
- (void)react_remountAllSubviews
{
if (_removeClippedSubviews) {
for (RCTUIView *view in self.reactSubviews) { // TODO(macOS ISS#3536887)
if (view.superview != self) {
[self addSubview:view];
[view react_remountAllSubviews];
}
}
} else {
// If _removeClippedSubviews is false, we must already be showing all subviews
[super react_remountAllSubviews];
}
}
- (void)react_updateClippedSubviewsWithClipRect:(CGRect)clipRect relativeToView:(RCTPlatformView *)clipView // TODO(macOS GH#774)
{
// TODO (#5906496): for scrollviews (the primary use-case) we could
// optimize this by only doing a range check along the scroll axis,
// instead of comparing the whole frame
if (!_removeClippedSubviews) {
// Use default behavior if unmounting is disabled
return [super react_updateClippedSubviewsWithClipRect:clipRect relativeToView:clipView];
}
if (self.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];
clipRect = CGRectIntersection(clipRect, self.bounds);
clipView = self;
// Mount / unmount views
for (RCTUIView *view in self.reactSubviews) { // TODO(macOS ISS#3536887)
if (!CGSizeEqualToSize(CGRectIntersection(clipRect, view.frame).size, CGSizeZero)) {
// View is at least partially visible, so remount it if unmounted
[self addSubview:view];
// Then test its subviews
if (CGRectContainsRect(clipRect, view.frame)) {
// View is fully visible, so remount all subviews
[view react_remountAllSubviews];
} else {
// View is partially visible, so update clipped subviews
[view react_updateClippedSubviewsWithClipRect:clipRect relativeToView:clipView];
}
} else if (view.superview) {
// View is completely outside the clipRect, so unmount it
[view removeFromSuperview];
}
}
}
- (void)setRemoveClippedSubviews:(BOOL)removeClippedSubviews
{
if (!removeClippedSubviews && _removeClippedSubviews) {
[self react_remountAllSubviews];
}
_removeClippedSubviews = removeClippedSubviews;
}
- (void)didUpdateReactSubviews
{
if (_removeClippedSubviews) {
[self updateClippedSubviews];
} else {
[super didUpdateReactSubviews];
}
}
- (void)updateClippedSubviews
{
// Find a suitable view to use for clipping
RCTPlatformView *clipView = [self react_findClipView]; // TODO(macOS GH#774)
if (clipView) {
[self react_updateClippedSubviewsWithClipRect:clipView.bounds relativeToView:clipView];
}
}
- (void)layoutSubviews
{
// TODO (#5906496): this a nasty performance drain, but necessary
// to prevent gaps appearing when the loading spinner disappears.
// We might be able to fix this another way by triggering a call
// to updateClippedSubviews manually after loading
[super layoutSubviews];
if (_removeClippedSubviews) {
[self updateClippedSubviews];
}
}
// [TODO(OSS Candidate ISS#2710739)
- (BOOL)becomeFirstResponder
{
if (![super becomeFirstResponder]) {
return NO;
}
// If we've gained focus, notify listeners
[_eventDispatcher sendEvent:[RCTFocusChangeEvent focusEventWithReactTag:self.reactTag]];
return YES;
}
- (BOOL)resignFirstResponder
{
if (![super resignFirstResponder]) {
return NO;
}
// If we've lost focus, notify listeners
[_eventDispatcher sendEvent:[RCTFocusChangeEvent blurEventWithReactTag:self.reactTag]];
return YES;
}
#if !TARGET_OS_OSX
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
{
[super traitCollectionDidChange:previousTraitCollection];
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
if (@available(iOS 13.0, *)) {
if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {
[self.layer setNeedsDisplay];
}
}
#endif
}
#endif // !TARGET_OS_OSX
// ]TODO(OSS Candidate ISS#2710739)
#pragma mark - Borders
- (RCTUIColor *)backgroundColor // TODO(OSS Candidate ISS#2710739) RCTUIColor
{
return _backgroundColor;
}
- (void)setBackgroundColor:(RCTUIColor *)backgroundColor // TODO(OSS Candidate ISS#2710739) RCTUIColor
{
if ([_backgroundColor isEqual:backgroundColor]) {
return;
}
_backgroundColor = backgroundColor;
[self.layer setNeedsDisplay];
}
static CGFloat RCTDefaultIfNegativeTo(CGFloat defaultValue, CGFloat x)
{
return x >= 0 ? x : defaultValue;
};