Files
u-desktop/config.go
绝尘 9fd3acede3 新增: 星座运势+AI资讯+知识卡片+桌面设置窗口+秒显示开关
- 星座运势: 天聚数行API集成,5维进度条+幸运标签+今日概述
- AI资讯: 天聚数行API,图文布局5条展示,文件缓存2小时刷新
- 知识卡片: AI生成,关键字+提示词配置,30分钟刷新
- 桌面设置: 独立WebView2窗口,760x1350,含壁纸/布局/城市/颜色等配置
- 显示控制: 壁纸/时间/天气/星座/知识/AI资讯独立开关,秒显示开关
- 文件缓存: 星座运势+AI资讯缓存到本地,启动即显示上次数据
- initDone防抖: 防止设置窗口初始化触发卡片重载
2026-05-26 04:34:00 +08:00

144 lines
3.4 KiB
Go

package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"image"
"image/color"
"image/png"
"os"
"path/filepath"
)
type WallpaperType string
const (
WPTheme WallpaperType = "theme"
WPImage WallpaperType = "image"
WPBing WallpaperType = "bing"
WPColor WallpaperType = "color"
)
type Layout string
const (
LayoutSingle Layout = "single"
LayoutMulti Layout = "multi"
)
type ThemeName string
const (
ThemeAurora ThemeName = "aurora"
ThemeStar ThemeName = "starfield"
ThemeGradient ThemeName = "gradient"
ThemeParticle ThemeName = "particles"
ThemeFractal ThemeName = "fractal"
ThemeText ThemeName = "text"
)
type SavedColor struct {
Color1 string `json:"color1"`
Color2 string `json:"color2"`
Gradient bool `json:"gradient"`
}
type Config struct {
Zodiac string `json:"zodiac"`
City string `json:"city"`
WallpaperType WallpaperType `json:"wallpaperType"`
Theme ThemeName `json:"theme"`
ImagePath string `json:"imagePath"`
Color1 string `json:"color1"`
Color2 string `json:"color2"`
ColorGradient bool `json:"colorGradient"`
WallpaperText string `json:"wallpaperText"`
Layout Layout `json:"layout"`
HideWallpaper bool `json:"hideWallpaper"`
HideTime bool `json:"hideTime"`
HideWeather bool `json:"hideWeather"`
HideZodiac bool `json:"hideZodiac"`
HideAINews bool `json:"hideAINews"`
ShowSeconds bool `json:"showSeconds"`
KnowledgeKeyword string `json:"knowledgeKeyword"`
KnowledgePrompt string `json:"knowledgePrompt"`
HideKnowledge bool `json:"hideKnowledge"`
SavedColors []SavedColor `json:"savedColors"`
BingAutoRefresh bool `json:"bingAutoRefresh"`
}
const defaultZodiac = "射手座"
var configPath string
func configDir() string {
return filepath.Dir(configPath)
}
func loadConfig() *Config {
data, err := os.ReadFile(configPath)
if err != nil {
return defaultConfig()
}
var cfg Config
if json.Unmarshal(data, &cfg) != nil {
return defaultConfig()
}
if cfg.Zodiac == "" {
cfg.Zodiac = defaultZodiac
}
if cfg.WallpaperType == "" {
cfg.WallpaperType = WPTheme
}
if cfg.Theme == "" {
cfg.Theme = ThemeAurora
}
if cfg.Color1 == "" {
cfg.Color1 = "#1a1a2e"
}
if cfg.Layout == "" {
cfg.Layout = LayoutSingle
}
return &cfg
}
func defaultConfig() *Config {
return &Config{
Zodiac: defaultZodiac,
WallpaperType: WPTheme,
Theme: ThemeAurora,
Color1: "#1a1a2e",
Color2: "#16213e",
}
}
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
}