Skip to content
Open
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 @@ -1112,6 +1112,12 @@ - (void)_remountChildren
return;
}

// Only views with `removeClippedSubviews` act on the pass below. Without any, it visits every
// view in the content and changes nothing, after each mounting transaction.
if (![RCTViewComponentView anyViewRemovesClippedSubviews]) {
return;
}

[_scrollView updateClippedSubviewsWithClipRect:CGRectInset(_scrollView.bounds, -kClippingLeeway, -kClippingLeeway)
relativeToView:_scrollView];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (NSString *)componentViewName_DO_NOT_USE_THIS_IS_BROKEN;

/*
* Returns `YES` when at least one view has `removeClippedSubviews` enabled.
* Scroll views use it to skip the clipping pass when it cannot change anything.
* Main thread only.
*/
+ (BOOL)anyViewRemovesClippedSubviews;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ static BOOL RCTViewIsInteractiveAccessibilityElement(UIView *view, const ViewPro
}
#endif

// The number of views with `_removeClippedSubviews` set. Main thread only.
static NSUInteger RCTViewsRemovingClippedSubviewsCount = 0;

@implementation RCTViewComponentView {
UIColor *_backgroundColor;
CALayer *_backgroundColorLayer;
Expand Down Expand Up @@ -132,6 +135,11 @@ + (void)load
}
#endif

+ (BOOL)anyViewRemovesClippedSubviews
{
return RCTViewsRemovingClippedSubviewsCount > 0;
}

- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
Expand All @@ -146,6 +154,26 @@ - (instancetype)initWithFrame:(CGRect)frame
return self;
}

- (void)dealloc
{
if (_removeClippedSubviews) {
RCTViewsRemovingClippedSubviewsCount--;
}
}

- (void)_setRemoveClippedSubviews:(BOOL)removeClippedSubviews
{
if (_removeClippedSubviews == removeClippedSubviews) {
return;
}
_removeClippedSubviews = removeClippedSubviews;
if (removeClippedSubviews) {
RCTViewsRemovingClippedSubviewsCount++;
} else {
RCTViewsRemovingClippedSubviewsCount--;
}
}

- (facebook::react::Props::Shared)props
{
return _props;
Expand Down Expand Up @@ -369,7 +397,7 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
// Compare against the current state, not `_props`: `prepareForRecycle` resets
// `_removeClippedSubviews` but keeps `_props`.
if (_removeClippedSubviews != newViewProps.removeClippedSubviews) {
_removeClippedSubviews = newViewProps.removeClippedSubviews;
[self _setRemoveClippedSubviews:newViewProps.removeClippedSubviews];
[self _updateRemoveClippedSubviewsState];
}
}
Expand Down Expand Up @@ -895,7 +923,7 @@ - (void)prepareForRecycle
_propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN = nil;
_eventEmitter.reset();
_isJSResponder = NO;
_removeClippedSubviews = NO;
[self _setRemoveClippedSubviews:NO];
_reactSubviews = [NSMutableArray new];
_layoutMetrics = EmptyLayoutMetrics;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
#import <React/RCTEnhancedScrollView.h>
#import <React/RCTScrollContentViewComponentView.h>
#import <React/RCTScrollViewComponentView.h>
#import <React/RCTViewComponentView.h>
#import <XCTest/XCTest.h>
#import <react/featureflags/ReactNativeFeatureFlags.h>
#import <react/featureflags/ReactNativeFeatureFlagsDefaults.h>
#import <react/renderer/components/scrollview/ScrollContentViewComponentDescriptor.h>
#import <react/renderer/components/scrollview/ScrollContentViewState.h>
#import <react/renderer/components/scrollview/ScrollViewProps.h>
#import <react/renderer/components/scrollview/ScrollViewShadowNode.h>
#import <react/renderer/components/view/ViewProps.h>
#import <react/renderer/components/view/ViewShadowNode.h>
#import <react/renderer/core/EventBeat.h>
#import <react/renderer/core/EventDispatcher.h>
#import <react/renderer/core/EventQueueProcessor.h>
Expand Down Expand Up @@ -194,6 +197,19 @@ - (UIEdgeInsets)adjustedContentInset

@end

@interface RCTClippingPassSpyView : UIView
@property (nonatomic, assign) NSUInteger clippingPassCount;
@end

@implementation RCTClippingPassSpyView

- (void)updateClippedSubviewsWithClipRect:(CGRect)clipRect relativeToView:(UIView *)clipView
{
self.clippingPassCount++;
}

@end

@interface RCTScrollViewComponentViewTests : XCTestCase
@end

Expand Down Expand Up @@ -443,6 +459,30 @@ - (void)testScrollContentFinalizeFlagUsesImmediateStateUpdate
}
// NOLINTEND(cppcoreguidelines-pro-type-vararg, misc-throw-by-value-catch-by-reference)

- (void)testClippingPassRunsOnlyWhenAViewRemovesClippedSubviews
{
RCTScrollViewComponentView *view = [[RCTScrollViewComponentView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
RCTClippingPassSpyView *child = [[RCTClippingPassSpyView alloc] initWithFrame:CGRectMake(0, 0, 100, 1000)];
[view mountChildComponentView:(id)child index:0];
XCTAssertFalse([RCTViewComponentView anyViewRemovesClippedSubviews]);

// Scrolling past the clipping leeway runs the clipping pass, unless no view can use it.
view.scrollView.contentOffset = CGPointMake(0, 100);
[(id<UIScrollViewDelegate>)view scrollViewDidScroll:view.scrollView];
XCTAssertEqual(child.clippingPassCount, 0u);

RCTViewComponentView *clippingView = [RCTViewComponentView new];
auto clippingProps = std::make_shared<facebook::react::ViewProps>();
clippingProps->removeClippedSubviews = true;
[clippingView updateProps:clippingProps oldProps:facebook::react::ViewShadowNode::defaultSharedProps()];

view.scrollView.contentOffset = CGPointMake(0, 200);
[(id<UIScrollViewDelegate>)view scrollViewDidScroll:view.scrollView];
XCTAssertGreaterThan(child.clippingPassCount, 0u);

[view unmountChildComponentView:(id)child index:0];
}

@end

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,26 @@ - (void)testRemoveClippedSubviewsIsKeptOnRecycledView
XCTAssertEqual(reactSubviews.count, 1u);
}

- (void)testAnyViewRemovesClippedSubviewsFollowsPropsRecyclingAndDeallocation
{
XCTAssertFalse([RCTViewComponentView anyViewRemovesClippedSubviews]);

@autoreleasepool {
RCTViewComponentView *view = [RCTViewComponentView new];
[view updateProps:makeViewProps(true) oldProps:ViewShadowNode::defaultSharedProps()];
XCTAssertTrue([RCTViewComponentView anyViewRemovesClippedSubviews]);

[view prepareForRecycle];
XCTAssertFalse([RCTViewComponentView anyViewRemovesClippedSubviews]);

[view updateProps:makeViewProps(true) oldProps:nullptr];
XCTAssertTrue([RCTViewComponentView anyViewRemovesClippedSubviews]);
}

// The view is deallocated with `removeClippedSubviews` still on.
XCTAssertFalse([RCTViewComponentView anyViewRemovesClippedSubviews]);
}

#pragma mark - hitTest against non-invertible transforms (#50797)

- (void)testHitTestReturnsNilForZeroScaleYView
Expand Down