objects/input.go

98 lines
2.6 KiB
Go
Raw Normal View History

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/theme"
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 {
enter event.FuncBroadcaster
}
}
// NewTextInput creates a new text input containing the specified text.
func NewTextInput (text string) *TextInput {
this := &TextInput { TextBox: tomo.NewTextBox() }
2023-08-10 15:52:01 -06:00
theme.Apply(this, theme.R("objects", "TextInput", ""))
2023-08-09 09:35:24 -06:00
this.SetText(text)
this.SetFocusable(true)
this.SetSelectable(true)
2023-09-14 12:48:08 -06:00
this.SetOverflow(true, false)
2023-08-09 09:35:24 -06:00
this.OnKeyDown(this.handleKeyDown)
2023-09-14 12:48:08 -06:00
this.OnScroll(this.handleScroll)
2023-08-09 09:35:24 -06:00
return this
}
// SetText sets the text content of the input.
func (this *TextInput) SetText (text string) {
this.text = []rune(text)
this.TextBox.SetText(text)
}
// Text returns the text content of the input.
func (this *TextInput) Text () string {
return string(this.text)
}
// OnEnter specifies a function to be called when the user presses enter within
// the text input.
func (this *TextInput) OnEnter (callback func ()) event.Cookie {
return this.on.enter.Connect(callback)
}
func (this *TextInput) handleKeyDown (key input.Key, numpad bool) {
dot := this.Dot()
modifiers := this.Modifiers()
word := modifiers.Control
sel := modifiers.Shift
changed := false
switch {
case key == input.KeyEnter:
this.on.enter.Broadcast()
case key == input.KeyHome || (modifiers.Alt && key == input.KeyLeft):
dot.End = 0
if !sel { dot.Start = dot.End }
case key == input.KeyEnd || (modifiers.Alt && key == input.KeyRight):
dot.End = len(this.text)
if !sel { dot.Start = dot.End }
case key == input.KeyLeft:
if sel {
dot = text.SelectLeft(this.text, dot, word)
} else {
dot = text.MoveLeft(this.text, dot, word)
}
case key == input.KeyRight:
if sel {
dot = text.SelectRight(this.text, dot, word)
} else {
dot = text.MoveRight(this.text, dot, word)
}
case key == input.KeyBackspace:
this.text, dot = text.Backspace(this.text, dot, word)
changed = true
case key == input.KeyDelete:
this.text, dot = text.Delete(this.text, dot, word)
changed = true
case key == input.Key('a') && modifiers.Control:
dot.Start = 0
dot.End = len(this.text)
case key.Printable():
this.text, dot = text.Type(this.text, dot, rune(key))
changed = true
}
this.Select(dot)
if changed { this.SetText(string(this.text)) }
}
2023-09-14 12:48:08 -06:00
func (this *TextInput) handleScroll (x, y float64) {
this.ScrollTo(this.ContentBounds().Min.Add(image.Pt(int(x), int(y))))
}