panel.rs

  1use std::marker::PhantomData;
  2
  3use gpui3::{AbsoluteLength, AnyElement};
  4use smallvec::SmallVec;
  5
  6use crate::prelude::*;
  7use crate::settings::user_settings;
  8use crate::v_stack;
  9
 10#[derive(Default, Debug, PartialEq, Eq, Hash, Clone, Copy)]
 11pub enum PanelAllowedSides {
 12    LeftOnly,
 13    RightOnly,
 14    BottomOnly,
 15    #[default]
 16    LeftAndRight,
 17    All,
 18}
 19
 20impl PanelAllowedSides {
 21    /// Return a `HashSet` that contains the allowable `PanelSide`s.
 22    pub fn allowed_sides(&self) -> HashSet<PanelSide> {
 23        match self {
 24            Self::LeftOnly => HashSet::from_iter([PanelSide::Left]),
 25            Self::RightOnly => HashSet::from_iter([PanelSide::Right]),
 26            Self::BottomOnly => HashSet::from_iter([PanelSide::Bottom]),
 27            Self::LeftAndRight => HashSet::from_iter([PanelSide::Left, PanelSide::Right]),
 28            Self::All => HashSet::from_iter([PanelSide::Left, PanelSide::Right, PanelSide::Bottom]),
 29        }
 30    }
 31}
 32
 33#[derive(Default, Debug, PartialEq, Eq, Hash, Clone, Copy)]
 34pub enum PanelSide {
 35    #[default]
 36    Left,
 37    Right,
 38    Bottom,
 39}
 40
 41use std::collections::HashSet;
 42
 43#[derive(Element)]
 44pub struct Panel<S: 'static + Send + Sync> {
 45    state_type: PhantomData<S>,
 46    scroll_state: ScrollState,
 47    current_side: PanelSide,
 48    /// Defaults to PanelAllowedSides::LeftAndRight
 49    allowed_sides: PanelAllowedSides,
 50    initial_width: AbsoluteLength,
 51    width: Option<AbsoluteLength>,
 52    children: SmallVec<[AnyElement<S>; 2]>,
 53}
 54
 55impl<S: 'static + Send + Sync> Panel<S> {
 56    pub fn new(cx: &mut WindowContext) -> Self {
 57        let settings = user_settings(cx);
 58
 59        Self {
 60            state_type: PhantomData,
 61            scroll_state: ScrollState::default(),
 62            current_side: PanelSide::default(),
 63            allowed_sides: PanelAllowedSides::default(),
 64            initial_width: *settings.default_panel_size,
 65            width: None,
 66            children: SmallVec::new(),
 67        }
 68    }
 69
 70    pub fn initial_width(mut self, initial_width: AbsoluteLength) -> Self {
 71        self.initial_width = initial_width;
 72        self
 73    }
 74
 75    pub fn width(mut self, width: AbsoluteLength) -> Self {
 76        self.width = Some(width);
 77        self
 78    }
 79
 80    pub fn allowed_sides(mut self, allowed_sides: PanelAllowedSides) -> Self {
 81        self.allowed_sides = allowed_sides;
 82        self
 83    }
 84
 85    pub fn side(mut self, side: PanelSide) -> Self {
 86        let allowed_sides = self.allowed_sides.allowed_sides();
 87
 88        if allowed_sides.contains(&side) {
 89            self.current_side = side;
 90        } else {
 91            panic!(
 92                "The panel side {:?} was not added as allowed before it was set.",
 93                side
 94            );
 95        }
 96        self
 97    }
 98
 99    fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
100        let color = ThemeColor::new(cx);
101
102        let current_size = self.width.unwrap_or(self.initial_width);
103
104        v_stack()
105            .flex_initial()
106            .when(
107                self.current_side == PanelSide::Left || self.current_side == PanelSide::Right,
108                |this| this.h_full().w(current_size),
109            )
110            .when(self.current_side == PanelSide::Left, |this| this.border_r())
111            .when(self.current_side == PanelSide::Right, |this| {
112                this.border_l()
113            })
114            .when(self.current_side == PanelSide::Bottom, |this| {
115                this.border_b().w_full().h(current_size)
116            })
117            .bg(color.surface)
118            .border_color(color.border)
119            .children(self.children.drain(..))
120    }
121}
122
123impl<S: 'static + Send + Sync> ParentElement for Panel<S> {
124    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::ViewState>; 2]> {
125        &mut self.children
126    }
127}
128
129#[cfg(feature = "stories")]
130pub use stories::*;
131
132#[cfg(feature = "stories")]
133mod stories {
134    use crate::{Label, Story};
135
136    use super::*;
137
138    #[derive(Element)]
139    pub struct PanelStory<S: 'static + Send + Sync + Clone> {
140        state_type: PhantomData<S>,
141    }
142
143    impl<S: 'static + Send + Sync + Clone> PanelStory<S> {
144        pub fn new() -> Self {
145            Self {
146                state_type: PhantomData,
147            }
148        }
149
150        fn render(
151            &mut self,
152            _view: &mut S,
153            cx: &mut ViewContext<S>,
154        ) -> impl Element<ViewState = S> {
155            Story::container(cx)
156                .child(Story::title_for::<_, Panel<S>>(cx))
157                .child(Story::label(cx, "Default"))
158                .child(
159                    Panel::new(cx).child(
160                        div()
161                            .overflow_y_scroll(ScrollState::default())
162                            .children((0..100).map(|ix| Label::new(format!("Item {}", ix + 1)))),
163                    ),
164                )
165        }
166    }
167}