Skip to content

Commit 1dc6be1

Browse files
committed
Make ScrollbarState implement official ScrollIndicatorState
1 parent 0baf2d5 commit 1dc6be1

5 files changed

Lines changed: 62 additions & 6 deletions

File tree

‎library/composables/src/commonMain/kotlin/com/tunjid/composables/scrollbars/ScrollbarState.kt‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.tunjid.composables.scrollbars
1818

19+
import androidx.compose.foundation.ScrollIndicatorState
1920
import androidx.compose.foundation.gestures.Orientation
2021
import androidx.compose.foundation.gestures.detectHorizontalDragGestures
2122
import androidx.compose.foundation.gestures.detectTapGestures
@@ -72,7 +73,9 @@ private const val SCROLLBAR_PRESS_DELAY_MS = 10L
7273
private const val SCROLLBAR_PRESS_DELTA_PCT = 0.02f
7374

7475
@Stable
75-
class ScrollbarState {
76+
class ScrollbarState private constructor(
77+
private val viewportSizePx: () -> Int,
78+
) : ScrollIndicatorState {
7679
private var packedValue by mutableLongStateOf(0L)
7780

7881
internal fun onScroll(stateValue: ScrollbarStateValue) {
@@ -96,6 +99,30 @@ class ScrollbarState {
9699
*/
97100
val thumbTrackSizePercent
98101
get() = 1f - thumbSizePercent
102+
103+
override val viewportSize: Int
104+
get() = if (thumbSizePercent > 0f) viewportSizePx() else Int.MAX_VALUE
105+
106+
override val contentSize: Int
107+
get() = if (thumbSizePercent > 0f) {
108+
(viewportSizePx() / thumbSizePercent).roundToInt()
109+
} else {
110+
Int.MAX_VALUE
111+
}
112+
113+
override val scrollOffset: Int
114+
get() {
115+
if (thumbSizePercent <= 0f) return Int.MAX_VALUE
116+
val track = thumbTrackSizePercent
117+
if (track <= 0f) return 0
118+
val progress = (thumbMovedPercent / track).coerceIn(0f, 1f)
119+
return (progress * (contentSize - viewportSize)).roundToInt()
120+
}
121+
122+
internal companion object {
123+
internal operator fun invoke(viewportSizePx: () -> Int): ScrollbarState =
124+
ScrollbarState(viewportSizePx)
125+
}
99126
}
100127

101128
/**
@@ -215,7 +242,7 @@ fun Scrollbar(
215242
withTimeout(viewConfiguration.longPressTimeoutMillis) {
216243
tryAwaitRelease()
217244
}
218-
} catch (e: TimeoutCancellationException) {
245+
} catch (_: TimeoutCancellationException) {
219246
// Start the press triggered scroll
220247
val initialPress = PressInteraction.Press(offset)
221248
interactionSource?.tryEmit(initialPress)

‎library/composables/src/commonMain/kotlin/com/tunjid/composables/scrollbars/scrollable/grid/ScrollbarState.kt‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,15 @@ fun LazyGridState.scrollbarState(
7474
itemsAvailable: Int,
7575
itemIndex: (LazyGridItemInfo) -> Int = LazyGridItemInfo::index,
7676
): ScrollbarState {
77-
val state = remember { ScrollbarState() }
77+
val state = remember(this) {
78+
ScrollbarState(
79+
viewportSizePx = {
80+
layoutInfo.orientation.valueOf(
81+
intSize = layoutInfo.viewportSize,
82+
)
83+
},
84+
)
85+
}
7886
LaunchedEffect(this, itemsAvailable) {
7987
snapshotFlow {
8088
if (itemsAvailable == 0) return@snapshotFlow null

‎library/composables/src/commonMain/kotlin/com/tunjid/composables/scrollbars/scrollable/list/ScrollbarState.kt‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import com.tunjid.composables.scrollbars.ScrollbarState
3232
import com.tunjid.composables.scrollbars.scrollable.rememberScrollbarThumbMover
3333
import com.tunjid.composables.scrollbars.scrollable.sumOf
3434
import com.tunjid.composables.scrollbars.scrollbarStateValue
35+
import com.tunjid.composables.valueOf
3536
import kotlin.math.min
3637
import kotlinx.coroutines.flow.distinctUntilChanged
3738
import kotlinx.coroutines.flow.filterNotNull
@@ -73,7 +74,15 @@ fun LazyListState.scrollbarState(
7374
itemsAvailable: Int,
7475
itemIndex: (LazyListItemInfo) -> Int = LazyListItemInfo::index,
7576
): ScrollbarState {
76-
val state = remember { ScrollbarState() }
77+
val state = remember(this) {
78+
ScrollbarState(
79+
viewportSizePx = {
80+
layoutInfo.orientation.valueOf(
81+
intSize = layoutInfo.viewportSize,
82+
)
83+
},
84+
)
85+
}
7786
LaunchedEffect(this, itemsAvailable) {
7887
snapshotFlow {
7988
if (itemsAvailable == 0) return@snapshotFlow null

‎library/composables/src/commonMain/kotlin/com/tunjid/composables/scrollbars/scrollable/scroll/ScrollbarState.kt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ fun ScrollState.rememberScrollbarThumbMover(): (Float) -> Unit {
5151
*/
5252
@Composable
5353
fun ScrollState.scrollbarState(): ScrollbarState {
54-
val state = remember { ScrollbarState() }
54+
val state = remember(this) {
55+
ScrollbarState(
56+
viewportSizePx = ::viewportSize,
57+
)
58+
}
5559
LaunchedEffect(this) {
5660
snapshotFlow {
5761
scrollbarStateValue(

‎library/composables/src/commonMain/kotlin/com/tunjid/composables/scrollbars/scrollable/staggeredgrid/ScrollbarState.kt‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,15 @@ fun LazyStaggeredGridState.scrollbarState(
7676
itemsAvailable: Int,
7777
itemIndex: (LazyStaggeredGridItemInfo) -> Int = LazyStaggeredGridItemInfo::index,
7878
): ScrollbarState {
79-
val state = remember { ScrollbarState() }
79+
val state = remember(this) {
80+
ScrollbarState(
81+
viewportSizePx = {
82+
layoutInfo.orientation.valueOf(
83+
intSize = layoutInfo.viewportSize,
84+
)
85+
},
86+
)
87+
}
8088
LaunchedEffect(this, itemsAvailable) {
8189
snapshotFlow {
8290
if (itemsAvailable == 0) return@snapshotFlow null

0 commit comments

Comments
 (0)