panel.rs

  1use gpui::px;
  2use gpui::{prelude::*, AbsoluteLength, AnyElement, Div, RenderOnce};
  3use smallvec::SmallVec;
  4
  5use crate::prelude::*;
  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(RenderOnce)]
 42pub struct Panel {
 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; 2]>,
 50}
 51
 52impl Component for Panel {
 53    type Rendered = gpui::Stateful<Div>;
 54
 55    fn render(self, cx: &mut WindowContext) -> Self::Rendered {
 56        let current_size = self.width.unwrap_or(self.initial_width);
 57
 58        v_stack()
 59            .id(self.id.clone())
 60            .flex_initial()
 61            .map(|this| match self.current_side {
 62                PanelSide::Left | PanelSide::Right => this.h_full().w(current_size),
 63                PanelSide::Bottom => this,
 64            })
 65            .map(|this| match self.current_side {
 66                PanelSide::Left => this.border_r(),
 67                PanelSide::Right => this.border_l(),
 68                PanelSide::Bottom => this.border_b().w_full().h(current_size),
 69            })
 70            .bg(cx.theme().colors().surface_background)
 71            .border_color(cx.theme().colors().border)
 72            .children(self.children)
 73    }
 74}
 75
 76impl Panel {
 77    pub fn new(id: impl Into<ElementId>, cx: &mut WindowContext) -> Self {
 78        Self {
 79            id: id.into(),
 80            current_side: PanelSide::default(),
 81            allowed_sides: PanelAllowedSides::default(),
 82            initial_width: px(320.).into(),
 83            width: None,
 84            children: SmallVec::new(),
 85        }
 86    }
 87
 88    pub fn initial_width(mut self, initial_width: AbsoluteLength) -> Self {
 89        self.initial_width = initial_width;
 90        self
 91    }
 92
 93    pub fn width(mut self, width: AbsoluteLength) -> Self {
 94        self.width = Some(width);
 95        self
 96    }
 97
 98    pub fn allowed_sides(mut self, allowed_sides: PanelAllowedSides) -> Self {
 99        self.allowed_sides = allowed_sides;
100        self
101    }
102
103    pub fn side(mut self, side: PanelSide) -> Self {
104        let allowed_sides = self.allowed_sides.allowed_sides();
105
106        if allowed_sides.contains(&side) {
107            self.current_side = side;
108        } else {
109            panic!(
110                "The panel side {:?} was not added as allowed before it was set.",
111                side
112            );
113        }
114        self
115    }
116}
117
118impl ParentElement for Panel {
119    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
120        &mut self.children
121    }
122}
123
124#[cfg(feature = "stories")]
125pub use stories::*;
126
127#[cfg(feature = "stories")]
128mod stories {
129    use super::*;
130    use crate::{Label, Story};
131    use gpui::{Div, InteractiveElement, Render};
132
133    pub struct PanelStory;
134
135    impl Render for PanelStory {
136        type Element = Div;
137
138        fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
139            Story::container(cx)
140                .child(Story::title_for::<Panel>(cx))
141                .child(Story::label(cx, "Default"))
142                .child(
143                    Panel::new("panel", cx).child(
144                        div()
145                            .id("panel-contents")
146                            .overflow_y_scroll()
147                            .children((0..100).map(|ix| Label::new(format!("Item {}", ix + 1)))),
148                    ),
149                )
150        }
151    }
152}