forked from steverusso/mdedit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgets.go
211 lines (195 loc) · 4.93 KB
/
widgets.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
211
package mdedit
import (
"image"
"image/color"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"golang.org/x/exp/shiny/materialdesign/icons"
)
type (
C = layout.Context
D = layout.Dimensions
)
var (
iconDirectory = mustIcon(icons.FileFolderOpen)
iconEdit = mustIcon(icons.EditorModeEdit)
iconHome = mustIcon(icons.ActionHome)
iconReader = mustIcon(icons.ActionChromeReaderMode)
iconRegFile = mustIcon(icons.ActionDescription)
iconVisibility = mustIcon(icons.ActionVisibility)
iconWebAsset = mustIcon(icons.AVWebAsset)
)
// mustIcon returns a new `*widget.Icon` for the given byte slice. It panics on error.
func mustIcon(iconBytes []byte) *widget.Icon {
ic, err := widget.NewIcon(iconBytes)
if err != nil {
panic(err)
}
return ic
}
type rule struct {
width int
color color.NRGBA
axis layout.Axis
}
func (rl rule) Layout(gtx C) D {
if rl.width == 0 {
rl.width = 1
}
size := image.Point{gtx.Constraints.Max.X, rl.width}
if rl.axis == layout.Vertical {
size = image.Point{rl.width, gtx.Constraints.Max.Y}
}
rect := clip.Rect{Max: size}.Op()
paint.FillShape(gtx.Ops, rl.color, rect)
return D{Size: size}
}
type buttonGroup struct {
bg color.NRGBA
fg color.NRGBA
shaper text.Shaper
textSize unit.Sp
}
type groupButton struct {
click *widget.Clickable
icon *widget.Icon
text string
disabled bool
}
func (g buttonGroup) layout(gtx C, buttons []groupButton) D {
if len(buttons) == 0 {
return D{}
}
// Determine the height of the tallest element.
var maxHeight int
{
for _, b := range buttons {
m := op.Record(gtx.Ops)
dims := g.drawButton(gtx, b)
_ = m.Stop()
if h := dims.Size.Y; h > maxHeight {
maxHeight = h
}
}
}
// Make the flex wrapped buttons with dividers in between.
border := merge(g.fg, g.bg, 0.3)
divider := func(gtx C) D {
gtx.Constraints.Max.Y = maxHeight
return rule{color: border, axis: layout.Vertical}.Layout(gtx)
}
flexBtns := make([]layout.FlexChild, 0, len(buttons)*2)
for i := 0; i < len(buttons); i++ {
b := buttons[i]
if i != 0 {
flexBtns = append(flexBtns, layout.Rigid(divider))
}
flexBtns = append(flexBtns, layout.Rigid(func(gtx C) D {
return g.drawButtonWithBg(gtx, b, maxHeight)
}))
}
// Determine this button group's full size for the rounded rectangle clip.
const radius = 5
m := op.Record(gtx.Ops)
fullDims := widget.Border{
Color: border,
CornerRadius: radius,
Width: 1,
}.Layout(gtx, func(gtx C) D {
return layout.Flex{}.Layout(gtx, flexBtns...)
})
fullDraw := m.Stop()
// Clip the rounded rectangle area and draw the button group.
defer clip.RRect{
Rect: image.Rectangle{Max: image.Point{
X: fullDims.Size.X - 1,
Y: fullDims.Size.Y - 1,
}},
SE: radius, SW: radius, NW: radius, NE: radius,
}.Push(gtx.Ops).Pop()
fullDraw.Add(gtx.Ops)
return fullDims
}
func (g *buttonGroup) drawButtonWithBg(gtx C, b groupButton, height int) D {
// Pre-draw in order to get the width for filling the background.
m := op.Record(gtx.Ops)
dims := g.drawButton(gtx, b)
call := m.Stop()
// Adjust background color under certain conditions.
bg := g.bg
switch {
case b.disabled:
bg = darken(bg, 0.05)
case b.click.Pressed():
bg = lighten(bg, 0.1)
case b.click.Hovered():
bg = lighten(bg, 0.4)
case !b.disabled:
bg = lighten(bg, 0.2)
}
size := image.Point{X: dims.Size.X, Y: height}
paint.FillShape(gtx.Ops, bg, clip.Rect{Max: size}.Op())
// Vertically center the button content.
defer op.Offset(image.Point{Y: height/2 - dims.Size.Y/2}).Push(gtx.Ops).Pop()
return b.click.Layout(gtx, func(gtx C) D {
call.Add(gtx.Ops)
return D{Size: size}
})
}
func (g *buttonGroup) drawButton(gtx C, b groupButton) D {
fg := g.fg
if b.disabled {
fg.A /= 2
}
var content layout.Widget
switch {
case b.icon != nil:
content = func(gtx C) D {
return b.icon.Layout(gtx, fg)
}
case b.text != "":
content = func(gtx C) D {
paint.ColorOp{Color: fg}.Add(gtx.Ops)
return layout.Inset{Left: 2, Right: 2}.Layout(gtx, func(gtx C) D {
return widget.Label{MaxLines: 1}.Layout(gtx, g.shaper, text.Font{}, g.textSize, b.text)
})
}
default:
return D{}
}
return layout.UniformInset(6).Layout(gtx, content)
}
func darken(c color.NRGBA, f float32) color.NRGBA {
return color.NRGBA{
R: uint8(float32(c.R) * (1 - f)),
G: uint8(float32(c.G) * (1 - f)),
B: uint8(float32(c.B) * (1 - f)),
A: 255,
}
}
func lighten(c color.NRGBA, f float32) color.NRGBA {
return color.NRGBA{
R: c.R + uint8(float32(255-c.R)*f),
G: c.G + uint8(float32(255-c.G)*f),
B: c.B + uint8(float32(255-c.B)*f),
A: 255,
}
}
func merge(c1, c2 color.NRGBA, p float32) color.NRGBA {
return color.NRGBA{
R: mergeCalc(c1.R, c2.R, p),
G: mergeCalc(c1.G, c2.G, p),
B: mergeCalc(c1.B, c2.B, p),
A: 255,
}
}
func mergeCalc(a, b uint8, p float32) uint8 {
v1 := float32(a) * (1 - p)
v2 := float32(b) * p
return uint8(v1 + v2)
}