diff --git a/.github/scatterlab/allowed-tarball-diff.txt b/.github/scatterlab/allowed-tarball-diff.txt index 554d4f75bd44..1494c1245748 100644 --- a/.github/scatterlab/allowed-tarball-diff.txt +++ b/.github/scatterlab/allowed-tarball-diff.txt @@ -60,3 +60,16 @@ scripts/android/scatterlab-prebuilt-maven.gradle # on the iOS side only. C++ reaches Android only through this fork's Android prebuilt. ReactCommon/react/renderer/scheduler/SurfaceHandler.cpp React/Fabric/Surface/RCTFabricSurface.mm + +# An iOS ScrollView walks its whole content after every mounting transaction and every +# few points of scrolling, to reach the views that have `removeClippedSubviews` on. Every +# mounted ScrollView does this on every transaction, and where no view has the prop on +# the walk only converts a rect per view. RCTViewComponentView now counts the live views +# that have it on, and -[RCTScrollViewComponentView _remountChildren] returns early while +# the count is 0. Upstream https://github.com/react/react-native/pull/59284 (open) has +# the same aim. The last two files also carry upstream +# https://github.com/react/react-native/pull/58915 (in 0.89), which keeps the prop on a +# recycled view. Compiled into React.xcframework, like the IME files above. +React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm +React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +React/Tests/Mounting/RCTViewComponentViewTests.mm 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 14a08b49cb7c..b113114a5450 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm @@ -34,6 +34,9 @@ static const CGFloat kClippingLeeway = 44.0; +// Defined in RCTViewComponentView.mm. +RCT_EXTERN NSInteger RCTViewComponentViewClippingViewCount(void); + static UIScrollViewKeyboardDismissMode RCTUIKeyboardDismissModeFromProps(const ScrollViewProps &props) { switch (props.keyboardDismissMode) { @@ -1002,6 +1005,10 @@ - (void)_remountChildren return; } + if (RCTViewComponentViewClippingViewCount() == 0) { + return; + } + [_scrollView updateClippedSubviewsWithClipRect:CGRectInset(_scrollView.bounds, -kClippingLeeway, -kClippingLeeway) relativeToView:_scrollView]; } 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 d44d1e02ca51..fb207e7ed4f6 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -11,6 +11,7 @@ #import #import #import +#import #import #import #import @@ -40,6 +41,29 @@ const CGFloat BACKGROUND_COLOR_ZPOSITION = -1024.0f; +// Live views with `removeClippedSubviews` on. A ScrollView walks its whole content after +// every mounting transaction and every few points of scrolling only to reach such views, +// so it skips the walk while there are none (`-[RCTScrollViewComponentView _remountChildren]`). +// A count that is too low would leave the children of a clipping view unattached, so it +// follows the flag itself, not the props: `prepareForRecycle` resets the flag but keeps +// `_props`. Every change to the flag goes through `RCTSetRemoveClippedSubviews`. +static std::atomic gClippingViewCount{0}; + +RCT_EXTERN NSInteger RCTViewComponentViewClippingViewCount(void); +NSInteger RCTViewComponentViewClippingViewCount(void) +{ + return gClippingViewCount.load(); +} + +static void RCTSetRemoveClippedSubviews(BOOL &removeClippedSubviews, BOOL value) +{ + if (removeClippedSubviews == value) { + return; + } + removeClippedSubviews = value; + gClippingViewCount += value ? 1 : -1; +} + #if !TARGET_OS_TV // iOS Full Keyboard Access only focuses a view when it is an accessibility // element that also exposes an interactive trait. Views that surface their @@ -145,6 +169,11 @@ - (instancetype)initWithFrame:(CGRect)frame return self; } +- (void)dealloc +{ + RCTSetRemoveClippedSubviews(_removeClippedSubviews, NO); +} + - (facebook::react::Props::Shared)props { return _props; @@ -359,8 +388,10 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared & // Disable `removeClippedSubviews` when Fabric View Culling is enabled. if (!ReactNativeFeatureFlags::enableViewCulling()) { - if (oldViewProps.removeClippedSubviews != newViewProps.removeClippedSubviews) { - _removeClippedSubviews = newViewProps.removeClippedSubviews; + // Compare against the current state, not `_props`: `prepareForRecycle` resets + // `_removeClippedSubviews` but keeps `_props`. + if (_removeClippedSubviews != newViewProps.removeClippedSubviews) { + RCTSetRemoveClippedSubviews(_removeClippedSubviews, newViewProps.removeClippedSubviews); [self _updateRemoveClippedSubviewsState]; } } @@ -778,7 +809,7 @@ - (void)prepareForRecycle _propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN = nil; _eventEmitter.reset(); _isJSResponder = NO; - _removeClippedSubviews = NO; + RCTSetRemoveClippedSubviews(_removeClippedSubviews, NO); _reactSubviews = [NSMutableArray new]; _layoutMetrics = EmptyLayoutMetrics; } diff --git a/packages/react-native/React/Tests/Mounting/RCTViewComponentViewTests.mm b/packages/react-native/React/Tests/Mounting/RCTViewComponentViewTests.mm index 91e100d28df1..b130d4e98dfb 100644 --- a/packages/react-native/React/Tests/Mounting/RCTViewComponentViewTests.mm +++ b/packages/react-native/React/Tests/Mounting/RCTViewComponentViewTests.mm @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +#import #import #import #import @@ -20,6 +21,34 @@ return props; } +// Defined in RCTViewComponentView.mm. +RCT_EXTERN NSInteger RCTViewComponentViewClippingViewCount(void); + +@interface RCTScrollViewComponentView (ClippingWalkTests) +- (void)_remountChildren; +@end + +// Counts the clipping walks that reach it. +@interface RCTClippingWalkSpyView : UIView +@property (nonatomic, assign) NSUInteger walkCount; +@end + +@implementation RCTClippingWalkSpyView +- (void)updateClippedSubviewsWithClipRect:(CGRect)clipRect relativeToView:(UIView *)clipView +{ + self.walkCount++; +} +@end + +// A ScrollView, 400 points tall, with `content` as its content view. +static RCTScrollViewComponentView *makeScrollView(UIView *content) +{ + RCTScrollViewComponentView *scrollView = + [[RCTScrollViewComponentView alloc] initWithFrame:CGRectMake(0, 0, 100, 400)]; + [scrollView mountChildComponentView:(id)content index:0]; + return scrollView; +} + @interface RCTViewComponentViewTests : XCTestCase @end @@ -143,6 +172,121 @@ - (void)testUnmountAfterToggleOffCleansUpReactSubviews XCTAssertNil(child2.superview); } +- (void)testRemoveClippedSubviewsIsKeptOnRecycledView +{ + RCTViewComponentView *view = [RCTViewComponentView new]; + [view updateProps:makeViewProps(true) oldProps:ViewShadowNode::defaultSharedProps()]; + [view prepareForRecycle]; + + // Reused for another view with removeClippedSubviews. Like the mounting layer + // does for an Insert, pass no old props: the view diffs against its own. + [view updateProps:makeViewProps(true) oldProps:nullptr]; + + UIView *child = [UIView new]; + child.frame = CGRectMake(0, 400, 50, 50); + [view mountChildComponentView:(id)child index:0]; + + // Clipping is on, so the child is tracked and left for the clipping pass to attach. + XCTAssertNil(child.superview); + NSMutableArray *reactSubviews = [view valueForKey:@"_reactSubviews"]; + XCTAssertEqual(reactSubviews.count, 1u); +} + +#pragma mark - clipping view count + +- (void)testClippingViewCountIsNotLoweredTwiceForARecycledView +{ + NSInteger baseline = RCTViewComponentViewClippingViewCount(); + + RCTViewComponentView *recycled = [RCTViewComponentView new]; + [recycled updateProps:makeViewProps(true) oldProps:ViewShadowNode::defaultSharedProps()]; + XCTAssertEqual(RCTViewComponentViewClippingViewCount(), baseline + 1); + + // Recycling turns clipping off but keeps `_props`, which still have it on. + [recycled prepareForRecycle]; + XCTAssertEqual(RCTViewComponentViewClippingViewCount(), baseline); + + // Reused for a view without removeClippedSubviews: the props go from on to off, the view does not. + [recycled updateProps:makeViewProps(false) oldProps:nullptr]; + XCTAssertEqual(RCTViewComponentViewClippingViewCount(), baseline); + + // A count that went below the baseline here would read 0 with this view clipping. + RCTViewComponentView *clipping = [RCTViewComponentView new]; + [clipping updateProps:makeViewProps(true) oldProps:ViewShadowNode::defaultSharedProps()]; + XCTAssertEqual(RCTViewComponentViewClippingViewCount(), baseline + 1); +} + +- (void)testClippingViewCountFollowsRepeatedUpdatesRecyclingAndDealloc +{ + NSInteger baseline = RCTViewComponentViewClippingViewCount(); + + @autoreleasepool { + RCTViewComponentView *view = [RCTViewComponentView new]; + auto propsOn = makeViewProps(true); + [view updateProps:propsOn oldProps:ViewShadowNode::defaultSharedProps()]; + [view updateProps:makeViewProps(true) oldProps:propsOn]; + XCTAssertEqual(RCTViewComponentViewClippingViewCount(), baseline + 1); + + [view prepareForRecycle]; + [view prepareForRecycle]; + XCTAssertEqual(RCTViewComponentViewClippingViewCount(), baseline); + + // Deallocated below with clipping still on. + [view updateProps:makeViewProps(true) oldProps:nullptr]; + XCTAssertEqual(RCTViewComponentViewClippingViewCount(), baseline + 1); + } + + XCTAssertEqual(RCTViewComponentViewClippingViewCount(), baseline); +} + +- (void)testScrollViewSkipsClippingWalkWhileNoViewClips +{ + // The skip only shows while no view in the process clips, so this needs 0 rather than a baseline. + XCTAssertEqual(RCTViewComponentViewClippingViewCount(), 0, @"A clipping view from outside this test is alive."); + + RCTClippingWalkSpyView *spy = [RCTClippingWalkSpyView new]; + RCTViewComponentView *content = [RCTViewComponentView new]; + [content mountChildComponentView:(id)spy index:0]; + RCTScrollViewComponentView *scrollView = makeScrollView(content); + + [scrollView _remountChildren]; + XCTAssertEqual(spy.walkCount, 0u); + + // A clipping view anywhere, here in another ScrollView, turns the walk back on. + RCTViewComponentView *otherContent = [RCTViewComponentView new]; + __unused RCTScrollViewComponentView *otherScrollView = makeScrollView(otherContent); + [otherContent updateProps:makeViewProps(true) oldProps:ViewShadowNode::defaultSharedProps()]; + + [scrollView _remountChildren]; + XCTAssertEqual(spy.walkCount, 1u); + + [otherContent updateProps:makeViewProps(false) oldProps:nullptr]; + [scrollView _remountChildren]; + XCTAssertEqual(spy.walkCount, 1u); +} + +- (void)testScrollViewAttachesVisibleChildrenOfClippingView +{ + RCTViewComponentView *content = [RCTViewComponentView new]; + content.frame = CGRectMake(0, 0, 100, 1000); + RCTScrollViewComponentView *scrollView = makeScrollView(content); + [content updateProps:makeViewProps(true) oldProps:ViewShadowNode::defaultSharedProps()]; + + UIView *visibleRow = [UIView new]; + visibleRow.frame = CGRectMake(0, 0, 100, 50); + UIView *offscreenRow = [UIView new]; + offscreenRow.frame = CGRectMake(0, 900, 100, 50); + [content mountChildComponentView:(id)visibleRow index:0]; + [content mountChildComponentView:(id)offscreenRow index:1]; + + // A clipping view only tracks new children. The ScrollView's walk attaches the visible ones. + XCTAssertNil(visibleRow.superview); + + [scrollView _remountChildren]; + XCTAssertEqual(visibleRow.superview, content); + XCTAssertNil(offscreenRow.superview); +} + #pragma mark - hitTest against non-invertible transforms (#50797) - (void)testHitTestReturnsNilForZeroScaleYView