Skip to content

主题

所有组件都支持内置主题系统。ActiveTheme trait 用于访问当前主题中的颜色值:

rs
use gpui_component::{ActiveTheme as _};

// Access theme colors in your components
cx.theme().primary
cx.theme().background
cx.theme().foreground

因此,如果你希望组件使用当前主题的颜色,组件或视图就需要运行在带有 App 上下文的环境中。

Theme Registry

仓库在 themes 目录下内置了 20+ 主题。

你可以通过 ThemeRegistry 来加载和监听这些主题文件:

rs
use std::path::PathBuf;
use gpui::{App, SharedString};
use gpui_component::{Theme, ThemeRegistry};

pub fn init(cx: &mut App) {
    let theme_name = SharedString::from("Ayu Light");
    // Load and watch themes from ./themes directory
    if let Err(err) = ThemeRegistry::watch_dir(PathBuf::from("./themes"), cx, move |cx| {
        if let Some(theme) = ThemeRegistry::global(cx)
            .themes()
            .get(&theme_name)
            .cloned()
        {
            Theme::global_mut(cx).apply_config(&theme);
        }
    }) {
        tracing::error!("Failed to watch themes directory: {}", err);
    }
}