From 7cc49288269452e3a17bcf38f554c4eb2b9147b8 Mon Sep 17 00:00:00 2001 From: delvh Date: Sun, 20 Sep 2020 16:06:13 +0200 Subject: [PATCH] Fixed bug removing \n and added ability to use "ctrl"+"enter" for LB Fixes #34 --- .../envoy/client/ui/controller/ChatScene.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 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 c022ac7..fbec656 100644 --- a/client/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/client/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -562,7 +562,7 @@ public final class ChatScene implements EventListener, Restorable { // 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 && e != null) checkPostConditions(e); - + // This is needed for the messageTA context menu else if (e == null) checkPostConditions(false); } @@ -585,14 +585,23 @@ public final class ChatScene implements EventListener, Restorable { */ @FXML private void checkPostConditions(KeyEvent e) { - final var messagePosted = settings.isEnterToSend() && e.getCode() == KeyCode.ENTER - || !settings.isEnterToSend() && e.getCode() == KeyCode.ENTER && e.isControlDown(); + final var enterPressed = e.getCode() == KeyCode.ENTER; + final var messagePosted = enterPressed ? settings.isEnterToSend() ? !e.isControlDown() : e.isControlDown() : false; 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()); + final var textPosition = messageTextArea.getCaretPosition() - 1; + if (!e.isControlDown() && !text.isEmpty() && text.charAt(textPosition) == '\n') + messageTextArea.setText(new StringBuilder(text).deleteCharAt(textPosition).toString()); + } + + // if control is pressed, the enter press is originally invalidated. Here it'll + // be inserted again + else if (enterPressed && e.isControlDown()) { + var caretPosition = messageTextArea.getCaretPosition(); + messageTextArea.setText(new StringBuilder(messageTextArea.getText()).insert(caretPosition, '\n').toString()); + messageTextArea.positionCaret(++caretPosition); } checkPostConditions(messagePosted); }