euv-ui 0.49.4

Cross-platform UI component library and 17-scheme design system for the euv framework with pixel-consistent rendering across Web, Android, iOS, iPadOS, macOS, Windows, and Linux.
Documentation
use super::*;

/// Child composition helpers for `EuvCard`.
impl EuvCard {
    /// Sets the card subtitle text fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source card.
    /// - `impl Into<String>` - Muted subtitle string.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated card.
    #[must_use]
    pub fn subtitle(mut self, subtitle: impl Into<String>) -> Self {
        self.set_subtitle(subtitle.into());
        self
    }

    /// Appends a child `NodeId` to the card body.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source card.
    /// - `NodeId` - Mounted child node.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated card.
    #[must_use]
    pub fn child(mut self, child: NodeId) -> Self {
        self.get_mut_children().push(child);
        self
    }
}

/// Declarative `#[component]` implementation for `EuvCard`.
#[component]
/// Inherent implementation of [`EuvCard`].
impl EuvCard {
    /// Renders `EuvCard` into the ambient framework-managed `Context`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Component descriptor reference.
    ///
    /// # Returns
    ///
    /// - `NodeId` - Mounted root scene node handle.
    fn render(&self) -> NodeId {
        let ctx: Context = current_context();
        let tokens: ThemeTokens = live_tokens_untracked(self.get_tokens());
        let theme_palette: ThemePalette = tokens.get_palette();
        let geometry: ThemeGeometry = tokens.get_geometry();
        let card_surface: SurfaceColors = theme_palette.surface(SurfaceRole::Card);
        let Self {
            title,
            subtitle,
            children,
            style,
            ..
        } = self;
        let mut root_st: Style = tokens.card_style();
        let _: &mut LayoutStyle = root_st.get_mut_layout().set_width(Length::Percent(100.0));
        root_st = root_st.with_overrides(*style);
        ctx.get_scene().with_write(
            |tree: &mut SceneTree| {
                let root: NodeId = tree.create_view(root_st);
                let mut head_nodes: [NodeId; 2] = [NodeId::default(); 2];
                let mut head_len: usize = 0;
                if !title.is_empty() {
                    let mut title_st: Style = Style::default();
                    let _: &mut LayoutStyle =
                        title_st.get_mut_layout().set_width(Length::Percent(100.0));
                    let _: &mut TextStyle = title_st
                        .get_mut_text()
                        .set_font_size(geometry.get_font_heading())
                        .set_weight(FontWeight::SemiBold)
                        .set_color(card_surface.get_fg());
                    head_nodes[head_len] = tree.create_text(title.clone(), title_st);
                    head_len += 1;
                }
                if !subtitle.is_empty() {
                    let mut sub_st: Style = Style::default();
                    let _: &mut LayoutStyle =
                        sub_st.get_mut_layout().set_width(Length::Percent(100.0));
                    let _: &mut TextStyle = sub_st
                        .get_mut_text()
                        .set_font_size(13.0)
                        .set_color(theme_palette.get_muted_foreground());
                    head_nodes[head_len] = tree.create_text(subtitle.clone(), sub_st);
                    head_len += 1;
                }
                if head_len > 0 {
                    tree.append_children(root, &head_nodes[..head_len]);
                }
                if !children.is_empty() {
                    tree.append_children(root, children);
                }
                root
            },
            NodeId::default(),
        )
    }
}

/// Declarative `#[component]` implementation for `EuvHeader`.
#[component]
/// Inherent implementation of [`EuvHeader`].
impl EuvHeader {
    /// Renders `EuvHeader` into the ambient framework-managed `Context`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Component descriptor reference.
    ///
    /// # Returns
    ///
    /// - `NodeId` - Mounted root scene node handle.
    fn render(&self) -> NodeId {
        let ctx: Context = current_context();
        let tokens: ThemeTokens = live_tokens_untracked(self.get_tokens());
        let theme_palette: ThemePalette = tokens.get_palette();
        let geometry: ThemeGeometry = tokens.get_geometry();
        let bg_surface: SurfaceColors = theme_palette.surface(SurfaceRole::Background);
        let Self {
            title, subtitle, ..
        } = self;
        let mut a11y: A11yProps = A11yProps::new(A11yRole::Heading, title.clone());
        let _: &mut A11yProps = a11y.set_heading_level(1);
        let mut col_st: Style = Style::default();
        let _: &mut LayoutStyle = col_st
            .get_mut_layout()
            .set_flex_direction(FlexDirection::Column)
            .set_width(Length::Percent(100.0))
            .set_min_width(Length::Px(0.0))
            .set_row_gap(SPACE_XS)
            .set_column_gap(SPACE_XS)
            .set_padding(Edges::new(0.0, 0.0, SPACE_MD, 0.0));
        let mut row_st: Style = Style::default();
        let _: &mut LayoutStyle = row_st
            .get_mut_layout()
            .set_flex_direction(FlexDirection::Row)
            .set_width(Length::Percent(100.0))
            .set_min_width(Length::Px(0.0))
            .set_align_items(AlignItems::Center)
            .set_row_gap(SPACE_SM)
            .set_column_gap(SPACE_SM);
        let mut accent_st: Style = Style::default();
        let _: &mut LayoutStyle = accent_st
            .get_mut_layout()
            .set_width(Length::Px(4.0))
            .set_height(Length::Px(24.0))
            .set_flex_shrink(0.0);
        let _: &mut PaintStyle = accent_st
            .get_mut_paint()
            .set_radius(CornerRadius::all(RADIUS_PILL))
            .set_background(Some(theme_palette.get_primary()));
        let mut title_st: Style = Style::default();
        let _: &mut LayoutStyle = title_st
            .get_mut_layout()
            .set_flex_grow(1.0)
            .set_flex_shrink(1.0)
            .set_min_width(Length::Px(0.0));
        let _: &mut TextStyle = title_st
            .get_mut_text()
            .set_font_size(geometry.get_font_title())
            .set_weight(FontWeight::Bold)
            .set_overflow(TextOverflow::Wrap)
            .set_color(bg_surface.get_fg());
        ctx.get_scene().with_write(
            |tree: &mut SceneTree| {
                let root: NodeId = tree.create_view(col_st);
                let header_row: NodeId = tree.create_view(row_st);
                let accent: NodeId = tree.create_view(accent_st);
                let title_id: NodeId = tree.create_text(title.clone(), title_st);
                if let Some(node) = tree.get_node_mut(title_id) {
                    let _: &mut SceneNode = node.set_a11y(Some(a11y));
                }
                tree.append_children(header_row, &[accent, title_id]);
                if !subtitle.is_empty() {
                    let mut sub_st: Style = Style::default();
                    let _: &mut LayoutStyle =
                        sub_st.get_mut_layout().set_width(Length::Percent(100.0));
                    let _: &mut TextStyle = sub_st
                        .get_mut_text()
                        .set_font_size(geometry.get_font_body())
                        .set_color(theme_palette.get_muted_foreground());
                    let sub_id: NodeId = tree.create_text(subtitle.clone(), sub_st);
                    tree.append_children(root, &[header_row, sub_id]);
                } else {
                    tree.append_child(root, header_row);
                }
                root
            },
            NodeId::default(),
        )
    }
}

/// Palette helpers for `EuvStatCard`.
impl EuvStatCard {
    /// Resolves contrast-safe `(badge_bg, badge_fg)` colors for the stat card caption pill.
    ///
    /// # Arguments
    ///
    /// - `ThemePalette` - Live theme palette.
    ///
    /// # Returns
    ///
    /// - `(Color, Color)` - Translucent tint background and `>= 4.5:1` readable foreground over `card`.
    #[must_use]
    pub fn caption_colors(palette: ThemePalette) -> (Color, Color) {
        let cap_bg: Color = palette.get_primary().with_alpha(24);
        let composited: Color = blend_over(cap_bg, palette.get_card());
        let cap_fg: Color = palette.readable_foreground_with(palette.get_primary(), composited);
        (cap_bg, cap_fg)
    }
}

/// Declarative `#[component]` implementation for `EuvStatCard`.
#[component]
/// Inherent implementation of [`EuvStatCard`].
impl EuvStatCard {
    /// Renders `EuvStatCard` into the ambient framework-managed `Context`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Component descriptor reference.
    ///
    /// # Returns
    ///
    /// - `NodeId` - Mounted root scene node handle.
    fn render(&self) -> NodeId {
        let ctx: Context = current_context();
        let base_tokens: ThemeTokens = self.get_tokens();
        let init_tok: ThemeTokens = live_tokens_untracked(base_tokens);
        let theme_palette: ThemePalette = init_tok.get_palette();
        let geometry: ThemeGeometry = init_tok.get_geometry();
        let card_surface: SurfaceColors = theme_palette.surface(SurfaceRole::Card);
        let Self {
            label,
            value,
            caption,
            ..
        } = self;
        let mut root_st: Style = init_tok.card_style();
        let _: &mut LayoutStyle = root_st
            .get_mut_layout()
            .set_width(Length::Percent(100.0))
            .set_row_gap(SPACE_SM)
            .set_column_gap(SPACE_SM);
        let mut head_row_st: Style = Style::default();
        let _: &mut LayoutStyle = head_row_st
            .get_mut_layout()
            .set_flex_direction(FlexDirection::Row)
            .set_width(Length::Percent(100.0))
            .set_justify_content(JustifyContent::SpaceBetween)
            .set_align_items(AlignItems::Center)
            .set_column_gap(SPACE_SM);
        let mut lbl_st: Style = Style::default();
        let _: &mut LayoutStyle = lbl_st
            .get_mut_layout()
            .set_flex_shrink(1.0)
            .set_min_width(Length::Px(0.0));
        let _: &mut TextStyle = lbl_st
            .get_mut_text()
            .set_font_size(12.0)
            .set_weight(FontWeight::SemiBold)
            .set_color(theme_palette.get_muted_foreground());
        let mut bar_st: Style = Style::default();
        let _: &mut LayoutStyle = bar_st
            .get_mut_layout()
            .set_width(Length::Px(24.0))
            .set_height(Length::Px(4.0))
            .set_flex_shrink(0.0);
        let _: &mut PaintStyle = bar_st
            .get_mut_paint()
            .set_radius(CornerRadius::all(RADIUS_PILL))
            .set_background(Some(theme_palette.get_primary()));
        let mut val_st: Style = Style::default();
        let _: &mut LayoutStyle = val_st.get_mut_layout().set_width(Length::Percent(100.0));
        let _: &mut TextStyle = val_st
            .get_mut_text()
            .set_font_size(26.0)
            .set_weight(FontWeight::Bold)
            .set_color(card_surface.get_fg());
        let (root, cap_nodes): (NodeId, Option<(NodeId, NodeId)>) = ctx.get_scene().with_write(
            |tree: &mut SceneTree| {
                let root: NodeId = tree.create_view(root_st);
                let head_row: NodeId = tree.create_view(head_row_st);
                let lbl_id: NodeId = tree.create_text(label.clone(), lbl_st);
                let bar_id: NodeId = tree.create_view(bar_st);
                tree.append_children(head_row, &[lbl_id, bar_id]);
                let val_id: NodeId = tree.create_text(value.clone(), val_st);
                if !caption.is_empty() {
                    let (cap_bg, cap_fg): (Color, Color) = Self::caption_colors(theme_palette);
                    let mut cap_row_st: Style = Style::default();
                    let _: &mut LayoutStyle = cap_row_st
                        .get_mut_layout()
                        .set_flex_direction(FlexDirection::Row)
                        .set_max_width(Length::Percent(100.0))
                        .set_padding(Edges::symmetric(SPACE_XS, 10.0))
                        .set_align_items(AlignItems::Center)
                        .set_overflow(Overflow::Hidden);
                    let _: &mut PaintStyle = cap_row_st
                        .get_mut_paint()
                        .set_radius(CornerRadius::all(geometry.get_radius_sm().max(0.0)))
                        .set_background(Some(cap_bg));
                    let mut cap_txt_st: Style = Style::default();
                    let _: &mut LayoutStyle = cap_txt_st
                        .get_mut_layout()
                        .set_max_width(Length::Percent(100.0))
                        .set_flex_shrink(1.0)
                        .set_min_width(Length::Px(0.0));
                    let _: &mut TextStyle = cap_txt_st
                        .get_mut_text()
                        .set_font_size(11.0)
                        .set_weight(FontWeight::SemiBold)
                        .set_color(cap_fg);
                    let cap_row: NodeId = tree.create_view(cap_row_st);
                    let cap_txt: NodeId = tree.create_text(caption.clone(), cap_txt_st);
                    tree.append_child(cap_row, cap_txt);
                    tree.append_children(root, &[head_row, val_id, cap_row]);
                    (root, Some((cap_row, cap_txt)))
                } else {
                    tree.append_children(root, &[head_row, val_id]);
                    (root, None)
                }
            },
            (NodeId::default(), None),
        );
        if let Some((cap_row, cap_txt)) = cap_nodes {
            let handle: SceneHandle = ctx.get_scene().clone();
            let prev_key: Cell<ThemeKey> = Cell::new(init_tok.key());
            let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
                Effect::new(move || {
                    let tok: ThemeTokens = use_live_tokens(base_tokens);
                    let old_key: ThemeKey = prev_key.replace(tok.key());
                    if old_key == tok.key() {
                        return;
                    }
                    let (cap_bg, cap_fg): (Color, Color) = Self::caption_colors(tok.get_palette());
                    handle.with_write(
                        |tree: &mut SceneTree| {
                            if let Some(item_count) = tree.get_node_mut(cap_row) {
                                let _: &mut PaintStyle = item_count
                                    .get_mut_style()
                                    .get_mut_paint()
                                    .set_background(Some(cap_bg));
                                tree.mark_dirty(cap_row, DIRTY_PAINT);
                            }
                            if let Some(node_entry) = tree.get_node_mut(cap_txt) {
                                let _: &mut TextStyle =
                                    node_entry.get_mut_style().get_mut_text().set_color(cap_fg);
                                tree.mark_dirty(cap_txt, DIRTY_PAINT);
                            }
                        },
                        (),
                    );
                })
            });
            ctx.get_scene().with_write(
                |tree: &mut SceneTree| {
                    mark_theme_owned(tree, &[cap_row, cap_txt]);
                    tree.attach_scope(root, scope);
                },
                (),
            );
        }
        root
    }
}