Skip to main content

subspace_dashboard/
dock_panel.rs

1//! One product-composed panel that the shell docks beside every Space, such
2//! as the terminal. The panel owns its content and its header; the shell owns
3//! whether it shows, where it docks, its size and whether it fills the
4//! workspace, and tells the panel through [`PanelLayout`]. The panel asks the
5//! shell for changes with [`DockPanelEvent`]s.
6
7use 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/// The window edge the panel docks against.
16#[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    /// The stored value.
28    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    /// The stored value read back; anything else is `None`.
37    pub fn parse(value: &str) -> Option<Self> {
38        Self::ALL
39            .into_iter()
40            .find(|placement| placement.key() == value)
41    }
42
43    /// The name a menu or a select shows.
44    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/// How wide a bottom panel is: beside the chat, which keeps the full body
54/// height, or across the whole window below the Space and the chat.
55#[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    /// The stored value.
66    pub fn key(self) -> &'static str {
67        match self {
68            Self::BesideChat => "beside-chat",
69            Self::FullWidth => "full-width",
70        }
71    }
72
73    /// The stored value read back; anything else is `None`.
74    pub fn parse(value: &str) -> Option<Self> {
75        Self::ALL.into_iter().find(|span| span.key() == value)
76    }
77
78    /// The name a select shows.
79    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/// What the shell shows, for the panel's latched header buttons.
88#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
89pub struct PanelLayout {
90    pub placement: Placement,
91    pub span: BottomSpan,
92    pub maximized: bool,
93    /// Whether the header's layout buttons show their text.
94    pub labels: bool,
95}
96
97/// What a dock panel asks of the shell. The shell applies each request at
98/// once, stores it and sends the panel the new [`PanelLayout`].
99#[derive(Debug, Clone, Copy, PartialEq, Eq)]
100pub enum DockPanelEvent {
101    Hide,
102    ToggleMaximized,
103    Move(Placement),
104    SetBottomSpan(BottomSpan),
105    /// Show or hide the text of the header's layout buttons.
106    SetLabels(bool),
107}
108
109/// A panel the shell docks. Created once per window by its registration's
110/// `create`; it keeps running while hidden.
111pub trait DockPanel: Render + Focusable + EventEmitter<DockPanelEvent> {
112    /// The panel is shown: start its content when this is the first time and
113    /// focus it.
114    fn show(&mut self, window: &mut Window, cx: &mut gpui_kit::Context<Self>);
115
116    /// The shell's current layout, for the header's latched buttons.
117    fn set_layout(&mut self, layout: PanelLayout, cx: &mut gpui_kit::Context<Self>);
118
119    /// Running work that quitting would end, named for the quit confirmation.
120    fn quit_warnings(&self, _cx: &App) -> Vec<SharedString> {
121        Vec::new()
122    }
123
124    /// A short status the shell adds to the status bar, such as configuration
125    /// warnings.
126    fn status(&self, _cx: &App) -> Option<SharedString> {
127        None
128    }
129
130    /// The least size the panel's content needs. The shell keeps the Space
131    /// at its own minimum beside it.
132    fn minimum_size(&self, _cx: &App) -> Option<Size<Pixels>> {
133        None
134    }
135
136    /// Reload the panel's configuration, as Reload does while it has focus.
137    fn reload(&mut self, _window: &mut Window, _cx: &mut gpui_kit::Context<Self>) {}
138}
139
140/// Receives a dock panel's events in the shell.
141pub 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
195/// A created dock panel, with its type erased for the shell.
196pub 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    /// Creates `P` with `create` and wraps it.
208    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    /// See [`DockPanel::show`].
224    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/// What a product supplies so the shell can dock its panel: how to create
259/// it, its keys, its help guide and its settings editor. The editor edits the
260/// config file's `terminal` object.
261#[derive(Clone, Copy)]
262pub struct DockPanelRegistration {
263    pub create: fn(&mut Window, &mut App) -> DockPanelHandle,
264    /// Binds the panel's own keys, once.
265    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}