-
Notifications
You must be signed in to change notification settings - Fork 0
/
icons.go
120 lines (102 loc) · 3.09 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
// Copyright (c) 2023, 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 icons provides Go constant names for Material Design Symbols as SVG files.
package icons
import (
"embed"
"io/fs"
"strings"
_ "github.com/iancoleman/strcase" // needed so that it gets included in the mod (the generator uses it)
"goki.dev/glop/dirs"
"goki.dev/grr"
)
//go:generate go run gen.go
// Icons contains all of the embedded svg icons. It is initialized
// to contain of the default icons located in the svg directory
// (https://github.com/goki/icons/tree/main/svg), but it can be extended
// by any packages by using a merged fs package. All icons should be stored
// in the root directory of the fs, which can be accomplished using [fs.Sub]
// if you have icons in a subdirectory.
var Icons fs.FS = grr.Log1(fs.Sub(defaults, "svg"))
// defaults contains the default icons.
//
//go:embed svg/*.svg
var defaults embed.FS
const (
// None is an icon that indicates to not use an icon.
// It completely prevents the rendering of an icon,
// whereas [Blank] renders a blank icon.
None Icon = "none"
// Blank is a blank icon that can be used as a
// placeholder when no other icon is appropriate.
// It still renders an icon, just a blank one,
// whereas [None] indicates to not render one at all.
Blank Icon = "blank"
)
// Icon contains the name of an icon
type Icon string
func (i Icon) String() string {
return string(i)
}
// Fill returns the icon as a filled icon.
// It returns the icon unchanged if it is already filled.
func (i Icon) Fill() Icon {
if i.IsFilled() {
return i
}
return i + "-fill"
}
// Unfill returns the icon as an unfilled icon.
// It returns the icon unchanged if it is already unfilled.
// Icons are unfilled by default, so you only
// need to call this to reverse a prior [Icon.Fill] call
func (i Icon) Unfill() Icon {
return Icon(strings.TrimSuffix(string(i), "-fill"))
}
// IsFilled returns whether the icon
// is a filled icon.
func (i Icon) IsFilled() bool {
return strings.HasSuffix(string(i), "-fill")
}
// IsNil returns whether the icon name is "" or
// [None], which indicates not to use an icon.
func (i Icon) IsNil() bool {
return i == "" || i == None
}
// Filename returns the filename of the icon in [Icons]
func (i Icon) Filename() string {
return string(i) + ".svg"
}
// IsValid returns whether the icon name corresponds to
// a valid existing icon.
func (i Icon) IsValid() bool {
if i.IsNil() {
return false
}
ex, _ := dirs.FileExistsFS(Icons, i.Filename())
return ex
}
// AllIcons is a list of all icons
var AllIcons []Icon
// All returns a list of all the Icons (excluding "fill" versions)
func All() []Icon {
if AllIcons != nil {
return AllIcons
}
files, err := fs.ReadDir(Icons, ".")
if err != nil {
return nil
}
ics := make([]Icon, 0, len(files)/2) // no fill
for _, fi := range files {
nm := fi.Name()
if strings.HasSuffix(nm, "-fill.svg") {
continue
}
ic := Icon(strings.TrimSuffix(fi.Name(), ".svg"))
ics = append(ics, ic)
}
AllIcons = ics
return AllIcons
}