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

@ -10,27 +10,47 @@ var _ tomo.ContentObject = new(TextInput)
// TextInput is a single-line editable text box. // TextInput is a single-line editable text box.
type TextInput struct { type TextInput struct {
box tomo.TextBox box tomo.TextBox
text []rune text []rune
multiline bool
on struct { on struct {
valueChange event.FuncBroadcaster valueChange event.FuncBroadcaster
confirm event.FuncBroadcaster confirm event.FuncBroadcaster
} }
} }
// NewTextInput creates a new text input containing the specified text. func newTextInput (text string, multiline bool) *TextInput {
func NewTextInput (text string) *TextInput { textInput := &TextInput {
textInput := &TextInput { box: tomo.NewTextBox() } box: tomo.NewTextBox(),
multiline: multiline,
}
textInput.box.SetRole(tomo.R("objects", "TextInput")) textInput.box.SetRole(tomo.R("objects", "TextInput"))
textInput.box.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle)) textInput.box.SetTag("multiline", multiline)
textInput.box.SetAttr(tomo.AOverflow(true, false)) 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.SetValue(text)
textInput.box.SetFocusable(true) textInput.box.SetFocusable(true)
textInput.box.SetSelectable(true) textInput.box.SetSelectable(true)
textInput.box.OnKeyDown(textInput.handleKeyDown) textInput.box.OnKeyDown(textInput.handleKeyDown)
textInput.box.OnKeyUp(textInput.handleKeyUp) textInput.box.OnKeyUp(textInput.handleKeyUp)
textInput.box.OnScroll(textInput.handleScroll) textInput.box.OnScroll(textInput.handleScroll)
return 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. // GetBox returns the underlying 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 { switch {
case isConfirmationKey(key): case isConfirmationKey(key):
this.on.confirm.Broadcast() this.on.confirm.Broadcast()
@ -136,8 +169,7 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
changed = true changed = true
return true return true
case key.Printable() && !modifiers.Control: case key.Printable() && !modifiers.Control:
this.text, dot = text.Type(this.text, dot, rune(key)) typ()
changed = true
return true return true
default: default:
return false 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 { func (this *TextInput) handleKeyUp (key input.Key, numpad bool) bool {
modifiers := this.box.Window().Modifiers() modifiers := this.box.Window().Modifiers()
if this.multiline && !modifiers.Control {
switch {
case key == '\n', key == '\t':
return true
}
}
switch { switch {
case isConfirmationKey(key): case isConfirmationKey(key):
return true return true