Skip to main content

gpui_material_foundation/root/
base_projection.rs

1use gpui::{App, px};
2use gpui_base::{ResizableTheme, ScrollbarStyles, ScrollbarTheme, Theme, ThemeAppearance};
3use gpui_material_theme::{
4    Appearance, ColorRole, CornerRole, MaterialTheme, TypeRole, TypographyEmphasis,
5};
6
7use crate::{color, font_for_style};
8
9/// Projects a [`MaterialTheme`] into a matching `gpui_base::Theme` without
10/// mutating the application-global Base theme.
11///
12/// Use this (or [`crate::current_base_theme`]) when styling standalone Base
13/// controls inside a window override or [`crate::ThemeScope`].
14pub fn project_base_theme(theme: &MaterialTheme, cx: &App) -> Theme {
15    let mut projected = Theme::global(cx);
16    project_base_colors(&mut projected, theme);
17    project_base_metrics(&mut projected, theme, cx);
18    projected
19}
20
21pub(super) fn project_base_colors(projected: &mut Theme, theme: &MaterialTheme) {
22    use ColorRole::*;
23
24    projected.appearance = match theme.colors.appearance() {
25        Appearance::Light => ThemeAppearance::Light,
26        Appearance::Dark => ThemeAppearance::Dark,
27    };
28    let role = |role| color(theme.colors.color(role));
29    let colors = &mut projected.tokens.colors;
30    colors.background = role(Surface);
31    colors.foreground = role(OnSurface);
32    colors.surface = role(SurfaceContainer);
33    colors.surface_foreground = role(OnSurface);
34    colors.primary = role(Primary);
35    colors.primary_foreground = role(OnPrimary);
36    colors.secondary = role(Secondary);
37    colors.secondary_foreground = role(OnSecondary);
38    colors.muted = role(SurfaceContainerHighest);
39    colors.muted_foreground = role(OnSurfaceVariant);
40    colors.accent = role(SecondaryContainer);
41    colors.accent_foreground = role(OnSecondaryContainer);
42    colors.destructive = role(Error);
43    colors.destructive_foreground = role(OnError);
44    colors.border = role(OutlineVariant);
45    colors.input = role(Outline);
46    colors.ring = role(Secondary);
47    colors.selection = role(Primary).opacity(0.3);
48    projected.tokens.shadow = gpui_base::ShadowTokens::elevations(role(Shadow).opacity(0.22));
49    let thumb = role(OnSurfaceVariant);
50    let styles = ScrollbarStyles::default()
51        .track(|track| track.bg(gpui::transparent_black()).width(px(8.)))
52        .thumb(|t| {
53            t.bg(thumb.opacity(0.38))
54                .width(px(4.))
55                .radius(px(2.))
56                .min_length(px(24.))
57        })
58        .thumb_hover(|t| t.bg(thumb.opacity(0.56)))
59        .thumb_active(|t| t.bg(thumb.opacity(0.72)));
60    projected.scrollbar = ScrollbarTheme::new()
61        .with_mode(projected.scrollbar.mode())
62        .with_motion(projected.scrollbar.motion())
63        .with_styles(styles);
64    let mut resizable = ResizableTheme::default();
65    resizable.handle = Some(role(OutlineVariant));
66    resizable.active_handle = Some(role(Primary));
67    projected.resizable = resizable;
68}
69
70pub(super) fn project_base_metrics(projected: &mut Theme, theme: &MaterialTheme, cx: &App) {
71    let typography = &theme.typography;
72    let radii = &mut projected.tokens.radius;
73    let radius = |role: CornerRole| px(role.shape().radii(100., 100., Default::default())[0]);
74    radii.sm = radius(CornerRole::ExtraSmall);
75    radii.md = radius(CornerRole::Small);
76    radii.lg = radius(CornerRole::Medium);
77    radii.xl = radius(CornerRole::Large);
78    let text = &mut projected.tokens.typography;
79    text.sans = font_for_style(
80        typography,
81        TypeRole::BodyMedium.style(TypographyEmphasis::Baseline),
82        cx,
83    )
84    .family;
85    for (slot, role) in [
86        (&mut text.xs, TypeRole::BodySmall),
87        (&mut text.sm, TypeRole::BodyMedium),
88        (&mut text.md, TypeRole::BodyLarge),
89        (&mut text.lg, TypeRole::TitleMedium),
90        (&mut text.xl, TypeRole::TitleLarge),
91    ] {
92        let style = typography.style(role, TypographyEmphasis::Baseline);
93        slot.size = px(style.size);
94        slot.line_height = px(style.line_height);
95        slot.weight = gpui::FontWeight(f32::from(style.weight));
96    }
97}