Skip to content

Commit

Permalink
Support RTL in HorizontalScrollSpec
Browse files Browse the repository at this point in the history
Summary: as title. check layout direction and scroll to right if RTL

Reviewed By: muraziz

Differential Revision: D6845558

fbshipit-source-id: fb7284d138f17f805b3cfce5f05bcc5e3565e00b
  • Loading branch information
mihaelao authored and facebook-github-bot committed Jan 30, 2018
1 parent b944bea commit 5b9286e
Showing 1 changed file with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.facebook.litho.annotations.PropDefault;
import com.facebook.litho.annotations.ResType;
import com.facebook.litho.annotations.State;
import com.facebook.yoga.YogaDirection;

/**
* A component that wraps another component and allow it to be horizontally scrollable. It's
Expand All @@ -53,6 +54,8 @@
@MountSpec
class HorizontalScrollSpec {

private static final int LAST_SCROLL_POSITION_UNSET = -1;

@PropDefault static final boolean scrollbarEnabled = true;

private static final SynchronizedPool<Size> sSizePool =
Expand Down Expand Up @@ -137,7 +140,8 @@ static void onBoundsDefined(
@FromMeasure Integer measuredComponentWidth,
@FromMeasure Integer measuredComponentHeight,
Output<Integer> componentWidth,
Output<Integer> componentHeight) {
Output<Integer> componentHeight,
Output<YogaDirection> layoutDirection) {

// If onMeasure() has been called, this means the content component already
// has a defined size, no need to calculate it again.
Expand All @@ -163,6 +167,8 @@ static void onBoundsDefined(
componentWidth.set(measuredWidth);
componentHeight.set(measuredHeight);
}

layoutDirection.set(layout.getResolvedLayoutDirection());
}

@OnCreateMountContent
Expand All @@ -172,13 +178,14 @@ static HorizontalScrollLithoView onCreateMountContent(ComponentContext c) {

@OnMount
static void onMount(
ComponentContext context,
final ComponentContext context,
final HorizontalScrollLithoView horizontalScrollLithoView,
@Prop(optional = true, resType = ResType.BOOL) boolean scrollbarEnabled,
@State final ScrollPosition lastScrollPosition,
@State(canUpdateLazily = true) final int lastScrollPosition,
@FromPrepare ComponentTree contentComponent,
@FromBoundsDefined int componentWidth,
@FromBoundsDefined int componentHeight) {
@FromBoundsDefined int componentHeight,
@FromBoundsDefined final YogaDirection layoutDirection) {

horizontalScrollLithoView.setHorizontalScrollBarEnabled(scrollbarEnabled);
horizontalScrollLithoView.mount(contentComponent, componentWidth, componentHeight);
Expand All @@ -188,15 +195,26 @@ static void onMount(
@Override
public boolean onPreDraw() {
horizontalScrollLithoView.getViewTreeObserver().removeOnPreDrawListener(this);
horizontalScrollLithoView.setScrollX(lastScrollPosition.x);

if (lastScrollPosition == LAST_SCROLL_POSITION_UNSET) {
if (layoutDirection == YogaDirection.RTL) {
horizontalScrollLithoView.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
HorizontalScroll.lazyUpdateLastScrollPosition(
context, horizontalScrollLithoView.getScrollX());
} else {
horizontalScrollLithoView.setScrollX(lastScrollPosition);
}

return true;
}
});
viewTreeObserver.addOnScrollChangedListener(
new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
lastScrollPosition.x = horizontalScrollLithoView.getScrollX();
HorizontalScroll.lazyUpdateLastScrollPosition(
context, horizontalScrollLithoView.getScrollX());
}
});
}
Expand All @@ -209,10 +227,8 @@ static void onUnmount(
}

@OnCreateInitialState
static void onCreateInitialState(
ComponentContext c,
StateValue<ScrollPosition> lastScrollPosition) {
lastScrollPosition.set(new ScrollPosition());
static void onCreateInitialState(ComponentContext c, StateValue<Integer> lastScrollPosition) {
lastScrollPosition.set(LAST_SCROLL_POSITION_UNSET);
}

static class HorizontalScrollLithoView extends HorizontalScrollView {
Expand Down Expand Up @@ -269,8 +285,4 @@ private static Size acquireSize() {
private static void releaseSize(Size size) {
sSizePool.release(size);
}

static class ScrollPosition {
int x = 0;
}
}

0 comments on commit 5b9286e

Please sign in to comment.