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()
content.Min = geom.ZeroPtF32
err := o.content.Update(ctx, content.Size())
if err != nil && err != ErrNewBufferSize {
if err != nil {
panic(err)
}
o.content.Render(ctx, o.bounds.Min, func(Context, geom.PointF32) {

View File

@ -37,8 +37,6 @@ type TextBox struct {
Focus bool
Text string
Selection TextSelection
textChanged Events
}
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)
}
func (b *TextBox) TextChanged() *Events { return &b.textChanged }
func (b *TextBox) mousePosToCaretPos(ctx Context, e MouseEvent) int {
p := b.ToControlPosition(e.Pos())
offset := p.X - b.box.bounds.Min.X
@ -97,13 +93,13 @@ func (b *TextBox) mousePosToCaretPos(ctx Context, e MouseEvent) int {
return carets[2]
}
func (b *TextBox) cut(ctx Context) string {
func (b *TextBox) cut() string {
start, end := b.selectionRange()
if end == 0 {
return ""
}
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.SetSelectionToCaret()
return cut
@ -174,20 +170,20 @@ func (b *TextBox) Handle(ctx Context, e Event) bool {
switch {
case e.Key == KeyDelete:
if b.Selection.HasSelection() {
b.cut(ctx)
b.cut()
} else {
caret := b.Selection.Caret
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:
if b.Selection.HasSelection() {
b.cut(ctx)
b.cut()
} else {
caret := b.Selection.Caret
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.SetSelectionToCaret()
}
@ -225,22 +221,22 @@ func (b *TextBox) Handle(ctx Context, e Event) bool {
return r
}, text)
if err == nil {
b.cut(ctx)
b.cut()
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)
}
case KeyX:
DefaultClipboard.WriteText(b.cut(ctx))
DefaultClipboard.WriteText(b.cut())
}
}
return true
case *TextInputEvent:
if b.Selection.HasSelection() {
b.cut(ctx)
b.cut()
}
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.SetSelectionToCaret()
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)
}