Skip to content

Commit

Permalink
consistent cursor positioning between one- and multiuline label-text …
Browse files Browse the repository at this point in the history
…editing
  • Loading branch information
IARI committed Jun 13, 2020
1 parent 2e48a6c commit 831d7ef
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,49 @@ 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.*

class TextContentFragment : ItemFragment<TextContent>() {
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 -> {
}
}
}
}
}
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) {
Expand Down

0 comments on commit 831d7ef

Please sign in to comment.