-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more control options for the hide/show animation.
There are now 4 parameters you can control: - hideDelayMillis - hideDisplacement - hideEasingAnimation - durationAnimationMillis
- Loading branch information
1 parent
257bf93
commit 5406b96
Showing
7 changed files
with
178 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
lib/src/main/java/my/nanihadesuka/compose/foundation/ScrollbarLayoutState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package my.nanihadesuka.compose.foundation | ||
|
||
import androidx.compose.animation.animateColorAsState | ||
import androidx.compose.animation.core.animateDpAsState | ||
import androidx.compose.animation.core.animateFloatAsState | ||
import androidx.compose.animation.core.tween | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.State | ||
import androidx.compose.runtime.derivedStateOf | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberUpdatedState | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.unit.Dp | ||
import androidx.compose.ui.unit.dp | ||
import kotlinx.coroutines.delay | ||
import my.nanihadesuka.compose.ScrollbarSelectionActionable | ||
|
||
|
||
@Composable | ||
internal fun rememberScrollbarLayoutState( | ||
thumbIsInAction: Boolean, | ||
thumbIsSelected: Boolean, | ||
settings: ScrollbarLayoutSettings, | ||
): ScrollbarLayoutState { | ||
val settingsUpdated by rememberUpdatedState(settings) | ||
val thumbIsInActionUpdated by rememberUpdatedState(thumbIsInAction) | ||
|
||
val isInActionSelectable = remember { mutableStateOf(thumbIsInAction) } | ||
|
||
LaunchedEffect(thumbIsInAction) { | ||
if (thumbIsInAction) { | ||
isInActionSelectable.value = true | ||
} else { | ||
delay(timeMillis = settingsUpdated.durationAnimationMillis.toLong() + settingsUpdated.hideDelayMillis.toLong()) | ||
isInActionSelectable.value = false | ||
} | ||
} | ||
|
||
val activeDraggableModifier = remember { | ||
derivedStateOf { | ||
when (settingsUpdated.selectionActionable) { | ||
ScrollbarSelectionActionable.Always -> true | ||
ScrollbarSelectionActionable.WhenVisible -> isInActionSelectable.value | ||
} | ||
} | ||
} | ||
|
||
val thumbColor = animateColorAsState( | ||
targetValue = if (thumbIsSelected) settingsUpdated.thumbSelectedColor else settingsUpdated.thumbUnselectedColor, | ||
animationSpec = tween(durationMillis = 50), | ||
label = "scrollbar thumb color value" | ||
) | ||
|
||
val currentDurationMillis = remember { | ||
derivedStateOf { | ||
val reductionRatio: Int = if (thumbIsInActionUpdated) 4 else 1 | ||
settingsUpdated.durationAnimationMillis / reductionRatio | ||
} | ||
} | ||
|
||
val hideAlpha = animateFloatAsState( | ||
targetValue = if (thumbIsInActionUpdated) 1f else 0f, | ||
animationSpec = tween( | ||
durationMillis = currentDurationMillis.value, | ||
delayMillis = if (thumbIsInActionUpdated) 0 else settingsUpdated.hideDelayMillis, | ||
easing = settingsUpdated.hideEasingAnimation | ||
), | ||
label = "scrollbar alpha value" | ||
) | ||
|
||
val hideDisplacement = animateDpAsState( | ||
targetValue = if (thumbIsInActionUpdated) 0.dp else settingsUpdated.hideDisplacement, | ||
animationSpec = tween( | ||
durationMillis = currentDurationMillis.value, | ||
delayMillis = if (thumbIsInActionUpdated) 0 else settingsUpdated.hideDelayMillis, | ||
easing = settingsUpdated.hideEasingAnimation | ||
), | ||
label = "scrollbar displacement value" | ||
) | ||
|
||
return remember { | ||
ScrollbarLayoutState( | ||
activeDraggableModifier = activeDraggableModifier, | ||
thumbColor = thumbColor, | ||
hideDisplacement = hideDisplacement, | ||
hideAlpha = hideAlpha | ||
) | ||
} | ||
|
||
} | ||
|
||
internal data class ScrollbarLayoutState( | ||
val activeDraggableModifier: State<Boolean>, | ||
val thumbColor: State<Color>, | ||
val hideAlpha: State<Float>, | ||
val hideDisplacement: State<Dp> | ||
) |
Oops, something went wrong.