details.rs

 1use crate::{prelude::*, v_stack, ButtonGroup};
 2
 3#[derive(Component)]
 4pub struct Details<V: 'static> {
 5    text: &'static str,
 6    meta: Option<&'static str>,
 7    actions: Option<ButtonGroup<V>>,
 8}
 9
10impl<V: 'static> Details<V> {
11    pub fn new(text: &'static str) -> Self {
12        Self {
13            text,
14            meta: None,
15            actions: None,
16        }
17    }
18
19    pub fn meta_text(mut self, meta: &'static str) -> Self {
20        self.meta = Some(meta);
21        self
22    }
23
24    pub fn actions(mut self, actions: ButtonGroup<V>) -> Self {
25        self.actions = Some(actions);
26        self
27    }
28
29    fn render(self, _view: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
30        let theme = theme(cx);
31
32        v_stack()
33            .p_1()
34            .gap_0p5()
35            .text_xs()
36            .text_color(theme.text)
37            .size_full()
38            .child(self.text)
39            .children(self.meta.map(|m| m))
40            .children(self.actions.map(|a| a))
41    }
42}
43
44#[cfg(feature = "stories")]
45pub use stories::*;
46
47#[cfg(feature = "stories")]
48mod stories {
49    use super::*;
50    use crate::{Button, Story};
51    use gpui2::{Div, Render};
52
53    pub struct DetailsStory;
54
55    impl Render for DetailsStory {
56        type Element = Div<Self>;
57
58        fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
59            Story::container(cx)
60                .child(Story::title_for::<_, Details<Self>>(cx))
61                .child(Story::label(cx, "Default"))
62                .child(Details::new("The quick brown fox jumps over the lazy dog"))
63                .child(Story::label(cx, "With meta"))
64                .child(
65                    Details::new("The quick brown fox jumps over the lazy dog")
66                        .meta_text("Sphinx of black quartz, judge my vow."),
67                )
68                .child(Story::label(cx, "With meta and actions"))
69                .child(
70                    Details::new("The quick brown fox jumps over the lazy dog")
71                        .meta_text("Sphinx of black quartz, judge my vow.")
72                        .actions(ButtonGroup::new(vec![
73                            Button::new("Decline"),
74                            Button::new("Accept").variant(crate::ButtonVariant::Filled),
75                        ])),
76                )
77        }
78    }
79}