forked from marcetin/parallelcoin
/
input.go
155 lines (147 loc) · 3.66 KB
/
input.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
148
149
150
151
152
153
154
155
package p9
import (
"regexp"
icons2 "golang.org/x/exp/shiny/materialdesign/icons"
l "gioui.org/layout"
"github.com/atotto/clipboard"
)
type Input struct {
*Theme
editor *Editor
input *TextInput
clearClickable *Clickable
clearButton *IconButton
copyClickable *Clickable
copyButton *IconButton
pasteClickable *Clickable
pasteButton *IconButton
GetText func() string
borderColor string
borderColorUnfocused string
borderColorFocused string
backgroundColor string
focused bool
}
var findSpaceRegexp = regexp.MustCompile(`\s+`)
func (th *Theme) Input(txt, hint, borderColorFocused, borderColorUnfocused, backgroundColor string, handle func(txt string), ) *Input {
editor := th.Editor().SingleLine().Submit(true)
input := th.TextInput(editor, hint)
p := &Input{
Theme: th,
clearClickable: th.Clickable(),
copyClickable: th.Clickable(),
pasteClickable: th.Clickable(),
editor: editor,
input: input,
borderColorUnfocused: borderColorUnfocused,
borderColorFocused: borderColorFocused,
backgroundColor: backgroundColor,
}
p.GetText = func() string {
return p.editor.Text()
}
p.clearButton = th.IconButton(p.clearClickable)
p.copyButton = th.IconButton(p.copyClickable)
p.pasteButton = th.IconButton(p.pasteClickable)
clearClickableFn := func() {
p.editor.SetText("")
p.editor.Focus()
}
copyClickableFn := func() {
go clipboard.WriteAll(p.editor.Text())
p.editor.Focus()
}
pasteClickableFn := func() {
col := p.editor.Caret.Col
go func() {
txt := p.editor.Text()
var err error
var cb string
if cb, err = clipboard.ReadAll(); Check(err) {
}
cb = findSpaceRegexp.ReplaceAllString(cb, " ")
txt = txt[:col] + cb + txt[col:]
p.editor.SetText(txt)
p.editor.Move(col + len(cb))
}()
p.editor.Focus()
}
p.clearButton.
Icon(
th.Icon().
Color("DocText").
Src(&icons2.ContentBackspace),
)
p.copyButton.
Icon(
th.Icon().
Color("DocText").
Src(&icons2.ContentContentCopy),
)
p.pasteButton.
Icon(
th.Icon().
Color("DocText").
Src(&icons2.ContentContentPaste),
)
p.input.Color("DocText")
p.clearClickable.SetClick(clearClickableFn)
p.copyClickable.SetClick(copyClickableFn)
p.pasteClickable.SetClick(pasteClickableFn)
p.editor.SetText(txt).SetSubmit(
func(txt string) {
go func() {
handle(txt)
}()
},
).SetChange(
func(txt string) {
// send keystrokes to the NSA
},
)
p.editor.SetFocus(
func(is bool) {
if is {
p.borderColor = p.borderColorFocused
} else {
p.borderColor = p.borderColorUnfocused
}
},
)
return p
}
func (in *Input) Fn(gtx l.Context) l.Dimensions {
// gtx.Constraints.Max.X = int(in.TextSize.Scale(float32(in.size)).V)
// gtx.Constraints.Min.X = 0
// width := int(in.Theme.TextSize.Scale(in.size).V)
// gtx.Constraints.Max.X, gtx.Constraints.Min.X = width, width
return in.Border().Color(in.borderColor).Embed(
in.Flex().
Flexed(
1,
in.Inset(0.25, in.input.Color("DocText").Fn).Fn,
).
Rigid(
in.copyButton.
Background("").
Icon(in.Icon().Color(in.borderColor).Scale(Scales["H6"]).Src(&icons2.ContentContentCopy)).
Inset(0.25).
Fn,
).
Rigid(
in.pasteButton.
Background("").
Icon(in.Icon().Color(in.borderColor).Scale(Scales["H6"]).Src(&icons2.ContentContentPaste)).
Inset(0.25).
Fn,
).
Rigid(
in.clearButton.
Background("").
Icon(in.Icon().Color(in.borderColor).Scale(Scales["H6"]).Src(&icons2.ContentBackspace)).
Inset(0.25).
Fn,
).
Fn,
).Fn(gtx)
}