From 16a0786d540d12481a2bbffeb0bf0811e4b9b44c Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 19 Sep 2020 17:27:54 +0200 Subject: [PATCH] Fixed bug adding line break in messages sent using "Enter" --- .../envoy/client/ui/controller/ChatScene.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/client/src/main/java/envoy/client/ui/controller/ChatScene.java b/client/src/main/java/envoy/client/ui/controller/ChatScene.java index 912076a..42e9f87 100644 --- a/client/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/client/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -542,16 +542,20 @@ public final class ChatScene implements EventListener, Restorable { */ @FXML private void checkKeyCombination(KeyEvent e) { + // Checks whether the text is too long messageTextUpdated(); + // Sending an IsTyping event if none has been sent for // IsTyping#millisecondsActive if (currentChat.getLastWritingEvent() + IsTyping.millisecondsActive <= System.currentTimeMillis()) { eventBus.dispatch(new SendEvent(new IsTyping(getChatID(), currentChat.getRecipient().getID()))); currentChat.lastWritingEventWasNow(); } - // Automatic sending of messages via (ctrl +) enter - checkPostConditions(e); + + // KeyPressed will be called before the char has been added to the text, hence + // this is needed for the first char + if (messageTextArea.getText().length() == 1) checkPostConditions(e); } /** @@ -572,8 +576,16 @@ public final class ChatScene implements EventListener, Restorable { */ @FXML private void checkPostConditions(KeyEvent e) { - checkPostConditions(settings.isEnterToSend() && e.getCode() == KeyCode.ENTER - || !settings.isEnterToSend() && e.getCode() == KeyCode.ENTER && e.isControlDown()); + final var messagePosted = settings.isEnterToSend() && e.getCode() == KeyCode.ENTER + || !settings.isEnterToSend() && e.getCode() == KeyCode.ENTER && e.isControlDown(); + if (messagePosted) { + + // Removing an inserted line break if added by pressing enter + final var text = messageTextArea.getText(); + final var caretPosition = messageTextArea.getCaretPosition(); + if (text.charAt(caretPosition - 1) == '\n') messageTextArea.setText(new StringBuilder(text).deleteCharAt(caretPosition - 1).toString()); + } + checkPostConditions(messagePosted); } private void checkPostConditions(boolean postMessage) {