From 2f211074b9eaed0c6b020fee724598ee33b15d02 Mon Sep 17 00:00:00 2001 From: Kevin Subileau Date: Thu, 5 Dec 2019 19:37:28 +0100 Subject: [PATCH 1/2] Fix icon not changed after multiple SetIcon calls --- systray_windows.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/systray_windows.go b/systray_windows.go index 11893b1f..ae9583db 100644 --- a/systray_windows.go +++ b/systray_windows.go @@ -3,6 +3,7 @@ package systray import ( + "crypto/md5" "fmt" "io/ioutil" "os" @@ -76,15 +77,21 @@ func quit() { // iconBytes should be the content of .ico for windows and .ico/.jpg/.png // for other platforms. func SetIcon(iconBytes []byte) { - filename := "systray.ico" - 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("%x", md5) + ".ico" + // First, try to find a previously loaded icon in walk cache icon, err := walk.Resources.Icon(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.Icon(filename) + if err != nil { + fail("Unable to load icon", err) + } } err = notifyIcon.SetIcon(icon) if err != nil { From 57616c66d8b7eee09ca0cd3e0cc357b13c03ee67 Mon Sep 17 00:00:00 2001 From: Kevin Subileau Date: Thu, 5 Dec 2019 20:08:18 +0100 Subject: [PATCH 2/2] 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 ae9583db..a64ce385 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) }