1use std::marker::PhantomData;
2
3use gpui3::{AbsoluteLength, AnyElement};
4use smallvec::SmallVec;
5
6use crate::{prelude::*, theme};
7use crate::{token, v_stack};
8
9#[derive(Default, Debug, PartialEq, Eq, Hash, Clone, Copy)]
10pub enum PanelAllowedSides {
11 LeftOnly,
12 RightOnly,
13 BottomOnly,
14 #[default]
15 LeftAndRight,
16 All,
17}
18
19impl PanelAllowedSides {
20 /// Return a `HashSet` that contains the allowable `PanelSide`s.
21 pub fn allowed_sides(&self) -> HashSet<PanelSide> {
22 match self {
23 Self::LeftOnly => HashSet::from_iter([PanelSide::Left]),
24 Self::RightOnly => HashSet::from_iter([PanelSide::Right]),
25 Self::BottomOnly => HashSet::from_iter([PanelSide::Bottom]),
26 Self::LeftAndRight => HashSet::from_iter([PanelSide::Left, PanelSide::Right]),
27 Self::All => HashSet::from_iter([PanelSide::Left, PanelSide::Right, PanelSide::Bottom]),
28 }
29 }
30}
31
32#[derive(Default, Debug, PartialEq, Eq, Hash, Clone, Copy)]
33pub enum PanelSide {
34 #[default]
35 Left,
36 Right,
37 Bottom,
38}
39
40use std::collections::HashSet;
41
42#[derive(Element)]
43pub struct Panel<S: 'static + Send + Sync> {
44 state_type: PhantomData<S>,
45 scroll_state: ScrollState,
46 current_side: PanelSide,
47 /// Defaults to PanelAllowedSides::LeftAndRight
48 allowed_sides: PanelAllowedSides,
49 initial_width: AbsoluteLength,
50 width: Option<AbsoluteLength>,
51 children: SmallVec<[AnyElement<S>; 2]>,
52}
53
54impl<S: 'static + Send + Sync> Panel<S> {
55 pub fn new(scroll_state: ScrollState) -> Self {
56 let token = token();
57
58 Self {
59 state_type: PhantomData,
60 scroll_state,
61 current_side: PanelSide::default(),
62 allowed_sides: PanelAllowedSides::default(),
63 initial_width: token.default_panel_size,
64 width: None,
65 children: SmallVec::new(),
66 }
67 }
68
69 pub fn initial_width(mut self, initial_width: AbsoluteLength) -> Self {
70 self.initial_width = initial_width;
71 self
72 }
73
74 pub fn width(mut self, width: AbsoluteLength) -> Self {
75 self.width = Some(width);
76 self
77 }
78
79 pub fn allowed_sides(mut self, allowed_sides: PanelAllowedSides) -> Self {
80 self.allowed_sides = allowed_sides;
81 self
82 }
83
84 pub fn side(mut self, side: PanelSide) -> Self {
85 let allowed_sides = self.allowed_sides.allowed_sides();
86
87 if allowed_sides.contains(&side) {
88 self.current_side = side;
89 } else {
90 panic!(
91 "The panel side {:?} was not added as allowed before it was set.",
92 side
93 );
94 }
95 self
96 }
97
98 fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
99 let token = token();
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 .fill(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 .fill(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 .fill(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 type State = S;
144
145 fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::State>; 2]> {
146 &mut self.children
147 }
148}
149
150#[cfg(feature = "stories")]
151pub use stories::*;
152
153#[cfg(feature = "stories")]
154mod stories {
155 use crate::{Label, Story};
156
157 use super::*;
158
159 #[derive(Element)]
160 pub struct PanelStory<S: 'static + Send + Sync + Clone> {
161 state_type: PhantomData<S>,
162 }
163
164 impl<S: 'static + Send + Sync + Clone> PanelStory<S> {
165 pub fn new() -> Self {
166 Self {
167 state_type: PhantomData,
168 }
169 }
170
171 fn render(
172 &mut self,
173 _view: &mut S,
174 cx: &mut ViewContext<S>,
175 ) -> impl Element<ViewState = S> {
176 Story::container(cx)
177 .child(Story::title_for::<_, Panel<S>>(cx))
178 .child(Story::label(cx, "Default"))
179 .child(
180 Panel::new(ScrollState::default()).child(
181 div()
182 .overflow_y_scroll(ScrollState::default())
183 .children((0..100).map(|ix| Label::new(format!("Item {}", ix + 1)))),
184 ),
185 )
186 }
187 }
188}