Text input history is looking good

This commit is contained in:
Sasha Koshka 2024-09-06 00:12:24 -04:00
parent 63ad06e214
commit 8b1b2e4199

View File

@ -24,6 +24,7 @@ type TextInput struct {
multiline bool multiline bool
history *history.History[textHistoryItem] history *history.History[textHistoryItem]
on struct { on struct {
dotChange event.FuncBroadcaster
valueChange event.FuncBroadcaster valueChange event.FuncBroadcaster
confirm event.FuncBroadcaster confirm event.FuncBroadcaster
} }
@ -54,6 +55,7 @@ func newTextInput (text string, multiline bool) *TextInput {
textInput.box.OnKeyDown(textInput.handleKeyDown) textInput.box.OnKeyDown(textInput.handleKeyDown)
textInput.box.OnKeyUp(textInput.handleKeyUp) textInput.box.OnKeyUp(textInput.handleKeyUp)
textInput.box.OnScroll(textInput.handleScroll) textInput.box.OnScroll(textInput.handleScroll)
textInput.box.OnDotChange(textInput.handleDotChange)
return textInput return textInput
} }
@ -83,6 +85,7 @@ func (this *TextInput) SetFocused (focused bool) {
// Select sets the text cursor or selection. // Select sets the text cursor or selection.
func (this *TextInput) Select (dot text.Dot) { func (this *TextInput) Select (dot text.Dot) {
this.box.Select(dot) this.box.Select(dot)
this.historySwapDot()
} }
// Dot returns the text cursor or selection. // Dot returns the text cursor or selection.
@ -93,7 +96,7 @@ func (this *TextInput) Dot () text.Dot {
// OnDotChange specifies a function to be called when the text cursor or // OnDotChange specifies a function to be called when the text cursor or
// selection changes. // selection changes.
func (this *TextInput) OnDotChange (callback func ()) event.Cookie { func (this *TextInput) OnDotChange (callback func ()) event.Cookie {
return this.box.OnDotChange(callback) return this.on.dotChange.Connect(callback)
} }
// SetAlign sets the X and Y alignment of the text input. // SetAlign sets the X and Y alignment of the text input.
@ -176,6 +179,12 @@ func (this *TextInput) logLargeAction () {
this.history.Push(this.currentHistoryState()) 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 { func (this *TextInput) currentHistoryState () textHistoryItem {
return textHistoryItem { return textHistoryItem {
text: string(this.text), text: string(this.text),
@ -193,29 +202,31 @@ func (this *TextInput) recoverHistoryItem (item textHistoryItem) {
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()
txt := this.text
modifiers := this.box.Window().Modifiers() modifiers := this.box.Window().Modifiers()
word := modifiers.Control word := modifiers.Control
changed := false changed := false
defer func () { defer func () {
this.Select(dot)
if changed { if changed {
this.box.SetText(string(this.text)) this.historySwapDot()
this.text = txt
this.box.SetText(string(txt))
this.box.Select(dot)
this.on.valueChange.Broadcast() this.on.valueChange.Broadcast()
this.logKeystroke() this.logKeystroke()
} }
} () } ()
typ := func () { typeRune := func () {
this.text, dot = text.Type(this.text, dot, rune(key)) txt, dot = text.Type(txt, dot, rune(key))
changed = true changed = true
} }
if this.multiline && !modifiers.Control { if this.multiline && !modifiers.Control {
switch { switch {
case key == '\n', key == '\t': case key == '\n', key == '\t':
typ() typeRune()
this.logKeystroke()
return true return true
} }
} }
@ -225,15 +236,15 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
this.on.confirm.Broadcast() this.on.confirm.Broadcast()
return true return true
case key == input.KeyBackspace: case key == input.KeyBackspace:
this.text, dot = text.Backspace(this.text, dot, word) txt, dot = text.Backspace(txt, dot, word)
changed = true changed = true
return true return true
case key == input.KeyDelete: case key == input.KeyDelete:
this.text, dot = text.Delete(this.text, dot, word) txt, dot = text.Delete(txt, dot, word)
changed = true changed = true
return true return true
case key.Printable() && !modifiers.Control: case key.Printable() && !modifiers.Control:
typ() typeRune()
return true return true
case key == 'z' && modifiers.Control: case key == 'z' && modifiers.Control:
if modifiers.Shift { if modifiers.Shift {
@ -283,3 +294,9 @@ func (this *TextInput) handleScroll (x, y float64) bool {
this.ScrollTo(this.ContentBounds().Min.Sub(image.Pt(int(x), int(y)))) this.ScrollTo(this.ContentBounds().Min.Sub(image.Pt(int(x), int(y))))
return true return true
} }
func (this *TextInput) handleDotChange () {
println("asdjhk")
this.historySwapDot()
this.on.dotChange.Broadcast()
}