aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-05-06 18:13:51 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2019-05-07 07:30:36 +0200
commit630504d0190570813fabdfc45d4fd798ffb4d372 (patch)
treee7fbbd65401176ea338893ff68ccab1d00e0f350
parentui: confview should be more picky about which tunnel (diff)
downloadwireguard-windows-630504d0190570813fabdfc45d4fd798ffb4d372.tar.xz
wireguard-windows-630504d0190570813fabdfc45d4fd798ffb4d372.zip
ui: move things out of iconprovider
-rw-r--r--ui/confview.go22
-rw-r--r--ui/iconprovider.go139
-rw-r--r--ui/listview.go18
-rw-r--r--ui/managewindow.go5
-rw-r--r--ui/ui.go2
5 files changed, 60 insertions, 126 deletions
diff --git a/ui/confview.go b/ui/confview.go
index ad5e89b..518164c 100644
--- a/ui/confview.go
+++ b/ui/confview.go
@@ -79,11 +79,27 @@ func (lsl *labelStatusLine) widgets() (walk.Widget, walk.Widget) {
}
func (lsl *labelStatusLine) update(state service.TunnelState) {
- labelSize := lsl.label.SizeHint()
- imageRect := walk.Rectangle{0, 0, labelSize.Height, labelSize.Height}
- img, err := iconProvider.ImageForState(state, imageRect)
+ icon, err := iconProvider.IconForState(state)
if err == nil {
+ margin := lsl.label.DPI() / 48 //TODO: Do some sort of dynamic DPI calculation here
+ labelSize := lsl.label.SizeHint()
+ imageRect := walk.Rectangle{0, 0, labelSize.Height, labelSize.Height}
+ img, _ := walk.NewBitmapWithTransparentPixels(imageRect.Size())
+ canvas, _ := walk.NewCanvasFromImage(img)
+ imageRect.X += margin
+ imageRect.Y += margin * 2 //TODO: the *2 here fixes weird alignment bugs. Why?
+ imageRect.Height -= margin * 2
+ imageRect.Width -= margin * 2
+ canvas.DrawImageStretched(icon, imageRect)
+ icon.Dispose()
+ canvas.Dispose()
+ prior := lsl.statusImage.Image()
lsl.statusImage.SetImage(img)
+ if prior != nil {
+ prior.Dispose()
+ }
+ } else {
+ lsl.statusImage.SetImage(nil)
}
s, e := lsl.statusLabel.TextSelection()
switch state {
diff --git a/ui/iconprovider.go b/ui/iconprovider.go
index e1bee86..ac373d9 100644
--- a/ui/iconprovider.go
+++ b/ui/iconprovider.go
@@ -14,51 +14,26 @@ import (
"path"
)
-type rectAndState struct {
- size walk.Rectangle
- state service.TunnelState
-}
-
type IconProvider struct {
- wireguardIcon *walk.Icon
- imagesByRectAndState map[rectAndState]*walk.Bitmap
- iconsByState map[service.TunnelState]*walk.Icon
- scale float64
+ wireguardIcon *walk.Icon
+ overlayIconsByState map[service.TunnelState]*walk.Icon
}
-func NewIconProvider(dpi int) (*IconProvider, error) {
- tsip := &IconProvider{
- imagesByRectAndState: make(map[rectAndState]*walk.Bitmap),
- iconsByState: make(map[service.TunnelState]*walk.Icon),
- scale: float64(dpi) / 96.0,
- }
-
+func NewIconProvider() (*IconProvider, error) {
+ tsip := &IconProvider{overlayIconsByState: make(map[service.TunnelState]*walk.Icon)}
var err error
-
- var disposables walk.Disposables
- defer disposables.Treat()
-
if tsip.wireguardIcon, err = walk.NewIconFromResource("$wireguard.ico"); err != nil {
return nil, err
}
- disposables.Add(tsip.wireguardIcon)
-
- disposables.Spare()
return tsip, nil
}
func (tsip *IconProvider) Dispose() {
- if tsip.imagesByRectAndState != nil {
- for _, img := range tsip.imagesByRectAndState {
- img.Dispose()
- }
- tsip.imagesByRectAndState = nil
- }
- if tsip.iconsByState != nil {
- for _, icon := range tsip.iconsByState {
+ if tsip.overlayIconsByState != nil {
+ for _, icon := range tsip.overlayIconsByState {
icon.Dispose()
}
- tsip.iconsByState = nil
+ tsip.overlayIconsByState = nil
}
if tsip.wireguardIcon != nil {
tsip.wireguardIcon.Dispose()
@@ -66,48 +41,8 @@ func (tsip *IconProvider) Dispose() {
}
}
-func (tsip *IconProvider) scaleForDPI(i int) int {
- return int(tsip.scale * float64(i))
-}
-
-func (tsip *IconProvider) ImageForTunnel(tunnel *service.Tunnel, size walk.Size) (*walk.Bitmap, error) {
- state, err := tunnel.State()
- if err != nil {
- return nil, err
- }
-
- return tsip.ImageForState(state, walk.Rectangle{0, 0, size.Width, size.Height})
-}
-
-func (tsip *IconProvider) ImageForState(state service.TunnelState, rect walk.Rectangle) (*walk.Bitmap, error) {
- key := rectAndState{rect, state}
-
- if img, ok := tsip.imagesByRectAndState[key]; ok {
- return img, nil
- }
-
- img, err := walk.NewBitmapWithTransparentPixels(rect.Size())
- if err != nil {
- return nil, err
- }
-
- canvas, err := walk.NewCanvasFromImage(img)
- if err != nil {
- return nil, err
- }
- defer canvas.Dispose()
-
- if err := tsip.PaintForState(state, canvas, rect); err != nil {
- return nil, err
- }
-
- tsip.imagesByRectAndState[key] = img
-
- return img, nil
-}
-
func (tsip *IconProvider) IconWithOverlayForState(state service.TunnelState) (*walk.Icon, error) {
- if icon, ok := tsip.iconsByState[state]; ok {
+ if icon, ok := tsip.overlayIconsByState[state]; ok {
return icon, nil
}
@@ -129,67 +64,41 @@ func (tsip *IconProvider) IconWithOverlayForState(state service.TunnelState) (*w
return nil, err
}
- w := int(float64(size.Width) * 0.75)
- h := int(float64(size.Height) * 0.75)
- margin := tsip.scaleForDPI(2)
- bounds := walk.Rectangle{margin + size.Width - w, margin + size.Height - h, w, h}
-
- if err := tsip.PaintForState(state, canvas, bounds); err != nil {
+ overlayIcon, err := tsip.IconForState(state)
+ if err != nil {
return nil, err
}
+ defer overlayIcon.Dispose()
+ w := int(float64(size.Width) * 0.65)
+ h := int(float64(size.Height) * 0.65)
+ bounds := walk.Rectangle{size.Width - w, size.Height - h, w, h}
+
+ if err := canvas.DrawImageStretched(overlayIcon, bounds); err != nil {
+ return nil, err
+ }
canvas.Dispose()
icon, err := walk.NewIconFromBitmap(bmp)
if err != nil {
return nil, err
}
-
- tsip.iconsByState[state] = icon
-
+ tsip.overlayIconsByState[state] = icon
return icon, nil
}
-func (tsip *IconProvider) PaintForTunnel(tunnel *service.Tunnel, canvas *walk.Canvas, bounds walk.Rectangle) error {
- state, err := tunnel.State()
- if err != nil {
- return err
- }
-
- return tsip.PaintForState(state, canvas, bounds)
-}
-
-func (tsip *IconProvider) PaintForState(state service.TunnelState, canvas *walk.Canvas, bounds walk.Rectangle) error {
- iconSize := tsip.scaleForDPI(bounds.Height)
- var dot *walk.Icon
- var err error
-
+func (tsip *IconProvider) IconForState(state service.TunnelState) (icon *walk.Icon, err error) {
switch state {
case service.TunnelStarted:
- dot, err = loadSystemIcon("imageres", 101)
+ icon, err = loadSystemIcon("imageres", 101)
case service.TunnelStopped:
- dot, err = walk.NewIconFromResourceWithSize("dot-gray.ico", walk.Size{iconSize, iconSize}) //TODO: replace with real icon
+ icon, err = walk.NewIconFromResource("dot-gray.ico") //TODO: replace with real icon
default:
- dot, err = loadSystemIcon("shell32", 238) //TODO: this doesn't look that great overlayed on the app icon
+ icon, err = loadSystemIcon("shell32", 238) //TODO: this doesn't look that great overlayed on the app icon
}
- if err != nil {
- return err
- }
- defer dot.Dispose()
-
- b := bounds
- b.X += tsip.scaleForDPI(2)
- b.Y += tsip.scaleForDPI(2)
- b.Height -= tsip.scaleForDPI(4)
- b.Width = b.Height
-
- if err := canvas.DrawImageStretched(dot, b); err != nil {
- return err
- }
-
- return nil
+ return
}
func loadSystemIcon(dll string, index uint) (*walk.Icon, error) {
diff --git a/ui/listview.go b/ui/listview.go
index 2e9ee3a..814a799 100644
--- a/ui/listview.go
+++ b/ui/listview.go
@@ -122,10 +122,22 @@ func (tv *ListView) StyleCell(style *walk.CellStyle) {
b.Width -= b.Height
canvas.DrawText(tunnel.Name, tv.Font(), 0, b, walk.TextVCenter|walk.TextSingleLine)
- b.X = 0
+ //TODO: don't make an IPC call from the drawing thread like this!
+ state, err := tunnel.State()
+ if err != nil {
+ return
+ }
+ margin := tv.DPI() / 48 //TODO: Do some sort of dynamic DPI calculation here
+ b.X = margin
+ b.Y += margin
+ b.Height -= margin * 2
b.Width = b.Height
-
- iconProvider.PaintForTunnel(tunnel, canvas, b)
+ icon, err := iconProvider.IconForState(state)
+ if err != nil {
+ return
+ }
+ canvas.DrawImageStretched(icon, b)
+ icon.Dispose()
}
func (tv *ListView) CurrentTunnel() *service.Tunnel {
diff --git a/ui/managewindow.go b/ui/managewindow.go
index dd2de7f..b8a34c3 100644
--- a/ui/managewindow.go
+++ b/ui/managewindow.go
@@ -36,11 +36,6 @@ func NewManageTunnelsWindow() (*ManageTunnelsWindow, error) {
}
disposables.Add(mtw)
- iconProvider, err = NewIconProvider(mtw.DPI())
- if err != nil {
- return nil, err
- }
-
mtw.SetIcon(iconProvider.wireguardIcon)
mtw.SetTitle("WireGuard")
font, err := walk.NewFont("Segoe UI", 9, 0)
diff --git a/ui/ui.go b/ui/ui.go
index 2e2b56e..11bc0ef 100644
--- a/ui/ui.go
+++ b/ui/ui.go
@@ -35,6 +35,8 @@ func RunUI() {
tray *Tray
)
+ iconProvider, _ = NewIconProvider()
+
for mtw == nil {
mtw, err = NewManageTunnelsWindow()
if err != nil {