- main.go: 入口+单实例互斥锁 - win32.go: Win32 API/embed/evalJS/全局变量 - config.go: 配置读写+图标生成 - weather.go: 天气API/定位/天气循环 - systray.go: 托盘菜单/WebView/全屏监控 - wallpaper.html → web/wallpaper.html
65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"image"
|
|
"image/color"
|
|
"image/png"
|
|
"os"
|
|
)
|
|
|
|
type Config struct {
|
|
Zodiac string `json:"zodiac"`
|
|
City string `json:"city"`
|
|
}
|
|
|
|
const defaultZodiac = "射手座"
|
|
|
|
var configPath string
|
|
|
|
func loadConfig() *Config {
|
|
data, err := os.ReadFile(configPath)
|
|
if err != nil {
|
|
return &Config{Zodiac: defaultZodiac}
|
|
}
|
|
var cfg Config
|
|
if json.Unmarshal(data, &cfg) != nil {
|
|
return &Config{Zodiac: defaultZodiac}
|
|
}
|
|
if cfg.Zodiac == "" {
|
|
cfg.Zodiac = defaultZodiac
|
|
}
|
|
return &cfg
|
|
}
|
|
|
|
func saveConfig(cfg *Config) error {
|
|
data, _ := json.MarshalIndent(cfg, "", " ")
|
|
return os.WriteFile(configPath, data, 0644)
|
|
}
|
|
|
|
func generateIcon() []byte {
|
|
img := image.NewRGBA(image.Rect(0, 0, 16, 16))
|
|
c := color.RGBA{R: 88, G: 101, B: 242, A: 255}
|
|
for y := 0; y < 16; y++ {
|
|
for x := 0; x < 16; x++ {
|
|
img.Set(x, y, c)
|
|
}
|
|
}
|
|
var buf bytes.Buffer
|
|
png.Encode(&buf, img)
|
|
pngData := buf.Bytes()
|
|
ico := make([]byte, 22+len(pngData))
|
|
binary.LittleEndian.PutUint16(ico[0:], 0)
|
|
binary.LittleEndian.PutUint16(ico[2:], 1)
|
|
binary.LittleEndian.PutUint16(ico[4:], 1)
|
|
ico[6], ico[7], ico[8], ico[9] = 16, 16, 0, 0
|
|
binary.LittleEndian.PutUint16(ico[10:], 1)
|
|
binary.LittleEndian.PutUint16(ico[12:], 32)
|
|
binary.LittleEndian.PutUint32(ico[14:], uint32(len(pngData)))
|
|
binary.LittleEndian.PutUint32(ico[18:], 22)
|
|
copy(ico[22:], pngData)
|
|
return ico
|
|
}
|