-
Notifications
You must be signed in to change notification settings - Fork 1
/
textinput.go
147 lines (134 loc) · 3.65 KB
/
textinput.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
144
145
146
147
package gel
import (
"image/color"
l "github.com/cybriq/p9/pkg/gel/gio/layout"
"github.com/cybriq/p9/pkg/gel/gio/op"
"github.com/cybriq/p9/pkg/gel/gio/op/paint"
"github.com/cybriq/p9/pkg/gel/gio/text"
"github.com/cybriq/p9/pkg/gel/gio/unit"
"github.com/cybriq/p9/pkg/gel/f32color"
)
// TextInput is a simple text input widget
type TextInput struct {
*Window
font text.Font
textSize unit.Value
// Color is the text color.
color color.NRGBA
// Hint contains the text displayed when the editor is empty.
hint string
// HintColor is the color of hint text.
hintColor color.NRGBA
// SelectionColor is the color of the background for selected text.
selectionColor color.NRGBA
editor *Editor
shaper text.Shaper
}
// TextInput creates a simple text input widget
func (w *Window) TextInput(editor *Editor, hint string) *TextInput {
var fon text.Font
var e error
if fon, e = w.collection.Font("bariol regular"); E.Chk(e) {
panic(e)
}
ti := &TextInput{
Window: w,
editor: editor,
textSize: w.TextSize,
font: fon,
color: w.Colors.GetNRGBAFromName("DocText"),
shaper: w.shaper,
hint: hint,
hintColor: w.Colors.GetNRGBAFromName("Hint"),
selectionColor: w.Colors.GetNRGBAFromName("scrim"),
}
ti.Font("bariol regular")
return ti
}
// Font sets the font for the text input widget
func (ti *TextInput) Font(font string) *TextInput {
var fon text.Font
var e error
if fon, e = ti.Theme.collection.Font(font); !E.Chk(e) {
ti.editor.font = fon
}
return ti
}
// TextScale sets the size of the text relative to the base font size
func (ti *TextInput) TextScale(scale float32) *TextInput {
ti.textSize = ti.Theme.TextSize.Scale(scale)
return ti
}
// Color sets the color to render the text
func (ti *TextInput) Color(color string) *TextInput {
ti.color = ti.Theme.Colors.GetNRGBAFromName(color)
return ti
}
// SelectionColor sets the color to render the text
func (ti *TextInput) SelectionColor(color string) *TextInput {
ti.selectionColor = ti.Theme.Colors.GetNRGBAFromName(color)
return ti
}
// Hint sets the text to show when the box is empty
func (ti *TextInput) Hint(hint string) *TextInput {
ti.hint = hint
return ti
}
// HintColor sets the color of the hint text
func (ti *TextInput) HintColor(color string) *TextInput {
ti.hintColor = ti.Theme.Colors.GetNRGBAFromName(color)
return ti
}
// Fn renders the text input widget
func (ti *TextInput) Fn(gtx l.Context) l.Dimensions {
defer op.Save(gtx.Ops).Load()
macro := op.Record(gtx.Ops)
paint.ColorOp{Color: ti.hintColor}.Add(gtx.Ops)
var maxlines int
if ti.editor.singleLine {
maxlines = 1
}
tl := Label{
Window: ti.Window,
font: ti.font,
color: ti.hintColor,
alignment: ti.editor.alignment,
maxLines: maxlines,
text: ti.hint,
textSize: ti.textSize,
shaper: ti.shaper,
}
dims := tl.Fn(gtx)
call := macro.Stop()
if w := dims.Size.X; gtx.Constraints.Min.X < w {
gtx.Constraints.Min.X = w
}
if h := dims.Size.Y; gtx.Constraints.Min.Y < h {
gtx.Constraints.Min.Y = h
}
dims = ti.editor.Layout(gtx, ti.shaper, ti.font, ti.textSize)
disabled := gtx.Queue == nil
if ti.editor.Len() > 0 {
paint.ColorOp{
Color: blendDisabledColor(disabled, ti.selectionColor),
}.Add(gtx.Ops)
ti.editor.PaintSelection(gtx)
paint.ColorOp{
Color: blendDisabledColor(disabled, ti.color),
}.Add(gtx.Ops)
ti.editor.PaintText(gtx)
} else {
call.Add(gtx.Ops)
}
if !disabled {
paint.ColorOp{Color: ti.color}.Add(gtx.Ops)
ti.editor.PaintCaret(gtx)
}
return dims
}
func blendDisabledColor(disabled bool, c color.NRGBA) color.NRGBA {
if disabled {
return f32color.Disabled(c)
}
return c
}