forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathRCTScrollView.m
More file actions
1563 lines (1371 loc) · 57.2 KB
/
RCTScrollView.m
File metadata and controls
1563 lines (1371 loc) · 57.2 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 "RCTScrollView.h"
#import <React/RCTUIKit.h> // [macOS]
#import "RCTConvert.h"
#import "RCTHandledKey.h" // [macOS]
#import "RCTLog.h"
#import "RCTScrollEvent.h"
#import "RCTUIManager.h"
#import "RCTUIManagerObserverCoordinator.h"
#import "RCTUIManagerUtils.h"
#import "RCTUtils.h"
#import "RCTViewUtils.h"
#import "UIView+Private.h"
#import "UIView+React.h"
#if !TARGET_OS_OSX // [macOS]
#import "RCTRefreshControl.h"
#else // [macOS
#import "RCTI18nUtil.h"
#import "RCTViewKeyboardEvent.h"
#endif // macOS]
/**
* Include a custom scroll view subclass because we want to limit certain
* default UIKit behaviors such as textFields automatically scrolling
* scroll views that contain them.
*/
@interface RCTCustomScrollView :
#if !TARGET_OS_OSX // [macOS]
UIScrollView <UIGestureRecognizerDelegate>
#else // [macOS
RCTUIScrollView
#endif // macOS]
@property (nonatomic, assign) BOOL centerContent;
#if !TARGET_OS_OSX // [macOS]
@property (nonatomic, strong) UIView<RCTCustomRefreshControlProtocol> *customRefreshControl;
@property (nonatomic, assign) BOOL pinchGestureEnabled;
#else // [macOS
+ (BOOL)isCompatibleWithResponsiveScrolling;
@property (nonatomic, assign, getter=isInverted) BOOL inverted;
@property (nonatomic, assign, getter=isScrollEnabled) BOOL scrollEnabled;
@property (nonatomic, strong) NSPanGestureRecognizer *panGestureRecognizer;
#endif // macOS]
@end
@implementation RCTCustomScrollView
- (instancetype)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
#if !TARGET_OS_OSX // [macOS]
[self.panGestureRecognizer addTarget:self action:@selector(handleCustomPan:)];
if ([self respondsToSelector:@selector(setSemanticContentAttribute:)]) {
// We intentionally force `UIScrollView`s `semanticContentAttribute` to `LTR` here
// because this attribute affects a position of vertical scrollbar; we don't want this
// scrollbar flip because we also flip it with whole `UIScrollView` flip.
self.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
}
#else // [macOS
self.scrollEnabled = YES;
self.hasHorizontalScroller = YES;
self.hasVerticalScroller = YES;
self.autohidesScrollers = YES;
self.panGestureRecognizer = [[NSPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleCustomPan:)];
#endif // macOS]
#if !TARGET_OS_OSX // [macOS]
_pinchGestureEnabled = YES;
#endif // [macOS]
}
return self;
}
#if !TARGET_OS_OSX // [macOS] NSScrollView's contentView is an NSClipView. Use documentView to access NSScrollView's content.
- (UIView *)contentView
{
return ((RCTScrollView *)self.superview).contentView;
}
#endif // [macOS]
/**
* @return Whether or not the scroll view interaction should be blocked because
* JS was found to be the responder.
*/
- (BOOL)_shouldDisableScrollInteraction
{
// Since this may be called on every pan, we need to make sure to only climb
// the hierarchy on rare occasions.
RCTPlatformView *JSResponder = [RCTUIManager JSResponder]; // [macOS]
if (JSResponder && JSResponder != self.superview) {
BOOL superviewHasResponder = RCTUIViewIsDescendantOfView(self, JSResponder); // [macOS]
return superviewHasResponder;
}
return NO;
}
#if TARGET_OS_OSX // [macOS
+ (BOOL)isCompatibleWithResponsiveScrolling
{
return YES;
}
- (BOOL)isFlipped
{
return !self.inverted;
}
- (void)scrollWheel:(NSEvent *)theEvent
{
if (!self.isScrollEnabled) {
[[self nextResponder] scrollWheel:theEvent];
return;
}
[super scrollWheel:theEvent];
}
#endif // macOS]
- (void)handleCustomPan:(__unused UIGestureRecognizer *)sender // [macOS]
{
if ([self _shouldDisableScrollInteraction] && ![[RCTUIManager JSResponder] isKindOfClass:[RCTScrollView class]]) {
self.panGestureRecognizer.enabled = NO;
self.panGestureRecognizer.enabled = YES;
// TODO: If mid bounce, animate the scroll view to a non-bounced position
// while disabling (but only if `stopScrollInteractionIfJSHasResponder` was
// called *during* a `pan`). Currently, it will just snap into place which
// is not so bad either.
// Another approach:
// self.scrollEnabled = NO;
// self.scrollEnabled = YES;
}
}
- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated
{
// Limiting scroll area to an area where we actually have content.
CGSize contentSize = self.contentSize;
UIEdgeInsets contentInset = self.contentInset;
CGSize fullSize = CGSizeMake(
contentSize.width + contentInset.left + contentInset.right,
contentSize.height + contentInset.top + contentInset.bottom);
rect = CGRectIntersection((CGRect){CGPointZero, fullSize}, rect);
if (CGRectIsNull(rect)) {
return;
}
#if !TARGET_OS_OSX // [macOS]
[super scrollRectToVisible:rect animated:animated];
#else // [macOS
[super scrollRectToVisible:rect];
#endif // macOS]
}
/**
* Returning `YES` cancels touches for the "inner" `view` and causes a scroll.
* Returning `NO` causes touches to be directed to that inner view and prevents
* the scroll view from scrolling.
*
* `YES` -> Allows scrolling.
* `NO` -> Doesn't allow scrolling.
*
* By default this returns NO for all views that are UIControls and YES for
* everything else. What that does is allows scroll views to scroll even when a
* touch started inside of a `UIControl` (`UIButton` etc). For React scroll
* views, we want the default to be the same behavior as `UIControl`s so we
* return `YES` by default. But there's one case where we want to block the
* scrolling no matter what: When JS believes it has its own responder lock on
* a view that is *above* the scroll view in the hierarchy. So we abuse this
* `touchesShouldCancelInContentView` API in order to stop the scroll view from
* scrolling in this case.
*
* We are not aware of *any* other solution to the problem because alternative
* approaches require that we disable the scrollview *before* touches begin or
* move. This approach (`touchesShouldCancelInContentView`) works even if the
* JS responder is set after touches start/move because
* `touchesShouldCancelInContentView` is called as soon as the scroll view has
* been touched and dragged *just* far enough to decide to begin the "drag"
* movement of the scroll interaction. Returning `NO`, will cause the drag
* operation to fail.
*
* `touchesShouldCancelInContentView` will stop the *initialization* of a
* scroll pan gesture and most of the time this is sufficient. On rare
* occasion, the scroll gesture would have already initialized right before JS
* notifies native of the JS responder being set. In order to recover from that
* timing issue we have a fallback that kills any ongoing pan gesture that
* occurs when native is notified of a JS responder.
*
* Note: Explicitly returning `YES`, instead of relying on the default fixes
* (at least) one bug where if you have a UIControl inside a UIScrollView and
* tap on the UIControl and then start dragging (to scroll), it won't scroll.
* Chat with @andras for more details.
*
* In order to have this called, you must have delaysContentTouches set to NO
* (which is the not the `UIKit` default).
*/
- (BOOL)touchesShouldCancelInContentView:(__unused RCTUIView *)view // [macOS]
{
BOOL shouldDisableScrollInteraction = [self _shouldDisableScrollInteraction];
#if !TARGET_OS_OSX // [macOS]
if (shouldDisableScrollInteraction == NO) {
[super touchesShouldCancelInContentView:view];
}
#endif // [macOS]
return !shouldDisableScrollInteraction;
}
- (void)setContentOffset:(CGPoint)contentOffset
{
super.contentOffset = CGPointMake(
RCTSanitizeNaNValue(contentOffset.x, @"scrollView.contentOffset.x"),
RCTSanitizeNaNValue(contentOffset.y, @"scrollView.contentOffset.y"));
}
- (void)setFrame:(CGRect)frame
{
// Preserving and revalidating `contentOffset`.
CGPoint originalOffset = self.contentOffset;
[super setFrame:frame];
UIEdgeInsets contentInset = self.contentInset;
CGSize contentSize = self.contentSize;
// If contentSize has not been measured yet we can't check bounds.
if (CGSizeEqualToSize(contentSize, CGSizeZero)) {
self.contentOffset = originalOffset;
} else {
#if !TARGET_OS_OSX // [macOS]
if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, self.adjustedContentInset)) {
contentInset = self.adjustedContentInset;
}
#endif // [macOS]
CGSize boundsSize = self.bounds.size;
CGFloat xMaxOffset = contentSize.width - boundsSize.width + contentInset.right;
CGFloat yMaxOffset = contentSize.height - boundsSize.height + contentInset.bottom;
// Make sure offset doesn't exceed bounds. This can happen on screen rotation.
if ((originalOffset.x >= -contentInset.left) && (originalOffset.x <= xMaxOffset) &&
(originalOffset.y >= -contentInset.top) && (originalOffset.y <= yMaxOffset)) {
return;
}
self.contentOffset = CGPointMake(
MAX(-contentInset.left, MIN(xMaxOffset, originalOffset.x)),
MAX(-contentInset.top, MIN(yMaxOffset, originalOffset.y)));
}
}
#if !TARGET_OS_OSX // [macOS]
- (void)setCustomRefreshControl:(UIView<RCTCustomRefreshControlProtocol> *)refreshControl
{
if (_customRefreshControl) {
[_customRefreshControl removeFromSuperview];
}
_customRefreshControl = refreshControl;
// We have to set this because we can't always guarantee the
// `RCTCustomRefreshControlProtocol`'s superview will always be of class
// `UIScrollView` like we were previously
if ([_customRefreshControl respondsToSelector:@selector(setScrollView:)]) {
_customRefreshControl.scrollView = self;
}
if ([refreshControl isKindOfClass:UIRefreshControl.class]) {
self.refreshControl = (UIRefreshControl *)refreshControl;
} else {
[self addSubview:_customRefreshControl];
}
}
- (void)setPinchGestureEnabled:(BOOL)pinchGestureEnabled
{
self.pinchGestureRecognizer.enabled = pinchGestureEnabled;
_pinchGestureEnabled = pinchGestureEnabled;
}
- (void)didMoveToWindow
{
[super didMoveToWindow];
// ScrollView enables pinch gesture late in its lifecycle. So simply setting it
// in the setter gets overridden when the view loads.
self.pinchGestureRecognizer.enabled = _pinchGestureEnabled;
}
#endif // [macOS]
#if TARGET_OS_OSX // [macOS
- (void)setAccessibilityLabel:(NSString *)accessibilityLabel
{
[super setAccessibilityLabel:accessibilityLabel];
[[self documentView] setAccessibilityLabel:accessibilityLabel];
}
- (void)setDocumentView:(__kindof NSView *)documentView
{
[super setDocumentView:documentView];
[documentView setAccessibilityLabel:[self accessibilityLabel]];
}
#endif // macOS]
- (BOOL)shouldGroupAccessibilityChildren
{
return YES;
}
@end
@interface RCTScrollView () <RCTUIManagerObserver>
@end
@implementation RCTScrollView {
id<RCTEventDispatcherProtocol> _eventDispatcher;
CGRect _prevFirstVisibleFrame;
__weak RCTUIView *_firstVisibleView; // [macOS]
RCTCustomScrollView *_scrollView;
#if !TARGET_OS_OSX // [macOS]
UIView *_contentView;
#endif // [macOS]
NSTimeInterval _lastScrollDispatchTime;
NSMutableArray<NSValue *> *_cachedChildFrames;
BOOL _allowNextScrollNoMatterWhat;
#if TARGET_OS_OSX // [macOS
BOOL _notifyDidScroll;
BOOL _disableScrollEvents;
NSPoint _lastScrollPosition;
#endif // macOS]
CGRect _lastClippedToRect;
uint16_t _coalescingKey;
NSString *_lastEmittedEventName;
NSHashTable *_scrollListeners;
}
#if !TARGET_OS_OSX // [macOS] UIKeyboard notifications not needed on macOS
- (void)_registerKeyboardListener
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_keyboardWillChangeFrame:)
name:UIKeyboardWillChangeFrameNotification
object:nil];
}
- (void)_unregisterKeyboardListener
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
}
static inline UIViewAnimationOptions animationOptionsWithCurve(UIViewAnimationCurve curve)
{
// UIViewAnimationCurve #7 is used for keyboard and therefore private - so we can't use switch/case here.
// source: https://stackoverflow.com/a/7327374/5281431
RCTAssert(
UIViewAnimationCurveLinear << 16 == UIViewAnimationOptionCurveLinear,
@"Unexpected implementation of UIViewAnimationCurve");
return curve << 16;
}
- (void)_keyboardWillChangeFrame:(NSNotification *)notification
{
if (![self automaticallyAdjustKeyboardInsets]) {
return;
}
if ([self isHorizontal:_scrollView]) {
return;
}
double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve =
(UIViewAnimationCurve)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue];
CGRect beginFrame = [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGPoint absoluteViewOrigin = [self convertPoint:self.bounds.origin toView:nil];
CGFloat scrollViewLowerY = self.inverted ? absoluteViewOrigin.y : absoluteViewOrigin.y + self.bounds.size.height;
UIEdgeInsets newEdgeInsets = _scrollView.contentInset;
CGFloat inset = MAX(scrollViewLowerY - endFrame.origin.y, 0);
if (self.inverted) {
newEdgeInsets.top = MAX(inset, _contentInset.top);
} else {
newEdgeInsets.bottom = MAX(inset, _contentInset.bottom);
}
CGPoint newContentOffset = _scrollView.contentOffset;
self.firstResponderFocus = CGRectNull;
CGFloat contentDiff = 0;
if ([[UIApplication sharedApplication] sendAction:@selector(reactUpdateResponderOffsetForScrollView:)
to:nil
from:self
forEvent:nil]) {
// Inner text field focused
CGFloat focusEnd = CGRectGetMaxY(self.firstResponderFocus);
BOOL didFocusExternalTextField = focusEnd == INFINITY;
if (!didFocusExternalTextField && focusEnd > endFrame.origin.y) {
// Text field active region is below visible area with keyboard - update diff to bring into view
contentDiff = endFrame.origin.y - focusEnd;
} else {
#if !TARGET_OS_VISION
UIView *inputAccessoryView = _firstResponderViewOutsideScrollView.inputAccessoryView;
if (inputAccessoryView) {
// Text input view is within the inputAccessoryView.
contentDiff = endFrame.origin.y - beginFrame.origin.y;
}
#endif // !TARGET_OS_VISION
}
} else if (endFrame.origin.y <= beginFrame.origin.y) {
// Keyboard opened for other reason
contentDiff = endFrame.origin.y - beginFrame.origin.y;
}
if (self.inverted) {
newContentOffset.y += contentDiff;
} else {
newContentOffset.y -= contentDiff;
}
if (@available(iOS 14.0, *)) {
// On iOS when Prefer Cross-Fade Transitions is enabled, the keyboard position
// & height is reported differently (0 instead of Y position value matching height of frame)
// Fixes similar issue we saw with https://github.com/facebook/react-native/pull/34503
if (UIAccessibilityPrefersCrossFadeTransitions() && endFrame.size.height == 0) {
newContentOffset.y = 0;
newEdgeInsets.bottom = 0;
}
}
[UIView animateWithDuration:duration
delay:0.0
options:animationOptionsWithCurve(curve)
animations:^{
self->_scrollView.contentInset = newEdgeInsets;
self->_scrollView.verticalScrollIndicatorInsets = newEdgeInsets;
[self scrollToOffset:newContentOffset animated:NO];
}
completion:nil];
}
#endif // [macOS]
- (instancetype)initWithEventDispatcher:(id<RCTEventDispatcherProtocol>)eventDispatcher
{
RCTAssertParam(eventDispatcher);
if ((self = [super initWithFrame:CGRectZero])) {
#if !TARGET_OS_OSX // [macOS]
[self _registerKeyboardListener];
#endif // [macOS]
_eventDispatcher = eventDispatcher;
_scrollView = [[RCTCustomScrollView alloc] initWithFrame:CGRectZero];
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
#if !TARGET_OS_OSX // [macOS]
_scrollView.delegate = self;
_scrollView.delaysContentTouches = NO;
#else // [macOS
_scrollView.postsBoundsChangedNotifications = YES;
_lastScrollPosition = NSZeroPoint;
#endif // macOS]
#if !TARGET_OS_OSX // [macOS]
// We set the default behavior to "never" so that iOS
// doesn't do weird things to UIScrollView insets automatically
// and keeps it as an opt-in behavior.
_scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
#endif // [macOS]
_automaticallyAdjustContentInsets = YES;
_contentInset = UIEdgeInsetsZero;
_lastClippedToRect = CGRectNull;
_scrollEventThrottle = 0.0;
_lastScrollDispatchTime = 0;
_cachedChildFrames = [NSMutableArray new];
_scrollListeners = [NSHashTable weakObjectsHashTable];
[self addSubview:_scrollView];
}
return self;
}
#if TARGET_OS_OSX // [macOS
- (BOOL)canBecomeKeyView
{
return [self focusable];
}
- (CGRect)focusRingMaskBounds
{
return [self bounds];
}
- (void)drawFocusRingMask
{
if (self.enableFocusRing) {
NSBezierPath *borderPath = [NSBezierPath bezierPathWithRoundedRect:self.bounds xRadius:2.0 yRadius:2.0];
[borderPath stroke];
}
}
- (RCTBridge *)bridge
{
return [_eventDispatcher bridge];
}
- (RCTUIView *)contentView // [macOS]
{
return _scrollView.documentView;
}
- (void)setAccessibilityLabel:(NSString *)accessibilityLabel
{
[_scrollView setAccessibilityLabel:accessibilityLabel];
}
- (void)setAccessibilityRole:(NSAccessibilityRole)accessibilityRole
{
[_scrollView setAccessibilityRole:accessibilityRole];
}
- (void)setInverted:(BOOL)inverted
{
BOOL changed = _inverted != inverted;
_inverted = inverted;
if (changed && _onInvertedDidChange) {
_onInvertedDidChange(@{});
}
}
- (void)setHasOverlayStyleIndicator:(BOOL)hasOverlayStyle
{
if (hasOverlayStyle == true) {
self.scrollView.scrollerStyle = NSScrollerStyleOverlay;
} else {
self.scrollView.scrollerStyle = NSScrollerStyleLegacy;
}
}
#endif // macOS]
RCT_NOT_IMPLEMENTED(-(instancetype)initWithFrame : (CGRect)frame)
RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)aDecoder)
static inline void RCTApplyTransformationAccordingLayoutDirection(
RCTPlatformView *view, // [macOS]
UIUserInterfaceLayoutDirection layoutDirection)
{
#if !TARGET_OS_OSX // [macOS]
view.transform = layoutDirection == UIUserInterfaceLayoutDirectionLeftToRight ? CGAffineTransformIdentity
: CGAffineTransformMakeScale(-1, 1);
#endif // [macOS]
}
- (void)setReactLayoutDirection:(UIUserInterfaceLayoutDirection)layoutDirection
{
[super setReactLayoutDirection:layoutDirection];
RCTApplyTransformationAccordingLayoutDirection(_scrollView, layoutDirection);
RCTApplyTransformationAccordingLayoutDirection(self.contentView, layoutDirection); // macOS use property instead of ivar for mac
}
- (void)setRemoveClippedSubviews:(__unused BOOL)removeClippedSubviews
{
// Does nothing
}
- (void)setFrame:(CGRect)frame
{
#if !TARGET_OS_OSX // [macOS]
[super setFrame:frame];
#else // [macOS
/**
* Setting the frame on the scroll view will randomly generate between 0 and 4 scroll events. These events happen
* during the layout phase of the view which generates layout notifications that are sent through the bridge.
* Because the bridge is heavily used, the scroll events are throttled and reach the JS thread with a random delay.
* Because the scroll event stores the clip and content view size, delayed scroll events will submit stale layout
* information that can break virtual list implemenations.
* By disabling scroll events during the execution of the setFrame method and scheduling one notification on
* the next run loop, we can mitigate the delayed scroll event by sending it at a time where the bridge is not busy.
*/
_disableScrollEvents = YES;
[super setFrame:frame];
_disableScrollEvents = NO;
if (self.window != nil && !self.window.inLiveResize) {
[self performSelector:@selector(scrollViewDocumentViewBoundsDidChange:) withObject:nil afterDelay:0];
}
#endif // macOS]
[self centerContentIfNeeded];
}
- (void)insertReactSubview:(RCTPlatformView *)view atIndex:(NSInteger)atIndex // [macOS]
{
[super insertReactSubview:view atIndex:atIndex];
#if !TARGET_OS_OSX // [macOS]
if ([view conformsToProtocol:@protocol(RCTCustomRefreshControlProtocol)]) {
[_scrollView setCustomRefreshControl:(UIView<RCTCustomRefreshControlProtocol> *)view];
if (![view isKindOfClass:[UIRefreshControl class]] && [view conformsToProtocol:@protocol(UIScrollViewDelegate)]) {
[self addScrollListener:(UIView<UIScrollViewDelegate> *)view];
}
} else {
RCTAssert(
_contentView == nil,
@"RCTScrollView may only contain a single subview, the already set subview looks like: %@",
[_contentView react_recursiveDescription]);
_contentView = view;
RCTApplyTransformationAccordingLayoutDirection(_contentView, self.reactLayoutDirection);
[_scrollView addSubview:view];
}
#else // [macOS
RCTAssert(self.contentView == nil, @"RCTScrollView may only contain a single subview");
_scrollView.documentView = view;
#endif // macOS]
[self centerContentIfNeeded];
}
- (void)removeReactSubview:(RCTUIView *)subview // [macOS]
{
[super removeReactSubview:subview];
#if TARGET_OS_OSX // [macOS
_scrollView.documentView = nil;
#else // [macOS
if ([subview conformsToProtocol:@protocol(RCTCustomRefreshControlProtocol)]) {
[_scrollView setCustomRefreshControl:nil];
if (![subview isKindOfClass:[UIRefreshControl class]] &&
[subview conformsToProtocol:@protocol(UIScrollViewDelegate)]) {
[self removeScrollListener:(UIView<UIScrollViewDelegate> *)subview];
}
} else {
RCTAssert(_contentView == subview, @"Attempted to remove non-existent subview");
_contentView = nil;
}
#endif // macOS]
}
- (void)didUpdateReactSubviews
{
// Do nothing, as subviews are managed by `insertReactSubview:atIndex:`
}
- (void)didSetProps:(NSArray<NSString *> *)changedProps
{
if ([changedProps containsObject:@"contentSize"]) {
[self updateContentSizeIfNeeded];
}
}
- (BOOL)centerContent
{
return _scrollView.centerContent;
}
- (void)setCenterContent:(BOOL)centerContent
{
_scrollView.centerContent = centerContent;
}
- (void)setClipsToBounds:(BOOL)clipsToBounds
{
super.clipsToBounds = clipsToBounds;
#if !TARGET_OS_OSX // [macOS]
_scrollView.clipsToBounds = clipsToBounds;
#endif // [macOS]
}
- (void)dealloc
{
#if !TARGET_OS_OSX // [macOS]
_scrollView.delegate = nil;
#endif // [macOS]
[_eventDispatcher.bridge.uiManager.observerCoordinator removeObserver:self];
#if !TARGET_OS_OSX // [macOS]
[self _unregisterKeyboardListener];
#endif // [macOS]
}
- (void)layoutSubviews
{
[super layoutSubviews];
RCTAssert(self.subviews.count == 1, @"we should only have exactly one subview");
RCTAssert([self.subviews lastObject] == _scrollView, @"our only subview should be a scrollview");
#if !TARGET_OS_TV && !TARGET_OS_OSX // [macOS]
// Adjust the refresh control frame if the scrollview layout changes.
UIView<RCTCustomRefreshControlProtocol> *refreshControl = _scrollView.customRefreshControl;
if (refreshControl && refreshControl.isRefreshing && ![refreshControl isKindOfClass:UIRefreshControl.class]) {
refreshControl.frame =
(CGRect){_scrollView.contentOffset, {_scrollView.frame.size.width, refreshControl.frame.size.height}};
}
#endif
[self updateClippedSubviews];
}
- (void)updateClippedSubviews
{
// Find a suitable view to use for clipping
RCTPlatformView *clipView = [self react_findClipView]; // [macOS]
if (!clipView) {
return;
}
static const CGFloat leeway = 1.0;
const CGSize contentSize = _scrollView.contentSize;
#if !TARGET_OS_OSX // [macOS]
const CGRect bounds = _scrollView.bounds;
#else // [macOS
const CGRect bounds = _scrollView.contentView.bounds;
#endif // macOS]
const BOOL scrollsHorizontally = contentSize.width > bounds.size.width;
const BOOL scrollsVertically = contentSize.height > bounds.size.height;
const BOOL shouldClipAgain = CGRectIsNull(_lastClippedToRect) || !CGRectEqualToRect(_lastClippedToRect, bounds) ||
(scrollsHorizontally &&
(bounds.size.width < leeway || fabs(_lastClippedToRect.origin.x - bounds.origin.x) >= leeway)) ||
(scrollsVertically &&
(bounds.size.height < leeway || fabs(_lastClippedToRect.origin.y - bounds.origin.y) >= leeway));
if (shouldClipAgain) {
const CGRect clipRect = CGRectInset(clipView.bounds, -leeway, -leeway);
[self react_updateClippedSubviewsWithClipRect:clipRect relativeToView:clipView];
_lastClippedToRect = bounds;
}
}
#if TARGET_OS_OSX // [macOS
- (void)viewDidMoveToWindow
{
[super viewDidMoveToWindow];
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
if ([self window] == nil) {
// Unregister for bounds change notifications
[defaultCenter removeObserver:self
name:NSViewBoundsDidChangeNotification
object:_scrollView.contentView];
[defaultCenter removeObserver:self
name:NSPreferredScrollerStyleDidChangeNotification
object:nil];
} else {
// Register for bounds change notifications so we can track scrolling
[defaultCenter addObserver:self
selector:@selector(scrollViewDocumentViewBoundsDidChange:)
name:NSViewBoundsDidChangeNotification
object:_scrollView.contentView]; // NSClipView
[defaultCenter addObserver:self
selector:@selector(preferredScrollerStyleDidChange:)
name:NSPreferredScrollerStyleDidChangeNotification
object:nil];
}
_notifyDidScroll = ([self window] != nil);
}
#endif // macOS]
- (void)setContentInset:(UIEdgeInsets)contentInset
{
if (UIEdgeInsetsEqualToEdgeInsets(contentInset, _contentInset)) {
return;
}
CGPoint contentOffset = _scrollView.contentOffset;
_contentInset = contentInset;
[RCTView autoAdjustInsetsForView:self withScrollView:_scrollView updateOffset:NO];
_scrollView.contentOffset = contentOffset;
}
#if !TARGET_OS_OSX // [macOS]
- (BOOL)isHorizontal:(UIScrollView *)scrollView
#else // [macOS
- (BOOL)isHorizontal:(RCTCustomScrollView *)scrollView
#endif // macOS]
{
return scrollView.contentSize.width > self.frame.size.width;
}
#if TARGET_OS_OSX // [macOS
- (BOOL)isVertical:(RCTCustomScrollView *)scrollView
{
return scrollView.contentSize.height > self.frame.size.height;
}
#endif // macOS]
- (void)scrollToOffset:(CGPoint)offset
{
[self scrollToOffset:offset animated:YES];
}
- (void)scrollToOffset:(CGPoint)offset animated:(BOOL)animated
{
if ([self reactLayoutDirection] == UIUserInterfaceLayoutDirectionRightToLeft) {
offset.x = _scrollView.contentSize.width - _scrollView.frame.size.width - offset.x;
}
if (!CGPointEqualToPoint(_scrollView.contentOffset, offset)) {
CGRect maxRect = CGRectMake(
fmin(-_scrollView.contentInset.left, 0),
fmin(-_scrollView.contentInset.top, 0),
fmax(
_scrollView.contentSize.width - _scrollView.bounds.size.width + _scrollView.contentInset.right +
fmax(_scrollView.contentInset.left, 0),
0.01),
fmax(
_scrollView.contentSize.height - _scrollView.bounds.size.height + _scrollView.contentInset.bottom +
fmax(_scrollView.contentInset.top, 0),
0.01)); // Make width and height greater than 0
// Ensure at least one scroll event will fire
_allowNextScrollNoMatterWhat = YES;
if (!CGRectContainsPoint(maxRect, offset) && !self.scrollToOverflowEnabled) {
CGFloat x = fmax(offset.x, CGRectGetMinX(maxRect));
x = fmin(x, CGRectGetMaxX(maxRect));
CGFloat y = fmax(offset.y, CGRectGetMinY(maxRect));
y = fmin(y, CGRectGetMaxY(maxRect));
offset = CGPointMake(x, y);
}
[_scrollView setContentOffset:offset animated:animated];
}
}
/**
* If this is a vertical scroll view, scrolls to the bottom.
* If this is a horizontal scroll view, scrolls to the right.
*/
- (void)scrollToEnd:(BOOL)animated
{
BOOL isHorizontal = [self isHorizontal:_scrollView];
#if !TARGET_OS_OSX // [macOS
CGSize boundsSize = _scrollView.bounds.size;
#else
CGSize boundsSize = _scrollView.contentView.bounds.size;
#endif // macOS]
CGPoint offset;
if (isHorizontal) {
CGFloat offsetX = _scrollView.contentSize.width - boundsSize.width + _scrollView.contentInset.right; // [macOS]
offset = CGPointMake(fmax(offsetX, 0), 0);
} else {
CGFloat offsetY = _scrollView.contentSize.height - boundsSize.height + _scrollView.contentInset.bottom; // [macOS]
offset = CGPointMake(0, fmax(offsetY, 0));
}
if (!CGPointEqualToPoint(_scrollView.contentOffset, offset)) {
// Ensure at least one scroll event will fire
_allowNextScrollNoMatterWhat = YES;
[_scrollView setContentOffset:offset animated:animated];
}
}
- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated
{
#if TARGET_OS_OSX // [macOS
(void) animated;
[_scrollView magnifyToFitRect:rect];
#else // [macOS
[_scrollView zoomToRect:rect animated:animated];
#endif // macOS]
}
- (void)refreshContentInset
{
[RCTView autoAdjustInsetsForView:self withScrollView:_scrollView updateOffset:YES];
}
// [macOS
- (void)flashScrollIndicators
{
#if !TARGET_OS_OSX
[_scrollView flashScrollIndicators];
#else
[_scrollView flashScrollers];
#endif
}
// macOS]
#pragma mark - ScrollView delegate
#if TARGET_OS_OSX // [macOS
- (void)scrollViewDocumentViewBoundsDidChange:(__unused NSNotification *)notification
{
if (_disableScrollEvents) {
return;
}
if (_scrollView.centerContent) {
// contentOffset setter dynamically centers content when _centerContent == YES
[_scrollView setContentOffset:_scrollView.contentOffset];
}
if (_notifyDidScroll) {
[self scrollViewDidScroll:_scrollView];
}
}
#endif // macOS]
#define RCT_SEND_SCROLL_EVENT(_eventName, _userData) \
{ \
NSString *eventName = NSStringFromSelector(@selector(_eventName)); \
[self sendScrollEventWithName:eventName scrollView:_scrollView userData:_userData]; \
}
#define RCT_FORWARD_SCROLL_EVENT(call) \
for (NSObject<UIScrollViewDelegate> * scrollViewListener in _scrollListeners) { \
if ([scrollViewListener respondsToSelector:_cmd]) { \
[scrollViewListener call]; \
} \
}
#define RCT_SCROLL_EVENT_HANDLER(delegateMethod, eventName) \
-(void)delegateMethod : (UIScrollView *)scrollView \
{ \
RCT_SEND_SCROLL_EVENT(eventName, nil); \
RCT_FORWARD_SCROLL_EVENT(delegateMethod : scrollView); \
}
#if !TARGET_OS_OSX // [macOS]
RCT_SCROLL_EVENT_HANDLER(scrollViewWillBeginDecelerating, onMomentumScrollBegin)
RCT_SCROLL_EVENT_HANDLER(scrollViewDidScrollToTop, onScrollToTop)
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
[self centerContentIfNeeded];
RCT_SEND_SCROLL_EVENT(onScroll, nil);
RCT_FORWARD_SCROLL_EVENT(scrollViewDidZoom : scrollView);
}
#endif // macOS]
/*
* Automatically centers the content such that if the content is smaller than the
* ScrollView, we force it to be centered, but when you zoom or the content otherwise
* becomes larger than the ScrollView, there is no padding around the content but it
* can still fill the whole view.
* This implementation is based on https://petersteinberger.com/blog/2013/how-to-center-uiscrollview/.
*/
- (void)centerContentIfNeeded
{
if (!_scrollView.centerContent) {
return;
}
CGSize contentSize = self.contentSize;
CGSize boundsSize = self.bounds.size;
if (CGSizeEqualToSize(contentSize, CGSizeZero) || CGSizeEqualToSize(boundsSize, CGSizeZero)) {
return;
}
CGFloat top = 0, left = 0;
if (contentSize.width < boundsSize.width) {
left = (boundsSize.width - contentSize.width) * 0.5f;
}
if (contentSize.height < boundsSize.height) {
top = (boundsSize.height - contentSize.height) * 0.5f;
}
_scrollView.contentInset = UIEdgeInsetsMake(top, left, top, left);
}
#if !TARGET_OS_OSX // [macOS]
- (void)addScrollListener:(NSObject<UIScrollViewDelegate> *)scrollListener
{
[_scrollListeners addObject:scrollListener];
}
- (void)removeScrollListener:(NSObject<UIScrollViewDelegate> *)scrollListener
{
[_scrollListeners removeObject:scrollListener];
}
#endif // [macOS]
- (void)scrollViewDidScroll:(RCTCustomScrollView *)scrollView // [macOS]
{
NSTimeInterval now = CACurrentMediaTime();
[self updateClippedSubviews];
#if TARGET_OS_OSX // [macOS
/**
* To check for effective scroll position changes, the comparison with lastScrollPosition should happen
* after updateClippedSubviews. updateClippedSubviews will update the display of the vertical/horizontal
* scrollers which can change the clipview bounds.
* This change also ensures that no onScroll events are sent when the React setFrame call is running,
* which could submit onScroll events while the content view was not setup yet.
*/
BOOL didScroll = !NSEqualPoints(scrollView.contentView.bounds.origin, _lastScrollPosition);
if (!didScroll) {
return;
}
_lastScrollPosition = scrollView.contentView.bounds.origin;
#endif // macOS]
/**