-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathicon_windows.go
More file actions
47 lines (38 loc) · 1.28 KB
/
icon_windows.go
File metadata and controls
47 lines (38 loc) · 1.28 KB
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
//go:build windows
package main
import (
"encoding/binary"
"os"
"path/filepath"
)
func setAppIcon() {
// Save .ico to config directory for use in shortcuts
iconPath := filepath.Join(configDir(), "icon.ico")
if _, err := os.Stat(iconPath); err == nil {
return // already exists
}
png := getAppIconPNG()
ico := pngToICO(png)
os.MkdirAll(configDir(), 0755)
os.WriteFile(iconPath, ico, 0644)
}
// pngToICO wraps a PNG image in the ICO container format (Windows Vista+).
func pngToICO(png []byte) []byte {
const headerSize = 6 + 16 // ICONDIR + 1 ICONDIRENTRY
ico := make([]byte, headerSize+len(png))
// ICONDIR
binary.LittleEndian.PutUint16(ico[0:], 0) // reserved
binary.LittleEndian.PutUint16(ico[2:], 1) // type: ICO
binary.LittleEndian.PutUint16(ico[4:], 1) // count: 1 image
// ICONDIRENTRY
ico[6] = 0 // width: 0 means 256
ico[7] = 0 // height: 0 means 256
ico[8] = 0 // color count
ico[9] = 0 // reserved
binary.LittleEndian.PutUint16(ico[10:], 1) // color planes
binary.LittleEndian.PutUint16(ico[12:], 32) // bits per pixel
binary.LittleEndian.PutUint32(ico[14:], uint32(len(png))) // image data size
binary.LittleEndian.PutUint32(ico[18:], uint32(headerSize)) // offset to data
copy(ico[headerSize:], png)
return ico
}