Skip to main content

fc_ui/capabilities/primitives/display/
card.rs

1//! Card - Content container with header, body, and footer sections.
2
3use crate::capabilities::foundation::theme::use_theme;
4use gpui::{prelude::FluentBuilder as _, *};
5
6#[derive(IntoElement)]
7pub struct Card {
8    header: Option<AnyElement>,
9    content: Option<AnyElement>,
10    footer: Option<AnyElement>,
11    style: StyleRefinement,
12}
13
14impl Default for Card {
15    fn default() -> Self {
16        Self::new()
17    }
18}
19
20impl Card {
21    pub fn new() -> Self {
22        Self {
23            header: None,
24            content: None,
25            footer: None,
26            style: StyleRefinement::default(),
27        }
28    }
29
30    pub fn header(mut self, header: impl IntoElement) -> Self {
31        self.header = Some(header.into_any_element());
32        self
33    }
34
35    pub fn content(mut self, content: impl IntoElement) -> Self {
36        self.content = Some(content.into_any_element());
37        self
38    }
39
40    pub fn footer(mut self, footer: impl IntoElement) -> Self {
41        self.footer = Some(footer.into_any_element());
42        self
43    }
44}
45
46impl Styled for Card {
47    fn style(&mut self) -> &mut StyleRefinement {
48        &mut self.style
49    }
50}
51
52impl RenderOnce for Card {
53    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
54        let theme = use_theme(cx);
55        let user_style = self.style;
56
57        let shadow_sm = BoxShadow {
58            offset: theme.tokens.shadow_sm.offset,
59            blur_radius: theme.tokens.shadow_sm.blur_radius,
60            spread_radius: theme.tokens.shadow_sm.spread_radius,
61            inset: false,
62            color: theme.tokens.shadow_sm.color,
63        };
64
65        let mut base = div()
66            .bg(theme.tokens.card)
67            .border_1()
68            .border_color(theme.tokens.border)
69            .rounded(theme.tokens.radius_lg)
70            .shadow(smallvec::smallvec![shadow_sm])
71            .overflow_hidden();
72
73        if let Some(header) = self.header {
74            base = base.child(
75                div()
76                    .px(px(24.0))
77                    .py(px(16.0))
78                    .border_b_1()
79                    .border_color(theme.tokens.border)
80                    .child(header),
81            );
82        }
83
84        if let Some(content) = self.content {
85            base = base.child(div().px(px(24.0)).py(px(16.0)).child(content));
86        }
87
88        if let Some(footer) = self.footer {
89            base = base.child(
90                div()
91                    .px(px(24.0))
92                    .py(px(16.0))
93                    .border_t_1()
94                    .border_color(theme.tokens.border)
95                    .child(footer),
96            );
97        }
98
99        base.map(|this| {
100            let mut div = this;
101            div.style().refine(&user_style);
102            div
103        })
104    }
105}