~pierrec/giox

ref: c164e218831e giox/cmd/iconx/main.go -rw-r--r-- 8.7 KiB
c164e218Pierre Curto cm/iconx: update the set of icons 6 months ago
                                                                                
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Package iconx provides a simple command line utility to browse
// icons from the `golang.org/x/exp/shiny/materialdesign/icons` package.
package main

import (
	"fmt"
	"image"
	"image/color"
	"os"
	"sort"
	"strconv"
	"strings"
	"time"

	"gioui.org/app"
	"gioui.org/font/gofont"
	"gioui.org/io/clipboard"
	"gioui.org/io/pointer"
	"gioui.org/io/system"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/op/clip"
	"gioui.org/op/paint"
	"gioui.org/text"
	"gioui.org/unit"
	"gioui.org/widget"
	"gioui.org/widget/material"
	"git.sr.ht/~pierrec/giox/layoutx"
	"git.sr.ht/~pierrec/giox/widgetx"
	"git.sr.ht/~pierrec/giox/widgetx/materialx"

	"golang.org/x/exp/shiny/materialdesign/icons"
	"golang.org/x/image/colornames"
)

//go:generate go get -u golang.org/x/exp/shiny/materialdesign/icons

var (
	iconsMap  map[string][]iconData
	iconsTree tree
)

func init() {
	var err error
	iconsMap, err = loadIcons()
	if err != nil {
		panic(err)
	}
	// Sort icon names.
	keys := make([]string, 0, len(iconsMap))
	for name, d := range iconsMap {
		keys = append(keys, name)
		iconsMap[name] = d[:len(d):len(d)]
	}
	sort.Strings(keys)
	for _, name := range keys {
		iconsTree.Add(name + "/list")
	}
}

func main() {
	go func() {
		w := app.NewWindow(
			app.Size(unit.Dp(500), unit.Dp(500)),
			app.Title("Material icons"),
		)
		if err := loop(w); err != nil {
			fmt.Println(err)
		}
		os.Exit(0)
	}()
	app.Main()
}

func loop(w *app.Window) error {
	th := material.NewTheme(gofont.Collection())
	ui := &uiMain{th: th}

	var ops op.Ops
	for ev := range w.Events() {
		switch e := ev.(type) {
		case system.DestroyEvent:
			return e.Err

		case system.FrameEvent:
			gtx := layout.NewContext(&ops, e)
			ui.Layout(gtx)
			e.Frame(gtx.Ops)
		}
	}
	return nil
}

type uiMain struct {
	Hover widgetx.Hover

	th      *material.Theme
	tree    widgetx.Tree
	menu    widgetx.Icons
	num     widgetx.Input
	search  widgetx.Input
	lists   map[int]*uiIconList
	overlay time.Time
	copied  string
}

type uiIconList struct {
	clicks []widget.Clickable
	list   layoutx.ListWrap
}

// menu icon indexes.
const (
	menuFoldLess = iota
	menuFoldMore
)

// Apply the filter to icon names, moving them around in place.
func updateChildren(name, filter string) {
	icons := iconsMap[name]
	icons = icons[:cap(icons)]
	if filter != "" {
		n := len(icons)
		for i := 0; i < n; {
			if strings.Contains(icons[i].Name, filter) {
				i++
				continue
			}
			n--
			icons[i], icons[n] = icons[n], icons[i]
		}
		icons = icons[:n]
	}
	sort.Slice(icons, func(i, j int) bool { return icons[i].Name < icons[j].Name })
	iconsMap[name] = icons
}

func (ui *uiMain) init() {
	if ui.lists == nil {
		ui.lists = make(map[int]*uiIconList)
		ui.tree = widgetx.Tree{
			Root: func() []int {
				s := iconsTree.Root()
				filter := ui.search.Text()
				n := len(s)
				for i := 0; i < n; {
					name := iconsTree.Key(s[i])
					updateChildren(name, filter)
					if len(iconsMap[name]) == 0 {
						n--
						s[i], s[n] = s[n], s[i]
					} else {
						i++
					}
				}
				s = s[:n]
				sort.Slice(s, func(i, j int) bool { return iconsTree.Key(s[i]) < iconsTree.Key(s[j]) })
				return s
			},
			Children: iconsTree.Children,
			Axis:     layout.Vertical,
		}
		ui.Hover = widgetx.Hover{
			Background: color.NRGBA(colornames.Lightgrey),
			Foreground: ui.th.Palette.Fg,
		}
		ui.menu = widgetx.Icons{
			Hover: ui.Hover,
			Color: ui.th.Palette.Fg,
			Size:  ui.th.TextSize,
			Icons: widgetx.LoadIcons(icons.NavigationUnfoldLess, icons.NavigationUnfoldMore),
			List:  layout.List{Axis: layout.Horizontal},
		}
		ui.num.Editor = &widget.Editor{
			Alignment:  text.Middle,
			SingleLine: true,
			Submit:     true,
		}
		ui.num.Editor.SetText("10")
		ui.menu.Hide(menuFoldLess)
	}
}

func (ui *uiMain) onClick(gtx layout.Context, idx int) {
	switch root := ui.tree.Root(); idx {
	case menuFoldLess:
		ui.menu.Hide(menuFoldLess)
		ui.menu.Show(menuFoldMore)
		for _, id := range root {
			ui.tree.Close(id)
		}
	case menuFoldMore:
		ui.menu.Hide(menuFoldMore)
		ui.menu.Show(menuFoldLess)
		for _, id := range root {
			ui.tree.Open(id)
		}
	}
}

func (ui *uiMain) layoutOverlay(gtx layout.Context) {
	if ui.overlay.IsZero() {
		return
	}
	if ui.overlay.Before(gtx.Now) {
		ui.overlay = time.Time{}
		ui.copied = ""
		return
	}
	op.InvalidateOp{At: ui.overlay}.Add(gtx.Ops)
	m := op.Record(gtx.Ops)
	col := colornames.Gray
	col.A = 32
	paint.Fill(gtx.Ops, color.NRGBA(col))
	pointer.InputOp{
		Tag:   ui,
		Types: pointer.Cancel | pointer.Press,
	}.Add(gtx.Ops)
	layout.Stack{
		Alignment: layout.Center,
	}.Layout(gtx,
		layout.Expanded(func(gtx layout.Context) layout.Dimensions {
			size := gtx.Constraints.Min
			paint.FillShape(gtx.Ops, ui.th.Palette.ContrastBg, clip.Rect{Max: size}.Op())
			return layout.Dimensions{Size: size}
		}),
		layout.Stacked(func(gtx layout.Context) layout.Dimensions {
			return layout.UniformInset(unit.Dp(4)).Layout(gtx, func(gtx layout.Context) layout.Dimensions {
				txt := fmt.Sprintf("%s copied to clipboard", ui.copied)
				style := material.Body1(ui.th, txt)
				style.Color = ui.th.Palette.ContrastFg
				return style.Layout(gtx)
			})
		}),
	)
	op.Defer(gtx.Ops, m.Stop())
}

func (ui *uiMain) Layout(gtx layout.Context) layout.Dimensions {
	defer ui.layoutOverlay(gtx)
	ui.init()
	padding := layout.Inset{Right: unit.Dp(4)}
	return layout.Flex{
		Axis: layout.Vertical,
	}.Layout(gtx,
		layout.Rigid(func(gtx layout.Context) layout.Dimensions {
			if ui.search.Submitted() {
				op.InvalidateOp{}.Add(gtx.Ops)
			}
			return layout.Flex{}.Layout(gtx,
				layout.Rigid(func(gtx layout.Context) layout.Dimensions {
					return padding.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
						return ui.menu.Layout(gtx, ui.onClick, nil)
					})
				}),
				layout.Flexed(0.05, func(gtx layout.Context) layout.Dimensions {
					return padding.Layout(gtx, materialx.InputLayout(ui.th, &ui.num, "Num/column").Layout)
				}),
				layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
					return padding.Layout(gtx, materialx.InputLayout(ui.th, &ui.search, "Search").Layout)
				}),
			)
		}),
		layout.Rigid(func(gtx layout.Context) layout.Dimensions {
			defer ui.tree.Reset()
			return materialx.TreeLayout(ui.th, &ui.tree).Layout(gtx, func(gtx layout.Context, idx int) layout.Dimensions {
				name := iconsTree.Key(idx)
				icons, ok := iconsMap[name]
				if ok {
					return layout.Stack{}.Layout(gtx,
						layout.Expanded(func(gtx layout.Context) layout.Dimensions {
							gtx.Constraints.Min.X = gtx.Constraints.Max.X
							size := gtx.Constraints.Min
							col := ui.th.Palette.ContrastBg
							paint.FillShape(gtx.Ops, col, clip.Rect{Max: size}.Op())
							return layout.Dimensions{Size: size}
						}),
						layout.Stacked(func(gtx layout.Context) layout.Dimensions {
							txt := fmt.Sprintf("%s (%d)", name, len(icons))
							style := material.Body1(ui.th, txt)
							style.Color = ui.th.Palette.ContrastFg
							return style.Layout(gtx)
						}),
					)
				}
				name = strings.TrimSuffix(name, "/list")
				icons = iconsMap[name]
				list, ok := ui.lists[idx]
				if !ok {
					list = &uiIconList{
						clicks: make([]widget.Clickable, cap(icons)),
						list: layoutx.ListWrap{
							Axis:      layout.Vertical,
							Alignment: layout.Start,
						},
					}
					ui.lists[idx] = list
				}
				for i := range list.clicks {
					if list.clicks[i].Clicked() {
						txt := name + icons[i].Name
						ui.copied = txt
						clipboard.WriteOp{Text: txt}.Add(gtx.Ops)
						ui.overlay = gtx.Now.Add(1500 * time.Millisecond)
						break
					}
				}
				num := 5
				if n, _ := strconv.Atoi(ui.num.Text()); n > 0 {
					num = n
				}
				icSize := unit.Dp(20)
				gtx.Constraints.Max.Y = gtx.Metric.Px(icSize) * num
				return list.list.Layout(gtx, len(icons), func(gtx layout.Context, i int) layout.Dimensions {
					ic := icons[i]
					click := &list.clicks[i]
					return click.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
						return layout.Flex{
							Axis:      layout.Horizontal,
							Alignment: layout.Middle,
						}.Layout(gtx,
							layout.Rigid(func(gtx layout.Context) layout.Dimensions {
								return padding.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
									gtx.Constraints.Min = image.Pt(gtx.Px(icSize), 0)
									return ic.Icon.Layout(gtx, ui.th.Palette.ContrastBg)
								})
							}),
							layout.Rigid(func(gtx layout.Context) layout.Dimensions {
								pointer.CursorNameOp{Name: pointer.CursorPointer}.Add(gtx.Ops)
								return padding.Layout(gtx,
									func(gtx layout.Context) layout.Dimensions {
										txt := strings.TrimPrefix(ic.Name, name)
										style := material.Body1(ui.th, txt)
										if click.Hovered() {
											style.Font.Weight = text.Bold
										}
										return style.Layout(gtx)
									})
							}),
						)
					})
				}, nil)
			})
		}),
	)
}