aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ui/iconprovider.go
diff options
context:
space:
mode:
authorAlexander Neumann <alexander.neumann@picos-software.com>2019-05-14 18:18:06 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2019-05-15 20:11:19 +0200
commit6cefb70b18b4efab53cda023e643ac2aed67a1be (patch)
tree89e4461bc9aba159e91396dde0b165fda74e886b /ui/iconprovider.go
parentui: don't closehandle on a menu (diff)
downloadwireguard-windows-6cefb70b18b4efab53cda023e643ac2aed67a1be.tar.xz
wireguard-windows-6cefb70b18b4efab53cda023e643ac2aed67a1be.zip
ui: fix dpi related code smells
Signed-off-by: Alexander Neumann <alexander.neumann@picos-software.com> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ui/iconprovider.go')
-rw-r--r--ui/iconprovider.go67
1 files changed, 20 insertions, 47 deletions
diff --git a/ui/iconprovider.go b/ui/iconprovider.go
index 52150a46..d7d6fbcf 100644
--- a/ui/iconprovider.go
+++ b/ui/iconprovider.go
@@ -6,13 +6,7 @@
package ui
import (
- "fmt"
- "path"
- "syscall"
-
"github.com/lxn/walk"
- "github.com/lxn/win"
- "golang.org/x/sys/windows"
"golang.zx2c4.com/wireguard/windows/service"
)
@@ -27,56 +21,44 @@ type widthAndDllIdx struct {
dll string
}
-var cachedOverlayIconsForWidthAndState = make(map[widthAndState]*walk.Icon)
+var cachedOverlayIconsForWidthAndState = make(map[widthAndState]walk.Image)
-func iconWithOverlayForState(state service.TunnelState, size int) (icon *walk.Icon, err error) {
+func iconWithOverlayForState(state service.TunnelState, size int) (icon walk.Image, 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
- }
+ iconSize := wireguardIcon.Size()
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)
+ overlayBounds := walk.Rectangle{iconSize.Width - w, iconSize.Height - h, w, h}
+ overlayIcon, err := iconForState(state, overlayBounds.Width)
if err != nil {
return
}
- canvas.Dispose()
- icon, err = walk.NewIconFromBitmap(bmp)
- if err == nil {
- cachedOverlayIconsForWidthAndState[widthAndState{size, state}] = icon
- }
+ icon = walk.NewPaintFuncImage(walk.Size{size, size}, func(canvas *walk.Canvas, bounds walk.Rectangle) error {
+ if err := canvas.DrawImageStretched(wireguardIcon, bounds); err != nil {
+ return err
+ }
+ if err := canvas.DrawImageStretched(overlayIcon, overlayBounds); err != nil {
+ return err
+ }
+ return nil
+ })
+
+ cachedOverlayIconsForWidthAndState[widthAndState{size, state}] = icon
+
return
}
@@ -130,16 +112,7 @@ func loadSystemIcon(dll string, index int32, size int) (icon *walk.Icon, err err
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)
+ icon, err = walk.NewIconFromSysDLLWithSize(dll, int(index), size)
if err == nil {
cachedSystemIconsForWidthAndDllIdx[widthAndDllIdx{size, index, dll}] = icon
}