Skip to content

Commit 9ca460f

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
Take RTL into account in scrollTo view command
Summary: Changelog: [internal] ScrollView's `scrollTo` command doesn't work in RTL. It sets the offset from left of the screen instead of right. This diff fixes this for Fabric only. Reviewed By: JoshuaGross Differential Revision: D29164056 fbshipit-source-id: f685d3e013f474f9b445112333d8f5ad7ed36ea7
1 parent 279fb3e commit 9ca460f

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

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

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -563,31 +563,29 @@ - (void)flashScrollIndicators
563563
- (void)scrollTo:(double)x y:(double)y animated:(BOOL)animated
564564
{
565565
CGPoint offset = CGPointMake(x, y);
566-
if (!CGPointEqualToPoint(_scrollView.contentOffset, offset)) {
567-
CGRect maxRect = CGRectMake(
568-
fmin(-_scrollView.contentInset.left, 0),
569-
fmin(-_scrollView.contentInset.top, 0),
570-
fmax(
571-
_scrollView.contentSize.width - _scrollView.bounds.size.width + _scrollView.contentInset.right +
572-
fmax(_scrollView.contentInset.left, 0),
573-
0.01),
574-
fmax(
575-
_scrollView.contentSize.height - _scrollView.bounds.size.height + _scrollView.contentInset.bottom +
576-
fmax(_scrollView.contentInset.top, 0),
577-
0.01)); // Make width and height greater than 0
578-
579-
const auto &props = *std::static_pointer_cast<const ScrollViewProps>(_props);
580-
if (!CGRectContainsPoint(maxRect, offset) && !props.scrollToOverflowEnabled) {
581-
CGFloat localX = fmax(offset.x, CGRectGetMinX(maxRect));
582-
localX = fmin(localX, CGRectGetMaxX(maxRect));
583-
CGFloat localY = fmax(offset.y, CGRectGetMinY(maxRect));
584-
localY = fmin(localY, CGRectGetMaxY(maxRect));
585-
offset = CGPointMake(localX, localY);
586-
}
566+
CGRect maxRect = CGRectMake(
567+
fmin(-_scrollView.contentInset.left, 0),
568+
fmin(-_scrollView.contentInset.top, 0),
569+
fmax(
570+
_scrollView.contentSize.width - _scrollView.bounds.size.width + _scrollView.contentInset.right +
571+
fmax(_scrollView.contentInset.left, 0),
572+
0.01),
573+
fmax(
574+
_scrollView.contentSize.height - _scrollView.bounds.size.height + _scrollView.contentInset.bottom +
575+
fmax(_scrollView.contentInset.top, 0),
576+
0.01)); // Make width and height greater than 0
587577

588-
[self _forceDispatchNextScrollEvent];
589-
[_scrollView setContentOffset:offset animated:animated];
578+
const auto &props = *std::static_pointer_cast<const ScrollViewProps>(_props);
579+
if (!CGRectContainsPoint(maxRect, offset) && !props.scrollToOverflowEnabled) {
580+
CGFloat localX = fmax(offset.x, CGRectGetMinX(maxRect));
581+
localX = fmin(localX, CGRectGetMaxX(maxRect));
582+
CGFloat localY = fmax(offset.y, CGRectGetMinY(maxRect));
583+
localY = fmin(localY, CGRectGetMaxY(maxRect));
584+
offset = CGPointMake(localX, localY);
590585
}
586+
587+
[self _forceDispatchNextScrollEvent];
588+
[self scrollToOffset:offset animated:animated];
591589
}
592590

593591
- (void)scrollToEnd:(BOOL)animated
@@ -602,7 +600,7 @@ - (void)scrollToEnd:(BOOL)animated
602600
offset = CGPointMake(0, fmax(offsetY, 0));
603601
}
604602

605-
[_scrollView setContentOffset:offset animated:animated];
603+
[self scrollToOffset:offset animated:animated];
606604
}
607605

608606
#pragma mark - Child views mounting
@@ -707,7 +705,13 @@ - (void)scrollToOffset:(CGPoint)offset
707705
- (void)scrollToOffset:(CGPoint)offset animated:(BOOL)animated
708706
{
709707
[self _forceDispatchNextScrollEvent];
710-
[self.scrollView setContentOffset:offset animated:animated];
708+
709+
if (_layoutMetrics.layoutDirection == LayoutDirection::RightToLeft) {
710+
// Adjusting offset.x in right to left layout direction.
711+
offset.x = self.contentSize.width - _scrollView.frame.size.width - offset.x;
712+
}
713+
714+
[_scrollView setContentOffset:offset animated:animated];
711715
}
712716

713717
- (void)zoomToRect:(CGRect)rect animated:(BOOL)animated

0 commit comments

Comments
 (0)