2019-04-10 19:27:24 +00:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-04-11 20:22:23 +00:00
|
|
|
"strings"
|
2019-04-10 19:27:24 +00:00
|
|
|
"time"
|
|
|
|
"unicode"
|
|
|
|
|
|
|
|
"opslag.de/schobers/geom"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TextSelection struct {
|
|
|
|
Caret int
|
|
|
|
Start int
|
|
|
|
End int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TextSelection) SetSelectionToCaret() {
|
|
|
|
s.Start = s.Caret
|
|
|
|
s.End = s.Caret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TextSelection) MoveCaret(delta int) {
|
|
|
|
s.Caret = s.Caret + delta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s TextSelection) HasSelection() bool {
|
|
|
|
return s.Start != s.End
|
|
|
|
}
|
|
|
|
|
|
|
|
type TextBox struct {
|
|
|
|
ControlBase
|
|
|
|
|
|
|
|
box BufferControl
|
|
|
|
blink time.Time
|
|
|
|
|
2019-04-11 21:27:25 +00:00
|
|
|
Focus bool
|
|
|
|
Text string
|
|
|
|
Selection TextSelection
|
2021-06-17 17:34:37 +00:00
|
|
|
|
|
|
|
textChanged Events
|
2019-04-10 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BuildTextBox(fn func(*TextBox)) *TextBox {
|
|
|
|
var b = &TextBox{}
|
|
|
|
if fn != nil {
|
|
|
|
fn(b)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TextBox) pad(ctx Context) float32 {
|
2019-04-11 21:27:25 +00:00
|
|
|
return ctx.Style().Dimensions.TextPadding
|
2019-04-10 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 21:38:32 +00:00
|
|
|
func (b *TextBox) Arrange(ctx Context, bounds geom.RectangleF32, offset geom.PointF32, parent Control) {
|
|
|
|
b.ControlBase.Arrange(ctx, bounds, offset, parent)
|
|
|
|
b.box.Arrange(ctx, bounds.Inset(b.pad(ctx)), offset, b)
|
2019-04-10 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 13:37:53 +00:00
|
|
|
func (b *TextBox) DesiredSize(ctx Context, _ geom.PointF32) geom.PointF32 {
|
2020-12-12 13:47:25 +00:00
|
|
|
font := b.Font_(ctx)
|
2019-04-10 19:27:24 +00:00
|
|
|
var width = font.WidthOf(b.Text)
|
|
|
|
var height = font.Height()
|
|
|
|
var pad = b.pad(ctx)
|
|
|
|
return geom.PtF32(width+pad*2, height+pad*2)
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:34:37 +00:00
|
|
|
func (b *TextBox) TextChanged() *Events { return &b.textChanged }
|
|
|
|
|
2019-04-10 19:27:24 +00:00
|
|
|
func (b *TextBox) mousePosToCaretPos(ctx Context, e MouseEvent) int {
|
2019-04-11 21:38:32 +00:00
|
|
|
p := b.ToControlPosition(e.Pos())
|
2019-04-10 19:27:24 +00:00
|
|
|
offset := p.X - b.box.bounds.Min.X
|
2020-12-12 13:47:25 +00:00
|
|
|
f := b.Font_(ctx)
|
2019-04-10 19:27:24 +00:00
|
|
|
var carets = [3]int{0, 0, len(b.Text)}
|
|
|
|
var offsets = [3]float32{0, 0, f.WidthOf(b.Text)}
|
|
|
|
var updateCenter = func() {
|
|
|
|
var c = (carets[0] + carets[2]) / 2
|
|
|
|
carets[1] = c
|
|
|
|
offsets[1] = f.WidthOf(b.Text[:c])
|
|
|
|
}
|
|
|
|
updateCenter()
|
|
|
|
for (carets[2] - carets[0]) > 1 {
|
|
|
|
if offset > offsets[1] {
|
|
|
|
carets[0] = carets[1]
|
|
|
|
offsets[0] = offsets[1]
|
|
|
|
updateCenter()
|
|
|
|
} else {
|
|
|
|
carets[2] = carets[1]
|
|
|
|
offsets[2] = offsets[1]
|
|
|
|
updateCenter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if geom.Abs32(offsets[0]-offset) < geom.Abs32(offsets[2]-offset) {
|
|
|
|
return carets[0]
|
|
|
|
}
|
|
|
|
return carets[2]
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:34:37 +00:00
|
|
|
func (b *TextBox) cut(ctx Context) string {
|
2019-04-10 19:27:24 +00:00
|
|
|
start, end := b.selectionRange()
|
|
|
|
if end == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
cut := b.Text[start:end]
|
2021-06-17 17:34:37 +00:00
|
|
|
b.updateText(ctx, b.Text[:start]+b.Text[end:])
|
2019-04-10 19:27:24 +00:00
|
|
|
b.Selection.Caret = start
|
|
|
|
b.Selection.SetSelectionToCaret()
|
|
|
|
return cut
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TextBox) selection() string {
|
|
|
|
start, end := b.selectionRange()
|
|
|
|
if end == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return b.Text[start:end]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TextBox) selectionRange() (int, int) {
|
|
|
|
start, end := b.Selection.Start, b.Selection.End
|
|
|
|
if start == end {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
if start > end {
|
|
|
|
start, end = end, start
|
|
|
|
}
|
|
|
|
return start, end
|
|
|
|
}
|
|
|
|
|
2020-05-15 17:00:43 +00:00
|
|
|
func (b *TextBox) Handle(ctx Context, e Event) bool {
|
|
|
|
if b.ControlBase.Handle(ctx, e) {
|
|
|
|
return true
|
|
|
|
}
|
2019-04-10 19:27:24 +00:00
|
|
|
b.box.Handle(ctx, e)
|
2020-05-15 17:00:43 +00:00
|
|
|
|
|
|
|
if b.over {
|
2020-05-16 11:46:07 +00:00
|
|
|
if b.Disabled {
|
|
|
|
return true
|
|
|
|
}
|
2020-05-15 17:00:43 +00:00
|
|
|
ctx.Renderer().SetMouseCursor(MouseCursorText)
|
|
|
|
}
|
2020-05-17 13:31:06 +00:00
|
|
|
if b.Focus {
|
|
|
|
ctx.Animate()
|
|
|
|
}
|
2020-05-15 17:00:43 +00:00
|
|
|
|
2019-04-10 19:27:24 +00:00
|
|
|
switch e := e.(type) {
|
|
|
|
case *MouseButtonDownEvent:
|
|
|
|
if b.over {
|
|
|
|
b.Focus = true
|
|
|
|
b.Selection.Caret = b.mousePosToCaretPos(ctx, e.MouseEvent)
|
|
|
|
b.Selection.SetSelectionToCaret()
|
|
|
|
b.blink = time.Now()
|
2020-05-15 17:00:43 +00:00
|
|
|
return true
|
2019-04-10 19:27:24 +00:00
|
|
|
}
|
2020-05-15 17:00:43 +00:00
|
|
|
b.Focus = false
|
2019-04-10 19:27:24 +00:00
|
|
|
case *MouseMoveEvent:
|
|
|
|
if b.Focus && b.pressed && b.over {
|
|
|
|
b.Selection.Caret = b.mousePosToCaretPos(ctx, e.MouseEvent)
|
|
|
|
b.Selection.End = b.Selection.Caret
|
2020-05-15 17:00:43 +00:00
|
|
|
return true
|
2019-04-10 19:27:24 +00:00
|
|
|
}
|
2020-05-15 07:20:44 +00:00
|
|
|
case *KeyDownEvent:
|
2019-04-11 20:22:23 +00:00
|
|
|
if !b.Focus {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
selectAfterMove := func() {
|
|
|
|
if e.Modifiers&KeyModifierShift == KeyModifierShift {
|
|
|
|
b.Selection.End = b.Selection.Caret
|
|
|
|
} else {
|
|
|
|
b.Selection.SetSelectionToCaret()
|
|
|
|
}
|
|
|
|
}
|
2019-04-11 21:38:32 +00:00
|
|
|
switch {
|
|
|
|
case e.Key == KeyDelete:
|
|
|
|
if b.Selection.HasSelection() {
|
2021-06-17 17:34:37 +00:00
|
|
|
b.cut(ctx)
|
2019-04-11 21:38:32 +00:00
|
|
|
} else {
|
|
|
|
caret := b.Selection.Caret
|
|
|
|
if caret < len(b.Text) {
|
2021-06-17 17:34:37 +00:00
|
|
|
b.updateText(ctx, b.Text[:caret]+b.Text[caret+1:])
|
2019-04-10 19:27:24 +00:00
|
|
|
}
|
2019-04-11 21:38:32 +00:00
|
|
|
}
|
|
|
|
case e.Key == KeyBackspace:
|
|
|
|
if b.Selection.HasSelection() {
|
2021-06-17 17:34:37 +00:00
|
|
|
b.cut(ctx)
|
2019-04-11 21:38:32 +00:00
|
|
|
} else {
|
|
|
|
caret := b.Selection.Caret
|
|
|
|
if caret > 0 {
|
2021-06-17 17:34:37 +00:00
|
|
|
b.updateText(ctx, b.Text[:caret-1]+b.Text[caret:])
|
2019-04-11 21:38:32 +00:00
|
|
|
b.Selection.Caret = caret - 1
|
2019-04-11 20:22:23 +00:00
|
|
|
b.Selection.SetSelectionToCaret()
|
2019-04-10 19:27:24 +00:00
|
|
|
}
|
2019-04-11 21:38:32 +00:00
|
|
|
}
|
2019-04-11 20:22:23 +00:00
|
|
|
case e.Key == KeyEnd:
|
|
|
|
b.Selection.Caret = len(b.Text)
|
|
|
|
selectAfterMove()
|
|
|
|
case e.Key == KeyHome:
|
|
|
|
b.Selection.Caret = 0
|
|
|
|
selectAfterMove()
|
2019-04-11 21:38:32 +00:00
|
|
|
case e.Key == KeyLeft:
|
|
|
|
if b.Selection.Caret > 0 {
|
|
|
|
b.Selection.MoveCaret(-1)
|
2019-04-11 20:22:23 +00:00
|
|
|
selectAfterMove()
|
2019-04-11 21:38:32 +00:00
|
|
|
}
|
|
|
|
case e.Key == KeyRight:
|
|
|
|
if b.Selection.Caret < len(b.Text) {
|
|
|
|
b.Selection.MoveCaret(1)
|
2019-04-11 20:22:23 +00:00
|
|
|
selectAfterMove()
|
2019-04-11 21:38:32 +00:00
|
|
|
}
|
2020-05-13 13:57:04 +00:00
|
|
|
case e.Modifiers&KeyModifierControl == KeyModifierControl:
|
|
|
|
switch e.Key {
|
|
|
|
case KeyA:
|
|
|
|
b.Selection.Start = 0
|
|
|
|
b.Selection.End = len(b.Text)
|
|
|
|
b.Selection.Caret = b.Selection.End
|
|
|
|
case KeyC:
|
|
|
|
DefaultClipboard.WriteText(b.selection())
|
|
|
|
case KeyV:
|
|
|
|
text, err := DefaultClipboard.ReadText()
|
|
|
|
text = strings.Map(func(r rune) rune {
|
|
|
|
if unicode.IsControl(r) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}, text)
|
|
|
|
if err == nil {
|
2021-06-17 17:34:37 +00:00
|
|
|
b.cut(ctx)
|
2020-05-13 13:57:04 +00:00
|
|
|
caret := b.Selection.Caret
|
2021-06-17 17:34:37 +00:00
|
|
|
b.updateText(ctx, b.Text[:caret]+text+b.Text[caret:])
|
2020-05-13 13:57:04 +00:00
|
|
|
b.Selection.Caret = caret + len(text)
|
|
|
|
}
|
|
|
|
case KeyX:
|
2021-06-17 17:34:37 +00:00
|
|
|
DefaultClipboard.WriteText(b.cut(ctx))
|
2020-05-13 13:57:04 +00:00
|
|
|
}
|
2019-04-10 19:27:24 +00:00
|
|
|
}
|
2020-05-15 17:00:43 +00:00
|
|
|
return true
|
2020-05-15 07:20:44 +00:00
|
|
|
case *TextInputEvent:
|
2020-05-16 11:47:25 +00:00
|
|
|
if b.Selection.HasSelection() {
|
2021-06-17 17:34:37 +00:00
|
|
|
b.cut(ctx)
|
2020-05-16 11:47:25 +00:00
|
|
|
}
|
2020-05-15 07:20:44 +00:00
|
|
|
caret := b.Selection.Caret
|
2021-06-17 17:34:37 +00:00
|
|
|
b.updateText(ctx, fmt.Sprintf("%s%c%s", b.Text[:caret], e.Character, b.Text[caret:]))
|
2020-05-15 07:20:44 +00:00
|
|
|
b.Selection.Caret = caret + 1
|
|
|
|
b.Selection.SetSelectionToCaret()
|
2020-05-15 17:00:43 +00:00
|
|
|
return true
|
2019-04-11 21:38:32 +00:00
|
|
|
}
|
2020-05-15 17:00:43 +00:00
|
|
|
return false
|
2019-04-10 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *TextBox) Render(ctx Context) {
|
|
|
|
b.RenderBackground(ctx)
|
2019-04-11 21:27:25 +00:00
|
|
|
b.RenderOutline(ctx)
|
|
|
|
|
2020-05-16 11:46:07 +00:00
|
|
|
c := b.TextColor(ctx)
|
2019-04-10 19:27:24 +00:00
|
|
|
style := ctx.Style()
|
|
|
|
var caretWidth float32 = 1
|
|
|
|
b.box.RenderFn(ctx, func(_ Context, size geom.PointF32) {
|
|
|
|
var renderer = ctx.Renderer()
|
2020-05-15 07:20:44 +00:00
|
|
|
back := b.Background
|
|
|
|
if back == nil {
|
|
|
|
back = ctx.Style().Palette.Background
|
|
|
|
}
|
|
|
|
renderer.Clear(back)
|
2020-12-12 13:47:25 +00:00
|
|
|
font := b.Font_(ctx)
|
2019-04-10 19:27:24 +00:00
|
|
|
if b.Selection.Start != b.Selection.End {
|
2020-05-15 13:42:24 +00:00
|
|
|
left, right := font.WidthOf(b.Text[:b.Selection.Start]), font.WidthOf(b.Text[:b.Selection.End])
|
2019-04-11 18:02:15 +00:00
|
|
|
renderer.FillRectangle(geom.RectF32(left, 0, right, size.Y), style.Palette.PrimaryHighlight)
|
2019-04-10 19:27:24 +00:00
|
|
|
}
|
2020-05-15 13:42:24 +00:00
|
|
|
renderer.Text(font, geom.ZeroPtF32, c, b.Text)
|
2019-04-10 19:27:24 +00:00
|
|
|
const interval = 500 * time.Millisecond
|
|
|
|
if b.Focus && (time.Since(b.blink)/interval)%2 == 0 {
|
2020-05-15 13:42:24 +00:00
|
|
|
var w = font.WidthOf(b.Text[:b.Selection.Caret])
|
2019-04-10 19:27:24 +00:00
|
|
|
var caret = w + .5*caretWidth
|
|
|
|
renderer.Rectangle(geom.RectF32(caret, 0, caret, size.Y), c, caretWidth)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-06-17 17:34:37 +00:00
|
|
|
|
|
|
|
func (b *TextBox) updateText(ctx Context, text string) {
|
|
|
|
if b.Text == text {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
b.Text = text
|
|
|
|
b.textChanged.Notify(ctx, text)
|
|
|
|
}
|