/
textinput.go
117 lines (106 loc) · 2.77 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
package gel
import (
"image/color"
l "gioui.org/layout"
"gioui.org/op"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/unit"
"github.com/p9c/monorepo/pkg/gel/f32color"
)
// TextInput is a simple text input widget
type TextInput struct {
*Window
// Theme *Theme
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
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"),
}
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
}
// 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(c l.Context) l.Dimensions {
defer op.Save(c.Ops).Load()
macro := op.Record(c.Ops)
paint.ColorOp{Color: ti.hintColor}.Add(c.Ops)
tl := Text{alignment: ti.editor.alignment}
dims := tl.Fn(c, ti.shaper, ti.font, ti.textSize, ti.hint)
call := macro.Stop()
if w := dims.Size.X; c.Constraints.Min.X < w {
c.Constraints.Min.X = w
}
if h := dims.Size.Y; c.Constraints.Min.Y < h {
c.Constraints.Min.Y = h
}
dims = ti.editor.Layout(c, ti.shaper, ti.font, ti.textSize)
disabled := c.Queue == nil
if ti.editor.Len() > 0 {
textColor := ti.color
if disabled {
textColor = f32color.MulAlpha(textColor, 150)
}
paint.ColorOp{Color: textColor}.Add(c.Ops)
ti.editor.PaintText(c)
} else {
call.Add(c.Ops)
}
if !disabled {
paint.ColorOp{Color: ti.color}.Add(c.Ops)
ti.editor.PaintCaret(c)
}
return dims
}