diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm index d6ea9b26ca2..f1433b032b8 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm @@ -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]; } diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.h b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.h index 124a85c65c1..a91d64847d9 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.h +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.h @@ -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 diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm index 8d1295d8db3..ab485fe083f 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -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; @@ -132,6 +135,11 @@ + (void)load } #endif ++ (BOOL)anyViewRemovesClippedSubviews +{ + return RCTViewsRemovingClippedSubviewsCount > 0; +} + - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { @@ -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; @@ -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]; } } @@ -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; } diff --git a/packages/react-native/React/Tests/Mounting/RCTScrollViewComponentViewTests.mm b/packages/react-native/React/Tests/Mounting/RCTScrollViewComponentViewTests.mm index eb9f977dca6..bc2e0202e9a 100644 --- a/packages/react-native/React/Tests/Mounting/RCTScrollViewComponentViewTests.mm +++ b/packages/react-native/React/Tests/Mounting/RCTScrollViewComponentViewTests.mm @@ -8,6 +8,7 @@ #import #import #import +#import #import #import #import @@ -15,6 +16,8 @@ #import #import #import +#import +#import #import #import #import @@ -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 @@ -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)view scrollViewDidScroll:view.scrollView]; + XCTAssertEqual(child.clippingPassCount, 0u); + + RCTViewComponentView *clippingView = [RCTViewComponentView new]; + auto clippingProps = std::make_shared(); + clippingProps->removeClippedSubviews = true; + [clippingView updateProps:clippingProps oldProps:facebook::react::ViewShadowNode::defaultSharedProps()]; + + view.scrollView.contentOffset = CGPointMake(0, 200); + [(id)view scrollViewDidScroll:view.scrollView]; + XCTAssertGreaterThan(child.clippingPassCount, 0u); + + [view unmountChildComponentView:(id)child index:0]; +} + @end #endif diff --git a/packages/react-native/React/Tests/Mounting/RCTViewComponentViewTests.mm b/packages/react-native/React/Tests/Mounting/RCTViewComponentViewTests.mm index 5c00390702c..f6e23745e98 100644 --- a/packages/react-native/React/Tests/Mounting/RCTViewComponentViewTests.mm +++ b/packages/react-native/React/Tests/Mounting/RCTViewComponentViewTests.mm @@ -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