TextInput no longer embeds tomo.TextBox
This commit is contained in:
parent
1a2449d2b7
commit
2b354979aa
91
textinput.go
91
textinput.go
@ -6,9 +6,11 @@ import "git.tebibyte.media/tomo/tomo/text"
|
|||||||
import "git.tebibyte.media/tomo/tomo/input"
|
import "git.tebibyte.media/tomo/tomo/input"
|
||||||
import "git.tebibyte.media/tomo/tomo/event"
|
import "git.tebibyte.media/tomo/tomo/event"
|
||||||
|
|
||||||
|
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 {
|
||||||
tomo.TextBox
|
box tomo.TextBox
|
||||||
text []rune
|
text []rune
|
||||||
on struct {
|
on struct {
|
||||||
valueChange event.FuncBroadcaster
|
valueChange event.FuncBroadcaster
|
||||||
@ -18,23 +20,68 @@ type TextInput struct {
|
|||||||
|
|
||||||
// NewTextInput creates a new text input containing the specified text.
|
// NewTextInput creates a new text input containing the specified text.
|
||||||
func NewTextInput (text string) *TextInput {
|
func NewTextInput (text string) *TextInput {
|
||||||
this := &TextInput { TextBox: tomo.NewTextBox() }
|
textInput := &TextInput { box: tomo.NewTextBox() }
|
||||||
this.SetRole(tomo.R("objects", "TextInput"))
|
textInput.box.SetRole(tomo.R("objects", "TextInput"))
|
||||||
this.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
|
textInput.box.SetAttr(tomo.AAlign(tomo.AlignStart, tomo.AlignMiddle))
|
||||||
this.SetAttr(tomo.AOverflow(true, false))
|
textInput.box.SetAttr(tomo.AOverflow(true, false))
|
||||||
this.SetText(text)
|
textInput.SetValue(text)
|
||||||
this.SetFocusable(true)
|
textInput.box.SetFocusable(true)
|
||||||
this.SetSelectable(true)
|
textInput.box.SetSelectable(true)
|
||||||
this.OnKeyDown(this.handleKeyDown)
|
textInput.box.OnKeyDown(textInput.handleKeyDown)
|
||||||
this.OnKeyUp(this.handleKeyUp)
|
textInput.box.OnKeyUp(textInput.handleKeyUp)
|
||||||
this.OnScroll(this.handleScroll)
|
textInput.box.OnScroll(textInput.handleScroll)
|
||||||
return this
|
return textInput
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBox returns the underlying box.
|
||||||
|
func (this *TextInput) GetBox () tomo.Box {
|
||||||
|
return this.box
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFocused sets whether or not this text input has keyboard focus. If set to
|
||||||
|
// true, this method will steal focus away from whichever object currently has
|
||||||
|
// focus.
|
||||||
|
func (this *TextInput) SetFocused (focused bool) {
|
||||||
|
this.box.SetFocused(focused)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select sets the text cursor or selection.
|
||||||
|
func (this *TextInput) Select (dot text.Dot) {
|
||||||
|
this.box.Select(dot)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dot returns the text cursor or selection.
|
||||||
|
func (this *TextInput) Dot () text.Dot {
|
||||||
|
return this.box.Dot()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAlign sets the X and Y alignment of the label.
|
||||||
|
func (this *TextInput) SetAlign (x, y tomo.Align) {
|
||||||
|
this.box.SetAttr(tomo.AAlign(x, y))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ContentBounds returns the bounds of the inner content of the text input
|
||||||
|
// relative to the input's InnerBounds.
|
||||||
|
func (this *TextInput) ContentBounds () image.Rectangle {
|
||||||
|
return this.box.ContentBounds()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ScrollTo shifts the origin of the text input's content to the origin of the
|
||||||
|
// inputs's InnerBounds, offset by the given point.
|
||||||
|
func (this *TextInput) ScrollTo (position image.Point) {
|
||||||
|
this.box.ScrollTo(position)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnContentBoundsChange specifies a function to be called when the text input's
|
||||||
|
// ContentBounds or InnerBounds changes.
|
||||||
|
func (this *TextInput) OnContentBoundsChange (callback func ()) event.Cookie {
|
||||||
|
return this.box.OnContentBoundsChange(callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetValue sets the text content of the input.
|
// SetValue sets the text content of the input.
|
||||||
func (this *TextInput) SetValue (text string) {
|
func (this *TextInput) SetValue (text string) {
|
||||||
this.text = []rune(text)
|
this.text = []rune(text)
|
||||||
this.TextBox.SetText(text)
|
this.box.SetText(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value returns the text content of the input.
|
// Value returns the text content of the input.
|
||||||
@ -42,16 +89,6 @@ func (this *TextInput) Value () string {
|
|||||||
return string(this.text)
|
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
|
// OnConfirm specifies a function to be called when the user presses enter
|
||||||
// within the text input.
|
// within the text input.
|
||||||
func (this *TextInput) OnConfirm (callback func ()) event.Cookie {
|
func (this *TextInput) OnConfirm (callback func ()) event.Cookie {
|
||||||
@ -69,19 +106,19 @@ func (this *TextInput) Type (char rune) {
|
|||||||
dot := this.Dot()
|
dot := this.Dot()
|
||||||
this.text, dot = text.Type(this.text, dot, rune(char))
|
this.text, dot = text.Type(this.text, dot, rune(char))
|
||||||
this.Select(dot)
|
this.Select(dot)
|
||||||
this.SetText(string(this.text))
|
this.box.SetText(string(this.text))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
|
func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
|
||||||
dot := this.Dot()
|
dot := this.Dot()
|
||||||
modifiers := this.Window().Modifiers()
|
modifiers := this.box.Window().Modifiers()
|
||||||
word := modifiers.Control
|
word := modifiers.Control
|
||||||
changed := false
|
changed := false
|
||||||
|
|
||||||
defer func () {
|
defer func () {
|
||||||
this.Select(dot)
|
this.Select(dot)
|
||||||
if changed {
|
if changed {
|
||||||
this.SetText(string(this.text))
|
this.box.SetText(string(this.text))
|
||||||
this.on.valueChange.Broadcast()
|
this.on.valueChange.Broadcast()
|
||||||
}
|
}
|
||||||
} ()
|
} ()
|
||||||
@ -108,7 +145,7 @@ 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.Window().Modifiers()
|
modifiers := this.box.Window().Modifiers()
|
||||||
switch {
|
switch {
|
||||||
case isConfirmationKey(key):
|
case isConfirmationKey(key):
|
||||||
return true
|
return true
|
||||||
|
Loading…
Reference in New Issue
Block a user