mirror of
https://gitlab.com/cc-ru/ocelot/ocelot-desktop.git
synced 2026-01-07 19:52:39 +01:00
Auto focus text input
This commit is contained in:
parent
f00091da8d
commit
5eebba96ae
@ -15,6 +15,7 @@ class SetLabelDialog(node: Node) extends ModalDialog {
|
||||
}, Padding2D(bottom = 16))
|
||||
|
||||
children :+= new PaddingBox(new TextInput(node.label.getOrElse("")) {
|
||||
focus()
|
||||
override def onInput(text: String): Unit = {
|
||||
node.label = if (text.isEmpty) None else Some(text)
|
||||
}
|
||||
|
||||
@ -16,7 +16,7 @@ import scala.collection.mutable.ArrayBuffer
|
||||
class TextInput(val initialText: String = "") extends Widget with ClickHandler {
|
||||
def onInput(text: String): Unit = {}
|
||||
|
||||
private var isFocused = false
|
||||
var isFocused = false
|
||||
|
||||
private var cursorPos = 0
|
||||
private var cursorOffset = 0f
|
||||
@ -37,16 +37,11 @@ class TextInput(val initialText: String = "") extends Widget with ClickHandler {
|
||||
|
||||
eventHandlers += {
|
||||
case MouseEvent(MouseEvent.State.Release, MouseEvent.Button.Left) =>
|
||||
if (isFocused && !clippedBounds.contains(UiHandler.mousePosition)) {
|
||||
isFocused = false
|
||||
borderAnimation.goto(UnfocusedBorderColor)
|
||||
}
|
||||
if (isFocused && !clippedBounds.contains(UiHandler.mousePosition))
|
||||
unfocus()
|
||||
|
||||
case ClickEvent(MouseEvent.Button.Left, _) =>
|
||||
if (!isFocused)
|
||||
borderAnimation.goto(FocusedBorderColor)
|
||||
isFocused = true
|
||||
blinkTimer = 0
|
||||
focus()
|
||||
|
||||
case KeyEvent(KeyEvent.State.Press | KeyEvent.State.Repeat, Keyboard.KEY_LEFT, _) =>
|
||||
events += new CursorMoveLeft
|
||||
@ -78,6 +73,17 @@ class TextInput(val initialText: String = "") extends Widget with ClickHandler {
|
||||
override def minimumSize: Size2D = Size2D(200, 24)
|
||||
override def maximumSize: Size2D = Size2D(Float.PositiveInfinity, 24)
|
||||
|
||||
def focus(): Unit = {
|
||||
borderAnimation.goto(FocusedBorderColor)
|
||||
isFocused = true
|
||||
blinkTimer = 0
|
||||
}
|
||||
|
||||
def unfocus(): Unit = {
|
||||
isFocused = false
|
||||
borderAnimation.goto(UnfocusedBorderColor)
|
||||
}
|
||||
|
||||
override def draw(g: Graphics): Unit = {
|
||||
if (textWidth == 0f && text.nonEmpty)
|
||||
textWidth = text.map(g.font.charWidth(_)).sum
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user