forked from marcusolsson/tui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_edit.go
143 lines (124 loc) · 2.82 KB
/
text_edit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package tui
import (
"image"
"strings"
)
var _ Widget = &TextEdit{}
// TextEdit is a multi-line text editor.
type TextEdit struct {
WidgetBase
text RuneBuffer
offset int
onTextChange func(*TextEdit)
}
// NewTextEdit returns a new TextEdit.
func NewTextEdit() *TextEdit {
return &TextEdit{}
}
// Draw draws the entry.
func (e *TextEdit) Draw(p *Painter) {
style := "entry"
if e.IsFocused() {
style += ".focused"
}
p.WithStyle(style, func(p *Painter) {
s := e.Size()
lines := e.text.SplitByLine(s.X)
for i, line := range lines {
p.FillRect(0, i, s.X, 1)
p.DrawText(0, i, line)
}
if e.IsFocused() {
pos := e.text.CursorPos(s.X)
p.DrawCursor(pos.X, pos.Y)
}
})
}
// SizeHint returns the recommended size for the entry.
func (e *TextEdit) SizeHint() image.Point {
var max int
lines := strings.Split(e.text.String(), "\n")
for _, line := range lines {
if w := stringWidth(line); w > max {
max = w
}
}
return image.Point{max, e.text.heightForWidth(max)}
}
// OnKeyEvent handles key events.
func (e *TextEdit) OnKeyEvent(ev KeyEvent) {
if !e.IsFocused() {
return
}
if ev.Key != KeyRune {
switch ev.Key {
case KeyEnter:
e.text.WriteRune('\n')
case KeyBackspace2:
e.text.Backspace()
if e.offset > 0 && !e.isTextRemaining() {
e.offset--
}
if e.onTextChange != nil {
e.onTextChange(e)
}
case KeyDelete, KeyCtrlD:
e.text.Delete()
if e.onTextChange != nil {
e.onTextChange(e)
}
case KeyLeft, KeyCtrlB:
e.text.MoveBackward()
if e.offset > 0 {
e.offset--
}
case KeyRight, KeyCtrlF:
e.text.MoveForward()
screenWidth := e.Size().X
isCursorTooFar := e.text.CursorPos(screenWidth).X >= screenWidth
isTextLeft := (e.text.Width() - e.offset) > (screenWidth - 1)
if isCursorTooFar && isTextLeft {
e.offset++
}
case KeyHome, KeyCtrlA:
e.text.MoveToLineStart()
e.offset = 0
case KeyEnd, KeyCtrlE:
e.text.MoveToLineEnd()
left := e.text.Width() - (e.Size().X - 1)
if left >= 0 {
e.offset = left
}
case KeyCtrlK:
e.text.Kill()
}
return
}
e.text.WriteRune(ev.Rune)
if e.text.CursorPos(e.Size().X).X >= e.Size().X {
e.offset++
}
if e.onTextChange != nil {
e.onTextChange(e)
}
}
// OnTextChanged sets a function to be run whenever the text content of the
// widget has been changed.
func (e *TextEdit) OnTextChanged(fn func(entry *TextEdit)) {
e.onTextChange = fn
}
// SetText sets the text content of the entry.
func (e *TextEdit) SetText(text string) {
e.text.Set([]rune(text))
}
// Text returns the text content of the entry.
func (e *TextEdit) Text() string {
return e.text.String()
}
// SetWordWrap sets whether the text should wrap or not.
func (e *TextEdit) SetWordWrap(enabled bool) {
e.text.wordwrap = enabled
}
func (e *TextEdit) isTextRemaining() bool {
return e.text.Width()-e.offset > e.Size().X
}