From 8b1b2e4199759ec5efe23032b325c236b120c13f Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Fri, 6 Sep 2024 00:12:24 -0400 Subject: [PATCH] Text input history is looking good --- textinput.go | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/textinput.go b/textinput.go index 960824f..aac00fa 100644 --- a/textinput.go +++ b/textinput.go @@ -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() +}