From 6f417c970d21c371b9e5f3c5f912466ebb1f802c Mon Sep 17 00:00:00 2001 From: Shahzeb Mustafa Date: Tue, 10 Dec 2024 10:27:40 -0800 Subject: [PATCH] enable center snapping for font picker Summary: This diff: 1. Adds custom snap helper support to the LazyList litho component 2. Adds a custom snap helper for the text tool font helper that snaps elements to the center of the screen. This was previously broken such that first and last element couldnt be selected. Reviewed By: astreet Differential Revision: D66485165 fbshipit-source-id: 1d765dde730424db7d0db6f5a13723b31e66c8c5 --- .../facebook/litho/widget/collection/CollectionLayout.kt | 7 +++++++ .../com/facebook/litho/widget/collection/LazyList.kt | 3 +++ 2 files changed, 10 insertions(+) diff --git a/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/CollectionLayout.kt b/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/CollectionLayout.kt index e90778dea8d..957f1115ccc 100644 --- a/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/CollectionLayout.kt +++ b/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/CollectionLayout.kt @@ -18,6 +18,7 @@ package com.facebook.litho.widget.collection import androidx.annotation.Px import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.SnapHelper import androidx.recyclerview.widget.StaggeredGridLayoutManager import com.facebook.litho.ComponentContext import com.facebook.litho.config.PreAllocationHandler @@ -114,6 +115,7 @@ internal object CollectionLayouts { isCircular: Boolean, enableStableIds: Boolean, linearLayoutInfoFactory: LinearLayoutInfoFactory?, + snapHelper: SnapHelper?, ): CollectionLayout = object : CollectionLayout( @@ -132,6 +134,11 @@ internal object CollectionLayouts { .snapMode(snapMode) .linearLayoutInfoFactory(linearLayoutInfoFactory) .snapToStartOffset(snapToStartOffset) + .also { + if (snapHelper != null) { + it.snapHelper(snapHelper) + } + } } /** diff --git a/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/LazyList.kt b/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/LazyList.kt index 4be6bb8c874..13b3fe2891b 100644 --- a/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/LazyList.kt +++ b/litho-widget-kotlin/src/main/kotlin/com/facebook/litho/widget/collection/LazyList.kt @@ -17,6 +17,7 @@ package com.facebook.litho.widget.collection import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.SnapHelper import com.facebook.litho.Component import com.facebook.litho.Handle import com.facebook.litho.LithoStartupLogger @@ -36,6 +37,7 @@ import com.facebook.rendercore.dp inline fun ResourcesScope.LazyList( @RecyclerView.Orientation orientation: Int = RecyclerView.VERTICAL, @SnapUtil.SnapMode snapMode: Int = SnapUtil.SNAP_NONE, + snapHelper: SnapHelper? = null, snapToStartOffset: Dimen = 0.dp, reverse: Boolean = false, crossAxisWrapMode: CrossAxisWrapMode = CrossAxisWrapMode.NoWrap, @@ -95,6 +97,7 @@ inline fun ResourcesScope.LazyList( componentContext = context, orientation = orientation, snapMode = snapMode, + snapHelper = snapHelper, snapToStartOffset = snapToStartOffset.toPixels(resourceResolver), reverse = reverse, rangeRatio = rangeRatio,