1use gpui_kit::{
8 AnyView, App, AppContext as _, Entity, EventEmitter, FocusHandle, Focusable, Pixels, Render,
9 SharedString, Size, Subscription, Window,
10};
11
12use crate::settings::SettingsEditorFactory;
13use crate::{HelpImage, KeyHelp};
14
15#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
17pub enum Placement {
18 Left,
19 #[default]
20 Bottom,
21 Right,
22}
23
24impl Placement {
25 pub const ALL: [Self; 3] = [Self::Left, Self::Bottom, Self::Right];
26
27 pub fn key(self) -> &'static str {
29 match self {
30 Self::Left => "left",
31 Self::Bottom => "bottom",
32 Self::Right => "right",
33 }
34 }
35
36 pub fn parse(value: &str) -> Option<Self> {
38 Self::ALL
39 .into_iter()
40 .find(|placement| placement.key() == value)
41 }
42
43 pub fn label(self) -> &'static str {
45 match self {
46 Self::Left => "Left",
47 Self::Bottom => "Bottom",
48 Self::Right => "Right",
49 }
50 }
51}
52
53#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
56pub enum BottomSpan {
57 #[default]
58 BesideChat,
59 FullWidth,
60}
61
62impl BottomSpan {
63 pub const ALL: [Self; 2] = [Self::BesideChat, Self::FullWidth];
64
65 pub fn key(self) -> &'static str {
67 match self {
68 Self::BesideChat => "beside-chat",
69 Self::FullWidth => "full-width",
70 }
71 }
72
73 pub fn parse(value: &str) -> Option<Self> {
75 Self::ALL.into_iter().find(|span| span.key() == value)
76 }
77
78 pub fn label(self) -> &'static str {
80 match self {
81 Self::BesideChat => "Beside the chat",
82 Self::FullWidth => "Full window width",
83 }
84 }
85}
86
87#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
89pub struct PanelLayout {
90 pub placement: Placement,
91 pub span: BottomSpan,
92 pub maximized: bool,
93 pub labels: bool,
95}
96
97#[derive(Debug, Clone, Copy, PartialEq, Eq)]
100pub enum DockPanelEvent {
101 Hide,
102 ToggleMaximized,
103 Move(Placement),
104 SetBottomSpan(BottomSpan),
105 SetLabels(bool),
107}
108
109pub trait DockPanel: Render + Focusable + EventEmitter<DockPanelEvent> {
112 fn show(&mut self, window: &mut Window, cx: &mut gpui_kit::Context<Self>);
115
116 fn set_layout(&mut self, layout: PanelLayout, cx: &mut gpui_kit::Context<Self>);
118
119 fn quit_warnings(&self, _cx: &App) -> Vec<SharedString> {
121 Vec::new()
122 }
123
124 fn status(&self, _cx: &App) -> Option<SharedString> {
127 None
128 }
129
130 fn minimum_size(&self, _cx: &App) -> Option<Size<Pixels>> {
133 None
134 }
135
136 fn reload(&mut self, _window: &mut Window, _cx: &mut gpui_kit::Context<Self>) {}
138}
139
140pub type DockEventSink = Box<dyn Fn(&DockPanelEvent, &mut Window, &mut App)>;
142
143trait AnyDockPanel {
144 fn view(&self) -> AnyView;
145 fn focus_handle(&self, cx: &App) -> FocusHandle;
146 fn show(&self, window: &mut Window, cx: &mut App);
147 fn set_layout(&self, layout: PanelLayout, cx: &mut App);
148 fn quit_warnings(&self, cx: &App) -> Vec<SharedString>;
149 fn status(&self, cx: &App) -> Option<SharedString>;
150 fn minimum_size(&self, cx: &App) -> Option<Size<Pixels>>;
151 fn reload(&self, window: &mut Window, cx: &mut App);
152 fn subscribe(&self, window: &mut Window, cx: &mut App, sink: DockEventSink) -> Subscription;
153}
154
155impl<P: DockPanel> AnyDockPanel for Entity<P> {
156 fn view(&self) -> AnyView {
157 self.clone().into()
158 }
159
160 fn focus_handle(&self, cx: &App) -> FocusHandle {
161 self.read(cx).focus_handle(cx)
162 }
163
164 fn show(&self, window: &mut Window, cx: &mut App) {
165 self.update(cx, |panel, cx| panel.show(window, cx));
166 }
167
168 fn set_layout(&self, layout: PanelLayout, cx: &mut App) {
169 self.update(cx, |panel, cx| panel.set_layout(layout, cx));
170 }
171
172 fn quit_warnings(&self, cx: &App) -> Vec<SharedString> {
173 self.read(cx).quit_warnings(cx)
174 }
175
176 fn status(&self, cx: &App) -> Option<SharedString> {
177 self.read(cx).status(cx)
178 }
179
180 fn minimum_size(&self, cx: &App) -> Option<Size<Pixels>> {
181 self.read(cx).minimum_size(cx)
182 }
183
184 fn reload(&self, window: &mut Window, cx: &mut App) {
185 self.update(cx, |panel, cx| panel.reload(window, cx));
186 }
187
188 fn subscribe(&self, window: &mut Window, cx: &mut App, sink: DockEventSink) -> Subscription {
189 window.subscribe(self, cx, move |_, event: &DockPanelEvent, window, cx| {
190 sink(event, window, cx);
191 })
192 }
193}
194
195pub struct DockPanelHandle {
197 inner: Box<dyn AnyDockPanel>,
198}
199
200impl DockPanelHandle {
201 pub fn new<P: DockPanel>(panel: Entity<P>) -> Self {
202 Self {
203 inner: Box::new(panel),
204 }
205 }
206
207 pub fn create<P: DockPanel>(
209 cx: &mut App,
210 create: impl FnOnce(&mut gpui_kit::Context<P>) -> P,
211 ) -> Self {
212 Self::new(cx.new(create))
213 }
214
215 pub fn view(&self) -> AnyView {
216 self.inner.view()
217 }
218
219 pub fn focus_handle(&self, cx: &App) -> FocusHandle {
220 self.inner.focus_handle(cx)
221 }
222
223 pub fn show(&self, window: &mut Window, cx: &mut App) {
225 self.inner.show(window, cx);
226 }
227
228 pub fn set_layout(&self, layout: PanelLayout, cx: &mut App) {
229 self.inner.set_layout(layout, cx);
230 }
231
232 pub fn quit_warnings(&self, cx: &App) -> Vec<SharedString> {
233 self.inner.quit_warnings(cx)
234 }
235
236 pub fn status(&self, cx: &App) -> Option<SharedString> {
237 self.inner.status(cx)
238 }
239
240 pub fn minimum_size(&self, cx: &App) -> Option<Size<Pixels>> {
241 self.inner.minimum_size(cx)
242 }
243
244 pub fn reload(&self, window: &mut Window, cx: &mut App) {
245 self.inner.reload(window, cx);
246 }
247
248 pub fn subscribe(
249 &self,
250 window: &mut Window,
251 cx: &mut App,
252 sink: DockEventSink,
253 ) -> Subscription {
254 self.inner.subscribe(window, cx, sink)
255 }
256}
257
258#[derive(Clone, Copy)]
262pub struct DockPanelRegistration {
263 pub create: fn(&mut Window, &mut App) -> DockPanelHandle,
264 pub bind_keys: fn(&mut App),
266 pub help_markdown: &'static str,
267 pub help_images: &'static [HelpImage],
268 pub keys: &'static [KeyHelp],
269 pub settings_editor: Option<SettingsEditorFactory>,
270}
271
272#[cfg(test)]
273mod tests {
274 use super::{BottomSpan, Placement};
275
276 #[test]
277 fn placements_and_spans_round_trip_their_stored_values() {
278 for placement in Placement::ALL {
279 assert_eq!(Placement::parse(placement.key()), Some(placement));
280 }
281 for span in BottomSpan::ALL {
282 assert_eq!(BottomSpan::parse(span.key()), Some(span));
283 }
284 assert_eq!(Placement::parse("log-tab"), None);
285 assert_eq!(Placement::default(), Placement::Bottom);
286 assert_eq!(BottomSpan::default(), BottomSpan::BesideChat);
287 }
288}