panel.rs

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