系统外观
from uda import Uda, Theme
with Uda() as uda: theme = uda.theme # 'dark' | 'light' | 'unknown'
if theme == Theme.DARK: print("深色模式") elif theme == Theme.LIGHT: print("浅色模式") else: print("无法判定 —— 交给用户选择")Node.js 侧:
const { Uda } = require('./uda');console.log(new Uda().theme);Rust 侧:
let mode = uda_platform_linux::LinuxAppearanceManager::new().detect_theme()?;'unknown' 是合法答案:没有配色方案概念的合成器(裸窗口管理器、部分 Wayland 平铺器)确实无法判定,猜测比不猜更糟。请按「无法判定」处理,让用户自行选择。
| 平台 | 后端 |
|---|---|
| Linux(现代) | XDG Desktop Portal org.freedesktop.portal.Settings → Read("org.freedesktop.appearance", "color-scheme") |
| Linux(GNOME) | gsettings get org.gnome.desktop.interface color-scheme |
| Linux(KDE) | kreadconfig6 / kreadconfig5 读取 Colors:Scheme |
| Linux(XFCE) | xfconf-query -c xsettings -p /Net/ThemeName |
| Windows | 注册表 HKCU\...\Themes\Personalize\AppsUseLightTheme |
遵循 AGENTS.md Principle 2 的级联链:Portal → 原生桌面环境 IPC → CLI 工具 → 类型化错误。
with Uda() as uda: accent = uda.accent_color # (r, g, b, a) 或 None
if accent: r, g, b, a = accent print(f"强调色: #{r:02X}{g:02X}{b:02X}")平台支持情况
Section titled “平台支持情况”| 平台 | 状态 | 来源 |
|---|---|---|
| Windows | ✅ | 注册表 HKCU\...\DWM\AccentColor,解包为 RGBA |
| GNOME | ✅ | gsettings get org.gnome.desktop.interface accent-color |
| KDE / XFCE | ⚠️ | 尽力而为,缺失时返回 None |
| Wayland 平铺 WM | ❌ | 无系统级强调色概念 |
跟随系统切换
Section titled “跟随系统切换”UDA 不提供变更监听,读取为一次性快照。需要跟随用户选择时,请以合理间隔轮询 detect_theme:
import timefrom uda import Uda
with Uda() as uda: last = None while True: theme = uda.theme if theme != last: apply_theme(theme) last = theme time.sleep(1.0)若所用 UI 框架本身能处理主题变更,优先使用它的信号——框架的事件既更及时也比轮询更准确。
| 桌面 | 取色来源 | 说明 |
|---|---|---|
| GNOME 42+ | org.freedesktop.portal.Settings |
现代标准答案 |
| KDE Plasma 5 / 6 | org.freedesktop.portal.Settings → kreadconfig6 / kreadconfig5 读 Colors:Scheme |
完整支持 |
| XFCE | xfconf-query -c xsettings -p /Net/ThemeName |
无系统级配色方案,常返回 unknown |
| Hyprland / Sway | GSettings | 平铺合成器很少发布配色方案 |
| Windows 10 / 11 | 注册表 AppsUseLightTheme |
完整支持 |
强调色是与配色方案独立的另一项查询,支持情况不同:
| 平台 | 强调色 |
|---|---|
| Windows | ✅ 注册表 HKCU\...\DWM\AccentColor,解包为 RGBA |
| GNOME | ✅ gsettings get org.gnome.desktop.interface accent-color |
| KDE / XFCE | 尽力而为,缺失时返回 None |
| Wayland 平铺器 | ❌ 无系统级强调色概念 |
接到应用主题上
Section titled “接到应用主题上”from uda import Uda, Theme
FALLBACK_ACCENT = (0x33, 0x99, 0xFF, 0xFF)
with Uda() as uda: dark = uda.theme == Theme.DARK accent = uda.accent_color or FALLBACK_ACCENT
apply_theme(dark=dark, accent=accent)