From 831d7ef26274a5e173d3c4df5d5c3e11e82a04f3 Mon Sep 17 00:00:00 2001 From: IARI Date: Sat, 13 Jun 2020 10:20:30 +0200 Subject: [PATCH] consistent cursor positioning between one- and multiuline label-text editing --- .../view/fragments/TextContentFragment.kt | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/julianjarecki/ettiketten/view/fragments/TextContentFragment.kt b/src/main/kotlin/com/julianjarecki/ettiketten/view/fragments/TextContentFragment.kt index 138ff82..2bb86a3 100644 --- a/src/main/kotlin/com/julianjarecki/ettiketten/view/fragments/TextContentFragment.kt +++ b/src/main/kotlin/com/julianjarecki/ettiketten/view/fragments/TextContentFragment.kt @@ -6,6 +6,8 @@ import com.julianjarecki.ettiketten.styles.Styles import com.julianjarecki.tfxserializer.utils.colorpicker import javafx.beans.value.ObservableValue import javafx.event.EventTarget +import javafx.scene.control.TextArea +import javafx.scene.control.TextField import javafx.scene.input.KeyCode import javafx.scene.input.KeyEvent import tornadofx.* @@ -13,19 +15,28 @@ import tornadofx.* class TextContentFragment : ItemFragment() { val textContent = TextContentModel().bindTo(this) + var textField: TextField by singleAssign() + var textArea: TextArea by singleAssign() + override val root = vbox { hbox { - textfield(textContent.text) { + textField = textfield(textContent.text) { removeWhen(textContent.multiline) whenVisible { requestFocus() + positionCaret(textArea.caretPosition) } - this.addEventFilter(KeyEvent.KEY_PRESSED) { ev -> + addEventFilter(KeyEvent.KEY_PRESSED) { ev -> if (ev.isShortcutDown || ev.isShiftDown) { when (ev.code) { KeyCode.ENTER -> { - textContent.text.value += "\n" + val oldText = textContent.text.value + val caretPos = caretPosition + textContent.text.value = oldText.replaceRange( + caretPos..(caretPos - 1), "\n" + ) + textArea.positionCaret(caretPos + 1) } else -> { } @@ -33,12 +44,11 @@ class TextContentFragment : ItemFragment() { } } } - textarea(textContent.text) { + textArea = textarea(textContent.text) { minHeight = 30.0 removeWhen(!textContent.multiline) whenVisible { requestFocus() - positionCaret(textContent.text.value.length) } } colorpicker(textContent.color, ColorPickerMode.MenuButton) {