Fixed bug adding line break in messages sent using "Enter"

This commit is contained in:
Leon Hofmeister 2020-09-19 17:27:54 +02:00
parent 40447f3f42
commit 16a0786d54
Signed by: delvh
GPG Key ID: 3DECE05F6D9A647C

View File

@ -542,16 +542,20 @@ public final class ChatScene implements EventListener, Restorable {
*/ */
@FXML @FXML
private void checkKeyCombination(KeyEvent e) { private void checkKeyCombination(KeyEvent e) {
// Checks whether the text is too long // Checks whether the text is too long
messageTextUpdated(); messageTextUpdated();
// Sending an IsTyping event if none has been sent for // Sending an IsTyping event if none has been sent for
// IsTyping#millisecondsActive // IsTyping#millisecondsActive
if (currentChat.getLastWritingEvent() + IsTyping.millisecondsActive <= System.currentTimeMillis()) { if (currentChat.getLastWritingEvent() + IsTyping.millisecondsActive <= System.currentTimeMillis()) {
eventBus.dispatch(new SendEvent(new IsTyping(getChatID(), currentChat.getRecipient().getID()))); eventBus.dispatch(new SendEvent(new IsTyping(getChatID(), currentChat.getRecipient().getID())));
currentChat.lastWritingEventWasNow(); 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 @FXML
private void checkPostConditions(KeyEvent e) { private void checkPostConditions(KeyEvent e) {
checkPostConditions(settings.isEnterToSend() && e.getCode() == KeyCode.ENTER final var messagePosted = settings.isEnterToSend() && e.getCode() == KeyCode.ENTER
|| !settings.isEnterToSend() && e.getCode() == KeyCode.ENTER && e.isControlDown()); || !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) { private void checkPostConditions(boolean postMessage) {