2023-08-09 09:35:24 -06:00
|
|
|
package objects
|
|
|
|
|
2023-09-14 12:48:08 -06:00
|
|
|
import "image"
|
2023-08-09 09:35:24 -06:00
|
|
|
import "git.tebibyte.media/tomo/tomo"
|
|
|
|
import "git.tebibyte.media/tomo/tomo/text"
|
|
|
|
import "git.tebibyte.media/tomo/tomo/input"
|
|
|
|
import "git.tebibyte.media/tomo/tomo/event"
|
|
|
|
|
|
|
|
// TextInput is a single-line editable text box.
|
|
|
|
type TextInput struct {
|
|
|
|
tomo.TextBox
|
|
|
|
text []rune
|
|
|
|
on struct {
|
2024-06-27 12:01:14 -06:00
|
|
|
valueChange event.FuncBroadcaster
|
2024-06-27 12:09:58 -06:00
|
|
|
confirm event.FuncBroadcaster
|
2023-08-09 09:35:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTextInput creates a new text input containing the specified text.
|
|
|
|
func NewTextInput (text string) *TextInput {
|
|
|
|
this := &TextInput { TextBox: tomo.NewTextBox() }
|
2024-07-21 09:48:28 -06:00
|
|
|
this.SetRole(tomo.R("objects", "TextInput"))
|
|
|
|
this.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
|
2024-07-25 10:58:38 -06:00
|
|
|
this.SetAttr(tomo.AOverflow(true, false))
|
2023-08-09 09:35:24 -06:00
|
|
|
this.SetText(text)
|
|
|
|
this.SetFocusable(true)
|
|
|
|
this.SetSelectable(true)
|
|
|
|
this.OnKeyDown(this.handleKeyDown)
|
2024-07-25 10:58:38 -06:00
|
|
|
this.OnKeyUp(this.handleKeyUp)
|
2023-09-14 12:48:08 -06:00
|
|
|
this.OnScroll(this.handleScroll)
|
2023-08-09 09:35:24 -06:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2024-08-15 11:17:43 -06:00
|
|
|
// SetValue sets the text content of the input.
|
|
|
|
func (this *TextInput) SetValue (text string) {
|
2023-08-09 09:35:24 -06:00
|
|
|
this.text = []rune(text)
|
|
|
|
this.TextBox.SetText(text)
|
|
|
|
}
|
|
|
|
|
2024-08-15 11:17:43 -06:00
|
|
|
// Value returns the text content of the input.
|
|
|
|
func (this *TextInput) Value () string {
|
|
|
|
return string(this.text)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetText sets the text content of the input.
|
|
|
|
func (this *TextInput) SetText (text string) {
|
|
|
|
this.SetValue(text)
|
|
|
|
}
|
|
|
|
|
2023-08-09 09:35:24 -06:00
|
|
|
// Text returns the text content of the input.
|
|
|
|
func (this *TextInput) Text () string {
|
2024-08-15 11:17:43 -06:00
|
|
|
return this.Value()
|
2023-08-09 09:35:24 -06:00
|
|
|
}
|
|
|
|
|
2024-06-27 12:09:58 -06:00
|
|
|
// OnConfirm specifies a function to be called when the user presses enter
|
|
|
|
// within the text input.
|
|
|
|
func (this *TextInput) OnConfirm (callback func ()) event.Cookie {
|
|
|
|
return this.on.confirm.Connect(callback)
|
2023-08-09 09:35:24 -06:00
|
|
|
}
|
|
|
|
|
2024-06-27 12:01:14 -06:00
|
|
|
// OnValueChange specifies a function to be called when the user edits the input
|
|
|
|
// text.
|
|
|
|
func (this *TextInput) OnValueChange (callback func ()) event.Cookie {
|
|
|
|
return this.on.valueChange.Connect(callback)
|
2024-05-07 16:24:19 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
// Type types a character at the current dot position.
|
|
|
|
func (this *TextInput) Type (char rune) {
|
|
|
|
dot := this.Dot()
|
|
|
|
this.text, dot = text.Type(this.text, dot, rune(char))
|
|
|
|
this.Select(dot)
|
|
|
|
this.SetText(string(this.text))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
|
2023-08-09 09:35:24 -06:00
|
|
|
dot := this.Dot()
|
2024-07-21 09:48:28 -06:00
|
|
|
modifiers := this.Window().Modifiers()
|
2023-08-09 09:35:24 -06:00
|
|
|
word := modifiers.Control
|
|
|
|
changed := false
|
2023-09-14 15:03:19 -06:00
|
|
|
|
2024-07-26 16:49:54 -06:00
|
|
|
defer func () {
|
|
|
|
this.Select(dot)
|
|
|
|
if changed {
|
|
|
|
this.SetText(string(this.text))
|
|
|
|
this.on.valueChange.Broadcast()
|
|
|
|
}
|
|
|
|
} ()
|
2023-09-15 14:11:59 -06:00
|
|
|
|
2023-08-09 09:35:24 -06:00
|
|
|
switch {
|
2024-08-16 14:15:52 -06:00
|
|
|
case isConfirmationKey(key):
|
2024-06-27 12:09:58 -06:00
|
|
|
this.on.confirm.Broadcast()
|
2024-07-26 16:49:54 -06:00
|
|
|
return true
|
2023-08-09 09:35:24 -06:00
|
|
|
case key == input.KeyBackspace:
|
|
|
|
this.text, dot = text.Backspace(this.text, dot, word)
|
|
|
|
changed = true
|
2024-07-26 16:49:54 -06:00
|
|
|
return true
|
2023-08-09 09:35:24 -06:00
|
|
|
case key == input.KeyDelete:
|
|
|
|
this.text, dot = text.Delete(this.text, dot, word)
|
|
|
|
changed = true
|
2024-07-26 16:49:54 -06:00
|
|
|
return true
|
2024-08-16 14:17:11 -06:00
|
|
|
case key.Printable() && !modifiers.Control:
|
2023-08-09 09:35:24 -06:00
|
|
|
this.text, dot = text.Type(this.text, dot, rune(key))
|
|
|
|
changed = true
|
2024-07-26 16:49:54 -06:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
2023-08-09 09:35:24 -06:00
|
|
|
}
|
|
|
|
}
|
2023-09-14 12:48:08 -06:00
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *TextInput) handleKeyUp (key input.Key, numpad bool) bool {
|
2024-07-26 16:49:54 -06:00
|
|
|
modifiers := this.Window().Modifiers()
|
|
|
|
switch {
|
2024-08-16 14:15:52 -06:00
|
|
|
case isConfirmationKey(key):
|
2024-07-26 16:49:54 -06:00
|
|
|
return true
|
|
|
|
case key == input.KeyBackspace:
|
|
|
|
return true
|
|
|
|
case key == input.KeyDelete:
|
|
|
|
return true
|
2024-08-16 14:17:11 -06:00
|
|
|
case key.Printable() && !modifiers.Control:
|
2024-07-26 16:49:54 -06:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2024-05-26 15:13:40 -06:00
|
|
|
}
|
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *TextInput) handleScroll (x, y float64) bool {
|
2024-07-26 16:58:16 -06:00
|
|
|
if x == 0 { return false }
|
2024-07-26 16:11:02 -06:00
|
|
|
this.ScrollTo(this.ContentBounds().Min.Sub(image.Pt(int(x), int(y))))
|
2024-07-25 10:58:38 -06:00
|
|
|
return true
|
2023-09-14 12:48:08 -06:00
|
|
|
}
|