From 8d29c7136535524e387091a7387a33a243062f2b Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Wed, 24 Apr 2019 15:59:55 +0200 Subject: ui: introduce system icon loading This looks clearly worse in basically all ways and should be reverted. --- ui/systemimageprovider.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 ui/systemimageprovider.go (limited to 'ui/systemimageprovider.go') 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 +} -- cgit v1.2.3-59-g8ed1b