details.rs

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