Added text changed event to TextBox.

This commit is contained in:
Sander Schobers 2021-06-17 19:34:37 +02:00
parent 7fa5601307
commit e5bfd1394c

View File

@ -37,6 +37,8 @@ type TextBox struct {
Focus bool Focus bool
Text string Text string
Selection TextSelection Selection TextSelection
textChanged Events
} }
func BuildTextBox(fn func(*TextBox)) *TextBox { func BuildTextBox(fn func(*TextBox)) *TextBox {
@ -64,6 +66,8 @@ func (b *TextBox) DesiredSize(ctx Context, _ geom.PointF32) geom.PointF32 {
return geom.PtF32(width+pad*2, height+pad*2) return geom.PtF32(width+pad*2, height+pad*2)
} }
func (b *TextBox) TextChanged() *Events { return &b.textChanged }
func (b *TextBox) mousePosToCaretPos(ctx Context, e MouseEvent) int { func (b *TextBox) mousePosToCaretPos(ctx Context, e MouseEvent) int {
p := b.ToControlPosition(e.Pos()) p := b.ToControlPosition(e.Pos())
offset := p.X - b.box.bounds.Min.X offset := p.X - b.box.bounds.Min.X
@ -93,13 +97,13 @@ func (b *TextBox) mousePosToCaretPos(ctx Context, e MouseEvent) int {
return carets[2] return carets[2]
} }
func (b *TextBox) cut() string { func (b *TextBox) cut(ctx Context) string {
start, end := b.selectionRange() start, end := b.selectionRange()
if end == 0 { if end == 0 {
return "" return ""
} }
cut := b.Text[start:end] cut := b.Text[start:end]
b.Text = b.Text[:start] + b.Text[end:] b.updateText(ctx, b.Text[:start]+b.Text[end:])
b.Selection.Caret = start b.Selection.Caret = start
b.Selection.SetSelectionToCaret() b.Selection.SetSelectionToCaret()
return cut return cut
@ -170,20 +174,20 @@ func (b *TextBox) Handle(ctx Context, e Event) bool {
switch { switch {
case e.Key == KeyDelete: case e.Key == KeyDelete:
if b.Selection.HasSelection() { if b.Selection.HasSelection() {
b.cut() b.cut(ctx)
} else { } else {
caret := b.Selection.Caret caret := b.Selection.Caret
if caret < len(b.Text) { if caret < len(b.Text) {
b.Text = b.Text[:caret] + b.Text[caret+1:] b.updateText(ctx, b.Text[:caret]+b.Text[caret+1:])
} }
} }
case e.Key == KeyBackspace: case e.Key == KeyBackspace:
if b.Selection.HasSelection() { if b.Selection.HasSelection() {
b.cut() b.cut(ctx)
} else { } else {
caret := b.Selection.Caret caret := b.Selection.Caret
if caret > 0 { if caret > 0 {
b.Text = b.Text[:caret-1] + b.Text[caret:] b.updateText(ctx, b.Text[:caret-1]+b.Text[caret:])
b.Selection.Caret = caret - 1 b.Selection.Caret = caret - 1
b.Selection.SetSelectionToCaret() b.Selection.SetSelectionToCaret()
} }
@ -221,22 +225,22 @@ func (b *TextBox) Handle(ctx Context, e Event) bool {
return r return r
}, text) }, text)
if err == nil { if err == nil {
b.cut() b.cut(ctx)
caret := b.Selection.Caret caret := b.Selection.Caret
b.Text = b.Text[:caret] + text + b.Text[caret:] b.updateText(ctx, b.Text[:caret]+text+b.Text[caret:])
b.Selection.Caret = caret + len(text) b.Selection.Caret = caret + len(text)
} }
case KeyX: case KeyX:
DefaultClipboard.WriteText(b.cut()) DefaultClipboard.WriteText(b.cut(ctx))
} }
} }
return true return true
case *TextInputEvent: case *TextInputEvent:
if b.Selection.HasSelection() { if b.Selection.HasSelection() {
b.cut() b.cut(ctx)
} }
caret := b.Selection.Caret caret := b.Selection.Caret
b.Text = fmt.Sprintf("%s%c%s", b.Text[:caret], e.Character, b.Text[caret:]) b.updateText(ctx, fmt.Sprintf("%s%c%s", b.Text[:caret], e.Character, b.Text[caret:]))
b.Selection.Caret = caret + 1 b.Selection.Caret = caret + 1
b.Selection.SetSelectionToCaret() b.Selection.SetSelectionToCaret()
return true return true
@ -272,3 +276,11 @@ func (b *TextBox) Render(ctx Context) {
} }
}) })
} }
func (b *TextBox) updateText(ctx Context, text string) {
if b.Text == text {
return
}
b.Text = text
b.textChanged.Notify(ctx, text)
}