-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathicons.go
238 lines (198 loc) · 5.51 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
//
// Copyright (c) 2017 Dean Jackson <deanishe@deanishe.net>
//
// MIT Licence. See http://opensource.org/licenses/MIT
//
// Created on 2017-11-26
//
package main
import (
"encoding/hex"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"io"
"log"
"net"
"net/http"
"os"
"path/filepath"
"time"
aw "github.com/deanishe/awgo"
"github.com/deanishe/awgo/util"
"github.com/pkg/errors"
)
var (
// Static icons
iconAccount = &aw.Icon{Value: "icons/account.png"}
iconAccountAdd = &aw.Icon{Value: "icons/account-add.png"}
iconDefault = &aw.Icon{Value: "icon.png"} // Workflow icon
iconCalendar = &aw.Icon{Value: "icons/calendar.png"}
iconCalendars = &aw.Icon{Value: "icons/calendars.png"}
iconCalOff = &aw.Icon{Value: "icons/calendar-off.png"}
iconCalOn = &aw.Icon{Value: "icons/calendar-on.png"}
iconCalToday = &aw.Icon{Value: "icons/calendar-today.png"}
iconDay = &aw.Icon{Value: "icons/day.png"}
iconDelete = &aw.Icon{Value: "icons/trash.png"}
iconDocs = &aw.Icon{Value: "icons/docs.png"}
iconIssue = &aw.Icon{Value: "icons/issue.png"}
iconHelp = &aw.Icon{Value: "icons/help.png"}
iconMap = &aw.Icon{Value: "icons/map.png"}
iconNext = &aw.Icon{Value: "icons/next.png"}
iconPrevious = &aw.Icon{Value: "icons/previous.png"}
iconLoading = &aw.Icon{Value: "icons/loading.png"}
iconUpdateOK = &aw.Icon{Value: "icons/update-ok.png"}
iconUpdateAvailable = &aw.Icon{Value: "icons/update-available.png"}
iconWarning = &aw.Icon{Value: "icons/warning.png"}
iconAppleMaps = &aw.Icon{Value: "/Applications/Maps.app", Type: aw.IconTypeFileIcon}
iconGoogleMaps = &aw.Icon{Value: "icons/google-maps.png"}
)
func init() {
aw.IconWarning = iconWarning
// Maps.app has moved on Catalina
if util.PathExists("/System/Applications/Maps.app") {
iconAppleMaps.Value = "/System/Applications/Maps.app"
}
}
// ColouredIcon returns a version of icon in the given colour. If no colour
// is specified or something goes wrong, icon is simply returned.
func ColouredIcon(icon *aw.Icon, colour string) *aw.Icon {
var (
c color.RGBA
path string
err error
)
if c, err = ParseHexColour(colour); err != nil {
log.Printf("[icons] ERR: %s", err)
return icon
}
path = iconCachePath(icon, c)
if util.PathExists(path) {
return &aw.Icon{Value: path}
}
if err = generateIcon(icon.Value, path, c); err != nil {
log.Printf("[icons] ERR: generate icon: %v", err)
return icon
}
return &aw.Icon{Value: path}
}
var client = &http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 60 * time.Second,
KeepAlive: 60 * time.Second,
}).Dial,
TLSHandshakeTimeout: 30 * time.Second,
ResponseHeaderTimeout: 30 * time.Second,
ExpectContinueTimeout: 10 * time.Second,
},
}
// Save contents of URL to path.
func download(url, path string) error {
r, err := client.Get(url)
if err != nil {
return err
}
defer r.Body.Close()
log.Printf("[%d] %s", r.StatusCode, url)
if r.StatusCode > 299 {
return fmt.Errorf("bad HTTP response: [%d] %s", r.StatusCode, url)
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(f, r.Body); err != nil {
return err
}
log.Printf("[icons] saved %q to %q\n", url, path)
return nil
}
func generateIcon(src, dest string, c color.RGBA) error {
// defer util.Timed(time.Now(), "generate icon")
var (
f *os.File
mask image.Image
err error
)
if f, err = os.Open(src); err != nil {
return errors.Wrap(err, "open file")
}
defer f.Close()
if mask, _, err = image.Decode(f); err != nil {
return errors.Wrap(err, "decode image")
}
img := image.NewRGBA(mask.Bounds())
draw.DrawMask(img, img.Bounds(), &image.Uniform{c}, image.Point{}, mask, image.Point{}, draw.Src)
if f, err = os.Create(dest); err != nil {
return errors.Wrap(err, "create file")
}
defer f.Close()
if err = png.Encode(f, img); err != nil {
return errors.Wrap(err, "write PNG data")
}
rel, _ := filepath.Rel(wf.CacheDir(), dest)
log.Printf("[icons] new icon: %s", rel)
return nil
}
func iconCachePath(i *aw.Icon, c color.RGBA) string {
name := filepath.Base(i.Value)
dir := fmt.Sprintf("%02X/%02X/%02X/%02X", c.R, c.G, c.B, c.A)
dir = filepath.Join(cacheDirIcons, dir)
util.MustExist(dir)
return filepath.Join(dir, name)
}
// ParseHexColour parses a CSS hex (e.g. #ffffff) to RGBA.
//
// Input must be with preceding # and have 6 (RGB) or 8 (RGBA) characters.
func ParseHexColour(s string) (color.RGBA, error) {
var (
// default to 100% opaque
c = color.RGBA{A: 0xff}
err error
)
if s[0] == '#' {
s = s[1:]
}
b, err := hex.DecodeString(s)
if err != nil {
return c, fmt.Errorf("invalid colour: %q", s)
}
switch len(b) {
case 3:
c.R = b[0]
c.G = b[1]
c.B = b[2]
case 4:
c.R = b[0]
c.G = b[1]
c.B = b[2]
c.A = b[3]
default:
err = fmt.Errorf("invalid colour: %q", s)
}
return c, err
}
// ReloadIcon returns a spinner icon. It rotates by 15 deg on every
// subsequent call. Use with wf.Reload(0.1) to implement an animated
// spinner.
func ReloadIcon() *aw.Icon {
var (
step = 15
max = (45 / step) - 1
current = wf.Config.GetInt("RELOAD_PROGRESS", 0)
next = current + 1
)
if next > max {
next = 0
}
log.Printf("progress: current=%d, next=%d", current, next)
wf.Var("RELOAD_PROGRESS", fmt.Sprintf("%d", next))
if current == 0 {
return iconLoading
}
return &aw.Icon{Value: fmt.Sprintf("icons/loading-%d.png", current*step)}
}