Skip to content

Commit ecf967a

Browse files
janicduplessisfacebook-github-bot
authored andcommitted
Add fabric support for maintainVisibleContentPosition on iOS (#35319)
Summary: This adds support for the `maintainVisibleContentPosition` in iOS fabric. This was previously only implemented in the old renderer. The implementation is very similar to what we currently have. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [iOS] [Added] - Add fabric support for maintainVisibleContentPosition on iOS Pull Request resolved: #35319 Test Plan: Test in RN tester example. https://user-images.githubusercontent.com/2677334/201484543-f7944e34-6cb7-48d6-aa28-e2a7ccdfa666.mov Reviewed By: sammy-SC Differential Revision: D41273822 Pulled By: jacdebug fbshipit-source-id: 7900898f28280ff01619a4af609d2a37437c7240
1 parent 0daf83a commit ecf967a

7 files changed

Lines changed: 176 additions & 1 deletion

File tree

React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#import "RCTConversions.h"
2323
#import "RCTEnhancedScrollView.h"
2424
#import "RCTFabricComponentsPlugins.h"
25+
#import "RCTPullToRefreshViewComponentView.h"
2526

2627
using namespace facebook::react;
2728

@@ -99,6 +100,11 @@ @implementation RCTScrollViewComponentView {
99100
BOOL _shouldUpdateContentInsetAdjustmentBehavior;
100101

101102
CGPoint _contentOffsetWhenClipped;
103+
104+
__weak UIView *_contentView;
105+
106+
CGRect _prevFirstVisibleFrame;
107+
__weak UIView *_firstVisibleView;
102108
}
103109

104110
+ (RCTScrollViewComponentView *_Nullable)findScrollViewComponentViewForView:(UIView *)view
@@ -148,10 +154,17 @@ - (void)dealloc
148154

149155
#pragma mark - RCTMountingTransactionObserving
150156

157+
- (void)mountingTransactionWillMount:(const facebook::react::MountingTransaction &)transaction
158+
withSurfaceTelemetry:(const facebook::react::SurfaceTelemetry &)surfaceTelemetry
159+
{
160+
[self _prepareForMaintainVisibleScrollPosition];
161+
}
162+
151163
- (void)mountingTransactionDidMount:(MountingTransaction const &)transaction
152164
withSurfaceTelemetry:(facebook::react::SurfaceTelemetry const &)surfaceTelemetry
153165
{
154166
[self _remountChildren];
167+
[self _adjustForMaintainVisibleContentPosition];
155168
}
156169

157170
#pragma mark - RCTComponentViewProtocol
@@ -336,11 +349,23 @@ - (void)_preserveContentOffsetIfNeededWithBlock:(void (^)())block
336349
- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
337350
{
338351
[_containerView insertSubview:childComponentView atIndex:index];
352+
if ([childComponentView isKindOfClass:RCTPullToRefreshViewComponentView.class]) {
353+
// Ignore the pull to refresh component.
354+
} else {
355+
RCTAssert(_contentView == nil, @"RCTScrollView may only contain a single subview.");
356+
_contentView = childComponentView;
357+
}
339358
}
340359

341360
- (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
342361
{
343362
[childComponentView removeFromSuperview];
363+
if ([childComponentView isKindOfClass:RCTPullToRefreshViewComponentView.class]) {
364+
// Ignore the pull to refresh component.
365+
} else {
366+
RCTAssert(_contentView == childComponentView, @"Attempted to remove non-existent subview");
367+
_contentView = nil;
368+
}
344369
}
345370

346371
/*
@@ -403,6 +428,9 @@ - (void)prepareForRecycle
403428
CGRect oldFrame = self.frame;
404429
self.frame = CGRectZero;
405430
self.frame = oldFrame;
431+
_contentView = nil;
432+
_prevFirstVisibleFrame = CGRectZero;
433+
_firstVisibleView = nil;
406434
[super prepareForRecycle];
407435
}
408436

@@ -683,6 +711,74 @@ - (void)removeScrollListener:(NSObject<UIScrollViewDelegate> *)scrollListener
683711
[self.scrollViewDelegateSplitter removeDelegate:scrollListener];
684712
}
685713

714+
#pragma mark - Maintain visible content position
715+
716+
- (void)_prepareForMaintainVisibleScrollPosition
717+
{
718+
const auto &props = *std::static_pointer_cast<const ScrollViewProps>(_props);
719+
if (!props.maintainVisibleContentPosition) {
720+
return;
721+
}
722+
723+
BOOL horizontal = _scrollView.contentSize.width > self.frame.size.width;
724+
int minIdx = props.maintainVisibleContentPosition.value().minIndexForVisible;
725+
for (NSUInteger ii = minIdx; ii < _contentView.subviews.count; ++ii) {
726+
// Find the first entirely visible view.
727+
UIView *subview = _contentView.subviews[ii];
728+
BOOL hasNewView = NO;
729+
if (horizontal) {
730+
hasNewView = subview.frame.origin.x > _scrollView.contentOffset.x;
731+
} else {
732+
hasNewView = subview.frame.origin.y > _scrollView.contentOffset.y;
733+
}
734+
if (hasNewView || ii == _contentView.subviews.count - 1) {
735+
_prevFirstVisibleFrame = subview.frame;
736+
_firstVisibleView = subview;
737+
break;
738+
}
739+
}
740+
}
741+
742+
- (void)_adjustForMaintainVisibleContentPosition
743+
{
744+
const auto &props = *std::static_pointer_cast<const ScrollViewProps>(_props);
745+
if (!props.maintainVisibleContentPosition) {
746+
return;
747+
}
748+
749+
std::optional<int> autoscrollThreshold = props.maintainVisibleContentPosition.value().autoscrollToTopThreshold;
750+
BOOL horizontal = _scrollView.contentSize.width > self.frame.size.width;
751+
// TODO: detect and handle/ignore re-ordering
752+
if (horizontal) {
753+
CGFloat deltaX = _firstVisibleView.frame.origin.x - _prevFirstVisibleFrame.origin.x;
754+
if (ABS(deltaX) > 0.5) {
755+
CGFloat x = _scrollView.contentOffset.x;
756+
[self _forceDispatchNextScrollEvent];
757+
_scrollView.contentOffset = CGPointMake(_scrollView.contentOffset.x + deltaX, _scrollView.contentOffset.y);
758+
if (autoscrollThreshold) {
759+
// If the offset WAS within the threshold of the start, animate to the start.
760+
if (x <= autoscrollThreshold.value()) {
761+
[self scrollToOffset:CGPointMake(0, _scrollView.contentOffset.y) animated:YES];
762+
}
763+
}
764+
}
765+
} else {
766+
CGRect newFrame = _firstVisibleView.frame;
767+
CGFloat deltaY = newFrame.origin.y - _prevFirstVisibleFrame.origin.y;
768+
if (ABS(deltaY) > 0.5) {
769+
CGFloat y = _scrollView.contentOffset.y;
770+
[self _forceDispatchNextScrollEvent];
771+
_scrollView.contentOffset = CGPointMake(_scrollView.contentOffset.x, _scrollView.contentOffset.y + deltaY);
772+
if (autoscrollThreshold) {
773+
// If the offset WAS within the threshold of the start, animate to the start.
774+
if (y <= autoscrollThreshold.value()) {
775+
[self scrollToOffset:CGPointMake(_scrollView.contentOffset.x, 0) animated:YES];
776+
}
777+
}
778+
}
779+
}
780+
}
781+
686782
@end
687783

688784
Class<RCTComponentViewProtocol> RCTScrollViewCls(void)

ReactCommon/react/renderer/components/scrollview/ScrollViewProps.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ ScrollViewProps::ScrollViewProps(
127127
"keyboardDismissMode",
128128
sourceProps.keyboardDismissMode,
129129
{})),
130+
maintainVisibleContentPosition(
131+
CoreFeatures::enablePropIteratorSetter
132+
? sourceProps.maintainVisibleContentPosition
133+
: convertRawProp(
134+
context,
135+
rawProps,
136+
"maintainVisibleContentPosition",
137+
sourceProps.maintainVisibleContentPosition,
138+
{})),
130139
maximumZoomScale(
131140
CoreFeatures::enablePropIteratorSetter
132141
? sourceProps.maximumZoomScale
@@ -337,6 +346,7 @@ void ScrollViewProps::setProp(
337346
RAW_SET_PROP_SWITCH_CASE_BASIC(directionalLockEnabled);
338347
RAW_SET_PROP_SWITCH_CASE_BASIC(indicatorStyle);
339348
RAW_SET_PROP_SWITCH_CASE_BASIC(keyboardDismissMode);
349+
RAW_SET_PROP_SWITCH_CASE_BASIC(maintainVisibleContentPosition);
340350
RAW_SET_PROP_SWITCH_CASE_BASIC(maximumZoomScale);
341351
RAW_SET_PROP_SWITCH_CASE_BASIC(minimumZoomScale);
342352
RAW_SET_PROP_SWITCH_CASE_BASIC(scrollEnabled);
@@ -413,6 +423,10 @@ SharedDebugStringConvertibleList ScrollViewProps::getDebugProps() const {
413423
"keyboardDismissMode",
414424
keyboardDismissMode,
415425
defaultScrollViewProps.keyboardDismissMode),
426+
debugStringConvertibleItem(
427+
"maintainVisibleContentPosition",
428+
maintainVisibleContentPosition,
429+
defaultScrollViewProps.maintainVisibleContentPosition),
416430
debugStringConvertibleItem(
417431
"maximumZoomScale",
418432
maximumZoomScale,

ReactCommon/react/renderer/components/scrollview/ScrollViewProps.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <react/renderer/components/view/ViewProps.h>
1212
#include <react/renderer/core/PropsParserContext.h>
1313

14+
#include <optional>
15+
1416
namespace facebook {
1517
namespace react {
1618

@@ -43,6 +45,8 @@ class ScrollViewProps final : public ViewProps {
4345
bool directionalLockEnabled{};
4446
ScrollViewIndicatorStyle indicatorStyle{};
4547
ScrollViewKeyboardDismissMode keyboardDismissMode{};
48+
std::optional<ScrollViewMaintainVisibleContentPosition>
49+
maintainVisibleContentPosition{};
4650
Float maximumZoomScale{1.0f};
4751
Float minimumZoomScale{1.0f};
4852
bool scrollEnabled{true};

ReactCommon/react/renderer/components/scrollview/conversions.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <folly/dynamic.h>
1111
#include <react/renderer/components/scrollview/primitives.h>
1212
#include <react/renderer/core/PropsParserContext.h>
13+
#include <react/renderer/core/propsConversions.h>
1314

1415
namespace facebook {
1516
namespace react {
@@ -98,6 +99,26 @@ inline void fromRawValue(
9899
abort();
99100
}
100101

102+
inline void fromRawValue(
103+
const PropsParserContext &context,
104+
const RawValue &value,
105+
ScrollViewMaintainVisibleContentPosition &result) {
106+
auto map = (butter::map<std::string, RawValue>)value;
107+
108+
auto minIndexForVisible = map.find("minIndexForVisible");
109+
if (minIndexForVisible != map.end()) {
110+
fromRawValue(
111+
context, minIndexForVisible->second, result.minIndexForVisible);
112+
}
113+
auto autoscrollToTopThreshold = map.find("autoscrollToTopThreshold");
114+
if (autoscrollToTopThreshold != map.end()) {
115+
fromRawValue(
116+
context,
117+
autoscrollToTopThreshold->second,
118+
result.autoscrollToTopThreshold);
119+
}
120+
}
121+
101122
inline std::string toString(const ScrollViewSnapToAlignment &value) {
102123
switch (value) {
103124
case ScrollViewSnapToAlignment::Start:
@@ -109,6 +130,8 @@ inline std::string toString(const ScrollViewSnapToAlignment &value) {
109130
}
110131
}
111132

133+
#if RN_DEBUG_STRING_CONVERTIBLE
134+
112135
inline std::string toString(const ScrollViewIndicatorStyle &value) {
113136
switch (value) {
114137
case ScrollViewIndicatorStyle::Default:
@@ -144,5 +167,17 @@ inline std::string toString(const ContentInsetAdjustmentBehavior &value) {
144167
}
145168
}
146169

170+
inline std::string toString(
171+
const std::optional<ScrollViewMaintainVisibleContentPosition> &value) {
172+
if (!value) {
173+
return "null";
174+
}
175+
return "{minIndexForVisible: " + toString(value.value().minIndexForVisible) +
176+
", autoscrollToTopThreshold: " +
177+
toString(value.value().autoscrollToTopThreshold) + "}";
178+
}
179+
180+
#endif
181+
147182
} // namespace react
148183
} // namespace facebook

ReactCommon/react/renderer/components/scrollview/primitives.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#pragma once
99

10+
#include <optional>
11+
1012
namespace facebook {
1113
namespace react {
1214

@@ -23,5 +25,20 @@ enum class ContentInsetAdjustmentBehavior {
2325
Always
2426
};
2527

28+
class ScrollViewMaintainVisibleContentPosition final {
29+
public:
30+
int minIndexForVisible{0};
31+
std::optional<int> autoscrollToTopThreshold{};
32+
33+
bool operator==(const ScrollViewMaintainVisibleContentPosition &rhs) const {
34+
return std::tie(this->minIndexForVisible, this->autoscrollToTopThreshold) ==
35+
std::tie(rhs.minIndexForVisible, rhs.autoscrollToTopThreshold);
36+
}
37+
38+
bool operator!=(const ScrollViewMaintainVisibleContentPosition &rhs) const {
39+
return !(*this == rhs);
40+
}
41+
};
42+
2643
} // namespace react
2744
} // namespace facebook

ReactCommon/react/renderer/debug/DebugStringConvertible.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <climits>
1111
#include <memory>
12+
#include <optional>
1213
#include <string>
1314
#include <unordered_set>
1415
#include <vector>
@@ -98,6 +99,14 @@ std::string toString(float const &value);
9899
std::string toString(double const &value);
99100
std::string toString(void const *value);
100101

102+
template <typename T>
103+
std::string toString(const std::optional<T> &value) {
104+
if (!value) {
105+
return "null";
106+
}
107+
return toString(value.value());
108+
}
109+
101110
/*
102111
* *Informal* `DebugStringConvertible` interface.
103112
*

packages/rn-tester/js/examples/ScrollView/ScrollViewExample.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class AppendingList extends React.Component<
7676
<ScrollView
7777
automaticallyAdjustContentInsets={false}
7878
maintainVisibleContentPosition={{
79-
minIndexForVisible: 1,
79+
minIndexForVisible: 0,
8080
autoscrollToTopThreshold: 10,
8181
}}
8282
nestedScrollEnabled

0 commit comments

Comments
 (0)