Support RTL
This commit is contained in:
1 parent
5b2df7a200
commit
995a246a2f
5 files changed
+20
-6
No files matched your search
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -24,6 +24,8 @@ import androidx.compose.ui.graphics.drawscope.scale
|
||||
import androidx.compose.ui.graphics.graphicsLayer
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.layout.layout
|
||||
import androidx.compose.ui.platform.LocalLayoutDirection
|
||||
import androidx.compose.ui.unit.LayoutDirection
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.util.fastCoerceIn
|
||||
import androidx.compose.ui.util.fastRoundToInt
|
||||
@@ -69,6 +71,7 @@ fun LiquidSlider(
|
||||
) {
|
||||
val trackWidth = constraints.maxWidth
|
||||
|
||||
val isLtr = LocalLayoutDirection.current == LayoutDirection.Ltr
|
||||
val animationScope = rememberCoroutineScope()
|
||||
var didDrag by remember { mutableStateOf(false) }
|
||||
val dampedDragAnimation = remember(animationScope) {
|
||||
@@ -89,9 +92,10 @@ fun LiquidSlider(
|
||||
if (!didDrag) {
|
||||
didDrag = dragAmount.x != 0f
|
||||
}
|
||||
val delta = (valueRange.endInclusive - valueRange.start) * (dragAmount.x / trackWidth)
|
||||
onValueChange(
|
||||
(targetValue + (valueRange.endInclusive - valueRange.start) * (dragAmount.x / trackWidth))
|
||||
.coerceIn(valueRange)
|
||||
if (isLtr) (targetValue + delta).coerceIn(valueRange)
|
||||
else (targetValue - delta).coerceIn(valueRange)
|
||||
)
|
||||
}
|
||||
)
|
||||
@@ -112,8 +116,10 @@ fun LiquidSlider(
|
||||
.background(trackColor)
|
||||
.pointerInput(animationScope) {
|
||||
detectTapGestures { position ->
|
||||
val delta = (valueRange.endInclusive - valueRange.start) * (position.x / trackWidth)
|
||||
val targetValue =
|
||||
(valueRange.start + (valueRange.endInclusive - valueRange.start) * (position.x / trackWidth))
|
||||
(if (isLtr) valueRange.start + delta
|
||||
else valueRange.endInclusive - delta)
|
||||
.coerceIn(valueRange)
|
||||
dampedDragAnimation.animateToValue(targetValue)
|
||||
onValueChange(targetValue)
|
||||
@@ -143,7 +149,7 @@ fun LiquidSlider(
|
||||
.graphicsLayer {
|
||||
translationX =
|
||||
(-size.width / 2f + trackWidth * dampedDragAnimation.progress)
|
||||
.fastCoerceIn(-size.width / 4f, trackWidth - size.width * 3f / 4f)
|
||||
.fastCoerceIn(-size.width / 4f, trackWidth - size.width * 3f / 4f) * if (isLtr) 1f else -1f
|
||||
}
|
||||
.then(dampedDragAnimation.modifier)
|
||||
.drawBackdrop(
|
||||
|
||||
@@ -21,9 +21,11 @@ import androidx.compose.ui.graphics.drawscope.scale
|
||||
import androidx.compose.ui.graphics.graphicsLayer
|
||||
import androidx.compose.ui.graphics.lerp
|
||||
import androidx.compose.ui.platform.LocalDensity
|
||||
import androidx.compose.ui.platform.LocalLayoutDirection
|
||||
import androidx.compose.ui.semantics.Role
|
||||
import androidx.compose.ui.semantics.role
|
||||
import androidx.compose.ui.semantics.semantics
|
||||
import androidx.compose.ui.unit.LayoutDirection
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.util.fastCoerceIn
|
||||
import androidx.compose.ui.util.lerp
|
||||
@@ -59,6 +61,7 @@ fun LiquidToggle(
|
||||
else Color(0xFF787880).copy(0.36f)
|
||||
|
||||
val density = LocalDensity.current
|
||||
val isLtr = LocalLayoutDirection.current == LayoutDirection.Ltr
|
||||
val dragWidth = with(density) { 20f.dp.toPx() }
|
||||
val animationScope = rememberCoroutineScope()
|
||||
var didDrag by remember { mutableStateOf(false) }
|
||||
@@ -86,7 +89,10 @@ fun LiquidToggle(
|
||||
if (!didDrag) {
|
||||
didDrag = dragAmount.x != 0f
|
||||
}
|
||||
fraction = (fraction + dragAmount.x / dragWidth).fastCoerceIn(0f, 1f)
|
||||
val delta = dragAmount.x / dragWidth
|
||||
fraction =
|
||||
if (isLtr) (fraction + delta).fastCoerceIn(0f, 1f)
|
||||
else (fraction - delta).fastCoerceIn(0f, 1f)
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -129,7 +135,9 @@ fun LiquidToggle(
|
||||
.graphicsLayer {
|
||||
val fraction = dampedDragAnimation.value
|
||||
val padding = 2f.dp.toPx()
|
||||
translationX = lerp(padding, padding + dragWidth, fraction)
|
||||
translationX =
|
||||
if (isLtr) lerp(padding, padding + dragWidth, fraction)
|
||||
else lerp(-padding, -(padding + dragWidth), fraction)
|
||||
}
|
||||
.semantics {
|
||||
role = Role.Switch
|
||||
|
||||
Reference in new issue
Block a user