Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic when resources root path is not the working directory #112

Merged
merged 1 commit into from
Dec 9, 2019
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
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