From a3cd7ca89e1f656f20a78472a7de5162f264ffd7 Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Thu, 27 Jul 2023 15:44:35 +0300 Subject: [PATCH] desktop: enter + shift+enter keybindings (#2787) --- .../platform/PlatformTextField.android.kt | 3 +- .../common/platform/PlatformTextField.kt | 3 +- .../simplex/common/views/chat/SendMsgView.kt | 4 ++- .../platform/PlatformTextField.desktop.kt | 34 ++++++++++++++++--- 4 files changed, 36 insertions(+), 8 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 e6c6ebad09..e9ff5687d9 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 @@ -47,7 +47,8 @@ actual fun PlatformTextField( textStyle: MutableState, showDeleteTextButton: MutableState, userIsObserver: Boolean, - onMessageChange: (String) -> Unit + onMessageChange: (String) -> Unit, + onDone: () -> Unit, ) { val cs = composeState.value val textColor = MaterialTheme.colors.onBackground 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 6d60703e53..19c0fe01b3 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 @@ -11,5 +11,6 @@ expect fun PlatformTextField( textStyle: MutableState, showDeleteTextButton: MutableState, userIsObserver: Boolean, - onMessageChange: (String) -> Unit + onMessageChange: (String) -> Unit, + onDone: () -> 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 de806e7b53..a9ca677df9 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 @@ -67,7 +67,9 @@ fun SendMsgView( val showVoiceButton = cs.message.isEmpty() && showVoiceRecordIcon && !composeState.value.editing && cs.liveMessage == null && (cs.preview is ComposePreview.NoPreview || recState.value is RecordingState.Started) val showDeleteTextButton = rememberSaveable { mutableStateOf(false) } - PlatformTextField(composeState, textStyle, showDeleteTextButton, userIsObserver, onMessageChange) + PlatformTextField(composeState, textStyle, showDeleteTextButton, userIsObserver, onMessageChange) { + sendMessage(null) + } // Disable clicks on text field if (cs.preview is ComposePreview.VoicePreview || !userCanSend || cs.inProgress) { Box( 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 ea806c0622..a8278f5331 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 @@ -14,10 +14,13 @@ import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.SolidColor +import androidx.compose.ui.input.key.* import androidx.compose.ui.platform.* +import androidx.compose.ui.text.TextRange import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.input.KeyboardCapitalization +import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.unit.LayoutDirection import androidx.compose.ui.unit.dp import chat.simplex.common.views.chat.* @@ -33,7 +36,8 @@ actual fun PlatformTextField( textStyle: MutableState, showDeleteTextButton: MutableState, userIsObserver: Boolean, - onMessageChange: (String) -> Unit + onMessageChange: (String) -> Unit, + onDone: () -> Unit, ) { val cs = composeState.value val focusRequester = remember { FocusRequester() } @@ -47,11 +51,14 @@ actual fun PlatformTextField( keyboard?.show() } val isRtl = remember(cs.message) { isRtl(cs.message.subSequence(0, min(50, cs.message.length))) } + var textFieldValueState by remember { mutableStateOf(TextFieldValue(text = cs.message)) } + val textFieldValue = textFieldValueState.copy(text = cs.message) BasicTextField( - value = cs.message, + value = textFieldValue, onValueChange = { - if (!composeState.value.inProgress && !(composeState.value.preview is ComposePreview.VoicePreview && it != "")) { - onMessageChange(it) + if (!composeState.value.inProgress && !(composeState.value.preview is ComposePreview.VoicePreview && it.text != "")) { + textFieldValueState = it + onMessageChange(it.text) } }, textStyle = textStyle.value, @@ -60,7 +67,24 @@ actual fun PlatformTextField( capitalization = KeyboardCapitalization.Sentences, autoCorrect = true ), - modifier = Modifier.padding(vertical = 4.dp).focusRequester(focusRequester), + modifier = Modifier + .padding(vertical = 4.dp) + .focusRequester(focusRequester) + .onPreviewKeyEvent { + if (it.key == Key.Enter && it.type == KeyEventType.KeyDown) { + if (it.isShiftPressed) { + val newText = textFieldValue.text + "\n" + textFieldValueState = textFieldValue.copy( + text = newText, + selection = TextRange(newText.length, newText.length) + ) + onMessageChange(newText) + } else if (cs.message.isNotEmpty()) { + onDone() + } + true + } else false + }, cursorBrush = SolidColor(MaterialTheme.colors.secondary), decorationBox = { innerTextField -> Surface(