use gpui3::AnyElement; use smallvec::SmallVec; use crate::prelude::*; use crate::theme; #[derive(Clone)] pub struct ToolbarItem {} #[derive(Element)] pub struct Toolbar { left_items: SmallVec<[AnyElement; 2]>, right_items: SmallVec<[AnyElement; 2]>, } impl Toolbar { pub fn new() -> Self { Self { left_items: SmallVec::new(), right_items: SmallVec::new(), } } pub fn left_item(mut self, child: impl IntoAnyElement) -> Self where Self: Sized, { self.left_items.push(child.into_any()); self } pub fn left_items(mut self, iter: impl IntoIterator>) -> Self where Self: Sized, { self.left_items .extend(iter.into_iter().map(|item| item.into_any())); self } pub fn right_item(mut self, child: impl IntoAnyElement) -> Self where Self: Sized, { self.right_items.push(child.into_any()); self } pub fn right_items(mut self, iter: impl IntoIterator>) -> Self where Self: Sized, { self.right_items .extend(iter.into_iter().map(|item| item.into_any())); self } fn render(&mut self, _view: &mut S, cx: &mut ViewContext) -> impl Element { let theme = theme(cx); div() .bg(theme.highest.base.default.background) .p_2() .flex() .justify_between() .child(div().flex().children(self.left_items.drain(..))) .child(div().flex().children(self.right_items.drain(..))) } } #[cfg(feature = "stories")] pub use stories::*; #[cfg(feature = "stories")] mod stories { use std::marker::PhantomData; use std::path::PathBuf; use std::str::FromStr; use crate::{Breadcrumb, HighlightedText, Icon, IconButton, Story, Symbol}; use super::*; #[derive(Element)] pub struct ToolbarStory { state_type: PhantomData, } impl ToolbarStory { pub fn new() -> Self { Self { state_type: PhantomData, } } fn render( &mut self, _view: &mut S, cx: &mut ViewContext, ) -> impl Element { let theme = theme(cx); Story::container(cx) .child(Story::title_for::<_, Toolbar>(cx)) .child(Story::label(cx, "Default")) .child( Toolbar::new() .left_item(Breadcrumb::new( PathBuf::from_str("crates/ui/src/components/toolbar.rs").unwrap(), vec![ Symbol(vec![ HighlightedText { text: "impl ".to_string(), color: HighlightColor::Keyword.hsla(&theme), }, HighlightedText { text: "ToolbarStory".to_string(), color: HighlightColor::Function.hsla(&theme), }, ]), Symbol(vec![ HighlightedText { text: "fn ".to_string(), color: HighlightColor::Keyword.hsla(&theme), }, HighlightedText { text: "render".to_string(), color: HighlightColor::Function.hsla(&theme), }, ]), ], )) .right_items(vec![ IconButton::new(Icon::InlayHint), IconButton::new(Icon::MagnifyingGlass), IconButton::new(Icon::MagicWand), ]), ) } } }