🛠️ Fix offset issue with ModalDrawer for RTL languages

* Fixed incorrect offset/animation for Custom Modal Drawer in RTL languages (e.g. Arabic)
This commit is contained in:
Acclorite 2025-02-10 13:25:42 +02:00
parent cd18c430a5
commit 9afcbd237c

View file

@ -35,7 +35,9 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import ua.acclorite.book_story.presentation.core.components.common.AnimatedVisibility import ua.acclorite.book_story.presentation.core.components.common.AnimatedVisibility
import ua.acclorite.book_story.presentation.core.components.common.LazyColumnWithScrollbar import ua.acclorite.book_story.presentation.core.components.common.LazyColumnWithScrollbar
@ -63,7 +65,9 @@ fun ModalDrawer(
header: @Composable () -> Unit = {}, header: @Composable () -> Unit = {},
content: LazyListScope.() -> Unit content: LazyListScope.() -> Unit
) { ) {
val layoutDirection = LocalLayoutDirection.current
val density = LocalDensity.current val density = LocalDensity.current
val offset = remember { mutableFloatStateOf(0f) } val offset = remember { mutableFloatStateOf(0f) }
val offsetDp = remember { val offsetDp = remember {
derivedStateOf { derivedStateOf {
@ -93,8 +97,14 @@ fun ModalDrawer(
) { ) {
AnimatedVisibility( AnimatedVisibility(
visible = show, visible = show,
enter = slideInHorizontally { (-it + with(density) { -20.dp.toPx() }).toInt() }, enter = slideInHorizontally {
exit = slideOutHorizontally { (-it + with(density) { -20.dp.toPx() }).toInt() } if (layoutDirection == LayoutDirection.Ltr) (-it + with(density) { -20.dp.toPx() }).toInt()
else (it + with(density) { 20.dp.toPx() }).toInt()
},
exit = slideOutHorizontally {
if (layoutDirection == LayoutDirection.Ltr) (-it + with(density) { -20.dp.toPx() }).toInt()
else (it + with(density) { 20.dp.toPx() }).toInt()
}
) { ) {
Column( Column(
modifier = Modifier modifier = Modifier
@ -125,8 +135,9 @@ fun ModalDrawer(
else offset.floatValue = 0f else offset.floatValue = 0f
} }
) { _, dragAmount -> ) { _, dragAmount ->
offset.floatValue = (offset.floatValue + dragAmount) offset.floatValue = (if (layoutDirection == LayoutDirection.Ltr) {
.coerceAtMost(0f) offset.floatValue + dragAmount
} else offset.floatValue + (-dragAmount)).coerceAtMost(0f)
} }
} }
.systemBarsPadding() .systemBarsPadding()