package objects import "image" 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 { valueChange event.FuncBroadcaster confirm event.FuncBroadcaster } } // NewTextInput creates a new text input containing the specified text. func NewTextInput (text string) *TextInput { this := &TextInput { TextBox: tomo.NewTextBox() } this.SetRole(tomo.R("objects", "TextInput")) this.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle)) this.SetAttr(tomo.AOverflow(true, false)) this.SetText(text) this.SetFocusable(true) this.SetSelectable(true) this.OnKeyDown(this.handleKeyDown) this.OnKeyUp(this.handleKeyUp) this.OnScroll(this.handleScroll) return this } // SetValue sets the text content of the input. func (this *TextInput) SetValue (text string) { this.text = []rune(text) this.TextBox.SetText(text) } // 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) } // Text returns the text content of the input. func (this *TextInput) Text () string { return this.Value() } // 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) } // 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) } // 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 { dot := this.Dot() modifiers := this.Window().Modifiers() word := modifiers.Control changed := false defer func () { this.Select(dot) if changed { this.SetText(string(this.text)) this.on.valueChange.Broadcast() } } () switch { case isConfirmationKey(key): this.on.confirm.Broadcast() return true case key == input.KeyBackspace: this.text, dot = text.Backspace(this.text, dot, word) changed = true return true case key == input.KeyDelete: this.text, dot = text.Delete(this.text, dot, word) changed = true return true case key.Printable() && !modifiers.Control: this.text, dot = text.Type(this.text, dot, rune(key)) changed = true return true default: return false } } func (this *TextInput) handleKeyUp (key input.Key, numpad bool) bool { modifiers := this.Window().Modifiers() switch { case isConfirmationKey(key): return true case key == input.KeyBackspace: return true case key == input.KeyDelete: return true case key.Printable() && !modifiers.Control: return true default: return false } } func (this *TextInput) handleScroll (x, y float64) bool { if x == 0 { return false } this.ScrollTo(this.ContentBounds().Min.Sub(image.Pt(int(x), int(y)))) return true }