use super::*;
pub(crate) fn themes_role_colors(palette: &ThemePalette, idx: usize) -> (Color, Color) {
match idx {
1 => (palette.get_card(), palette.get_card_foreground()),
2 => (palette.get_primary(), palette.get_primary_foreground()),
3 => (palette.get_secondary(), palette.get_secondary_foreground()),
4 => (palette.get_muted(), palette.get_foreground()),
5 => (palette.get_accent(), palette.get_foreground()),
6 => (
palette.get_destructive(),
palette.get_destructive_foreground(),
),
7 => (palette.get_success(), palette.get_success_foreground()),
8 => (palette.get_warning(), palette.get_warning_foreground()),
_ => (palette.get_background(), palette.get_foreground()),
}
}
pub(crate) fn themes_radius_value(geometry: &ThemeGeometry, idx: usize) -> f32 {
match idx {
0 => geometry.get_radius_sm(),
1 => geometry.get_radius_md(),
2 => geometry.get_radius_lg(),
3 => geometry.get_radius_xl(),
_ => THEMES_PILL_RADIUS,
}
}
pub(crate) fn mount_theme_swatch(idx: usize, tokens: ThemeTokens) -> NodeId {
let label: &'static str = THEMES_ROLE_LABELS[idx];
let swatch_style = move || -> Style {
let live: ThemeTokens = use_live_tokens(tokens);
let (bg, _): (Color, Color) = themes_role_colors(&live.get_palette(), idx);
style! {
FlexDirection::Column
Width(Length::Percent(100.0))
Height(Length::Px(THEMES_SWATCH_HEIGHT))
Padding(Edges::symmetric(0.0, SPACE_MD))
Gap(SPACE_XS)
JustifyContent::Center
Overflow::Hidden
Background(bg)
Border::new(live.get_geometry().get_border_width(), live.get_palette().get_border())
Radius(live.get_geometry().get_radius_lg())
}
};
let name_style = move || -> Style {
let live: ThemeTokens = use_live_tokens(tokens);
let (_, fg): (Color, Color) = themes_role_colors(&live.get_palette(), idx);
let mut computed_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(live.get_geometry().get_font_body())
FontWeight::SemiBold
TextColor(fg)
};
computed_style.get_mut_text().set_line_height(1.25);
computed_style
};
let hex_style = move || -> Style {
let live: ThemeTokens = use_live_tokens(tokens);
let (_, fg): (Color, Color) = themes_role_colors(&live.get_palette(), idx);
let mut element_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(live.get_geometry().get_font_body() - 1.5)
FontFamily::Monospace
TextColor(fg)
};
element_style.get_mut_text().set_line_height(1.25);
element_style
};
let hex_text = move || -> String {
let live: ThemeTokens = use_live_tokens(tokens);
let (bg, _): (Color, Color) = themes_role_colors(&live.get_palette(), idx);
format!(
"#{:02X}{:02X}{:02X}",
bg.get_red(),
bg.get_green(),
bg.get_blue()
)
};
view! {
view {
bind_style: swatch_style
text {
bind_style: name_style
{ label }
}
text {
bind_style: hex_style
{ hex_text }
}
}
}
}
pub(crate) fn mount_radius_tile(idx: usize, tokens: ThemeTokens) -> NodeId {
let palette: ThemePalette = tokens.get_palette();
let caption: String = THEMES_RADIUS_LABELS[idx].to_string();
let tile_style = move || -> Style {
let live: ThemeTokens = use_live_tokens(tokens);
let live_p: ThemePalette = live.get_palette();
style! {
Width(Length::Px(THEMES_RADIUS_TILE))
Height(Length::Px(THEMES_RADIUS_TILE))
Background(live_p.get_primary().with_alpha(36))
Border::new(2.0, live_p.get_primary())
Radius(themes_radius_value(&live.get_geometry(), idx))
}
};
let column_tertiary_style: Style = style! {
AlignItems::Center
Gap(SPACE_XS)
};
let text_fifth_style: Style = style! {
FontSize(12.0)
FontFamily::Monospace
TextColor(palette.get_muted_foreground())
};
view! {
column {
style: column_tertiary_style
view {
bind_style: tile_style
}
text {
style: text_fifth_style
{ caption }
}
}
}
}
pub(crate) fn mount_token_row(
label: String,
value: f32,
sample: NodeId,
tokens: ThemeTokens,
) -> NodeId {
let palette: ThemePalette = tokens.get_palette();
let value_txt: String = format!("{value:.0}px");
let row_tertiary_style: Style = style! {
Width(Length::Percent(100.0))
AlignItems::Center
Gap(SPACE_MD)
};
let text_tertiary_style: Style = style! {
Width(Length::Px(96.0))
FlexShrink(0.0)
FontSize(12.5)
FontFamily::Monospace
TextColor(palette.get_muted_foreground())
};
let row_quaternary_style: Style = style! {
FlexGrow(1.0)
FlexShrink(1.0)
MinWidth(Length::Px(0.0))
AlignItems::Center
Overflow::Hidden
};
let text_quaternary_style: Style = style! {
FlexShrink(0.0)
FontSize(12.5)
FontFamily::Monospace
TextColor(palette.get_foreground())
};
view! {
row {
style: row_tertiary_style
text {
style: text_tertiary_style
{ label }
}
row {
style: row_quaternary_style
{ sample }
}
text {
style: text_quaternary_style
{ value_txt }
}
}
}
}
pub(crate) fn mount_scale_bar(width: f32, height: f32, tokens: ThemeTokens) -> NodeId {
let bar_style = move || -> Style {
let live: ThemeTokens = use_live_tokens(tokens);
style! {
Width(Length::Px(width))
Height(Length::Px(height))
Background(live.get_palette().get_primary().with_alpha(64))
Border::new(1.0, live.get_palette().get_primary())
Radius(live.get_geometry().get_radius_sm())
}
};
view! {
view {
bind_style: bar_style
}
}
}
pub fn mount_themes_page(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
let palette: ThemePalette = tokens.get_palette();
let geometry: ThemeGeometry = tokens.get_geometry();
let scheme_sig: Signal<DesignScheme> = state.get_theme().get_scheme();
let mode_sig: Signal<ThemeMode> = state.get_theme().get_mode();
let state_for_cycle: ShowcaseState = state.clone();
let state_for_mode: ShowcaseState = state.clone();
let active_text = move || -> String {
format!(
"{PREFIX_ACTIVE_SCHEME}{} ({:?})",
scheme_sig.get().as_str(),
mode_sig.get()
)
};
let on_next_scheme = move |_: &UiEvent| {
state_for_cycle.next_scheme();
};
let on_toggle_mode = move |_: &UiEvent| {
state_for_mode.toggle_theme_mode();
};
let scheme_buttons: Vec<NodeId> = DesignScheme::all()
.iter()
.copied()
.map(|scheme: DesignScheme| {
let on_pick = move |_: &UiEvent| {
let _: bool = scheme_sig.set_if_changed(scheme);
};
let btn_style = move || -> Style {
let live: ThemeTokens = use_live_tokens(tokens);
let live_p: ThemePalette = live.get_palette();
let live_g: ThemeGeometry = live.get_geometry();
let active: bool = scheme_sig.get() == scheme;
let bg: Color = if active {
live_p.get_primary()
} else {
live_p.get_background()
};
let border: Color = if active {
live_p.get_primary()
} else {
live_p.get_border()
};
style! {
FlexDirection::Row
Width(Length::Percent(100.0))
Height(Length::Px(live_g.get_control_sm()))
Padding(Edges::symmetric(0.0, SPACE_SM))
AlignItems::Center
Gap(SPACE_SM)
Overflow::Hidden
Background(bg)
Border::new(live_g.get_border_width(), border)
Radius(live_g.get_radius_md())
CursorIcon::Pointer
}
};
let dot_style = move || -> Style {
let mode: ThemeMode = mode_sig.get();
let preview: ThemePalette = ThemeTokens::resolve(scheme, mode).get_palette();
let live_p: ThemePalette = use_live_tokens(tokens).get_palette();
style! {
Width(Length::Px(10.0))
Height(Length::Px(10.0))
FlexShrink(0.0)
Background(preview.get_primary())
Border::new(1.0, live_p.get_border())
Radius(THEMES_PILL_RADIUS)
}
};
let lbl_style = move || -> Style {
let live: ThemeTokens = use_live_tokens(tokens);
let live_p: ThemePalette = live.get_palette();
let active: bool = scheme_sig.get() == scheme;
let fg: Color = if active {
live_p.get_primary_foreground()
} else {
live_p.get_foreground()
};
style! {
FlexGrow(1.0)
FlexShrink(1.0)
MinWidth(Length::Px(0.0))
FontSize(12.5)
FontWeight::Medium
TextColor(fg)
Overflow::Hidden
TextOverflow::Ellipsis
}
};
view! {
row {
bind_style: btn_style
role: A11yRole::Button
on_click: on_pick
view {
bind_style: dot_style
}
text {
bind_style: lbl_style
{ scheme.as_str().to_string() }
}
}
}
})
.collect();
let swatches: Vec<NodeId> = (0..THEMES_ROLE_LABELS.len())
.map(|idx: usize| mount_theme_swatch(idx, tokens))
.collect();
let palette_grid: NodeId = mount_responsive_grid(swatches, 3);
let radius_tiles: Vec<NodeId> = (0..THEMES_RADIUS_LABELS.len())
.map(|idx: usize| mount_radius_tile(idx, tokens))
.collect();
let control_values: [f32; 3] = [
geometry.get_control_sm(),
geometry.get_control_md(),
geometry.get_control_lg(),
];
let control_rows: Vec<NodeId> = control_values
.iter()
.enumerate()
.map(|(idx, value): (usize, &f32)| {
let bar: NodeId = mount_scale_bar(*value * 3.0, *value, tokens);
mount_token_row(THEMES_CONTROL_LABELS[idx].to_string(), *value, bar, tokens)
})
.collect();
let type_values: [f32; 3] = [
geometry.get_font_body(),
geometry.get_font_heading(),
geometry.get_font_title(),
];
let type_rows: Vec<NodeId> = type_values
.iter()
.enumerate()
.map(|(idx, value): (usize, &f32)| {
let size: f32 = *value;
let text_secondary_style: Style = style! {
FontSize(size)
FontWeight::SemiBold
TextColor(palette.get_foreground())
Overflow::Hidden
TextOverflow::Ellipsis
};
let sample: NodeId = view! {
text {
style: text_secondary_style
{ THEMES_TYPE_SAMPLE }
}
};
mount_token_row(THEMES_TYPE_LABELS[idx].to_string(), size, sample, tokens)
})
.collect();
let space_rows: Vec<NodeId> = THEMES_SPACE_VALUES
.iter()
.enumerate()
.map(|(idx, value): (usize, &f32)| {
let bar: NodeId = mount_scale_bar(*value * 4.0, SPACE_MD, tokens);
mount_token_row(THEMES_SPACE_LABELS[idx].to_string(), *value, bar, tokens)
})
.collect();
let preview_query: Signal<String> = Signal::new(String::new());
let preview_switch: Signal<bool> = Signal::new(true);
let text_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(14.0)
FontWeight::SemiBold
TextColor(palette.get_foreground())
};
let grid_style: Style = style! {
Width(Length::Percent(100.0))
GridColumns(2)
Gap(SPACE_SM)
};
let selector_card: NodeId = view! {
EuvCard {
title: THEMES_CARD_TITLE.to_string()
tokens: tokens
subtitle: THEMES_CARD_SUB
text {
style: text_style
{ active_text }
}
EuvActionRow {
EuvButton {
label: BTN_NEXT_SCHEME.to_string()
tokens: tokens
small()
on_click: on_next_scheme
}
EuvButton {
label: BTN_TOGGLE_MODE.to_string()
tokens: tokens
outline()
small()
on_click: on_toggle_mode
}
}
grid {
style: grid_style
..scheme_buttons
}
}
};
let row_secondary_style: Style = style! {
Width(Length::Percent(100.0))
FlexWrap::Wrap
AlignItems::Center
Gap(SPACE_SM)
};
let preview_card: NodeId = view! {
EuvCard {
title: THEMES_PREVIEW_TITLE.to_string()
tokens: tokens
subtitle: THEMES_PREVIEW_SUB
EuvInput {
value: preview_query
placeholder: THEMES_PREVIEW_INPUT.to_string()
tokens: tokens
}
EuvActionRow {
EuvButton {
label: THEMES_PREVIEW_PRIMARY.to_string()
tokens: tokens
small()
}
EuvButton {
label: THEMES_PREVIEW_SECONDARY.to_string()
tokens: tokens
secondary()
small()
}
EuvButton {
label: THEMES_PREVIEW_OUTLINE.to_string()
tokens: tokens
outline()
small()
}
}
row {
style: row_secondary_style
EuvBadge {
label: THEMES_BADGE_STABLE.to_string()
tone: EuvTone::Success
tokens: tokens
}
EuvBadge {
label: THEMES_BADGE_BETA.to_string()
tone: EuvTone::Warning
tokens: tokens
}
EuvBadge {
label: THEMES_BADGE_NEW.to_string()
tone: EuvTone::Primary
tokens: tokens
}
}
EuvSwitch {
checked: preview_switch
label: THEMES_PREVIEW_SWITCH.to_string()
tokens: tokens
}
}
};
let column_secondary_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_MD)
};
let rail: NodeId = view! {
column {
style: column_secondary_style
{ selector_card }
{ preview_card }
}
};
let scale_cards: Vec<NodeId> = vec![
{
let row_style: Style = style! {
Width(Length::Percent(100.0))
FlexWrap::Wrap
Gap(SPACE_LG)
};
view! {
EuvCard {
title: THEMES_RADIUS_TITLE.to_string()
tokens: tokens
subtitle: THEMES_RADIUS_SUB
row {
style: row_style
..radius_tiles
}
}
}
},
view! {
EuvCard {
title: THEMES_CONTROL_TITLE.to_string()
tokens: tokens
subtitle: THEMES_CONTROL_SUB
..control_rows
}
},
view! {
EuvCard {
title: THEMES_TYPE_TITLE.to_string()
tokens: tokens
subtitle: THEMES_TYPE_SUB
..type_rows
}
},
view! {
EuvCard {
title: THEMES_SPACE_TITLE.to_string()
tokens: tokens
subtitle: THEMES_SPACE_SUB
..space_rows
}
},
];
let scale_grid: NodeId = mount_responsive_grid(scale_cards, 2);
let column_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_MD)
};
let main: NodeId = view! {
column {
style: column_style
EuvCard {
title: THEMES_PALETTE_TITLE.to_string()
tokens: tokens
subtitle: THEMES_PALETTE_SUB
{ palette_grid }
}
{ scale_grid }
}
};
let studio: NodeId = mount_studio_layout(main, rail);
view! {
EuvPage {
title: THEMES_TITLE.to_string()
subtitle: THEMES_SUBTITLE.to_string()
tokens: tokens
{ studio }
}
}
}