Fixed hopefully every bug concerning "enter to send" ability #36

Merged
delvh merged 5 commits from b/message-text-area into develop 2020-09-21 20:21:47 +02:00
Showing only changes of commit 16a0786d54 - Show all commits

View File

@ -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) {