-
Notifications
You must be signed in to change notification settings - Fork 0
/
icons.go
392 lines (363 loc) · 10.8 KB
/
icons.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright (c) 2018, The GoKi Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package svg
import (
"fmt"
"image"
"image/color"
"log"
"os"
"path/filepath"
"sort"
"strings"
"github.com/goki/gi/gi"
"github.com/goki/gi/units"
"github.com/goki/ki"
"github.com/goki/ki/dirs"
"github.com/goki/ki/kit"
)
func init() {
gi.TheIconMgr = &IconMgr{}
DefaultIconSet = MakeDefaultIcons()
DefaultIconSet.OpenDefaultIcons()
CurIconSet = DefaultIconSet
gi.CurIconList = gi.TheIconMgr.IconList(true)
}
// svg.Icon is the actual SVG for a gi.Icon -- it should contain no color
// information -- it should just be a filled shape where the fill and stroke
// colors come from the surrounding context / paint settings. The rendered
// version is cached for a given size. Icons are always copied from an
// original source icon and then can be customized from there.
type Icon struct {
SVG
Filename string `desc:"file name with full path for icon if loaded from file"`
Rendered bool `json:"-" xml:"-" desc:"we have already rendered at RenderedSize -- doesn't re-render at same size -- if the paint params change, set this to false to re-render"`
RendSize image.Point `json:"-" xml:"-" desc:"size at which we previously rendered"`
}
var KiT_Icon = kit.Types.AddType(&Icon{}, IconProps)
var IconProps = ki.Props{
"background-color": color.Transparent,
}
// CopyFromIcon copies from a source icon, typically one from a library --
// preserves all the existing render state etc for the current icon, so that
// only a new render is required
func (ic *Icon) CopyFromIcon(cp *Icon) {
if cp == nil {
return
}
ic.CopyFrom(cp)
ic.Rendered = false
}
// IconAutoOpen controls auto-loading of icons -- can turn this off for debugging etc
var IconAutoOpen = true
func (ic *Icon) Init2D() {
ic.SVG.Init2D()
ic.Fill = true
}
func (ic *Icon) Size2D(iter int) {
ic.Viewport2D.Size2D(iter)
}
func (ic *Icon) Layout2D(parBBox image.Rectangle, iter int) bool {
if ic.Sty.Font.Size.Val == 0 { // not yet styled
ic.StyleSVG()
}
redo := ic.SVG.Layout2D(parBBox, iter)
ic.SetNormXForm()
return redo
}
// NeedsReRender tests whether the last render parameters (size, color) have changed or not
func (ic *Icon) NeedsReRender() bool {
if ic.NeedsFullReRender() || !ic.Rendered || ic.RendSize != ic.Geom.Size {
return true
}
return false
}
func (ic *Icon) Render2D() {
if ic.Viewport == nil {
ic.FullRender2DTree()
return
}
if ic.NeedsReRender() {
if ic.PushBounds() {
rs := &ic.Render
if ic.Fill {
ic.FillViewport()
}
ic.SetNormXForm()
rs.PushXFormLock(ic.Pnt.XForm)
ic.Render2DChildren() // we must do children first, then us!
rs.PopXFormLock()
ic.Rendered = true
ic.RendSize = ic.Geom.Size
ic.PopBounds()
}
}
ic.RenderViewport2D() // update our parent image
}
////////////////////////////////////////////////////////////////////////////////////////
// IconMgr
// svg.IconMgr is THE implementation of the gi.IconMgr interface
type IconMgr struct {
}
func (im *IconMgr) IsValid(iconName string) bool {
if im == nil {
fmt.Println("TheIconMgr is nil -- you MUST import gi/svg as e.g., import \"_ github.com/goki/gi/svg\" to properly initialize the SVG icon manager")
return false
}
if gi.IconName(iconName).IsNil() {
return false
}
if _, ok := (*CurIconSet)[iconName]; ok {
return true
}
if _, ok := (*DefaultIconSet)[iconName]; ok {
return true
}
return false
}
// IconByName is main function to get icon by name -- looks in CurIconSet and
// falls back to DefaultIconSet if not found there -- returns error and logs a
// message if not found
func (im *IconMgr) IconByName(name string) (*Icon, error) {
if gi.IconName(name).IsNil() {
return nil, nil
}
if !im.IsValid(name) {
err := fmt.Errorf("svg.IconMgr.IconByName -- icon name not found in CurIconSet or DefaultIconSet: %v\n", name)
return nil, err
}
ic, ok := (*CurIconSet)[name]
if !ok {
ic = (*DefaultIconSet)[name]
}
if ic.Filename != "" && !ic.HasChildren() && IconAutoOpen {
ic.OpenXML(ic.Filename)
}
return ic, nil
}
func (im *IconMgr) SetIcon(ic *gi.Icon, iconName string) error {
sic, err := im.IconByName(iconName)
if err != nil {
return err
}
ic.SetNChildren(1, KiT_Icon, "icon")
nic := ic.KnownChild(0).(*Icon)
nic.CopyFromIcon(sic)
ic.Filename = sic.Filename
return nil
}
func (im *IconMgr) IconList(alphaSort bool) []gi.IconName {
return CurIconSet.IconList(alphaSort)
}
////////////////////////////////////////////////////////////////////////////////////////
// IconSet is a list of icons
// IconSet is a collection of icons
type IconSet map[string]*Icon
// DefaultIconSet is the default icon set, initialized by default
var DefaultIconSet *IconSet
// CurIconSet is the current icon set -- defaults to default but can be
// changed to whatever you want
var CurIconSet *IconSet
func (iset *IconSet) OpenDefaultIcons() error {
path, err := dirs.GoSrcDir("github.com/goki/gi/icons")
if err != nil {
log.Println(err)
return err
}
// fmt.Printf("loading default icons: %v\n", path)
rval := iset.OpenIconsFromPath(path)
// tstpath := filepath.Join(gopath, "src/github.com/goki/gi/icons_svg_test")
// rval = iset.OpenIconsFromPath(tstpath)
return rval
}
// OpenIconsFromPath scans for .svg icon files in given path, adding them to
// the given IconSet, just storing the filename for later lazy loading
func (iset *IconSet) OpenIconsFromPath(path string) error {
ext := ".svg"
err := filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("gi.IconSet: error accessing path %q: %v\n", p, err)
return err
}
if filepath.Ext(p) == ext {
ps, fn := filepath.Split(p)
bfn := fn[:len(fn)-len(ext)]
nm := strings.ToLower(bfn)
pd := strings.TrimPrefix(ps, path)
if pd != "" {
pd = strings.ToLower(strings.Trim(strings.Trim(pd, string(filepath.Separator)), "/"))
if pd != "" {
nm = pd + "-" + nm
}
}
ic := Icon{}
ic.InitName(&ic, nm)
ic.Filename = p
(*iset)[nm] = &ic
}
return nil
})
if err != nil {
log.Printf("gi.IconSet: error walking the path %q: %v\n", path, err)
}
return err
}
// IconList returns a list of names of icons in the icon set
func (iset *IconSet) IconList(alphaSort bool) []gi.IconName {
il := make([]gi.IconName, len(*iset)+1)
il[0] = gi.IconName("none")
idx := 1
for _, ic := range *iset {
il[idx] = gi.IconName(ic.Nm)
idx++
}
if alphaSort {
sort.Slice(il, func(i, j int) bool {
return il[i] < il[j]
})
}
return il
}
func MakeDefaultIcons() *IconSet {
iset := make(IconSet, 100)
if true {
{
ic := Icon{}
ic.InitName(&ic, "widget-wedge-down")
ic.ViewBox.Size = gi.Vec2D{1, 1}
p := ic.AddNewChild(KiT_Path, "p").(*Path)
p.SetProp("stroke-width", units.NewValue(1, units.Pct))
p.SetData("M 0.05 0.05 .95 0.05 .5 .95 Z")
iset[ic.Nm] = &ic
}
{
ic := Icon{}
ic.InitName(&ic, "widget-wedge-up")
ic.ViewBox.Size = gi.Vec2D{1, 1}
p := ic.AddNewChild(KiT_Path, "p").(*Path)
p.SetProp("stroke-width", units.NewValue(1, units.Pct))
p.SetData("M 0.05 0.95 .95 0.95 .5 .05 Z")
iset[ic.Nm] = &ic
}
{
ic := Icon{}
ic.InitName(&ic, "widget-wedge-left")
ic.ViewBox.Size = gi.Vec2D{1, 1}
p := ic.AddNewChild(KiT_Path, "p").(*Path)
p.SetProp("stroke-width", units.NewValue(1, units.Pct))
p.SetData("M 0.95 0.05 .95 0.95 .05 .5 Z")
iset[ic.Nm] = &ic
}
{
ic := Icon{}
ic.InitName(&ic, "widget-wedge-right")
ic.ViewBox.Size = gi.Vec2D{1, 1}
p := ic.AddNewChild(KiT_Path, "p").(*Path)
p.SetProp("stroke-width", units.NewValue(1, units.Pct))
p.SetData("M 0.05 0.05 .05 0.95 .95 .5 Z")
iset[ic.Nm] = &ic
}
{
ic := Icon{}
ic.InitName(&ic, "widget-checkmark")
ic.ViewBox.Size = gi.Vec2D{1, 1}
p := ic.AddNewChild(KiT_Path, "p").(*Path)
p.SetProp("stroke-width", units.NewValue(20, units.Pct))
p.SetProp("fill", "none")
p.SetData("M 0.1 0.5 .5 0.9 .9 .1")
iset[ic.Nm] = &ic
}
{
ic := Icon{}
ic.InitName(&ic, "widget-checked-box")
ic.ViewBox.Size = gi.Vec2D{1, 1}
bx := ic.AddNewChild(KiT_Rect, "bx").(*Rect)
bx.Pos.Set(0.05, 0.05)
bx.Size.Set(0.9, 0.9)
bx.SetProp("stroke-width", units.NewValue(5, units.Pct))
// bx.Radius.Set(0.02, 0.02)
p := ic.AddNewChild(KiT_Path, "p").(*Path)
p.SetProp("stroke-width", units.NewValue(20, units.Pct))
p.SetProp("fill", "none")
p.SetData("M 0.2 0.5 .5 0.8 .8 .2")
iset[ic.Nm] = &ic
}
{
ic := Icon{}
ic.InitName(&ic, "widget-unchecked-box")
ic.ViewBox.Size = gi.Vec2D{1, 1}
bx := ic.AddNewChild(KiT_Rect, "bx").(*Rect)
bx.SetProp("stroke-width", units.NewValue(5, units.Pct))
bx.Pos.Set(0.05, 0.05)
bx.Size.Set(0.9, 0.9)
// bx.Radius.Set(0.02, 0.02) // not rendering well at small sizes
iset[ic.Nm] = &ic
}
{
ic := Icon{}
ic.InitName(&ic, "widget-circlebutton-on")
ic.ViewBox.Size = gi.Vec2D{1, 1}
oc := ic.AddNewChild(KiT_Circle, "oc").(*Circle)
oc.Pos.Set(0.5, 0.5)
oc.Radius = 0.4
oc.SetProp("fill", "none")
oc.SetProp("stroke-width", units.NewValue(10, units.Pct))
inc := ic.AddNewChild(KiT_Circle, "ic").(*Circle)
inc.Pos.Set(0.5, 0.5)
inc.Radius = 0.2
inc.SetProp("stroke-width", units.NewValue(10, units.Pct))
iset[ic.Nm] = &ic
}
{
ic := Icon{}
ic.InitName(&ic, "widget-circlebutton-off")
ic.ViewBox.Size = gi.Vec2D{1, 1}
oc := ic.AddNewChild(KiT_Circle, "oc").(*Circle)
oc.Pos.Set(0.5, 0.5)
oc.Radius = 0.4
oc.SetProp("fill", "none")
oc.SetProp("stroke-width", units.NewValue(10, units.Pct))
iset[ic.Nm] = &ic
}
{
ic := Icon{}
rad := gi.Vec2D{0.25, 0.12}
ic.InitName(&ic, "widget-handle-circles-vert")
ic.ViewBox.Size = gi.Vec2D{1, 1}
c0 := ic.AddNewChild(KiT_Ellipse, "c0").(*Ellipse)
c0.SetProp("stroke-width", units.NewValue(5, units.Pct))
c0.Pos.Set(0.5, 0.15)
c0.Radii = rad
c1 := ic.AddNewChild(KiT_Ellipse, "c1").(*Ellipse)
c1.SetProp("stroke-width", units.NewValue(5, units.Pct))
c1.Pos.Set(0.5, 0.5)
c1.Radii = rad
c2 := ic.AddNewChild(KiT_Ellipse, "c2").(*Ellipse)
c2.SetProp("stroke-width", units.NewValue(5, units.Pct))
c2.Pos.Set(0.5, 0.85)
c2.Radii = rad
iset[ic.Nm] = &ic
}
{
ic := Icon{}
rad := gi.Vec2D{0.12, 0.25}
ic.InitName(&ic, "widget-handle-circles-horiz")
ic.ViewBox.Size = gi.Vec2D{1, 1}
c0 := ic.AddNewChild(KiT_Ellipse, "c0").(*Ellipse)
c0.SetProp("stroke-width", units.NewValue(5, units.Pct))
c0.Pos.Set(0.15, 0.5)
c0.Radii = rad
c1 := ic.AddNewChild(KiT_Ellipse, "c1").(*Ellipse)
c1.SetProp("stroke-width", units.NewValue(5, units.Pct))
c1.Pos.Set(0.5, 0.5)
c1.Radii = rad
c2 := ic.AddNewChild(KiT_Ellipse, "c2").(*Ellipse)
c2.SetProp("stroke-width", units.NewValue(5, units.Pct))
c2.Pos.Set(0.85, 0.5)
c2.Radii = rad
iset[ic.Nm] = &ic
}
}
return &iset
}