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
history *history.History[textHistoryItem]
on struct {
dotChange event.FuncBroadcaster
valueChange event.FuncBroadcaster
confirm event.FuncBroadcaster
}
@ -54,6 +55,7 @@ func newTextInput (text string, multiline bool) *TextInput {
textInput.box.OnKeyDown(textInput.handleKeyDown)
textInput.box.OnKeyUp(textInput.handleKeyUp)
textInput.box.OnScroll(textInput.handleScroll)
textInput.box.OnDotChange(textInput.handleDotChange)
return textInput
}
@ -83,6 +85,7 @@ func (this *TextInput) SetFocused (focused bool) {
// 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.
@ -93,7 +96,7 @@ func (this *TextInput) Dot () text.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.box.OnDotChange(callback)
return this.on.dotChange.Connect(callback)
}
// SetAlign sets the X and Y alignment of the text input.
@ -176,6 +179,12 @@ 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),
@ -193,29 +202,31 @@ func (this *TextInput) recoverHistoryItem (item textHistoryItem) {
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 () {
this.Select(dot)
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.logKeystroke()
}
} ()
typ := func () {
this.text, dot = text.Type(this.text, dot, rune(key))
typeRune := func () {
txt, dot = text.Type(txt, dot, rune(key))
changed = true
}
if this.multiline && !modifiers.Control {
switch {
case key == '\n', key == '\t':
typ()
this.logKeystroke()
typeRune()
return true
}
}
@ -225,15 +236,15 @@ func (this *TextInput) handleKeyDown (key input.Key, numpad bool) bool {
this.on.confirm.Broadcast()
return true
case key == input.KeyBackspace:
this.text, dot = text.Backspace(this.text, dot, word)
txt, dot = text.Backspace(txt, dot, word)
changed = true
return true
case key == input.KeyDelete:
this.text, dot = text.Delete(this.text, dot, word)
txt, dot = text.Delete(txt, dot, word)
changed = true
return true
case key.Printable() && !modifiers.Control:
typ()
typeRune()
return true
case key == 'z' && modifiers.Control:
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))))
return true
}
func (this *TextInput) handleDotChange () {
println("asdjhk")
this.historySwapDot()
this.on.dotChange.Broadcast()
}