panel.rs

  1use gpui::{prelude::*, AbsoluteLength, AnyElement, Div, RenderOnce, Stateful};
  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(RenderOnce)]
 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> Component<V> for Panel<V> {
 53    type Rendered = Stateful<V, Div<V>>;
 54
 55    fn render(self, view: &mut V, cx: &mut ViewContext<V>) -> 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<V: 'static> Panel<V> {
 77    pub fn new(id: impl Into<ElementId>, cx: &mut WindowContext) -> Self {
 78        let settings = user_settings(cx);
 79
 80        Self {
 81            id: id.into(),
 82            current_side: PanelSide::default(),
 83            allowed_sides: PanelAllowedSides::default(),
 84            initial_width: *settings.default_panel_size,
 85            width: None,
 86            children: SmallVec::new(),
 87        }
 88    }
 89
 90    pub fn initial_width(mut self, initial_width: AbsoluteLength) -> Self {
 91        self.initial_width = initial_width;
 92        self
 93    }
 94
 95    pub fn width(mut self, width: AbsoluteLength) -> Self {
 96        self.width = Some(width);
 97        self
 98    }
 99
100    pub fn allowed_sides(mut self, allowed_sides: PanelAllowedSides) -> Self {
101        self.allowed_sides = allowed_sides;
102        self
103    }
104
105    pub fn side(mut self, side: PanelSide) -> Self {
106        let allowed_sides = self.allowed_sides.allowed_sides();
107
108        if allowed_sides.contains(&side) {
109            self.current_side = side;
110        } else {
111            panic!(
112                "The panel side {:?} was not added as allowed before it was set.",
113                side
114            );
115        }
116        self
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 super::*;
132    use crate::{Label, Story};
133    use gpui::{Div, InteractiveElement, Render};
134
135    pub struct PanelStory;
136
137    impl Render<Self> for PanelStory {
138        type Element = Div<Self>;
139
140        fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
141            Story::container(cx)
142                .child(Story::title_for::<_, Panel<Self>>(cx))
143                .child(Story::label(cx, "Default"))
144                .child(
145                    Panel::new("panel", cx).child(
146                        div()
147                            .id("panel-contents")
148                            .overflow_y_scroll()
149                            .children((0..100).map(|ix| Label::new(format!("Item {}", ix + 1)))),
150                    ),
151                )
152        }
153    }
154}