From 57616c66d8b7eee09ca0cd3e0cc357b13c03ee67 Mon Sep 17 00:00:00 2001 From: Kevin Subileau Date: Thu, 5 Dec 2019 20:08:18 +0100 Subject: [PATCH] Fix also the MenuItem SetIcon method --- systray_windows.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/systray_windows.go b/systray_windows.go index ae9583d..a64ce38 100644 --- a/systray_windows.go +++ b/systray_windows.go @@ -78,7 +78,7 @@ func quit() { // for other platforms. func SetIcon(iconBytes []byte) { md5 := md5.Sum(iconBytes) - filename := fmt.Sprintf("%x", md5) + ".ico" + filename := fmt.Sprintf("systray.%x.ico", md5) // First, try to find a previously loaded icon in walk cache icon, err := walk.Resources.Icon(filename) if err != nil { @@ -160,15 +160,21 @@ func addOrUpdateMenuItem(item *MenuItem) { } func (item *MenuItem) SetIcon(iconBytes []byte) { - filename := fmt.Sprintf("systray.%d.ico", item.id) - err := ioutil.WriteFile(filename, iconBytes, 0644) - if err != nil { - fail("Unable to save icon to disk", err) - } - defer os.Remove(filename) + md5 := md5.Sum(iconBytes) + filename := fmt.Sprintf("systray.%x.ico", md5) + // First, try to find a previously loaded icon in walk cache icon, err := walk.Resources.Image(filename) if err != nil { - fail("Unable to load icon", err) + // Cache miss, load the icon + err := ioutil.WriteFile(filename, iconBytes, 0644) + if err != nil { + fail("Unable to save icon to disk", err) + } + defer os.Remove(filename) + icon, err = walk.Resources.Image(filename) + if err != nil { + fail("Unable to load icon", err) + } } actions[item.id].SetImage(icon) }