2023-08-09 09:35:24 -06:00
|
|
|
package objects
|
|
|
|
|
2024-09-05 18:11:57 -06:00
|
|
|
import "time"
|
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"
|
2024-09-10 16:29:04 -06:00
|
|
|
import "git.tebibyte.media/tomo/objects/internal"
|
2023-08-09 09:35:24 -06:00
|
|
|
|
2024-09-05 18:11:57 -06:00
|
|
|
const textInputHistoryMaximum = 64
|
|
|
|
const textInputHistoryMaxAge = time.Second / 4
|
2024-08-24 23:31:55 -06:00
|
|
|
var _ tomo.ContentObject = new(TextInput)
|
|
|
|
|
2024-09-05 18:11:57 -06:00
|
|
|
type textHistoryItem struct {
|
|
|
|
text string
|
|
|
|
dot text.Dot
|
|
|
|
}
|
|
|
|
|
2023-08-09 09:35:24 -06:00
|
|
|
// TextInput is a single-line editable text box.
|
|
|
|
type TextInput struct {
|
2024-08-25 00:47:23 -06:00
|
|
|
box tomo.TextBox
|
|
|
|
text []rune
|
|
|
|
multiline bool
|
2024-09-10 16:29:04 -06:00
|
|
|
history *internal.History[textHistoryItem]
|
2023-08-09 09:35:24 -06:00
|
|
|
on struct {
|
2024-09-05 22:12:24 -06:00
|
|
|
dotChange event.FuncBroadcaster
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-25 00:47:23 -06:00
|
|
|
func newTextInput (text string, multiline bool) *TextInput {
|
|
|
|
textInput := &TextInput {
|
|
|
|
box: tomo.NewTextBox(),
|
2024-09-05 18:11:57 -06:00
|
|
|
text: []rune(text),
|
2024-08-25 00:47:23 -06:00
|
|
|
multiline: multiline,
|
2024-09-10 16:29:04 -06:00
|
|
|
history: internal.NewHistory[textHistoryItem] (
|
2024-09-05 18:11:57 -06:00
|
|
|
textHistoryItem { text: text },
|
|
|
|
textInputHistoryMaximum),
|
2024-08-25 00:47:23 -06:00
|
|
|
}
|
2024-08-24 23:31:55 -06:00
|
|
|
textInput.box.SetRole(tomo.R("objects", "TextInput"))
|
2024-08-25 00:47:23 -06:00
|
|
|
textInput.box.SetTag("multiline", multiline)
|
|
|
|
if multiline {
|
|
|
|
textInput.box.SetAttr(tomo.AOverflow(false, true))
|
2024-08-25 00:49:08 -06:00
|
|
|
textInput.box.SetAttr(tomo.AWrap(true))
|
2024-08-25 00:47:23 -06:00
|
|
|
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))
|
|
|
|
}
|
2024-09-05 18:11:57 -06:00
|
|
|
textInput.box.SetText(text)
|
2024-08-24 23:31:55 -06:00
|
|
|
textInput.box.SetFocusable(true)
|
|
|
|
textInput.box.SetSelectable(true)
|
|
|
|
textInput.box.OnKeyDown(textInput.handleKeyDown)
|
|
|
|
textInput.box.OnKeyUp(textInput.handleKeyUp)
|
|
|
|
textInput.box.OnScroll(textInput.handleScroll)
|
2024-09-05 22:12:24 -06:00
|
|
|
textInput.box.OnDotChange(textInput.handleDotChange)
|
2024-08-25 00:47:23 -06:00
|
|
|
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)
|
2024-08-24 23:31:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2024-09-05 22:12:24 -06:00
|
|
|
this.historySwapDot()
|
2024-08-24 23:31:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dot returns the text cursor or selection.
|
|
|
|
func (this *TextInput) Dot () text.Dot {
|
|
|
|
return this.box.Dot()
|
|
|
|
}
|
|
|
|
|
2024-08-25 16:55:43 -06:00
|
|
|
// OnDotChange specifies a function to be called when the text cursor or
|
|
|
|
// selection changes.
|
|
|
|
func (this *TextInput) OnDotChange (callback func ()) event.Cookie {
|
2024-09-05 22:12:24 -06:00
|
|
|
return this.on.dotChange.Connect(callback)
|
2024-08-25 16:55:43 -06:00
|
|
|
}
|
|
|
|
|
2024-08-24 23:38:42 -06:00
|
|
|
// SetAlign sets the X and Y alignment of the text input.
|
2024-08-24 23:31:55 -06:00
|
|
|
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)
|
2023-08-09 09:35:24 -06:00
|
|
|
}
|
|
|
|
|
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)
|
2024-08-24 23:31:55 -06:00
|
|
|
this.box.SetText(text)
|
2024-09-05 18:11:57 -06:00
|
|
|
this.logLargeAction()
|
2023-08-09 09:35:24 -06:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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-09-05 18:11:57 -06:00
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
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()
|
2024-09-05 22:16:27 -06:00
|
|
|
this.historySwapDot()
|
2024-07-25 10:58:38 -06:00
|
|
|
this.text, dot = text.Type(this.text, dot, rune(char))
|
|
|
|
this.Select(dot)
|
2024-08-24 23:31:55 -06:00
|
|
|
this.box.SetText(string(this.text))
|
2024-09-05 18:11:57 -06:00
|
|
|
this.logKeystroke()
|
2024-07-25 10:58:38 -06:00
|
|
|
}
|
|
|
|
|
2024-09-05 18:11:57 -06:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2024-09-05 22:12:24 -06:00
|
|
|
func (this *TextInput) historySwapDot () {
|
|
|
|
top := this.history.Top()
|
|
|
|
top.dot = this.Dot()
|
|
|
|
this.history.SwapSilently(top)
|
|
|
|
}
|
|
|
|
|
2024-09-05 18:11:57 -06:00
|
|
|
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
|
2024-08-25 16:55:43 -06:00
|
|
|
|
2024-07-25 10:58:38 -06:00
|
|
|
func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
|
2024-09-05 18:11:57 -06:00
|
|
|
dot := this.Dot()
|
2024-09-05 22:12:24 -06:00
|
|
|
txt := this.text
|
2024-09-05 18:11:57 -06:00
|
|
|
modifiers := this.box.Window().Modifiers()
|
2024-09-12 00:34:28 -06:00
|
|
|
word := modifiers.Control()
|
2024-09-05 18:11:57 -06:00
|
|
|
changed := false
|
2023-09-14 15:03:19 -06:00
|
|
|
|
2024-07-26 16:49:54 -06:00
|
|
|
defer func () {
|
|
|
|
if changed {
|
2024-09-05 22:12:24 -06:00
|
|
|
this.historySwapDot()
|
|
|
|
this.text = txt
|
|
|
|
this.box.SetText(string(txt))
|
|
|
|
this.box.Select(dot)
|
2024-07-26 16:49:54 -06:00
|
|
|
this.on.valueChange.Broadcast()
|
2024-09-05 22:14:10 -06:00
|
|
|
this.on.dotChange.Broadcast()
|
2024-09-05 18:11:57 -06:00
|
|
|
this.logKeystroke()
|
2024-07-26 16:49:54 -06:00
|
|
|
}
|
|
|
|
} ()
|
2023-09-15 14:11:59 -06:00
|
|
|
|
2024-09-05 22:12:24 -06:00
|
|
|
typeRune := func () {
|
|
|
|
txt, dot = text.Type(txt, dot, rune(key))
|
2024-08-25 00:47:23 -06:00
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
|
2024-09-12 00:34:28 -06:00
|
|
|
if this.multiline && !modifiers.Control() {
|
2024-08-25 00:47:23 -06:00
|
|
|
switch {
|
|
|
|
case key == '\n', key == '\t':
|
2024-09-05 22:12:24 -06:00
|
|
|
typeRune()
|
2024-08-25 00:47:23 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:
|
2024-09-05 22:12:24 -06:00
|
|
|
txt, dot = text.Backspace(txt, dot, word)
|
2023-08-09 09:35:24 -06:00
|
|
|
changed = true
|
2024-07-26 16:49:54 -06:00
|
|
|
return true
|
2023-08-09 09:35:24 -06:00
|
|
|
case key == input.KeyDelete:
|
2024-09-05 22:12:24 -06:00
|
|
|
txt, dot = text.Delete(txt, dot, word)
|
2023-08-09 09:35:24 -06:00
|
|
|
changed = true
|
2024-07-26 16:49:54 -06:00
|
|
|
return true
|
2024-09-12 00:34:28 -06:00
|
|
|
case key.Printable() && !modifiers.Control():
|
2024-09-05 22:12:24 -06:00
|
|
|
typeRune()
|
2024-07-26 16:49:54 -06:00
|
|
|
return true
|
2024-09-12 00:34:28 -06:00
|
|
|
case key == 'z' || key == 'Z' && modifiers.Control():
|
|
|
|
if modifiers.Shift() {
|
2024-09-05 18:11:57 -06:00
|
|
|
this.Redo()
|
|
|
|
} else {
|
|
|
|
this.Undo()
|
|
|
|
}
|
|
|
|
return true
|
2024-09-12 00:34:28 -06:00
|
|
|
case key == 'y' && modifiers.Control():
|
2024-09-05 18:11:57 -06:00
|
|
|
this.Redo()
|
|
|
|
return true
|
2024-07-26 16:49:54 -06:00
|
|
|
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-08-24 23:31:55 -06:00
|
|
|
modifiers := this.box.Window().Modifiers()
|
2024-08-25 00:47:23 -06:00
|
|
|
|
2024-09-12 00:34:28 -06:00
|
|
|
if this.multiline && !modifiers.Control() {
|
2024-08-25 00:47:23 -06:00
|
|
|
switch {
|
|
|
|
case key == '\n', key == '\t':
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 16:49:54 -06:00
|
|
|
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-09-12 00:34:28 -06:00
|
|
|
case key.Printable() && !modifiers.Control():
|
2024-07-26 16:49:54 -06:00
|
|
|
return true
|
2024-09-12 00:34:28 -06:00
|
|
|
case key == 'z' && modifiers.Control():
|
2024-09-05 18:11:57 -06:00
|
|
|
return true
|
2024-09-12 00:34:28 -06:00
|
|
|
case key == 'y' && modifiers.Control():
|
2024-09-05 18:11:57 -06:00
|
|
|
return true
|
2024-07-26 16:49:54 -06:00
|
|
|
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
|
|
|
}
|
2024-09-05 22:12:24 -06:00
|
|
|
|
|
|
|
func (this *TextInput) handleDotChange () {
|
|
|
|
this.historySwapDot()
|
|
|
|
this.on.dotChange.Broadcast()
|
|
|
|
}
|