Files
u-desktop/README.md

122 lines
5.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# u-desktop
Windows 桌面壁纸增强工具 — 将 WebView2 嵌入桌面壁纸层在壁纸上叠加时间、天气、星座运势、AI 资讯等信息卡片。
## 功能
| 模块 | 说明 |
|------|------|
| 壁纸 | 6 种主题动画(极光/星空/渐变/粒子/极光流体/文字、本地图片、Bing 每日壁纸、纯色/渐变 |
| 时间 | 大字时钟,支持秒显示开关,整点光晕动画,节日倒计时 |
| 天气 | 和风天气 API当前天气 + 24h/7d 预报IP 自动定位 + 手动选城 |
| 星座运势 | 天聚数行 API5 维指数进度条 + 幸运标签 + 今日概述 |
| AI 资讯 | 天聚数行 API图文布局 5 条展示 |
| 知识卡片 | AI 生成,自定义关键字 + 提示词 |
| 桌面设置 | 独立 WebView2 窗口,所有配置可视化操作 |
## 项目结构
```
u-desktop/
├── main.go # 入口: 单实例互斥锁 + 配置目录 + 托盘启动
├── win32.go # Win32 API 声明 (user32/kernel32/ole32)
├── systray.go # 系统托盘 + WebView2 壁纸嵌入 + 消息循环
├── wallpaper.go # 壁纸 HTML 构建 + 主题注入 + 重载
├── config.go # 配置结构体 + JSON 持久化
├── settings.go # 设置窗口 (独立 WebView2)
├── weather.go # 天气 API + IP 定位 + 城市列表
├── horoscope.go # 星座运势 API + 文件缓存
├── ainews.go # AI 资讯 API + 文件缓存
├── knowledge.go # 知识卡片 AI 生成
├── bing.go # Bing 壁纸下载 + 历史导航 + 收藏
├── dialog.go # Win32 对话框 (文件选择/颜色选择)
├── web/
│ ├── overlay.html # 桌面覆盖层 (时间/天气/星座/资讯/知识)
│ ├── settings.html # 设置窗口 UI
│ └── themes/ # 壁纸主题 HTML
│ ├── aurora.html
│ ├── starfield.html
│ ├── gradient.html
│ ├── particles.html
│ ├── fractal.html
│ └── text.html
├── config/ # 运行时配置 (settings.json + 缓存)
└── docs/
└── wallpaper-embedding.md # 壁纸嵌入技术笔记
```
## 架构
```
┌──────────────┐ ┌─────────────────┐
│ systray.go │────▶│ WebView2 主窗口 │ SetParent → WorkerW (壁纸层)
│ (托盘+消息循环) │ │ overlay.html │
└──────┬───────┘ └─────────────────┘
│ evalJS / SetHtml
┌──────────────┐ ┌─────────────────┐
│ settings.go │────▶│ WebView2 设置窗口 │ 独立窗口, 760x1350
│ │ │ settings.html │
└──────────────┘ └─────────────────┘
```
**核心通信机制**:
- `evalJS(cmd)` — 通过 channel + PostMessage 向壁纸 WebView 注入 JS
- `htmlQueue` — 通过 SetHtml 替换整个壁纸 HTML触发完整重载
- WebView2 `Bind` — 设置窗口通过 Go 绑定函数读写配置
**数据刷新策略**:
- 启动时推送文件缓存(即时显示),后台拉取新数据后替换
- 星座: 24h / AI 资讯: 2h / 知识卡片: 30min / 天气: 10min / Bing: 4h
## 配置
配置文件: `config/settings.json`(与 exe 同级目录)
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| layout | string | "single" | 布局: single(合并卡片) / multi(独立卡片) |
| wallpaperType | string | "theme" | 壁纸类型: theme/image/bing/color |
| theme | string | "aurora" | 主题动画 |
| zodiac | string | "射手座" | 星座 |
| city | string | "" | 天气城市 ID |
| showSeconds | bool | false | 显示秒 |
| hideWallpaper | bool | false | 隐藏壁纸 |
| hideTime | bool | false | 隐藏时间 |
| hideWeather | bool | false | 隐藏天气 |
| hideZodiac | bool | false | 隐藏星座 |
| hideAINews | bool | false | 隐藏 AI 资讯 |
| hideKnowledge | bool | false | 隐藏知识卡片 |
| bingAutoRefresh | bool | false | Bing 每小时自动切换 |
| knowledgeKeyword | string | "" | 知识卡片关键字 |
| knowledgePrompt | string | "" | 知识卡片提示词 |
## 构建
```bash
go build -o u-desktop.exe .
```
需要 Windows 10+ 和 WebView2 RuntimeWin11 已内置)。
## 依赖
```
github.com/jchv/go-webview2 # WebView2 绑定
github.com/getlantern/systray # 系统托盘
golang.org/x/sys/windows # Win32 API
github.com/anthropics/anthropic-sdk-go # Claude API (知识卡片)
```
## 开机自启
注册表 `HKCU\Software\Microsoft\Windows\CurrentVersion\Run\UDesktopWallpaper`
## 技术要点
- WebView2 嵌入桌面壁纸层通过 `SetParent` 到 WorkerW 实现(详见 [docs/wallpaper-embedding.md](docs/wallpaper-embedding.md)
- 自定义 Win32 消息循环替代 `wv.Run()`SetParent 后 Run() 消息路由异常)
- 全屏应用检测:定时检查前台窗口是否全屏,自动暂停/恢复壁纸渲染
- 配置变更即时生效:设置窗口通过 evalJS 直接操作壁纸层 DOM无需重启