From 760282cdfde74427a1fe1821b40bf0aaded8d554 Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Thu, 3 Aug 2023 12:32:01 +0300 Subject: [PATCH] desktop: edit previous message with Up arrow (#2841) --- .../simplex/common/platform/PlatformTextField.android.kt | 1 + .../chat/simplex/common/platform/PlatformTextField.kt | 1 + .../kotlin/chat/simplex/common/views/TerminalView.kt | 1 + .../kotlin/chat/simplex/common/views/chat/ComposeView.kt | 9 +++++++++ .../kotlin/chat/simplex/common/views/chat/SendMsgView.kt | 6 +++++- .../simplex/common/platform/PlatformTextField.desktop.kt | 7 ++++++- 6 files changed, 23 insertions(+), 2 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 e9ff5687d9..ad07c6a33c 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 @@ -48,6 +48,7 @@ actual fun PlatformTextField( showDeleteTextButton: MutableState, userIsObserver: Boolean, onMessageChange: (String) -> Unit, + onUpArrow: () -> Unit, onDone: () -> Unit, ) { val cs = composeState.value 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 19c0fe01b3..4a8a2e204f 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 @@ -12,5 +12,6 @@ expect fun PlatformTextField( showDeleteTextButton: MutableState, userIsObserver: Boolean, onMessageChange: (String) -> Unit, + onUpArrow: () -> Unit, onDone: () -> Unit, ) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/TerminalView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/TerminalView.kt index faf97b4854..e8af0e71a9 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/TerminalView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/TerminalView.kt @@ -93,6 +93,7 @@ fun TerminalLayout( sendMessage = { sendCommand() }, sendLiveMessage = null, updateLiveMessage = null, + editPrevMessage = {}, onMessageChange = ::onMessageChange, textStyle = textStyle ) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ComposeView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ComposeView.kt index 143815ddc2..d4c84624af 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ComposeView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ComposeView.kt @@ -556,6 +556,14 @@ fun ComposeView( } } + fun editPrevMessage() { + if (composeState.value.contextItem != ComposeContextItem.NoContextItem || composeState.value.preview != ComposePreview.NoPreview) return + val lastEditable = chatModel.chatItems.findLast { it.meta.editable } + if (lastEditable != null) { + composeState.value = ComposeState(editingItem = lastEditable, useLinkPreviews = useLinkPreviews) + } + } + @Composable fun previewView() { when (val preview = composeState.value.preview) { @@ -754,6 +762,7 @@ fun ComposeView( composeState.value = composeState.value.copy(liveMessage = null) chatModel.removeLiveDummy() }, + editPrevMessage = ::editPrevMessage, onMessageChange = ::onMessageChange, textStyle = textStyle ) 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 a9ca677df9..4d9e636ad8 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 @@ -48,6 +48,7 @@ fun SendMsgView( sendLiveMessage: (suspend () -> Unit)? = null, updateLiveMessage: (suspend () -> Unit)? = null, cancelLiveMessage: (() -> Unit)? = null, + editPrevMessage: () -> Unit, onMessageChange: (String) -> Unit, textStyle: MutableState ) { @@ -67,7 +68,7 @@ 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, editPrevMessage) { sendMessage(null) } // Disable clicks on text field @@ -593,6 +594,7 @@ fun PreviewSendMsgView() { allowVoiceToContact = {}, timedMessageAllowed = false, sendMessage = {}, + editPrevMessage = {}, onMessageChange = { _ -> }, textStyle = textStyle ) @@ -623,6 +625,7 @@ fun PreviewSendMsgViewEditing() { allowVoiceToContact = {}, timedMessageAllowed = false, sendMessage = {}, + editPrevMessage = {}, onMessageChange = { _ -> }, textStyle = textStyle ) @@ -653,6 +656,7 @@ fun PreviewSendMsgViewInProgress() { allowVoiceToContact = {}, timedMessageAllowed = false, sendMessage = {}, + editPrevMessage = {}, onMessageChange = { _ -> }, textStyle = textStyle ) 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 a8278f5331..eef710711d 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 @@ -37,6 +37,7 @@ actual fun PlatformTextField( showDeleteTextButton: MutableState, userIsObserver: Boolean, onMessageChange: (String) -> Unit, + onUpArrow: () -> Unit, onDone: () -> Unit, ) { val cs = composeState.value @@ -83,7 +84,11 @@ actual fun PlatformTextField( onDone() } true - } else false + } else if (it.key == Key.DirectionUp && it.type == KeyEventType.KeyDown && cs.message.isEmpty()) { + onUpArrow() + true + } + else false }, cursorBrush = SolidColor(MaterialTheme.colors.secondary), decorationBox = { innerTextField ->