aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ui/iconprovider.go
blob: 6d7e77bc8dccc43a3f92fbc7f3666bdeb2b0ccad (plain) (blame)
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
/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
 */

package ui

import (
	"github.com/lxn/walk"
	"golang.zx2c4.com/wireguard/windows/service"
	"math"
)

type rectAndState struct {
	size  walk.Rectangle
	state service.TunnelState
}

type IconProvider struct {
	baseIcon             *walk.Icon
	imagesByRectAndState map[rectAndState]*walk.Bitmap
	iconsByState         map[service.TunnelState]*walk.Icon
	stoppedBrush         *walk.SolidColorBrush
	startingBrush        *walk.SolidColorBrush
	startedBrush         *walk.SolidColorBrush
	stoppedPen           *walk.CosmeticPen
	startingPen          *walk.CosmeticPen
	startedPen           *walk.CosmeticPen
	updateAvailableImage *walk.Bitmap
	scale                float64
}

const (
	colorStopped         = 0xe1e1e1
	colorStarting        = 0xfec440
	colorStarted         = 0x01a405
	colorUpdateAvailable = 0xcb0110
)

func hexColor(c uint32) walk.Color {
	return walk.Color((((c >> 16) & 0xff) << 0) | (((c >> 8) & 0xff) << 8) | (((c >> 0) & 0xff) << 16))
}

func darkColor(c walk.Color) walk.Color {
	// Convert to HSL
	r, g, b := float64((uint32(c)>>16)&0xff)/255.0, float64((uint32(c)>>8)&0xff)/255.0, float64((uint32(c)>>0)&0xff)/255.0
	min := math.Min(r, math.Min(g, b))
	max := math.Max(r, math.Max(g, b))
	deltaMinMax := max - min
	l := (max + min) / 2
	h, s := 0.0, 0.0
	if deltaMinMax != 0 {
		if l < 0.5 {
			s = deltaMinMax / (max + min)
		} else {
			s = deltaMinMax / (2 - max - min)
		}
		deltaRed := (((max - r) / 6) + (deltaMinMax / 2)) / deltaMinMax
		deltaGreen := (((max - g) / 6) + (deltaMinMax / 2)) / deltaMinMax
		deltaBlue := (((max - b) / 6) + (deltaMinMax / 2)) / deltaMinMax
		if r == max {
			h = deltaBlue - deltaGreen
		} else if g == max {
			h = (1.0 / 3.0) + deltaRed - deltaBlue
		} else if b == max {
			h = (2.0 / 3.0) + deltaGreen - deltaRed
		}

		if h < 0 {
			h += 1
		} else if h > 1 {
			h -= 1
		}
	}

	// Darken by 10%
	l = math.Max(0, l-0.1)

	// Convert back to RGB
	if s == 0 {
		return walk.Color((uint32(l*255) << 16) | (uint32(l*255) << 8) | (uint32(l*255) << 0))
	}
	var v1, v2 float64
	if l < 0.5 {
		v2 = l * (1 + s)
	} else {
		v2 = (l + s) - (s * l)
	}
	v1 = 2.0*l - v2
	co := func(v1, v2, vH float64) float64 {
		if vH < 0 {
			vH += 1
		}
		if vH > 1 {
			vH -= 1
		}
		if (6.0 * vH) < 1 {
			return v1 + (v2-v1)*6.0*vH
		}
		if (2.0 * vH) < 1 {
			return v2
		}
		if (3.0 * vH) < 2 {
			return v1 + (v2-v1)*((2.0/3.0)-vH)*6.0
		}
		return v1
	}
	r, g, b = co(v1, v2, h+(1.0/3.0)), co(v1, v2, h), co(v1, v2, h-(1.0/3.0))
	return walk.Color((uint32(r*255) << 16) | (uint32(g*255) << 8) | (uint32(b*255) << 0))
}

func NewIconProvider(dpi int) (*IconProvider, error) {
	tsip := &IconProvider{
		imagesByRectAndState: make(map[rectAndState]*walk.Bitmap),
		iconsByState:         make(map[service.TunnelState]*walk.Icon),
		scale:                float64(dpi) / 96.0,
	}

	var err error

	var disposables walk.Disposables
	defer disposables.Treat()

	if tsip.baseIcon, err = walk.NewIconFromResourceId(1); err != nil {
		return nil, err
	}
	disposables.Add(tsip.baseIcon)

	if tsip.stoppedBrush, err = walk.NewSolidColorBrush(hexColor(colorStopped)); err != nil {
		return nil, err
	}
	disposables.Add(tsip.stoppedBrush)

	if tsip.startingBrush, err = walk.NewSolidColorBrush(hexColor(colorStarting)); err != nil {
		return nil, err
	}
	disposables.Add(tsip.startingBrush)

	if tsip.startedBrush, err = walk.NewSolidColorBrush(hexColor(colorStarted)); err != nil {
		return nil, err
	}
	disposables.Add(tsip.startedBrush)

	if tsip.stoppedPen, err = walk.NewCosmeticPen(walk.PenSolid, darkColor(hexColor(colorStopped))); err != nil {
		return nil, err
	}
	disposables.Add(tsip.stoppedPen)

	if tsip.startingPen, err = walk.NewCosmeticPen(walk.PenSolid, darkColor(hexColor(colorStarting))); err != nil {
		return nil, err
	}
	disposables.Add(tsip.startingPen)

	if tsip.startedPen, err = walk.NewCosmeticPen(walk.PenSolid, darkColor(hexColor(colorStarted))); err != nil {
		return nil, err
	}
	disposables.Add(tsip.startedPen)

	if tsip.updateAvailableImage, err = tsip.drawUpdateAvailableImage(16 /* This should be scaled for DPI, but isn't because of walk bug. */); err != nil {
		return nil, err
	}
	disposables.Add(tsip.updateAvailableImage)

	disposables.Spare()
	return tsip, nil
}

func (tsip *IconProvider) Dispose() {
	if tsip.imagesByRectAndState != nil {
		for _, img := range tsip.imagesByRectAndState {
			img.Dispose()
		}
		tsip.imagesByRectAndState = nil
	}
	if tsip.iconsByState != nil {
		for _, icon := range tsip.iconsByState {
			icon.Dispose()
		}
		tsip.iconsByState = nil
	}
	if tsip.stoppedBrush != nil {
		tsip.stoppedBrush.Dispose()
		tsip.stoppedBrush = nil
	}
	if tsip.startingBrush != nil {
		tsip.startingBrush.Dispose()
		tsip.startingBrush = nil
	}
	if tsip.startedBrush != nil {
		tsip.startedBrush.Dispose()
		tsip.startedBrush = nil
	}
	if tsip.stoppedPen != nil {
		tsip.stoppedPen.Dispose()
		tsip.stoppedPen = nil
	}
	if tsip.startingPen != nil {
		tsip.startingPen.Dispose()
		tsip.startingPen = nil
	}
	if tsip.startedPen != nil {
		tsip.startedPen.Dispose()
		tsip.startedPen = nil
	}
	if tsip.baseIcon != nil {
		tsip.baseIcon.Dispose()
		tsip.baseIcon = nil
	}
	if tsip.updateAvailableImage != nil {
		tsip.updateAvailableImage.Dispose()
		tsip.updateAvailableImage = nil
	}
}

func (tsip *IconProvider) scaleForDPI(i int) int {
	return int(tsip.scale * float64(i))
}

func (tsip *IconProvider) drawUpdateAvailableImage(size int) (*walk.Bitmap, error) {
	updateAvailableBrush, err := walk.NewSolidColorBrush(hexColor(colorUpdateAvailable))
	if err != nil {
		return nil, err
	}
	defer updateAvailableBrush.Dispose()
	updateAvailablePen, err := walk.NewCosmeticPen(walk.PenSolid, darkColor(hexColor(colorUpdateAvailable)))
	if err != nil {
		return nil, err
	}
	defer updateAvailablePen.Dispose()

	img, err := walk.NewBitmapWithTransparentPixels(walk.Size{size, size})
	if err != nil {
		return nil, err
	}

	canvas, err := walk.NewCanvasFromImage(img)
	if err != nil {
		img.Dispose()
		return nil, err
	}
	defer canvas.Dispose()

	// This should be scaled for DPI but instead we do the opposite, due to a walk bug.
	margin := int(3.0 - (tsip.scale - 1.0) * 3.0)
	if margin < 0 {
		margin = 0
	}
	rect := walk.Rectangle{margin, margin, size - margin*2, size - margin*2}

	if err := canvas.FillEllipse(updateAvailableBrush, rect); err != nil {
		img.Dispose()
		return nil, err
	}
	if err := canvas.DrawEllipse(updateAvailablePen, rect); err != nil {
		img.Dispose()
		return nil, err
	}
	return img, nil
}

func (tsip *IconProvider) ImageForTunnel(tunnel *service.Tunnel, size walk.Size) (*walk.Bitmap, error) {
	state, err := tunnel.State()
	if err != nil {
		return nil, err
	}

	return tsip.ImageForState(state, walk.Rectangle{0, 0, size.Width, size.Height})
}

func (tsip *IconProvider) ImageForState(state service.TunnelState, rect walk.Rectangle) (*walk.Bitmap, error) {
	key := rectAndState{rect, state}

	if img, ok := tsip.imagesByRectAndState[key]; ok {
		return img, nil
	}

	img, err := walk.NewBitmapWithTransparentPixels(rect.Size())
	if err != nil {
		return nil, err
	}

	canvas, err := walk.NewCanvasFromImage(img)
	if err != nil {
		return nil, err
	}
	defer canvas.Dispose()

	if err := tsip.PaintForState(state, canvas, rect); err != nil {
		return nil, err
	}

	tsip.imagesByRectAndState[key] = img

	return img, nil
}

func (tsip *IconProvider) IconWithOverlayForState(state service.TunnelState) (*walk.Icon, error) {
	if icon, ok := tsip.iconsByState[state]; ok {
		return icon, nil
	}

	size := tsip.baseIcon.Size()

	bmp, err := walk.NewBitmapWithTransparentPixels(size)
	if err != nil {
		return nil, err
	}
	defer bmp.Dispose()

	canvas, err := walk.NewCanvasFromImage(bmp)
	if err != nil {
		return nil, err
	}
	defer canvas.Dispose()

	if err := canvas.DrawImage(tsip.baseIcon, walk.Point{}); err != nil {
		return nil, err
	}

	w := int(float64(size.Width) * 0.75)
	h := int(float64(size.Height) * 0.75)
	margin := tsip.scaleForDPI(2)
	bounds := walk.Rectangle{margin + size.Width - w, margin + size.Height - h, w, h}

	if err := tsip.PaintForState(state, canvas, bounds); err != nil {
		return nil, err
	}

	canvas.Dispose()

	icon, err := walk.NewIconFromBitmap(bmp)
	if err != nil {
		return nil, err
	}

	tsip.iconsByState[state] = icon

	return icon, nil
}

func (tsip *IconProvider) PaintForTunnel(tunnel *service.Tunnel, canvas *walk.Canvas, bounds walk.Rectangle) error {
	state, err := tunnel.State()
	if err != nil {
		return err
	}

	return tsip.PaintForState(state, canvas, bounds)
}

func (tsip *IconProvider) PaintForState(state service.TunnelState, canvas *walk.Canvas, bounds walk.Rectangle) error {
	var (
		brush *walk.SolidColorBrush
		pen   *walk.CosmeticPen
	)

	switch state {
	case service.TunnelStarted:
		brush = tsip.startedBrush
		pen = tsip.startedPen

	case service.TunnelStopped:
		brush = tsip.stoppedBrush
		pen = tsip.stoppedPen

	default:
		brush = tsip.startingBrush
		pen = tsip.startingPen
	}

	b := bounds

	b.X += tsip.scaleForDPI(2)
	b.Y += tsip.scaleForDPI(2)
	b.Height -= tsip.scaleForDPI(4)
	b.Width = b.Height

	if err := canvas.FillEllipse(brush, b); err != nil {
		return err
	}
	if err := canvas.DrawEllipse(pen, b); err != nil {
		return err
	}

	return nil
}