/
prompt.go
210 lines (180 loc) · 4.94 KB
/
prompt.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package widgets
import (
"image"
"image/color"
"gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"github.com/mirzakhany/chapar/ui/chapartheme"
)
// Prompt is a modal dialog that prompts the user for a response.
const (
ModalTypeInfo = "info"
ModalTypeWarn = "warn"
ModalTypeErr = "err"
)
var (
colors = map[string]color.NRGBA{
// Red
ModalTypeErr: {R: 0xD1, G: 0x1E, B: 0x35, A: 0xFF},
// Light blue
ModalTypeInfo: {R: 0x1D, G: 0xBF, B: 0xEC, A: 0xFF},
// Yellow
ModalTypeWarn: {R: 0xFD, G: 0xB5, B: 0x0E, A: 0xFF},
}
)
type Prompt struct {
Title string
Content string
Type string
Visible bool
rememberBool *widget.Bool
options []Option
result string
onSubmit func(selectedOption string, remember bool)
}
type Option struct {
Text string
Button widget.Clickable
Icon *widget.Icon
}
func NewPrompt(title, content, modalType string, options ...Option) *Prompt {
return &Prompt{
Title: title,
Content: content,
Type: modalType,
options: options,
}
}
func (p *Prompt) SetOptions(options ...Option) {
p.options = options
}
func (p *Prompt) Show() {
p.Visible = true
}
func (p *Prompt) Hide() {
p.Visible = false
}
func (p *Prompt) IsVisible() bool {
return p.Visible
}
func (p *Prompt) WithRememberBool() {
p.rememberBool = &widget.Bool{Value: false}
}
func (p *Prompt) WithoutRememberBool() {
p.rememberBool = nil
}
func (p *Prompt) SetOnSubmit(f func(selectedOption string, remember bool)) {
p.onSubmit = f
}
func (p *Prompt) submit() {
if p.onSubmit == nil {
return
}
if !p.Visible {
return
}
if p.rememberBool == nil {
p.onSubmit(p.result, false)
return
}
p.onSubmit(p.result, p.rememberBool.Value)
}
func (p *Prompt) Result() (string, bool) {
if p.result == "" {
return "", false
}
if p.rememberBool != nil {
return p.result, p.rememberBool.Value
}
return p.result, false
}
func (p *Prompt) Layout(gtx layout.Context, theme *chapartheme.Theme) layout.Dimensions {
if !p.Visible {
return layout.Dimensions{}
}
textColor := theme.ContrastFg
switch p.Type {
case ModalTypeErr:
textColor = color.NRGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0xFF}
case ModalTypeInfo:
textColor = color.NRGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xFF}
case ModalTypeWarn:
textColor = color.NRGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xFF}
}
return layout.Background{}.Layout(gtx,
func(gtx layout.Context) layout.Dimensions {
defer clip.UniformRRect(image.Rectangle{Max: gtx.Constraints.Min}, 8).Push(gtx.Ops).Pop()
paint.Fill(gtx.Ops, colors[p.Type])
return layout.Dimensions{Size: gtx.Constraints.Min}
}, func(gtx layout.Context) layout.Dimensions {
return layout.UniformInset(unit.Dp(10)).Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Flex{
Axis: layout.Vertical,
Alignment: layout.Middle,
}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Inset{Bottom: unit.Dp(8)}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
h := material.H6(theme.Material(), p.Title)
h.Color = textColor
return h.Layout(gtx)
})
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Inset{Bottom: unit.Dp(8)}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
b := material.Body1(theme.Material(), p.Content)
b.Color = textColor
return b.Layout(gtx)
})
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
count := len(p.options)
if p.rememberBool != nil {
count++
}
items := make([]layout.FlexChild, 0, count)
if p.rememberBool != nil {
items = append(
items,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return material.CheckBox(theme.Material(), p.rememberBool, "Don't ask again").Layout(gtx)
}),
layout.Rigid(layout.Spacer{Width: unit.Dp(4)}.Layout),
)
}
for i := range p.options {
i := i
if p.options[i].Button.Clicked(gtx) {
p.result = p.options[i].Text
p.submit()
}
items = append(
items,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
btn := Button(theme.Material(), &p.options[i].Button, nil, IconPositionStart, p.options[i].Text)
btn.Background = chapartheme.White
btn.Color = chapartheme.Black
return btn.Layout(gtx, theme)
// return material.Button(theme.Material(), &p.optionsClickables[i], p.options[i]).Layout(gtx)
}),
layout.Rigid(layout.Spacer{Width: unit.Dp(4)}.Layout),
)
}
return layout.Inset{Top: unit.Dp(5)}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Flex{
Axis: layout.Horizontal,
Alignment: layout.Middle,
Spacing: layout.SpaceStart,
}.Layout(gtx,
items...,
)
})
}),
)
})
},
)
}