workspace.rs

  1use std::sync::Arc;
  2
  3use chrono::DateTime;
  4use gpui2::{px, relative, rems, view, Context, Size, View};
  5
  6use crate::{prelude::*, NotificationsPanel};
  7use crate::{
  8    static_livestream, old_theme, user_settings_mut, v_stack, AssistantPanel, Button, ChatMessage,
  9    ChatPanel, CollabPanel, EditorPane, FakeSettings, Label, LanguageSelector, Pane, PaneGroup,
 10    Panel, PanelAllowedSides, PanelSide, ProjectPanel, SettingValue, SplitDirection, StatusBar,
 11    Terminal, TitleBar, Toast, ToastOrigin,
 12};
 13
 14#[derive(Clone)]
 15pub struct Gpui2UiDebug {
 16    pub in_livestream: bool,
 17    pub enable_user_settings: bool,
 18    pub show_toast: bool,
 19}
 20
 21impl Default for Gpui2UiDebug {
 22    fn default() -> Self {
 23        Self {
 24            in_livestream: false,
 25            enable_user_settings: false,
 26            show_toast: false,
 27        }
 28    }
 29}
 30
 31#[derive(Clone)]
 32pub struct Workspace {
 33    title_bar: View<TitleBar>,
 34    editor_1: View<EditorPane>,
 35    show_project_panel: bool,
 36    show_collab_panel: bool,
 37    show_chat_panel: bool,
 38    show_assistant_panel: bool,
 39    show_notifications_panel: bool,
 40    show_terminal: bool,
 41    show_debug: bool,
 42    show_language_selector: bool,
 43    debug: Gpui2UiDebug,
 44}
 45
 46impl Workspace {
 47    pub fn new(cx: &mut ViewContext<Self>) -> Self {
 48        Self {
 49            title_bar: TitleBar::view(cx, None),
 50            editor_1: EditorPane::view(cx),
 51            show_project_panel: true,
 52            show_collab_panel: false,
 53            show_chat_panel: false,
 54            show_assistant_panel: false,
 55            show_terminal: true,
 56            show_language_selector: false,
 57            show_debug: false,
 58            show_notifications_panel: true,
 59            debug: Gpui2UiDebug::default(),
 60        }
 61    }
 62
 63    pub fn is_project_panel_open(&self) -> bool {
 64        self.show_project_panel
 65    }
 66
 67    pub fn toggle_project_panel(&mut self, cx: &mut ViewContext<Self>) {
 68        self.show_project_panel = !self.show_project_panel;
 69
 70        self.show_collab_panel = false;
 71
 72        cx.notify();
 73    }
 74
 75    pub fn is_collab_panel_open(&self) -> bool {
 76        self.show_collab_panel
 77    }
 78
 79    pub fn toggle_collab_panel(&mut self) {
 80        self.show_collab_panel = !self.show_collab_panel;
 81
 82        self.show_project_panel = false;
 83    }
 84
 85    pub fn is_terminal_open(&self) -> bool {
 86        self.show_terminal
 87    }
 88
 89    pub fn toggle_terminal(&mut self, cx: &mut ViewContext<Self>) {
 90        self.show_terminal = !self.show_terminal;
 91
 92        cx.notify();
 93    }
 94
 95    pub fn is_chat_panel_open(&self) -> bool {
 96        self.show_chat_panel
 97    }
 98
 99    pub fn toggle_chat_panel(&mut self, cx: &mut ViewContext<Self>) {
100        self.show_chat_panel = !self.show_chat_panel;
101
102        self.show_assistant_panel = false;
103        self.show_notifications_panel = false;
104
105        cx.notify();
106    }
107
108    pub fn is_notifications_panel_open(&self) -> bool {
109        self.show_notifications_panel
110    }
111
112    pub fn toggle_notifications_panel(&mut self, cx: &mut ViewContext<Self>) {
113        self.show_notifications_panel = !self.show_notifications_panel;
114
115        self.show_chat_panel = false;
116        self.show_assistant_panel = false;
117
118        cx.notify();
119    }
120
121    pub fn is_assistant_panel_open(&self) -> bool {
122        self.show_assistant_panel
123    }
124
125    pub fn toggle_assistant_panel(&mut self, cx: &mut ViewContext<Self>) {
126        self.show_assistant_panel = !self.show_assistant_panel;
127
128        self.show_chat_panel = false;
129        self.show_notifications_panel = false;
130
131        cx.notify();
132    }
133
134    pub fn is_language_selector_open(&self) -> bool {
135        self.show_language_selector
136    }
137
138    pub fn toggle_language_selector(&mut self, cx: &mut ViewContext<Self>) {
139        self.show_language_selector = !self.show_language_selector;
140
141        cx.notify();
142    }
143
144    pub fn toggle_debug(&mut self, cx: &mut ViewContext<Self>) {
145        self.show_debug = !self.show_debug;
146
147        cx.notify();
148    }
149
150    pub fn debug_toggle_user_settings(&mut self, cx: &mut ViewContext<Self>) {
151        self.debug.enable_user_settings = !self.debug.enable_user_settings;
152
153        cx.notify();
154    }
155
156    pub fn debug_toggle_livestream(&mut self, cx: &mut ViewContext<Self>) {
157        self.debug.in_livestream = !self.debug.in_livestream;
158
159        self.title_bar = TitleBar::view(
160            cx,
161            Some(static_livestream()).filter(|_| self.debug.in_livestream),
162        );
163
164        cx.notify();
165    }
166
167    pub fn debug_toggle_toast(&mut self, cx: &mut ViewContext<Self>) {
168        self.debug.show_toast = !self.debug.show_toast;
169
170        cx.notify();
171    }
172
173    pub fn view(cx: &mut WindowContext) -> View<Self> {
174        view(cx.entity(|cx| Self::new(cx)), Self::render)
175    }
176
177    pub fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Element<ViewState = Self> {
178        let theme = old_theme(cx).clone();
179
180        // HACK: This should happen inside of `debug_toggle_user_settings`, but
181        // we don't have `cx.global::<FakeSettings>()` in event handlers at the moment.
182        // Need to talk with Nathan/Antonio about this.
183        {
184            let settings = user_settings_mut(cx);
185
186            if self.debug.enable_user_settings {
187                settings.list_indent_depth = SettingValue::UserDefined(rems(0.5).into());
188                settings.ui_scale = SettingValue::UserDefined(1.25);
189            } else {
190                *settings = FakeSettings::default();
191            }
192        }
193
194        let root_group = PaneGroup::new_panes(
195            vec![Pane::new(
196                "pane-0",
197                Size {
198                    width: relative(1.).into(),
199                    height: relative(1.).into(),
200                },
201            )
202            .child(self.editor_1.clone())],
203            SplitDirection::Horizontal,
204        );
205
206        div()
207            .relative()
208            .size_full()
209            .flex()
210            .flex_col()
211            .font("Zed Sans")
212            .gap_0()
213            .justify_start()
214            .items_start()
215            .text_color(theme.lowest.base.default.foreground)
216            .bg(theme.lowest.base.default.background)
217            .child(self.title_bar.clone())
218            .child(
219                div()
220                    .flex_1()
221                    .w_full()
222                    .flex()
223                    .flex_row()
224                    .overflow_hidden()
225                    .border_t()
226                    .border_b()
227                    .border_color(theme.lowest.base.default.border)
228                    .children(
229                        Some(
230                            Panel::new("project-panel-outer", cx)
231                                .side(PanelSide::Left)
232                                .child(ProjectPanel::new("project-panel-inner")),
233                        )
234                        .filter(|_| self.is_project_panel_open()),
235                    )
236                    .children(
237                        Some(
238                            Panel::new("collab-panel-outer", cx)
239                                .child(CollabPanel::new("collab-panel-inner"))
240                                .side(PanelSide::Left),
241                        )
242                        .filter(|_| self.is_collab_panel_open()),
243                    )
244                    // .child(NotificationToast::new(
245                    //     "maxbrunsfeld has requested to add you as a contact.".into(),
246                    // ))
247                    .child(
248                        v_stack()
249                            .flex_1()
250                            .h_full()
251                            .child(div().flex().flex_1().child(root_group))
252                            .children(
253                                Some(
254                                    Panel::new("terminal-panel", cx)
255                                        .child(Terminal::new())
256                                        .allowed_sides(PanelAllowedSides::BottomOnly)
257                                        .side(PanelSide::Bottom),
258                                )
259                                .filter(|_| self.is_terminal_open()),
260                            ),
261                    )
262                    .children(
263                        Some(
264                            Panel::new("chat-panel-outer", cx)
265                                .side(PanelSide::Right)
266                                .child(ChatPanel::new("chat-panel-inner").messages(vec![
267                                    ChatMessage::new(
268                                        "osiewicz".to_string(),
269                                        "is this thing on?".to_string(),
270                                        DateTime::parse_from_rfc3339("2023-09-27T15:40:52.707Z")
271                                            .unwrap()
272                                            .naive_local(),
273                                    ),
274                                    ChatMessage::new(
275                                        "maxdeviant".to_string(),
276                                        "Reading you loud and clear!".to_string(),
277                                        DateTime::parse_from_rfc3339("2023-09-28T15:40:52.707Z")
278                                            .unwrap()
279                                            .naive_local(),
280                                    ),
281                                ])),
282                        )
283                        .filter(|_| self.is_chat_panel_open()),
284                    )
285                    .children(
286                        Some(
287                            Panel::new("notifications-panel-outer", cx)
288                                .side(PanelSide::Right)
289                                .child(NotificationsPanel::new("notifications-panel-inner")),
290                        )
291                        .filter(|_| self.is_notifications_panel_open()),
292                    )
293                    .children(
294                        Some(
295                            Panel::new("assistant-panel-outer", cx)
296                                .child(AssistantPanel::new("assistant-panel-inner")),
297                        )
298                        .filter(|_| self.is_assistant_panel_open()),
299                    ),
300            )
301            .child(StatusBar::new())
302            .when(self.debug.show_toast, |this| {
303                this.child(Toast::new(ToastOrigin::Bottom).child(Label::new("A toast")))
304            })
305            .children(
306                Some(
307                    div()
308                        .absolute()
309                        .top(px(50.))
310                        .left(px(640.))
311                        .z_index(8)
312                        .child(LanguageSelector::new("language-selector")),
313                )
314                .filter(|_| self.is_language_selector_open()),
315            )
316            .z_index(8)
317            // Debug
318            .child(
319                v_stack()
320                    .z_index(9)
321                    .absolute()
322                    .bottom_10()
323                    .left_1_4()
324                    .w_40()
325                    .gap_2()
326                    .when(self.show_debug, |this| {
327                        this.child(Button::<Workspace>::new("Toggle User Settings").on_click(
328                            Arc::new(|workspace, cx| workspace.debug_toggle_user_settings(cx)),
329                        ))
330                        .child(
331                            Button::<Workspace>::new("Toggle Toasts").on_click(Arc::new(
332                                |workspace, cx| workspace.debug_toggle_toast(cx),
333                            )),
334                        )
335                        .child(
336                            Button::<Workspace>::new("Toggle Livestream").on_click(Arc::new(
337                                |workspace, cx| workspace.debug_toggle_livestream(cx),
338                            )),
339                        )
340                    })
341                    .child(
342                        Button::<Workspace>::new("Toggle Debug")
343                            .on_click(Arc::new(|workspace, cx| workspace.toggle_debug(cx))),
344                    ),
345            )
346    }
347}
348
349#[cfg(feature = "stories")]
350pub use stories::*;
351
352#[cfg(feature = "stories")]
353mod stories {
354    use super::*;
355
356    pub struct WorkspaceStory {
357        workspace: View<Workspace>,
358    }
359
360    impl WorkspaceStory {
361        pub fn view(cx: &mut WindowContext) -> View<Self> {
362            view(
363                cx.entity(|cx| Self {
364                    workspace: Workspace::view(cx),
365                }),
366                |view, cx| view.workspace.clone(),
367            )
368        }
369    }
370}