Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ @interface RCTEnhancedScrollView () <UIScrollViewDelegate>
@implementation RCTEnhancedScrollView {
__weak id<UIScrollViewDelegate> _publicDelegate;
BOOL _isSetContentOffsetDisabled;
UIEdgeInsets _safeAreaContentInsetAdjustment;
}

+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key
Expand Down Expand Up @@ -117,18 +118,14 @@ - (void)centerContentIfNeeded
- (void)setBaseContentInset:(UIEdgeInsets)baseContentInset
{
_baseContentInset = baseContentInset;
if (_padsContentForSafeArea) {
[self applyBaseContentInset];
} else {
self.contentInset = baseContentInset;
}
[self applyContentInsetPreservingExternalInsets:NO];
}

- (void)setHorizontal:(BOOL)horizontal
{
_horizontal = horizontal;
if (_padsContentForSafeArea) {
[self applyBaseContentInset];
[self applyContentInsetPreservingExternalInsets:YES];
}
}

Expand All @@ -138,37 +135,45 @@ - (void)setPadsContentForSafeArea:(BOOL)padsContentForSafeArea
return;
}
_padsContentForSafeArea = padsContentForSafeArea;
[self applyBaseContentInset];
[self applyContentInsetPreservingExternalInsets:YES];
}

- (void)adjustedContentInsetDidChange
{
[super adjustedContentInsetDidChange];
if (_padsContentForSafeArea) {
[self applyBaseContentInset];
[self applyContentInsetPreservingExternalInsets:YES];
}
}

/*
* Applies `baseContentInset`, less what UIKit adds across the scrollable axis for the safe area while the content is
* padded for it. `ScrollView` lays the content out clear of the system UI rather than scrolling it under; the
* content inset would apply the safe area a second time.
* Cancels UIKit's safe area inset across the scrollable axis while the content is padded for it. Only this
* correction is replaced; native controls can independently adjust the content inset.
*/
- (void)applyBaseContentInset
- (void)applyContentInsetPreservingExternalInsets:(BOOL)preserveExternalInsets
{
UIEdgeInsets contentInset = _baseContentInset;
UIEdgeInsets current = self.contentInset;
UIEdgeInsets adjustment = UIEdgeInsetsZero;
if (_padsContentForSafeArea) {
UIEdgeInsets adjusted = self.adjustedContentInset;
UIEdgeInsets current = self.contentInset;
if (_horizontal) {
contentInset.top -= adjusted.top - current.top;
contentInset.bottom -= adjusted.bottom - current.bottom;
adjustment.top = adjusted.top - current.top;
adjustment.bottom = adjusted.bottom - current.bottom;
} else {
contentInset.left -= adjusted.left - current.left;
contentInset.right -= adjusted.right - current.right;
adjustment.left = adjusted.left - current.left;
adjustment.right = adjusted.right - current.right;
}
}
if (UIEdgeInsetsEqualToEdgeInsets(contentInset, self.contentInset)) {
UIEdgeInsets inset = preserveExternalInsets ? current : _baseContentInset;
UIEdgeInsets previousAdjustment = preserveExternalInsets ? _safeAreaContentInsetAdjustment : UIEdgeInsetsZero;
UIEdgeInsets contentInset = UIEdgeInsetsMake(
inset.top + (previousAdjustment.top - adjustment.top),
inset.left + (previousAdjustment.left - adjustment.left),
inset.bottom + (previousAdjustment.bottom - adjustment.bottom),
inset.right + (previousAdjustment.right - adjustment.right));
// Updating contentInset can synchronously call adjustedContentInsetDidChange.
_safeAreaContentInsetAdjustment = adjustment;
if (UIEdgeInsetsEqualToEdgeInsets(contentInset, current)) {
return;
}
// Content pinned to the leading edge across the scrollable axis stays pinned, as UIKit keeps it when it changes the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

#import <React/RCTEnhancedScrollView.h>
#import <React/RCTScrollViewComponentView.h>
#import <XCTest/XCTest.h>
#import <react/renderer/components/scrollview/ScrollViewProps.h>
Expand All @@ -27,6 +28,38 @@ @interface RCTScrollViewComponentView (Tests)
- (void)_keyboardWillChangeFrame:(NSNotification *)notification;
@end

@interface RCTInsetTestScrollView : RCTEnhancedScrollView
@property (nonatomic, assign) UIEdgeInsets simulatedSafeAreaInsets;
@property (nonatomic, assign) BOOL notifiesInsetChangesSynchronously;
@property (nonatomic, assign) BOOL keepsContentOffsetOnInsetChange;
@end

@implementation RCTInsetTestScrollView

- (void)setContentInset:(UIEdgeInsets)contentInset
{
CGPoint offset = self.contentOffset;
[super setContentInset:contentInset];
if (_keepsContentOffsetOnInsetChange) {
self.contentOffset = offset;
}
if (_notifiesInsetChangesSynchronously) {
[self adjustedContentInsetDidChange];
}
}

- (UIEdgeInsets)adjustedContentInset
{
UIEdgeInsets inset = self.contentInset;
inset.top += _simulatedSafeAreaInsets.top;
inset.left += _simulatedSafeAreaInsets.left;
inset.bottom += _simulatedSafeAreaInsets.bottom;
inset.right += _simulatedSafeAreaInsets.right;
return inset;
}

@end

@interface RCTScrollViewComponentViewTests : XCTestCase
@end

Expand Down Expand Up @@ -61,6 +94,130 @@ - (void)testAutomaticallyAdjustKeyboardInsetsAcrossRecycling
XCTAssertEqual(view.scrollView.contentInset.bottom, 50);
}

// XCTest assertion macros use variadic reporting and catch framework exceptions by value.
// NOLINTBEGIN(cppcoreguidelines-pro-type-vararg, misc-throw-by-value-catch-by-reference)
- (void)testSafeAreaCorrectionPreservesPullToRefreshInset
{
RCTInsetTestScrollView *view = [RCTInsetTestScrollView new];
view.padsContentForSafeArea = YES;
view.contentInset = UIEdgeInsetsMake(60, 0, 0, 0);

[view adjustedContentInsetDidChange];
view.horizontal = NO;
[view adjustedContentInsetDidChange];
XCTAssertEqual(view.contentInset.top, 60);

UIEdgeInsets inset = view.contentInset;
inset.top -= 60;
view.contentInset = inset;
[view adjustedContentInsetDidChange];
XCTAssertEqual(view.contentInset.top, 0);
}

- (void)testSafeAreaCorrectionPreservesKeyboardInset
{
RCTInsetTestScrollView *view = [RCTInsetTestScrollView new];
view.baseContentInset = UIEdgeInsetsMake(10, 20, 30, 40);
view.simulatedSafeAreaInsets = UIEdgeInsetsMake(0, 15, 0, 25);
view.padsContentForSafeArea = YES;
UIEdgeInsets inset = view.contentInset;
inset.bottom += 300;
view.contentInset = inset;

[view adjustedContentInsetDidChange];
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(10, 5, 330, 15)));
[view adjustedContentInsetDidChange];
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(10, 5, 330, 15)));
}

- (void)testSafeAreaChangesPreserveExternalInsets
{
RCTInsetTestScrollView *view = [RCTInsetTestScrollView new];
view.baseContentInset = UIEdgeInsetsMake(10, 20, 30, 40);
view.simulatedSafeAreaInsets = UIEdgeInsetsMake(0, 15, 0, 25);
view.padsContentForSafeArea = YES;
UIEdgeInsets inset = view.contentInset;
inset.top += 60;
inset.left += 7;
view.contentInset = inset;

view.simulatedSafeAreaInsets = UIEdgeInsetsMake(0, 25, 0, 35);
[view adjustedContentInsetDidChange];
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(70, 2, 30, 5)));

view.padsContentForSafeArea = NO;
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(70, 27, 30, 40)));
}

- (void)testHorizontalSafeAreaCorrectionPreservesExternalInsets
{
RCTInsetTestScrollView *view = [RCTInsetTestScrollView new];
view.horizontal = YES;
view.baseContentInset = UIEdgeInsetsMake(10, 20, 30, 40);
view.simulatedSafeAreaInsets = UIEdgeInsetsMake(5, 0, 15, 0);
view.padsContentForSafeArea = YES;
UIEdgeInsets inset = view.contentInset;
inset.left += 60;
view.contentInset = inset;

[view adjustedContentInsetDidChange];
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(5, 80, 15, 40)));

view.horizontal = NO;
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(10, 80, 30, 40)));
}

- (void)testBaseInsetReplacementKeepsContentPinnedAcrossTheScrollAxis
{
RCTInsetTestScrollView *view = [RCTInsetTestScrollView new];
view.keepsContentOffsetOnInsetChange = YES;
view.simulatedSafeAreaInsets = UIEdgeInsetsMake(0, 15, 0, 25);
view.padsContentForSafeArea = YES;
view.contentOffset = CGPointZero;

view.baseContentInset = UIEdgeInsetsMake(10, 20, 30, 40);
XCTAssertEqual(view.contentOffset.x, -20);

view.contentOffset = CGPointMake(100, 0);
view.baseContentInset = UIEdgeInsetsMake(20, 30, 40, 50);
XCTAssertEqual(view.contentOffset.x, 100);
}

- (void)testSynchronousInsetCallbacksPreserveFractionalInsets
{
RCTInsetTestScrollView *view = [RCTInsetTestScrollView new];
view.notifiesInsetChangesSynchronously = YES;
view.simulatedSafeAreaInsets = UIEdgeInsetsMake(0, 15.5, 0, 25.25);
view.padsContentForSafeArea = YES;
view.contentInset = UIEdgeInsetsMake(60.5, -15.5, 0, -25.25);

[view adjustedContentInsetDidChange];
[view adjustedContentInsetDidChange];
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(60.5, -15.5, 0, -25.25)));

view.baseContentInset = UIEdgeInsetsZero;
view.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
view.padsContentForSafeArea = NO;
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsZero));

view.baseContentInset = UIEdgeInsetsMake(1.25, 2.5, 3.75, 4.5);
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(1.25, 2.5, 3.75, 4.5)));
}

- (void)testBaseContentInsetReplacesPreviousInsetWithoutAccumulatingSafeAreaCorrection
{
RCTInsetTestScrollView *view = [RCTInsetTestScrollView new];
view.simulatedSafeAreaInsets = UIEdgeInsetsMake(0, 15, 0, 25);
view.padsContentForSafeArea = YES;
view.baseContentInset = UIEdgeInsetsMake(10, 20, 30, 40);
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(10, 5, 30, 15)));

view.baseContentInset = UIEdgeInsetsMake(20, 30, 40, 50);
[view adjustedContentInsetDidChange];
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(view.contentInset, UIEdgeInsetsMake(20, 15, 40, 25)));
}
// NOLINTEND(cppcoreguidelines-pro-type-vararg, misc-throw-by-value-catch-by-reference)

@end

#endif
Loading