Add multi-line text inputs

This commit is contained in:
Sasha Koshka 2024-08-25 02:47:23 -04:00
parent c7887c5ea4
commit 92e4eb970d

View File

@ -12,18 +12,27 @@ var _ tomo.ContentObject = new(TextInput)
type TextInput struct {
box tomo.TextBox
text []rune
multiline bool
on struct {
valueChange event.FuncBroadcaster
confirm event.FuncBroadcaster
}
}
// NewTextInput creates a new text input containing the specified text.
func NewTextInput (text string) *TextInput {
textInput := &TextInput { box: tomo.NewTextBox() }
func newTextInput (text string, multiline bool) *TextInput {
textInput := &TextInput {
box: tomo.NewTextBox(),
multiline: multiline,
}
textInput.box.SetRole(tomo.R("objects", "TextInput"))
textInput.box.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
textInput.box.SetTag("multiline", multiline)
if multiline {
textInput.box.SetAttr(tomo.AOverflow(false, true))
textInput.box.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignStart))
} else {
textInput.box.SetAttr(tomo.AOverflow(true, false))
textInput.box.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
}
textInput.SetValue(text)
textInput.box.SetFocusable(true)
textInput.box.SetSelectable(true)
@ -33,6 +42,17 @@ func NewTextInput (text string) *TextInput {
return textInput
}
// NewTextInput creates a new text input containing the specified text.
func NewTextInput (text string) *TextInput {
return newTextInput(text, false)
}
// NewMultilineTextInput creates a new multiline text input containing the
// specified text.
func NewMultilineTextInput (text string) *TextInput {
return newTextInput(text, true)
}
// GetBox returns the underlying box.
func (this *TextInput) GetBox () tomo.Box {
return this.box
@ -123,6 +143,19 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
}
} ()
typ := func () {
this.text, dot = text.Type(this.text, dot, rune(key))
changed = true
}
if this.multiline && !modifiers.Control {
switch {
case key == '\n', key == '\t':
typ()
return true
}
}
switch {
case isConfirmationKey(key):
this.on.confirm.Broadcast()
@ -136,8 +169,7 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
changed = true
return true
case key.Printable() && !modifiers.Control:
this.text, dot = text.Type(this.text, dot, rune(key))
changed = true
typ()
return true
default:
return false
@ -146,6 +178,14 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
func (this *TextInput) handleKeyUp (key input.Key, numpad bool) bool {
modifiers := this.box.Window().Modifiers()
if this.multiline && !modifiers.Control {
switch {
case key == '\n', key == '\t':
return true
}
}
switch {
case isConfirmationKey(key):
return true