Skip to content

Commit

Permalink
Merge pull request #112 from ksubileau/patch-1
Browse files Browse the repository at this point in the history
Fix panic when resources root path is not the working directory
  • Loading branch information
oxtoacart authored Dec 9, 2019
2 parents 6658a36 + f226302 commit 92663d2
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions systray_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/md5"
"fmt"
"io/ioutil"
"path/filepath"
"os"
"sync/atomic"

Expand Down Expand Up @@ -79,15 +80,16 @@ func quit() {
func SetIcon(iconBytes []byte) {
md5 := md5.Sum(iconBytes)
filename := fmt.Sprintf("systray.%x.ico", md5)
iconpath := filepath.Join(walk.Resources.RootDirPath(), filename)
// First, try to find a previously loaded icon in walk cache
icon, err := walk.Resources.Icon(filename)
if err != nil {
// Cache miss, load the icon
err := ioutil.WriteFile(filename, iconBytes, 0644)
err := ioutil.WriteFile(iconpath, iconBytes, 0644)
if err != nil {
fail("Unable to save icon to disk", err)
}
defer os.Remove(filename)
defer os.Remove(iconpath)
icon, err = walk.Resources.Icon(filename)
if err != nil {
fail("Unable to load icon", err)
Expand Down Expand Up @@ -162,15 +164,16 @@ func addOrUpdateMenuItem(item *MenuItem) {
func (item *MenuItem) SetIcon(iconBytes []byte) {
md5 := md5.Sum(iconBytes)
filename := fmt.Sprintf("systray.%x.ico", md5)
iconpath := filepath.Join(walk.Resources.RootDirPath(), filename)
// First, try to find a previously loaded icon in walk cache
icon, err := walk.Resources.Image(filename)
if err != nil {
// Cache miss, load the icon
err := ioutil.WriteFile(filename, iconBytes, 0644)
err := ioutil.WriteFile(iconpath, iconBytes, 0644)
if err != nil {
fail("Unable to save icon to disk", err)
}
defer os.Remove(filename)
defer os.Remove(iconpath)
icon, err = walk.Resources.Image(filename)
if err != nil {
fail("Unable to load icon", err)
Expand Down

0 comments on commit 92663d2

Please sign in to comment.