toolbar.rs

  1use crate::prelude::*;
  2use crate::theme;
  3
  4#[derive(Clone)]
  5pub struct ToolbarItem {}
  6
  7#[derive(Element)]
  8pub struct Toolbar<S: 'static + Send + Sync> {
  9    left_items: HackyChildren<S>,
 10    left_items_payload: HackyChildrenPayload,
 11    right_items: HackyChildren<S>,
 12    right_items_payload: HackyChildrenPayload,
 13}
 14
 15impl<S: 'static + Send + Sync> Toolbar<S> {
 16    pub fn new(
 17        left_items: HackyChildren<S>,
 18        left_items_payload: HackyChildrenPayload,
 19        right_items: HackyChildren<S>,
 20        right_items_payload: HackyChildrenPayload,
 21    ) -> Self {
 22        Self {
 23            left_items,
 24            left_items_payload,
 25            right_items,
 26            right_items_payload,
 27        }
 28    }
 29
 30    fn render(&mut self, cx: &mut ViewContext<S>) -> impl Element<State = S> {
 31        let theme = theme(cx);
 32
 33        div()
 34            .fill(theme.highest.base.default.background)
 35            .p_2()
 36            .flex()
 37            .justify_between()
 38            .child(
 39                div()
 40                    .flex()
 41                    .children_any((self.left_items)(cx, self.left_items_payload.as_ref())),
 42            )
 43            .child(
 44                div()
 45                    .flex()
 46                    .children_any((self.right_items)(cx, self.right_items_payload.as_ref())),
 47            )
 48    }
 49}
 50
 51#[cfg(feature = "stories")]
 52pub use stories::*;
 53
 54#[cfg(feature = "stories")]
 55mod stories {
 56    use std::marker::PhantomData;
 57    use std::path::PathBuf;
 58    use std::str::FromStr;
 59    use std::sync::Arc;
 60
 61    use crate::{Breadcrumb, HighlightedText, Icon, IconButton, Story, Symbol};
 62
 63    use super::*;
 64
 65    #[derive(Element)]
 66    pub struct ToolbarStory<S: 'static + Send + Sync + Clone> {
 67        state_type: PhantomData<S>,
 68    }
 69
 70    impl<S: 'static + Send + Sync + Clone> ToolbarStory<S> {
 71        pub fn new() -> Self {
 72            Self {
 73                state_type: PhantomData,
 74            }
 75        }
 76
 77        fn render(&mut self, cx: &mut ViewContext<S>) -> impl Element<State = S> {
 78            let theme = theme(cx);
 79
 80            struct LeftItemsPayload {
 81                pub theme: Arc<Theme>,
 82            }
 83
 84            Story::container(cx)
 85                .child(Story::title_for::<_, Toolbar<S>>(cx))
 86                .child(Story::label(cx, "Default"))
 87                .child(Toolbar::new(
 88                    |_, payload| {
 89                        let payload = payload.downcast_ref::<LeftItemsPayload>().unwrap();
 90
 91                        let theme = payload.theme.clone();
 92
 93                        vec![Breadcrumb::new(
 94                            PathBuf::from_str("crates/ui/src/components/toolbar.rs").unwrap(),
 95                            vec![
 96                                Symbol(vec![
 97                                    HighlightedText {
 98                                        text: "impl ".to_string(),
 99                                        color: HighlightColor::Keyword.hsla(&theme),
100                                    },
101                                    HighlightedText {
102                                        text: "ToolbarStory".to_string(),
103                                        color: HighlightColor::Function.hsla(&theme),
104                                    },
105                                ]),
106                                Symbol(vec![
107                                    HighlightedText {
108                                        text: "fn ".to_string(),
109                                        color: HighlightColor::Keyword.hsla(&theme),
110                                    },
111                                    HighlightedText {
112                                        text: "render".to_string(),
113                                        color: HighlightColor::Function.hsla(&theme),
114                                    },
115                                ]),
116                            ],
117                        )
118                        .into_any()]
119                    },
120                    Box::new(LeftItemsPayload {
121                        theme: theme.clone(),
122                    }),
123                    |_, _| {
124                        vec![
125                            IconButton::new(Icon::InlayHint).into_any(),
126                            IconButton::new(Icon::MagnifyingGlass).into_any(),
127                            IconButton::new(Icon::MagicWand).into_any(),
128                        ]
129                    },
130                    Box::new(()),
131                ))
132        }
133    }
134}