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 color = ThemeColor::new(cx);
35
36        v_stack()
37            .p_1()
38            .gap_0p5()
39            .text_xs()
40            .text_color(color.text)
41            .child(self.text)
42            .children(self.meta.map(|m| m))
43            .children(self.actions.take().map(|a| a))
44    }
45}
46
47#[cfg(feature = "stories")]
48pub use stories::*;
49
50#[cfg(feature = "stories")]
51mod stories {
52    use crate::Story;
53
54    use super::*;
55
56    #[derive(Element)]
57    pub struct DetailsStory<S: 'static + Send + Sync + Clone> {
58        state_type: PhantomData<S>,
59    }
60
61    impl<S: 'static + Send + Sync + Clone> DetailsStory<S> {
62        pub fn new() -> Self {
63            Self {
64                state_type: PhantomData,
65            }
66        }
67
68        fn render(
69            &mut self,
70            _view: &mut S,
71            cx: &mut ViewContext<S>,
72        ) -> impl Element<ViewState = S> {
73            Story::container(cx)
74                .child(Story::title_for::<_, Details<S>>(cx))
75                .child(Story::label(cx, "Default"))
76                .child(Details::new("The quick brown fox jumps over the lazy dog"))
77                .child(Story::label(cx, "With meta"))
78                .child(
79                    Details::new("The quick brown fox jumps over the lazy dog")
80                        .meta_text("Sphinx of black quartz, judge my vow."),
81                )
82        }
83    }
84}