Fixed bug removing \n and added ability to use "ctrl"+"enter" for LB
Fixes #34
This commit is contained in:
		@@ -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);
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user