panel.rs

  1use std::marker::PhantomData;
  2
  3use gpui3::{AbsoluteLength, AnyElement};
  4use smallvec::SmallVec;
  5
  6use crate::settings::user_settings;
  7use crate::v_stack;
  8use crate::{prelude::*, theme};
  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 theme = theme(cx);
101
102        let panel_base;
103        let current_width = self.width.unwrap_or(self.initial_width);
104
105        match self.current_side {
106            PanelSide::Left => {
107                panel_base = v_stack()
108                    .flex_initial()
109                    .h_full()
110                    // .w(current_width)
111                    .w_64()
112                    .bg(theme.middle.base.default.background)
113                    .border_r()
114                    .border_color(theme.middle.base.default.border);
115            }
116            PanelSide::Right => {
117                panel_base = v_stack()
118                    .flex_initial()
119                    .h_full()
120                    // .w(current_width)
121                    .w_64()
122                    .bg(theme.middle.base.default.background)
123                    .border_l()
124                    .border_color(theme.middle.base.default.border);
125            }
126            PanelSide::Bottom => {
127                panel_base = v_stack()
128                    .flex_initial()
129                    .w_full()
130                    // .h(current_width)
131                    .h_64()
132                    .bg(theme.middle.base.default.background)
133                    .border_t()
134                    .border_color(theme.middle.base.default.border);
135            }
136        }
137
138        panel_base.children(self.children.drain(..))
139    }
140}
141
142impl<S: 'static + Send + Sync> ParentElement for Panel<S> {
143    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::ViewState>; 2]> {
144        &mut self.children
145    }
146}
147
148#[cfg(feature = "stories")]
149pub use stories::*;
150
151#[cfg(feature = "stories")]
152mod stories {
153    use crate::{Label, Story};
154
155    use super::*;
156
157    #[derive(Element)]
158    pub struct PanelStory<S: 'static + Send + Sync + Clone> {
159        state_type: PhantomData<S>,
160    }
161
162    impl<S: 'static + Send + Sync + Clone> PanelStory<S> {
163        pub fn new() -> Self {
164            Self {
165                state_type: PhantomData,
166            }
167        }
168
169        fn render(
170            &mut self,
171            _view: &mut S,
172            cx: &mut ViewContext<S>,
173        ) -> impl Element<ViewState = S> {
174            Story::container(cx)
175                .child(Story::title_for::<_, Panel<S>>(cx))
176                .child(Story::label(cx, "Default"))
177                .child(
178                    Panel::new(cx).child(
179                        div()
180                            .overflow_y_scroll(ScrollState::default())
181                            .children((0..100).map(|ix| Label::new(format!("Item {}", ix + 1)))),
182                    ),
183                )
184        }
185    }
186}