diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-04-24 15:59:55 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-04-24 17:08:10 +0200 |
commit | c1e02896c1c5a08b155424c7a692097ec7cf2d14 (patch) | |
tree | 34edf25f46cc9be37e4496896097d9de879ec02d /ui/systemimageprovider.go | |
parent | Revert "ui: reduce icon overlay size" (diff) | |
download | wireguard-windows-c1e02896c1c5a08b155424c7a692097ec7cf2d14.tar.xz wireguard-windows-c1e02896c1c5a08b155424c7a692097ec7cf2d14.zip |
ui: introduce system icon loading
This looks clearly worse in basically all ways and should be reverted.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to '')
-rw-r--r-- | ui/systemimageprovider.go | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/ui/systemimageprovider.go b/ui/systemimageprovider.go new file mode 100644 index 00000000..26764ccc --- /dev/null +++ b/ui/systemimageprovider.go @@ -0,0 +1,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 +} |