Compare commits
2 Commits
ac1a952b40
...
8b1b2e4199
Author | SHA1 | Date | |
---|---|---|---|
8b1b2e4199 | |||
63ad06e214 |
@ -4,7 +4,7 @@ import "time"
|
|||||||
|
|
||||||
// History stores a stack of items, always keeping the bottom-most one. It must
|
// History stores a stack of items, always keeping the bottom-most one. It must
|
||||||
// be created using the NewHistory constructor, otherwise it will be invalid.
|
// be created using the NewHistory constructor, otherwise it will be invalid.
|
||||||
type History[T any] struct {
|
type History[T comparable] struct {
|
||||||
max int
|
max int
|
||||||
stack []T
|
stack []T
|
||||||
topIndex int
|
topIndex int
|
||||||
@ -14,7 +14,7 @@ type History[T any] struct {
|
|||||||
// NewHistory creates a new History. The initial item will be on the bottom, and
|
// NewHistory creates a new History. The initial item will be on the bottom, and
|
||||||
// it will remain there until the History overflows and chooses the item after
|
// it will remain there until the History overflows and chooses the item after
|
||||||
// it to be the initial item.
|
// it to be the initial item.
|
||||||
func NewHistory[T any] (initial T, max int) *History[T] {
|
func NewHistory[T comparable] (initial T, max int) *History[T] {
|
||||||
return &History[T] {
|
return &History[T] {
|
||||||
max: max,
|
max: max,
|
||||||
stack: []T { initial },
|
stack: []T { initial },
|
||||||
@ -29,14 +29,28 @@ func (this *History[T]) Top () T {
|
|||||||
// Swap replaces the most recent item with another.
|
// Swap replaces the most recent item with another.
|
||||||
func (this *History[T]) Swap (item T) {
|
func (this *History[T]) Swap (item T) {
|
||||||
this.topTime = time.Now()
|
this.topTime = time.Now()
|
||||||
|
this.SwapSilently(item)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SwapSilently replaces the most recent item with another without updating the
|
||||||
|
// time.
|
||||||
|
func (this *History[T]) SwapSilently (item T) {
|
||||||
this.stack[this.topIndex] = item
|
this.stack[this.topIndex] = item
|
||||||
}
|
}
|
||||||
|
|
||||||
// Push pushes a new item onto the stack.
|
// Push pushes a new item onto the stack. If the stack overflows (becomes bigger
|
||||||
|
// than the specified max value), the initial item is removed and the one on top
|
||||||
|
// of it takes its place.
|
||||||
func (this *History[T]) Push (item T) {
|
func (this *History[T]) Push (item T) {
|
||||||
this.topTime = time.Now()
|
this.topTime = time.Now()
|
||||||
this.topIndex ++
|
if this.Top() != item {
|
||||||
this.stack = append(this.stack[:this.topIndex], item)
|
this.topIndex ++
|
||||||
|
this.stack = append(this.stack[:this.topIndex], item)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(this.stack) > this.max {
|
||||||
|
this.stack = this.stack[1:]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PushWeak replaces the most recent item if it was added recently (sooner than
|
// PushWeak replaces the most recent item if it was added recently (sooner than
|
||||||
|
37
textinput.go
37
textinput.go
@ -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()
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user