panel.rs

  1use std::marker::PhantomData;
  2
  3use gpui2::{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    id: ElementId,
 46    state_type: PhantomData<S>,
 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(id: impl Into<ElementId>, cx: &mut WindowContext) -> Self {
 57        let settings = user_settings(cx);
 58
 59        Self {
 60            id: id.into(),
 61            state_type: PhantomData,
 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 theme = theme(cx);
101
102        let current_size = self.width.unwrap_or(self.initial_width);
103
104        v_stack()
105            .id(self.id.clone())
106            .flex_initial()
107            .when(
108                self.current_side == PanelSide::Left || self.current_side == PanelSide::Right,
109                |this| this.h_full().w(current_size),
110            )
111            .when(self.current_side == PanelSide::Left, |this| this.border_r())
112            .when(self.current_side == PanelSide::Right, |this| {
113                this.border_l()
114            })
115            .when(self.current_side == PanelSide::Bottom, |this| {
116                this.border_b().w_full().h(current_size)
117            })
118            .bg(theme.surface)
119            .border_color(theme.border)
120            .children(self.children.drain(..))
121    }
122}
123
124impl<S: 'static + Send + Sync> ParentElement for Panel<S> {
125    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::ViewState>; 2]> {
126        &mut self.children
127    }
128}
129
130#[cfg(feature = "stories")]
131pub use stories::*;
132
133#[cfg(feature = "stories")]
134mod stories {
135    use crate::{Label, Story};
136
137    use super::*;
138
139    #[derive(Element)]
140    pub struct PanelStory<S: 'static + Send + Sync + Clone> {
141        state_type: PhantomData<S>,
142    }
143
144    impl<S: 'static + Send + Sync + Clone> PanelStory<S> {
145        pub fn new() -> Self {
146            Self {
147                state_type: PhantomData,
148            }
149        }
150
151        fn render(
152            &mut self,
153            _view: &mut S,
154            cx: &mut ViewContext<S>,
155        ) -> impl Element<ViewState = S> {
156            Story::container(cx)
157                .child(Story::title_for::<_, Panel<S>>(cx))
158                .child(Story::label(cx, "Default"))
159                .child(
160                    Panel::new("panel", cx).child(
161                        div()
162                            .id("panel-contents")
163                            .overflow_y_scroll()
164                            .children((0..100).map(|ix| Label::new(format!("Item {}", ix + 1)))),
165                    ),
166                )
167        }
168    }
169}