Skip to content

Commit

Permalink
Rename from normalized prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
nanihadesuka committed Apr 20, 2024
1 parent 60a50c9 commit 0cdc988
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ internal fun rememberLazyGridStateController(
}


val normalizedThumbSizeReal = remember {
val thumbSizeNormalizedReal = remember {
derivedStateOf {
state.layoutInfo.let {
if (it.totalItemsCount == 0)
Expand All @@ -107,15 +107,15 @@ internal fun rememberLazyGridStateController(
}
}

val normalizedThumbSize = remember {
val thumbSizeNormalized = remember {
derivedStateOf {
normalizedThumbSizeReal.value.coerceAtLeast(thumbMinLengthUpdated.value)
thumbSizeNormalizedReal.value.coerceAtLeast(thumbMinLengthUpdated.value)
}
}

fun offsetCorrection(top: Float): Float {
val topRealMax = (1f - normalizedThumbSizeReal.value).coerceIn(0f, 1f)
if (normalizedThumbSizeReal.value >= thumbMinLengthUpdated.value) {
val topRealMax = (1f - thumbSizeNormalizedReal.value).coerceIn(0f, 1f)
if (thumbSizeNormalizedReal.value >= thumbMinLengthUpdated.value) {
return when {
reverseLayout.value -> topRealMax - top
else -> top
Expand All @@ -129,7 +129,7 @@ internal fun rememberLazyGridStateController(
}
}

val normalizedOffsetPosition = remember {
val thumbOffsetNormalized = remember {
derivedStateOf {
state.layoutInfo.let {
if (it.totalItemsCount == 0 || it.visibleItemsInfo.isEmpty())
Expand All @@ -152,15 +152,15 @@ internal fun rememberLazyGridStateController(

return remember {
LazyGridStateController(
normalizedThumbSize = normalizedThumbSize,
normalizedOffsetPosition = normalizedOffsetPosition,
thumbSizeNormalized = thumbSizeNormalized,
thumbSizeNormalizedReal = thumbSizeNormalizedReal,
thumbOffsetNormalized = thumbOffsetNormalized,
thumbIsInAction = thumbIsInAction,
_isSelected = isSelected,
dragOffset = dragOffset,
selectionMode = selectionModeUpdated,
realFirstVisibleItem = realFirstVisibleItem,
thumbMinLength = thumbMinLengthUpdated,
normalizedThumbSizeReal = normalizedThumbSizeReal,
reverseLayout = reverseLayout,
orientation = orientationUpdated,
nColumns = nColumns,
Expand All @@ -171,14 +171,14 @@ internal fun rememberLazyGridStateController(
}

internal class LazyGridStateController(
override val normalizedThumbSize: State<Float>,
override val normalizedOffsetPosition: State<Float>,
override val thumbSizeNormalized: State<Float>,
override val thumbOffsetNormalized: State<Float>,
override val thumbIsInAction: State<Boolean>,
private val _isSelected: MutableState<Boolean>,
private val dragOffset: MutableFloatState,
private val selectionMode: State<ScrollbarSelectionMode>,
private val realFirstVisibleItem: State<LazyGridItemInfo?>,
private val normalizedThumbSizeReal: State<Float>,
private val thumbSizeNormalizedReal: State<Float>,
private val thumbMinLength: State<Float>,
private val reverseLayout: State<Boolean>,
private val orientation: State<Orientation>,
Expand Down Expand Up @@ -207,21 +207,21 @@ internal class LazyGridStateController(
else -> offsetPixels / maxLengthPixels
}
val currentOffset = when {
reverseLayout.value -> 1f - normalizedOffsetPosition.value - normalizedThumbSize.value
else -> normalizedOffsetPosition.value
reverseLayout.value -> 1f - thumbOffsetNormalized.value - thumbSizeNormalized.value
else -> thumbOffsetNormalized.value
}

when (selectionMode.value) {
ScrollbarSelectionMode.Full -> {
if (newOffset in currentOffset..(currentOffset + normalizedThumbSize.value))
if (newOffset in currentOffset..(currentOffset + thumbSizeNormalized.value))
setDragOffset(currentOffset)
else
setScrollOffset(newOffset)
_isSelected.value = true
}

ScrollbarSelectionMode.Thumb -> {
if (newOffset in currentOffset..(currentOffset + normalizedThumbSize.value)) {
if (newOffset in currentOffset..(currentOffset + thumbSizeNormalized.value)) {
setDragOffset(currentOffset)
_isSelected.value = true
}
Expand Down Expand Up @@ -259,14 +259,14 @@ internal class LazyGridStateController(
}

private fun setDragOffset(value: Float) {
val maxValue = (1f - normalizedThumbSize.value).coerceAtLeast(0f)
val maxValue = (1f - thumbSizeNormalized.value).coerceAtLeast(0f)
dragOffset.floatValue = value.coerceIn(0f, maxValue)
}

private fun offsetCorrectionInverse(top: Float): Float {
if (normalizedThumbSizeReal.value >= thumbMinLength.value)
if (thumbSizeNormalizedReal.value >= thumbMinLength.value)
return top
val topRealMax = 1f - normalizedThumbSizeReal.value
val topRealMax = 1f - thumbSizeNormalizedReal.value
val topMax = 1f - thumbMinLength.value
return top * topRealMax / topMax
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal fun rememberLazyListStateController(
fun LazyListItemInfo.fractionVisibleBottom(viewportEndOffset: Int) =
if (size == 0) 0f else (viewportEndOffset - offset).toFloat() / size.toFloat()

val normalizedThumbSizeReal = remember {
val thumbSizeNormalizedReal = remember {
derivedStateOf {
state.layoutInfo.let {
if (it.totalItemsCount == 0)
Expand All @@ -78,15 +78,15 @@ internal fun rememberLazyListStateController(
}
}

val normalizedThumbSize = remember {
val thumbSizeNormalized = remember {
derivedStateOf {
normalizedThumbSizeReal.value.coerceAtLeast(thumbMinLengthUpdated.value)
thumbSizeNormalizedReal.value.coerceAtLeast(thumbMinLengthUpdated.value)
}
}

fun offsetCorrection(top: Float): Float {
val topRealMax = (1f - normalizedThumbSizeReal.value).coerceIn(0f, 1f)
if (normalizedThumbSizeReal.value >= thumbMinLengthUpdated.value) {
val topRealMax = (1f - thumbSizeNormalizedReal.value).coerceIn(0f, 1f)
if (thumbSizeNormalizedReal.value >= thumbMinLengthUpdated.value) {
return when {
reverseLayout.value -> topRealMax - top
else -> top
Expand All @@ -100,7 +100,7 @@ internal fun rememberLazyListStateController(
}
}

val normalizedOffsetPosition = remember {
val thumbOffsetNormalized = remember {
derivedStateOf {
state.layoutInfo.let {
if (it.totalItemsCount == 0 || it.visibleItemsInfo.isEmpty())
Expand All @@ -120,13 +120,13 @@ internal fun rememberLazyListStateController(

return remember {
LazyListStateController(
normalizedThumbSize = normalizedThumbSize,
normalizedOffsetPosition = normalizedOffsetPosition,
thumbSizeNormalized = thumbSizeNormalized,
thumbSizeNormalizedReal = thumbSizeNormalizedReal,
thumbOffsetNormalized = thumbOffsetNormalized,
thumbIsInAction = thumbIsInAction,
_isSelected = isSelected,
dragOffset = dragOffset,
selectionMode = selectionModeUpdated,
normalizedThumbSizeReal = normalizedThumbSizeReal,
realFirstVisibleItem = realFirstVisibleItem,
reverseLayout = reverseLayout,
thumbMinLength = thumbMinLengthUpdated,
Expand All @@ -137,12 +137,12 @@ internal fun rememberLazyListStateController(
}

internal class LazyListStateController(
override val normalizedThumbSize: State<Float>,
override val normalizedOffsetPosition: State<Float>,
override val thumbSizeNormalized: State<Float>,
override val thumbOffsetNormalized: State<Float>,
override val thumbIsInAction: State<Boolean>,
private val _isSelected: MutableState<Boolean>,
private val dragOffset: MutableFloatState,
private val normalizedThumbSizeReal: State<Float>,
private val thumbSizeNormalizedReal: State<Float>,
private val realFirstVisibleItem: State<LazyListItemInfo?>,
private val selectionMode: State<ScrollbarSelectionMode>,
private val reverseLayout: State<Boolean>,
Expand Down Expand Up @@ -170,21 +170,21 @@ internal class LazyListStateController(
else -> offsetPixels / maxLengthPixels
}
val currentOffset = when {
reverseLayout.value -> 1f - normalizedOffsetPosition.value - normalizedThumbSize.value
else -> normalizedOffsetPosition.value
reverseLayout.value -> 1f - thumbOffsetNormalized.value - thumbSizeNormalized.value
else -> thumbOffsetNormalized.value
}

when (selectionMode.value) {
ScrollbarSelectionMode.Full -> {
if (newOffset in currentOffset..(currentOffset + normalizedThumbSize.value))
if (newOffset in currentOffset..(currentOffset + thumbSizeNormalized.value))
setDragOffset(currentOffset)
else
setScrollOffset(newOffset)
_isSelected.value = true
}

ScrollbarSelectionMode.Thumb -> {
if (newOffset in currentOffset..(currentOffset + normalizedThumbSize.value)) {
if (newOffset in currentOffset..(currentOffset + thumbSizeNormalized.value)) {
setDragOffset(currentOffset)
_isSelected.value = true
}
Expand All @@ -199,14 +199,14 @@ internal class LazyListStateController(
}

private fun setDragOffset(value: Float) {
val maxValue = (1f - normalizedThumbSize.value).coerceAtLeast(0f)
val maxValue = (1f - thumbSizeNormalized.value).coerceAtLeast(0f)
dragOffset.floatValue = value.coerceIn(0f, maxValue)
}

private fun offsetCorrectionInverse(top: Float): Float {
if (normalizedThumbSizeReal.value >= thumbMinLength.value)
if (thumbSizeNormalizedReal.value >= thumbMinLength.value)
return top
val topRealMax = 1f - normalizedThumbSizeReal.value
val topRealMax = 1f - thumbSizeNormalizedReal.value
val topMax = 1f - thumbMinLength.value
return top * topRealMax / topMax
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal fun rememberScrollStateController(
}
}

val normalizedThumbSizeReal = remember {
val thumbSizeNormalizedReal = remember {
derivedStateOf {
if (fullLengthDp.value == 0.dp) 1f else {
val normalizedDp = visibleLengthDpUpdated.value / fullLengthDp.value
Expand All @@ -52,19 +52,19 @@ internal fun rememberScrollStateController(
}
}

val normalizedThumbSize = remember {
val thumbSizeNormalized = remember {
derivedStateOf {
normalizedThumbSizeReal.value.coerceAtLeast(thumbMinLengthUpdated.value)
thumbSizeNormalizedReal.value.coerceAtLeast(thumbMinLengthUpdated.value)
}
}

fun offsetCorrection(top: Float): Float {
val topRealMax = 1f
val topMax = (1f - normalizedThumbSize.value).coerceIn(0f, 1f)
val topMax = (1f - thumbSizeNormalized.value).coerceIn(0f, 1f)
return top * topMax / topRealMax
}

val normalizedOffsetPosition = remember {
val thumbOffsetNormalized = remember {
derivedStateOf {
if (state.maxValue == 0) return@derivedStateOf 0f
val normalized = state.value.toFloat() / state.maxValue.toFloat()
Expand All @@ -80,8 +80,8 @@ internal fun rememberScrollStateController(

return remember {
ScrollStateController(
normalizedThumbSize = normalizedThumbSize,
normalizedOffsetPosition = normalizedOffsetPosition,
thumbSizeNormalized = thumbSizeNormalized,
thumbOffsetNormalized = thumbOffsetNormalized,
thumbIsInAction = thumbIsInAction,
_isSelected = isSelected,
dragOffset = dragOffset,
Expand All @@ -93,8 +93,8 @@ internal fun rememberScrollStateController(
}

internal class ScrollStateController(
override val normalizedThumbSize: State<Float>,
override val normalizedOffsetPosition: State<Float>,
override val thumbSizeNormalized: State<Float>,
override val thumbOffsetNormalized: State<Float>,
override val thumbIsInAction: State<Boolean>,
private val _isSelected: MutableState<Boolean>,
private val dragOffset: MutableState<Float>,
Expand All @@ -106,19 +106,19 @@ internal class ScrollStateController(

override fun onDragStarted(offsetPixels: Float, maxLengthPixels: Float) {
val newOffset = offsetPixels / maxLengthPixels
val currentOffset = normalizedOffsetPosition.value
val currentOffset = thumbOffsetNormalized.value

when (selectionMode.value) {
ScrollbarSelectionMode.Full -> {
if (newOffset in currentOffset..(currentOffset + normalizedThumbSize.value))
if (newOffset in currentOffset..(currentOffset + thumbSizeNormalized.value))
setDragOffset(currentOffset)
else
setScrollOffset(newOffset)
_isSelected.value = true
}

ScrollbarSelectionMode.Thumb -> {
if (newOffset in currentOffset..(currentOffset + normalizedThumbSize.value)) {
if (newOffset in currentOffset..(currentOffset + thumbSizeNormalized.value)) {
setDragOffset(currentOffset)
_isSelected.value = true
}
Expand All @@ -139,12 +139,12 @@ internal class ScrollStateController(
}

override fun indicatorValue(): Float {
return offsetCorrectionInverse(normalizedOffsetPosition.value)
return offsetCorrectionInverse(thumbOffsetNormalized.value)
}

private fun offsetCorrectionInverse(top: Float): Float {
val topRealMax = 1f
val topMax = 1f - normalizedThumbSize.value
val topMax = 1f - thumbSizeNormalized.value
if (topMax == 0f) return top
return (top * topRealMax / topMax).coerceAtLeast(0f)
}
Expand All @@ -158,7 +158,7 @@ internal class ScrollStateController(
}

private fun setDragOffset(value: Float) {
val maxValue = (1f - normalizedThumbSize.value).coerceAtLeast(0f)
val maxValue = (1f - thumbSizeNormalized.value).coerceAtLeast(0f)
dragOffset.value = value.coerceIn(0f, maxValue)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import androidx.compose.runtime.State

@Stable
interface StateController<IndicatorValue> {
val normalizedThumbSize: State<Float>
val normalizedOffsetPosition: State<Float>
val thumbSizeNormalized: State<Float>
val thumbOffsetNormalized: State<Float>
val thumbIsInAction: State<Boolean>
val isSelected: State<Boolean>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ internal fun <IndicatorValue> ElementScrollbar(

ScrollbarLayout(
orientation = orientation,
thumbSizeNormalized = stateController.normalizedThumbSize.value,
thumbOffsetNormalized = stateController.normalizedOffsetPosition.value,
thumbSizeNormalized = stateController.thumbSizeNormalized.value,
thumbOffsetNormalized = stateController.thumbOffsetNormalized.value,
thumbIsInAction = stateController.thumbIsInAction.value,
settings = ScrollbarLayoutSettings(
durationAnimationMillis = 500,
Expand Down

0 comments on commit 0cdc988

Please sign in to comment.