304 lines
7.7 KiB
Go
304 lines
7.7 KiB
Go
package objects
|
|
|
|
import "time"
|
|
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"
|
|
import "git.tebibyte.media/tomo/objects/internal"
|
|
|
|
const textInputHistoryMaximum = 64
|
|
const textInputHistoryMaxAge = time.Second / 4
|
|
var _ tomo.ContentObject = new(TextInput)
|
|
|
|
type textHistoryItem struct {
|
|
text string
|
|
dot text.Dot
|
|
}
|
|
|
|
// TextInput is a single-line editable text box.
|
|
type TextInput struct {
|
|
box tomo.TextBox
|
|
text []rune
|
|
multiline bool
|
|
history *internal.History[textHistoryItem]
|
|
on struct {
|
|
dotChange event.FuncBroadcaster
|
|
valueChange event.FuncBroadcaster
|
|
confirm event.FuncBroadcaster
|
|
}
|
|
}
|
|
|
|
func newTextInput (text string, multiline bool) *TextInput {
|
|
textInput := &TextInput {
|
|
box: tomo.NewTextBox(),
|
|
text: []rune(text),
|
|
multiline: multiline,
|
|
history: internal.NewHistory[textHistoryItem] (
|
|
textHistoryItem { text: text },
|
|
textInputHistoryMaximum),
|
|
}
|
|
textInput.box.SetRole(tomo.R("objects", "TextInput"))
|
|
textInput.box.SetTag("multiline", multiline)
|
|
if multiline {
|
|
textInput.box.SetAttr(tomo.AOverflow(false, true))
|
|
textInput.box.SetAttr(tomo.AWrap(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.box.SetText(text)
|
|
textInput.box.SetFocusable(true)
|
|
textInput.box.SetSelectable(true)
|
|
textInput.box.OnKeyDown(textInput.handleKeyDown)
|
|
textInput.box.OnKeyUp(textInput.handleKeyUp)
|
|
textInput.box.OnScroll(textInput.handleScroll)
|
|
textInput.box.OnDotChange(textInput.handleDotChange)
|
|
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.
|
|
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)
|
|
this.historySwapDot()
|
|
}
|
|
|
|
// Dot returns the text cursor or selection.
|
|
func (this *TextInput) Dot () text.Dot {
|
|
return this.box.Dot()
|
|
}
|
|
|
|
// OnDotChange specifies a function to be called when the text cursor or
|
|
// selection changes.
|
|
func (this *TextInput) OnDotChange (callback func ()) event.Cookie {
|
|
return this.on.dotChange.Connect(callback)
|
|
}
|
|
|
|
// SetAlign sets the X and Y alignment of the text input.
|
|
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.
|
|
func (this *TextInput) SetValue (text string) {
|
|
this.text = []rune(text)
|
|
this.box.SetText(text)
|
|
this.logLargeAction()
|
|
}
|
|
|
|
// Value returns the text content of the input.
|
|
func (this *TextInput) Value () string {
|
|
return string(this.text)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// Undo undoes the last action.
|
|
func (this *TextInput) Undo () {
|
|
this.recoverHistoryItem(this.history.Undo())
|
|
}
|
|
|
|
// Redo redoes the last previously undone action.
|
|
func (this *TextInput) Redo () {
|
|
this.recoverHistoryItem(this.history.Redo())
|
|
}
|
|
|
|
// Type types a character at the current dot position.
|
|
func (this *TextInput) Type (char rune) {
|
|
dot := this.Dot()
|
|
this.historySwapDot()
|
|
this.text, dot = text.Type(this.text, dot, rune(char))
|
|
this.Select(dot)
|
|
this.box.SetText(string(this.text))
|
|
this.logKeystroke()
|
|
}
|
|
|
|
func (this *TextInput) logKeystroke () {
|
|
if this.Dot().Empty() {
|
|
this.history.PushWeak (
|
|
this.currentHistoryState(),
|
|
textInputHistoryMaxAge)
|
|
} else {
|
|
this.logLargeAction()
|
|
}
|
|
}
|
|
|
|
func (this *TextInput) logLargeAction () {
|
|
this.history.Push(this.currentHistoryState())
|
|
}
|
|
|
|
func (this *TextInput) historySwapDot () {
|
|
top := this.history.Top()
|
|
top.dot = this.Dot()
|
|
this.history.SwapSilently(top)
|
|
}
|
|
|
|
func (this *TextInput) currentHistoryState () textHistoryItem {
|
|
return textHistoryItem {
|
|
text: string(this.text),
|
|
dot: this.Dot(),
|
|
}
|
|
}
|
|
|
|
func (this *TextInput) recoverHistoryItem (item textHistoryItem) {
|
|
this.box.SetText(item.text)
|
|
this.text = []rune(item.text)
|
|
this.box.Select(item.dot)
|
|
}
|
|
|
|
// TODO: add things like alt+up/down to move lines
|
|
|
|
func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
|
|
dot := this.Dot()
|
|
txt := this.text
|
|
modifiers := this.box.Window().Modifiers()
|
|
word := modifiers.Control
|
|
changed := false
|
|
|
|
defer func () {
|
|
if changed {
|
|
this.historySwapDot()
|
|
this.text = txt
|
|
this.box.SetText(string(txt))
|
|
this.box.Select(dot)
|
|
this.on.valueChange.Broadcast()
|
|
this.on.dotChange.Broadcast()
|
|
this.logKeystroke()
|
|
}
|
|
} ()
|
|
|
|
typeRune := func () {
|
|
txt, dot = text.Type(txt, dot, rune(key))
|
|
changed = true
|
|
}
|
|
|
|
if this.multiline && !modifiers.Control {
|
|
switch {
|
|
case key == '\n', key == '\t':
|
|
typeRune()
|
|
return true
|
|
}
|
|
}
|
|
|
|
switch {
|
|
case isConfirmationKey(key):
|
|
this.on.confirm.Broadcast()
|
|
return true
|
|
case key == input.KeyBackspace:
|
|
txt, dot = text.Backspace(txt, dot, word)
|
|
changed = true
|
|
return true
|
|
case key == input.KeyDelete:
|
|
txt, dot = text.Delete(txt, dot, word)
|
|
changed = true
|
|
return true
|
|
case key.Printable() && !modifiers.Control:
|
|
typeRune()
|
|
return true
|
|
case key == 'z' || key == 'Z' && modifiers.Control:
|
|
if modifiers.Shift {
|
|
this.Redo()
|
|
} else {
|
|
this.Undo()
|
|
}
|
|
return true
|
|
case key == 'y' && modifiers.Control:
|
|
this.Redo()
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func (this *TextInput) handleKeyUp (key input.Key, numpad bool) bool {
|
|
modifiers := this.box.Window().Modifiers()
|
|
|
|
if this.multiline && !modifiers.Control {
|
|
switch {
|
|
case key == '\n', key == '\t':
|
|
return true
|
|
}
|
|
}
|
|
|
|
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
|
|
case key == 'z' && modifiers.Control:
|
|
return true
|
|
case key == 'y' && 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
|
|
}
|
|
|
|
func (this *TextInput) handleDotChange () {
|
|
this.historySwapDot()
|
|
this.on.dotChange.Broadcast()
|
|
}
|