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
|
/* 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"
)
type sizeAndState struct {
size walk.Size
state service.TunnelState
}
type iconAndState struct {
icon *walk.Icon
state service.TunnelState
}
type TunnelStatusImageProvider struct {
imagesBySizeAndState map[sizeAndState]*walk.Bitmap
iconsByBaseIconAndState map[iconAndState]*walk.Icon
stoppedBrush *walk.SolidColorBrush
startingBrush *walk.SolidColorBrush
startedBrush *walk.SolidColorBrush
stoppedPen *walk.CosmeticPen
startingPen *walk.CosmeticPen
startedPen *walk.CosmeticPen
}
func NewTunnelStatusImageProvider() (*TunnelStatusImageProvider, error) {
tsip := &TunnelStatusImageProvider{
imagesBySizeAndState: make(map[sizeAndState]*walk.Bitmap),
iconsByBaseIconAndState: make(map[iconAndState]*walk.Icon),
}
var err error
var disposables walk.Disposables
defer disposables.Treat()
if tsip.stoppedBrush, err = walk.NewSolidColorBrush(walk.RGB(225, 225, 225)); err != nil {
return nil, err
}
disposables.Add(tsip.stoppedBrush)
if tsip.startingBrush, err = walk.NewSolidColorBrush(walk.RGB(254, 192, 49)); err != nil {
return nil, err
}
disposables.Add(tsip.startingBrush)
if tsip.startedBrush, err = walk.NewSolidColorBrush(walk.RGB(54, 206, 66)); err != nil {
return nil, err
}
disposables.Add(tsip.startedBrush)
if tsip.stoppedPen, err = walk.NewCosmeticPen(walk.PenSolid, walk.RGB(225-10, 225-10, 225-10)); err != nil {
return nil, err
}
disposables.Add(tsip.stoppedPen)
if tsip.startingPen, err = walk.NewCosmeticPen(walk.PenSolid, walk.RGB(254-10, 192-10, 49-10)); err != nil {
return nil, err
}
disposables.Add(tsip.startingPen)
if tsip.startedPen, err = walk.NewCosmeticPen(walk.PenSolid, walk.RGB(54-10, 206-10, 66-10)); err != nil {
return nil, err
}
disposables.Add(tsip.startedPen)
disposables.Spare()
return tsip, nil
}
func (tsip *TunnelStatusImageProvider) Dispose() {
if tsip.imagesBySizeAndState != nil {
for _, img := range tsip.imagesBySizeAndState {
img.Dispose()
}
tsip.imagesBySizeAndState = nil
}
if tsip.iconsByBaseIconAndState != nil {
for _, icon := range tsip.iconsByBaseIconAndState {
icon.Dispose()
}
tsip.iconsByBaseIconAndState = 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
}
}
func (tsip *TunnelStatusImageProvider) ImageForTunnel(tunnel *service.Tunnel, size walk.Size) (*walk.Bitmap, error) {
state, err := tunnel.State()
if err != nil {
return nil, err
}
return tsip.ImageForState(state, size)
}
func (tsip *TunnelStatusImageProvider) ImageForState(state service.TunnelState, size walk.Size) (*walk.Bitmap, error) {
key := sizeAndState{size, state}
if img, ok := tsip.imagesBySizeAndState[key]; ok {
return img, nil
}
img, err := walk.NewBitmapWithTransparentPixels(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, walk.Rectangle{0, 0, size.Width, size.Height}); err != nil {
return nil, err
}
tsip.imagesBySizeAndState[key] = img
return img, nil
}
func (tsip *TunnelStatusImageProvider) IconWithOverlayForTunnel(baseIcon *walk.Icon, tunnel *service.Tunnel) (*walk.Icon, error) {
state, err := tunnel.State()
if err != nil {
return nil, err
}
return tsip.IconWithOverlayForState(baseIcon, state)
}
func (tsip *TunnelStatusImageProvider) IconWithOverlayForState(baseIcon *walk.Icon, state service.TunnelState) (*walk.Icon, error) {
key := iconAndState{baseIcon, state}
if icon, ok := tsip.iconsByBaseIconAndState[key]; ok {
return icon, nil
}
size := 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(baseIcon, walk.Point{}); err != nil {
return nil, err
}
w := int(float64(size.Width) * 0.6)
h := int(float64(size.Height) * 0.6)
bounds := walk.Rectangle{4 + size.Width - w, 4 + 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.iconsByBaseIconAndState[key] = icon
return icon, nil
}
func (tsip *TunnelStatusImageProvider) 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 *TunnelStatusImageProvider) 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.TunnelStarting:
brush = tsip.startingBrush
pen = tsip.startingPen
default:
brush = tsip.stoppedBrush
pen = tsip.stoppedPen
}
b := bounds
b.X += 4
b.Y += 4
b.Height -= 8
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
}
|