Fixed bug removing \n and added ability to use "ctrl"+"enter" for LB

Fixes #34
This commit is contained in:
Leon Hofmeister 2020-09-20 16:06:13 +02:00
parent 4959bc9634
commit 7cc4928826
Signed by: delvh
GPG Key ID: 3DECE05F6D9A647C

View File

@ -585,14 +585,23 @@ public final class ChatScene implements EventListener, Restorable {
*/ */
@FXML @FXML
private void checkPostConditions(KeyEvent e) { private void checkPostConditions(KeyEvent e) {
final var messagePosted = settings.isEnterToSend() && e.getCode() == KeyCode.ENTER final var enterPressed = e.getCode() == KeyCode.ENTER;
|| !settings.isEnterToSend() && e.getCode() == KeyCode.ENTER && e.isControlDown(); final var messagePosted = enterPressed ? settings.isEnterToSend() ? !e.isControlDown() : e.isControlDown() : false;
if (messagePosted) { if (messagePosted) {
// Removing an inserted line break if added by pressing enter // Removing an inserted line break if added by pressing enter
final var text = messageTextArea.getText(); final var text = messageTextArea.getText();
final var caretPosition = messageTextArea.getCaretPosition(); final var textPosition = messageTextArea.getCaretPosition() - 1;
if (text.charAt(caretPosition - 1) == '\n') messageTextArea.setText(new StringBuilder(text).deleteCharAt(caretPosition - 1).toString()); 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); checkPostConditions(messagePosted);
} }