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

package ui

import (
	"fmt"
	"path"

	"github.com/lxn/walk"
	"github.com/lxn/win"
	"golang.org/x/sys/windows"
)

var (
	systemIconAddTunnel,
	systemIconAddTunnelFromScratch,
	systemIconAddTunnelFromFile,
	systemIconDeleteTunnel,
	systemIconExportTunnels,
	systemIconSaveTunnelsToZip,
	_ *walk.Icon
)

func loadSystemIcon(dll string, index uint) (*walk.Icon, error) {
	system32, err := windows.GetSystemDirectory()
	if err != nil {
		return nil, err
	}
	hicon := win.ExtractIcon(win.GetModuleHandle(nil), windows.StringToUTF16Ptr(path.Join(system32, dll+".dll")), int32(index))
	if hicon <= 1 {
		return nil, fmt.Errorf("Unable to find icon %d of %s", index, dll)
	}
	return walk.NewIconFromHICON(hicon)
}

func setIconOnAction(wb *walk.WindowBase, action *walk.Action, icon *walk.Icon) error {
	//TODO: this is an unholy hack. Fix walk!
	bitmap, err := walk.NewBitmapFromIcon(icon, walk.Size{32, 32})
	if err != nil {
		return err
	}
	wb.AddDisposable(bitmap)
	return action.SetImage(bitmap)
}

func loadSystemIcons() (err error) {
	//TODO: this should probably be in an object that is disposable instead of using globals like this

	systemIconAddTunnel, err = loadSystemIcon("shell32", 149)
	if err != nil {
		return
	}
	systemIconAddTunnelFromScratch, err = loadSystemIcon("imageres", 2)
	if err != nil {
		return
	}
	systemIconAddTunnelFromFile, err = loadSystemIcon("imageres", 3)
	if err != nil {
		return
	}
	systemIconDeleteTunnel, err = loadSystemIcon("shell32", 131)
	if err != nil {
		return
	}
	systemIconExportTunnels, err = loadSystemIcon("shell32", 45)
	if err != nil {
		return
	}
	systemIconSaveTunnelsToZip, err = loadSystemIcon("imageres", 165)
	if err != nil {
		return
	}

	return
}