forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicon_cache_file.go
59 lines (47 loc) · 1.03 KB
/
icon_cache_file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package app
import (
"os"
"path/filepath"
"sync"
"fyne.io/fyne/v2"
)
var once sync.Once
func (a *fyneApp) cachedIconPath() string {
if a.Icon() == nil {
return ""
}
dirPath := filepath.Join(rootCacheDir(), a.UniqueID())
filePath := filepath.Join(dirPath, "icon.png")
once.Do(func() {
err := a.saveIconToCache(dirPath, filePath)
if err != nil {
filePath = ""
}
})
return filePath
}
func rootCacheDir() string {
desktopCache, _ := os.UserCacheDir()
return filepath.Join(desktopCache, "fyne")
}
func (a *fyneApp) saveIconToCache(dirPath, filePath string) error {
err := os.MkdirAll(dirPath, 0700)
if err != nil {
fyne.LogError("Unable to create application cache directory", err)
return err
}
file, err := os.Create(filePath)
if err != nil {
fyne.LogError("Unable to create icon file", err)
return err
}
defer file.Close()
if icon := a.Icon(); icon != nil {
_, err = file.Write(icon.Content())
if err != nil {
fyne.LogError("Unable to write icon contents", err)
return err
}
}
return nil
}