Compare commits

..

No commits in common. "bcd32f83725c98e73ec05b60e6abb769df2a4764" and "7fa56013073b7773bf51cf9e9447556a5952f5c4" have entirely different histories.

2 changed files with 12 additions and 24 deletions

View File

@ -146,7 +146,7 @@ func (o *overflow) Render(ctx Context) {
var content = o.Content.Bounds() var content = o.Content.Bounds()
content.Min = geom.ZeroPtF32 content.Min = geom.ZeroPtF32
err := o.content.Update(ctx, content.Size()) err := o.content.Update(ctx, content.Size())
if err != nil && err != ErrNewBufferSize { if err != nil {
panic(err) panic(err)
} }
o.content.Render(ctx, o.bounds.Min, func(Context, geom.PointF32) { o.content.Render(ctx, o.bounds.Min, func(Context, geom.PointF32) {

View File

@ -37,8 +37,6 @@ 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 {
@ -66,8 +64,6 @@ 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
@ -97,13 +93,13 @@ func (b *TextBox) mousePosToCaretPos(ctx Context, e MouseEvent) int {
return carets[2] return carets[2]
} }
func (b *TextBox) cut(ctx Context) string { func (b *TextBox) cut() 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.updateText(ctx, b.Text[:start]+b.Text[end:]) b.Text = b.Text[:start] + b.Text[end:]
b.Selection.Caret = start b.Selection.Caret = start
b.Selection.SetSelectionToCaret() b.Selection.SetSelectionToCaret()
return cut return cut
@ -174,20 +170,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(ctx) b.cut()
} else { } else {
caret := b.Selection.Caret caret := b.Selection.Caret
if caret < len(b.Text) { if caret < len(b.Text) {
b.updateText(ctx, b.Text[:caret]+b.Text[caret+1:]) b.Text = 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(ctx) b.cut()
} else { } else {
caret := b.Selection.Caret caret := b.Selection.Caret
if caret > 0 { if caret > 0 {
b.updateText(ctx, b.Text[:caret-1]+b.Text[caret:]) b.Text = b.Text[:caret-1] + b.Text[caret:]
b.Selection.Caret = caret - 1 b.Selection.Caret = caret - 1
b.Selection.SetSelectionToCaret() b.Selection.SetSelectionToCaret()
} }
@ -225,22 +221,22 @@ func (b *TextBox) Handle(ctx Context, e Event) bool {
return r return r
}, text) }, text)
if err == nil { if err == nil {
b.cut(ctx) b.cut()
caret := b.Selection.Caret caret := b.Selection.Caret
b.updateText(ctx, b.Text[:caret]+text+b.Text[caret:]) b.Text = 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(ctx)) DefaultClipboard.WriteText(b.cut())
} }
} }
return true return true
case *TextInputEvent: case *TextInputEvent:
if b.Selection.HasSelection() { if b.Selection.HasSelection() {
b.cut(ctx) b.cut()
} }
caret := b.Selection.Caret caret := b.Selection.Caret
b.updateText(ctx, fmt.Sprintf("%s%c%s", b.Text[:caret], e.Character, b.Text[caret:])) b.Text = 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
@ -276,11 +272,3 @@ 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)
}