1use gpui::{prelude::*, 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 .map(|this| match self.current_side {
102 PanelSide::Left | PanelSide::Right => this.h_full().w(current_size),
103 PanelSide::Bottom => this,
104 })
105 .map(|this| match self.current_side {
106 PanelSide::Left => this.border_r(),
107 PanelSide::Right => this.border_l(),
108 PanelSide::Bottom => this.border_b().w_full().h(current_size),
109 })
110 .bg(cx.theme().colors().surface_background)
111 .border_color(cx.theme().colors().border)
112 .children(self.children)
113 }
114}
115
116impl<V: 'static> ParentComponent<V> for Panel<V> {
117 fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
118 &mut self.children
119 }
120}
121
122#[cfg(feature = "stories")]
123pub use stories::*;
124
125#[cfg(feature = "stories")]
126mod stories {
127 use super::*;
128 use crate::{Label, Story};
129 use gpui::{Div, InteractiveComponent, Render};
130
131 pub struct PanelStory;
132
133 impl Render for PanelStory {
134 type Element = Div<Self>;
135
136 fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
137 Story::container(cx)
138 .child(Story::title_for::<_, Panel<Self>>(cx))
139 .child(Story::label(cx, "Default"))
140 .child(
141 Panel::new("panel", cx).child(
142 div()
143 .id("panel-contents")
144 .overflow_y_scroll()
145 .children((0..100).map(|ix| Label::new(format!("Item {}", ix + 1)))),
146 ),
147 )
148 }
149 }
150}