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
13 changes: 13 additions & 0 deletions .github/scatterlab/allowed-tarball-diff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -1002,6 +1005,10 @@ - (void)_remountChildren
return;
}

if (RCTViewComponentViewClippingViewCount() == 0) {
return;
}

[_scrollView updateClippedSubviewsWithClipRect:CGRectInset(_scrollView.bounds, -kClippingLeeway, -kClippingLeeway)
relativeToView:_scrollView];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import <CoreGraphics/CoreGraphics.h>
#import <QuartzCore/QuartzCore.h>
#import <objc/runtime.h>
#import <atomic>
#import <ranges>
#import <string_view>
#import <unordered_set>
Expand Down Expand Up @@ -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<NSInteger> 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
Expand Down Expand Up @@ -145,6 +169,11 @@ - (instancetype)initWithFrame:(CGRect)frame
return self;
}

- (void)dealloc
{
RCTSetRemoveClippedSubviews(_removeClippedSubviews, NO);
}

- (facebook::react::Props::Shared)props
{
return _props;
Expand Down Expand Up @@ -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];
}
}
Expand Down Expand Up @@ -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;
}
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/RCTScrollViewComponentView.h>
#import <React/RCTViewComponentView.h>
#import <XCTest/XCTest.h>
#import <react/renderer/components/view/ViewProps.h>
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down