From 2ae5a8bffd3bc6e7bacb9a45573b02d858b954a5 Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Tue, 13 Aug 2024 18:07:03 +0000 Subject: [PATCH] android, desktop: padding for RTL layout and remembering prefered layout (#4675) * android, desktop: padding for RTL layout and remembering prefered layout * refactor * changes --- .../platform/PlatformTextField.android.kt | 8 +++++-- .../common/platform/PlatformTextField.kt | 1 + .../simplex/common/views/chat/SendMsgView.kt | 1 + .../platform/PlatformTextField.desktop.kt | 22 ++++++++++++++----- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/PlatformTextField.android.kt b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/PlatformTextField.android.kt index 1f0f6d470e..ce4bada6e1 100644 --- a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/PlatformTextField.android.kt +++ b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/PlatformTextField.android.kt @@ -15,13 +15,14 @@ import androidx.compose.foundation.layout.* import androidx.compose.material.MaterialTheme import androidx.compose.material.Text import androidx.compose.runtime.* -import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.platform.LocalLayoutDirection import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontStyle +import androidx.compose.ui.unit.LayoutDirection import androidx.compose.ui.unit.dp import androidx.compose.ui.viewinterop.AndroidView import androidx.core.graphics.drawable.DrawableCompat @@ -52,6 +53,7 @@ actual fun PlatformTextField( showDeleteTextButton: MutableState, userIsObserver: Boolean, placeholder: String, + showVoiceButton: Boolean, onMessageChange: (String) -> Unit, onUpArrow: () -> Unit, onFilesPasted: (List) -> Unit, @@ -83,6 +85,7 @@ actual fun PlatformTextField( } } + val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl AndroidView(modifier = Modifier, factory = { val editText = @SuppressLint("AppCompatCustomView") object: EditText(it) { override fun setOnReceiveContentListener( @@ -113,7 +116,8 @@ actual fun PlatformTextField( editText.setTextColor(textColor.toArgb()) editText.textSize = textStyle.value.fontSize.value * appPrefs.fontScale.get() editText.background = ColorDrawable(Color.Transparent.toArgb()) - editText.setPadding(paddingStart, paddingTop, paddingEnd, paddingBottom) + editText.textDirection = if (isRtl) EditText.TEXT_DIRECTION_LOCALE else EditText.TEXT_DIRECTION_ANY_RTL + editText.setPaddingRelative(paddingStart, paddingTop, paddingEnd, paddingBottom) editText.setText(cs.message) editText.hint = placeholder editText.setHintTextColor(hintColor.toArgb()) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/PlatformTextField.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/PlatformTextField.kt index 475339f71a..1dff386684 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/PlatformTextField.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/PlatformTextField.kt @@ -15,6 +15,7 @@ expect fun PlatformTextField( showDeleteTextButton: MutableState, userIsObserver: Boolean, placeholder: String, + showVoiceButton: Boolean, onMessageChange: (String) -> Unit, onUpArrow: () -> Unit, onFilesPasted: (List) -> Unit, diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/SendMsgView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/SendMsgView.kt index 201d915fa7..162e753b18 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/SendMsgView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/SendMsgView.kt @@ -88,6 +88,7 @@ fun SendMsgView( showDeleteTextButton, userIsObserver, if (clicksOnTextFieldDisabled) "" else placeholder, + showVoiceButton, onMessageChange, editPrevMessage, onFilesPasted diff --git a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/PlatformTextField.desktop.kt b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/PlatformTextField.desktop.kt index f6b44f6035..5b0db7c94a 100644 --- a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/PlatformTextField.desktop.kt +++ b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/PlatformTextField.desktop.kt @@ -47,6 +47,7 @@ actual fun PlatformTextField( showDeleteTextButton: MutableState, userIsObserver: Boolean, placeholder: String, + showVoiceButton: Boolean, onMessageChange: (String) -> Unit, onUpArrow: () -> Unit, onFilesPasted: (List) -> Unit, @@ -56,7 +57,6 @@ actual fun PlatformTextField( val focusRequester = remember { FocusRequester() } val focusManager = LocalFocusManager.current val keyboard = LocalSoftwareKeyboardController.current - val padding = PaddingValues(0.dp, 12.dp, 50.dp, 0.dp) LaunchedEffect(cs.contextItem) { if (cs.contextItem !is ComposeContextItem.QuotedItem) return@LaunchedEffect // In replying state @@ -71,7 +71,20 @@ actual fun PlatformTextField( keyboard?.hide() } } - val isRtl = remember(cs.message) { isRtl(cs.message.subSequence(0, min(50, cs.message.length))) } + val lastTimeWasRtlByCharacters = remember { mutableStateOf(isRtl(cs.message.subSequence(0, min(50, cs.message.length)))) } + val isRtlByCharacters = remember(cs.message) { + if (cs.message.isNotEmpty()) isRtl(cs.message.subSequence(0, min(50, cs.message.length))) else lastTimeWasRtlByCharacters.value + } + LaunchedEffect(isRtlByCharacters) { + lastTimeWasRtlByCharacters.value = isRtlByCharacters + } + val isLtrGlobally = LocalLayoutDirection.current == LayoutDirection.Ltr + // Different padding here is for a text that is considered RTL with non-RTL locale set globally. + // In this case padding from right side should be bigger + val startEndPadding = if (cs.message.isEmpty() && showVoiceButton && isRtlByCharacters && isLtrGlobally) 95.dp else 50.dp + val startPadding = if (isRtlByCharacters && isLtrGlobally) startEndPadding else 0.dp + val endPadding = if (isRtlByCharacters && isLtrGlobally) 0.dp else startEndPadding + val padding = PaddingValues(startPadding, 12.dp, endPadding, 0.dp) var textFieldValueState by remember { mutableStateOf(TextFieldValue(text = cs.message)) } val textFieldValue = textFieldValueState.copy(text = cs.message) val clipboard = LocalClipboardManager.current @@ -165,9 +178,9 @@ actual fun PlatformTextField( decorationBox = { innerTextField -> Row(verticalAlignment = Alignment.Bottom) { CompositionLocalProvider( - LocalLayoutDirection provides if (isRtl) LayoutDirection.Rtl else LocalLayoutDirection.current + LocalLayoutDirection provides if (isRtlByCharacters) LayoutDirection.Rtl else LocalLayoutDirection.current ) { - Column(Modifier.weight(1f).padding(start = 0.dp, end = 50.dp)) { + Column(Modifier.weight(1f).padding(start = startPadding, end = endPadding)) { Spacer(Modifier.height(8.dp)) TextFieldDefaults.TextFieldDecorationBox( value = textFieldValue.text, @@ -186,7 +199,6 @@ actual fun PlatformTextField( } } }, - ) showDeleteTextButton.value = cs.message.split("\n").size >= 4 && !cs.inProgress if (composeState.value.preview is ComposePreview.VoicePreview) {