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 current_size = self.width.unwrap_or(self.initial_width);
97
98 v_stack()
99 .id(self.id.clone())
100 .flex_initial()
101 .when(
102 self.current_side == PanelSide::Left || self.current_side == PanelSide::Right,
103 |this| this.h_full().w(current_size),
104 )
105 .when(self.current_side == PanelSide::Left, |this| this.border_r())
106 .when(self.current_side == PanelSide::Right, |this| {
107 this.border_l()
108 })
109 .when(self.current_side == PanelSide::Bottom, |this| {
110 this.border_b().w_full().h(current_size)
111 })
112 .bg(cx.theme().colors().surface)
113 .border_color(cx.theme().colors().border)
114 .children(self.children)
115 }
116}
117
118impl<V: 'static> ParentElement<V> for Panel<V> {
119 fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 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 gpui2::{Div, Render};
132
133 pub struct PanelStory;
134
135 impl Render for PanelStory {
136 type Element = Div<Self>;
137
138 fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
139 Story::container(cx)
140 .child(Story::title_for::<_, Panel<Self>>(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}