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 7cc4928826 - Show all commits

View File

@ -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);
}