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

package ui

import (
	"fmt"
	"github.com/lxn/walk"
	"github.com/lxn/win"
	"golang.org/x/sys/windows"
	"golang.zx2c4.com/wireguard/windows/service"
	"path"
	"syscall"
)

type widthAndState struct {
	width int
	state service.TunnelState
}

type widthAndDllIdx struct {
	width int
	idx   int32
	dll   string
}

var cachedOverlayIconsForWidthAndState = make(map[widthAndState]*walk.Icon)

func iconWithOverlayForState(state service.TunnelState, size int) (icon *walk.Icon, err error) {
	icon = cachedOverlayIconsForWidthAndState[widthAndState{size, state}]
	if icon != nil {
		return
	}
	wireguardIcon, err := loadLogoIcon(size)
	if err != nil {
		return
	}
	if state == service.TunnelStopped {
		return wireguardIcon, err //TODO: if we find something prettier than the gray dot, then remove this clause
	}
	iconSize := wireguardIcon.Size()
	bmp, err := walk.NewBitmapWithTransparentPixels(iconSize)
	if err != nil {
		return
	}
	defer bmp.Dispose()

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

	err = canvas.DrawImage(wireguardIcon, walk.Point{})
	if err != nil {
		return
	}

	w := int(float64(iconSize.Width) * 0.65)
	h := int(float64(iconSize.Height) * 0.65)
	bounds := walk.Rectangle{iconSize.Width - w, iconSize.Height - h, w, h}
	overlayIcon, err := iconForState(state, bounds.Width)
	if err != nil {
		return
	}
	defer overlayIcon.Dispose()
	err = canvas.DrawImageStretched(overlayIcon, bounds)
	if err != nil {
		return
	}
	canvas.Dispose()

	icon, err = walk.NewIconFromBitmap(bmp)
	if err == nil {
		cachedOverlayIconsForWidthAndState[widthAndState{size, state}] = icon
	}
	return
}

var cachedIconsForWidthAndState = make(map[widthAndState]*walk.Icon)

func iconForState(state service.TunnelState, size int) (icon *walk.Icon, err error) {
	icon = cachedIconsForWidthAndState[widthAndState{size, state}]
	if icon != nil {
		return
	}
	switch state {
	case service.TunnelStarted:
		icon, err = loadSystemIcon("imageres", 101, size)
	case service.TunnelStopped:
		icon, err = walk.NewIconFromResourceWithSize("dot-gray.ico", walk.Size{size, size}) //TODO: replace with real icon
	default:
		icon, err = loadSystemIcon("shell32", 238, size) //TODO: this doesn't look that great overlayed on the app icon
	}
	if err == nil {
		cachedIconsForWidthAndState[widthAndState{size, state}] = icon
	}
	return
}

var cachedSystemIconsForWidthAndDllIdx = make(map[widthAndDllIdx]*walk.Icon)

func loadSystemIcon(dll string, index int32, size int) (icon *walk.Icon, err error) {
	icon = cachedSystemIconsForWidthAndDllIdx[widthAndDllIdx{size, index, dll}]
	if icon != nil {
		return
	}
	system32, err := windows.GetSystemDirectory()
	if err != nil {
		return
	}
	var hicon win.HICON
	ret := win.SHDefExtractIcon(windows.StringToUTF16Ptr(path.Join(system32, dll+".dll")), index, 0, &hicon, nil, uint32(size))
	if ret != 0 {
		return nil, fmt.Errorf("Unable to find icon %d of %s: %v", index, dll, syscall.Errno(ret))
	}
	icon, err = walk.NewIconFromHICON(hicon)
	if err == nil {
		cachedSystemIconsForWidthAndDllIdx[widthAndDllIdx{size, index, dll}] = icon
	}
	return
}

var cachedLogoIconsForWidth = make(map[int]*walk.Icon)

func loadLogoIcon(size int) (icon *walk.Icon, err error) {
	icon = cachedLogoIconsForWidth[size]
	if icon != nil {
		return
	}
	icon, err = walk.NewIconFromResourceWithSize("$wireguard.ico", walk.Size{size, size})
	if err == nil {
		cachedLogoIconsForWidth[size] = icon
	}
	return
}