Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions systray_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package systray

import (
"crypto/md5"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -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("systray.%x.ico", md5)
// 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 {
Expand Down Expand Up @@ -153,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)
}
Expand Down