workspace_view.rs

  1use super::{pane, Pane, PaneGroup, SplitDirection, Workspace};
  2use crate::{settings::Settings, watch};
  3use futures_core::future::LocalBoxFuture;
  4use gpui::{
  5    color::rgbu, elements::*, json::to_string_pretty, keymap::Binding, AnyViewHandle, AppContext,
  6    ClipboardItem, Entity, ModelHandle, MutableAppContext, View, ViewContext, ViewHandle,
  7};
  8use log::{error, info};
  9use std::{collections::HashSet, path::PathBuf};
 10
 11pub fn init(app: &mut MutableAppContext) {
 12    app.add_action("workspace:save", WorkspaceView::save_active_item);
 13    app.add_action("workspace:debug_elements", WorkspaceView::debug_elements);
 14    app.add_bindings(vec![
 15        Binding::new("cmd-s", "workspace:save", None),
 16        Binding::new("cmd-alt-i", "workspace:debug_elements", None),
 17    ]);
 18}
 19
 20pub trait ItemView: View {
 21    fn title(&self, app: &AppContext) -> String;
 22    fn entry_id(&self, app: &AppContext) -> Option<(usize, usize)>;
 23    fn clone_on_split(&self, _: &mut ViewContext<Self>) -> Option<Self>
 24    where
 25        Self: Sized,
 26    {
 27        None
 28    }
 29    fn is_dirty(&self, _: &AppContext) -> bool {
 30        false
 31    }
 32    fn save(&self, _: &mut ViewContext<Self>) -> LocalBoxFuture<'static, anyhow::Result<()>> {
 33        Box::pin(async { Ok(()) })
 34    }
 35    fn should_activate_item_on_event(_: &Self::Event) -> bool {
 36        false
 37    }
 38    fn should_update_tab_on_event(_: &Self::Event) -> bool {
 39        false
 40    }
 41}
 42
 43pub trait ItemViewHandle: Send + Sync {
 44    fn title(&self, app: &AppContext) -> String;
 45    fn entry_id(&self, app: &AppContext) -> Option<(usize, usize)>;
 46    fn boxed_clone(&self) -> Box<dyn ItemViewHandle>;
 47    fn clone_on_split(&self, app: &mut MutableAppContext) -> Option<Box<dyn ItemViewHandle>>;
 48    fn set_parent_pane(&self, pane: &ViewHandle<Pane>, app: &mut MutableAppContext);
 49    fn id(&self) -> usize;
 50    fn to_any(&self) -> AnyViewHandle;
 51    fn is_dirty(&self, ctx: &AppContext) -> bool;
 52    fn save(&self, ctx: &mut MutableAppContext) -> LocalBoxFuture<'static, anyhow::Result<()>>;
 53}
 54
 55impl<T: ItemView> ItemViewHandle for ViewHandle<T> {
 56    fn title(&self, app: &AppContext) -> String {
 57        self.read(app).title(app)
 58    }
 59
 60    fn entry_id(&self, app: &AppContext) -> Option<(usize, usize)> {
 61        self.read(app).entry_id(app)
 62    }
 63
 64    fn boxed_clone(&self) -> Box<dyn ItemViewHandle> {
 65        Box::new(self.clone())
 66    }
 67
 68    fn clone_on_split(&self, app: &mut MutableAppContext) -> Option<Box<dyn ItemViewHandle>> {
 69        self.update(app, |item, ctx| {
 70            ctx.add_option_view(|ctx| item.clone_on_split(ctx))
 71        })
 72        .map(|handle| Box::new(handle) as Box<dyn ItemViewHandle>)
 73    }
 74
 75    fn set_parent_pane(&self, pane: &ViewHandle<Pane>, app: &mut MutableAppContext) {
 76        pane.update(app, |_, ctx| {
 77            ctx.subscribe_to_view(self, |pane, item, event, ctx| {
 78                if T::should_activate_item_on_event(event) {
 79                    if let Some(ix) = pane.item_index(&item) {
 80                        pane.activate_item(ix, ctx);
 81                        pane.activate(ctx);
 82                    }
 83                }
 84                if T::should_update_tab_on_event(event) {
 85                    ctx.notify()
 86                }
 87            })
 88        })
 89    }
 90
 91    fn save(&self, ctx: &mut MutableAppContext) -> LocalBoxFuture<'static, anyhow::Result<()>> {
 92        self.update(ctx, |item, ctx| item.save(ctx))
 93    }
 94
 95    fn is_dirty(&self, ctx: &AppContext) -> bool {
 96        self.read(ctx).is_dirty(ctx)
 97    }
 98
 99    fn id(&self) -> usize {
100        self.id()
101    }
102
103    fn to_any(&self) -> AnyViewHandle {
104        self.into()
105    }
106}
107
108impl Clone for Box<dyn ItemViewHandle> {
109    fn clone(&self) -> Box<dyn ItemViewHandle> {
110        self.boxed_clone()
111    }
112}
113
114#[derive(Debug)]
115pub struct State {
116    pub modal: Option<usize>,
117    pub center: PaneGroup,
118}
119
120pub struct WorkspaceView {
121    pub workspace: ModelHandle<Workspace>,
122    pub settings: watch::Receiver<Settings>,
123    modal: Option<AnyViewHandle>,
124    center: PaneGroup,
125    panes: Vec<ViewHandle<Pane>>,
126    active_pane: ViewHandle<Pane>,
127    loading_entries: HashSet<(usize, usize)>,
128}
129
130impl WorkspaceView {
131    pub fn new(
132        workspace: ModelHandle<Workspace>,
133        settings: watch::Receiver<Settings>,
134        ctx: &mut ViewContext<Self>,
135    ) -> Self {
136        ctx.observe(&workspace, Self::workspace_updated);
137
138        let pane = ctx.add_view(|_| Pane::new(settings.clone()));
139        let pane_id = pane.id();
140        ctx.subscribe_to_view(&pane, move |me, _, event, ctx| {
141            me.handle_pane_event(pane_id, event, ctx)
142        });
143        ctx.focus(&pane);
144
145        WorkspaceView {
146            workspace,
147            modal: None,
148            center: PaneGroup::new(pane.id()),
149            panes: vec![pane.clone()],
150            active_pane: pane.clone(),
151            loading_entries: HashSet::new(),
152            settings,
153        }
154    }
155
156    pub fn contains_paths(&self, paths: &[PathBuf], app: &AppContext) -> bool {
157        self.workspace.read(app).contains_paths(paths, app)
158    }
159
160    pub fn open_paths(&self, paths: &[PathBuf], app: &mut MutableAppContext) {
161        self.workspace
162            .update(app, |workspace, ctx| workspace.open_paths(paths, ctx));
163    }
164
165    pub fn toggle_modal<V, F>(&mut self, ctx: &mut ViewContext<Self>, add_view: F)
166    where
167        V: 'static + View,
168        F: FnOnce(&mut ViewContext<Self>, &mut Self) -> ViewHandle<V>,
169    {
170        if self.modal.as_ref().map_or(false, |modal| modal.is::<V>()) {
171            self.modal.take();
172            ctx.focus_self();
173        } else {
174            let modal = add_view(ctx, self);
175            ctx.focus(&modal);
176            self.modal = Some(modal.into());
177        }
178        ctx.notify();
179    }
180
181    pub fn modal(&self) -> Option<&AnyViewHandle> {
182        self.modal.as_ref()
183    }
184
185    pub fn dismiss_modal(&mut self, ctx: &mut ViewContext<Self>) {
186        if self.modal.take().is_some() {
187            ctx.focus(&self.active_pane);
188            ctx.notify();
189        }
190    }
191
192    pub fn open_entry(&mut self, entry: (usize, usize), ctx: &mut ViewContext<Self>) {
193        if self.loading_entries.contains(&entry) {
194            return;
195        }
196
197        if self
198            .active_pane()
199            .update(ctx, |pane, ctx| pane.activate_entry(entry, ctx))
200        {
201            return;
202        }
203
204        self.loading_entries.insert(entry);
205
206        match self
207            .workspace
208            .update(ctx, |workspace, ctx| workspace.open_entry(entry, ctx))
209        {
210            Err(error) => error!("{}", error),
211            Ok(item) => {
212                let settings = self.settings.clone();
213                ctx.spawn(item, move |me, item, ctx| {
214                    me.loading_entries.remove(&entry);
215                    match item {
216                        Ok(item) => {
217                            let item_view = item.add_view(ctx.window_id(), settings, ctx.as_mut());
218                            me.add_item(item_view, ctx);
219                        }
220                        Err(error) => {
221                            error!("{}", error);
222                        }
223                    }
224                })
225                .detach();
226            }
227        }
228    }
229
230    pub fn open_example_entry(&mut self, ctx: &mut ViewContext<Self>) {
231        if let Some(tree) = self.workspace.read(ctx).worktrees().iter().next() {
232            if let Some(file) = tree.read(ctx).files().next() {
233                info!("open_entry ({}, {})", tree.id(), file.entry_id);
234                self.open_entry((tree.id(), file.entry_id), ctx);
235            } else {
236                error!("No example file found for worktree {}", tree.id());
237            }
238        } else {
239            error!("No worktree found while opening example entry");
240        }
241    }
242
243    pub fn save_active_item(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
244        self.active_pane.update(ctx, |pane, ctx| {
245            if let Some(item) = pane.active_item() {
246                let task = item.save(ctx.as_mut());
247                ctx.spawn(task, |_, result, _| {
248                    if let Err(e) = result {
249                        // TODO - present this error to the user
250                        error!("failed to save item: {:?}, ", e);
251                    }
252                })
253                .detach()
254            }
255        });
256    }
257
258    pub fn debug_elements(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
259        match to_string_pretty(&ctx.debug_elements()) {
260            Ok(json) => {
261                let kib = json.len() as f32 / 1024.;
262                ctx.as_mut().write_to_clipboard(ClipboardItem::new(json));
263                log::info!(
264                    "copied {:.1} KiB of element debug JSON to the clipboard",
265                    kib
266                );
267            }
268            Err(error) => {
269                log::error!("error debugging elements: {}", error);
270            }
271        };
272    }
273
274    fn workspace_updated(&mut self, _: ModelHandle<Workspace>, ctx: &mut ViewContext<Self>) {
275        ctx.notify();
276    }
277
278    fn add_pane(&mut self, ctx: &mut ViewContext<Self>) -> ViewHandle<Pane> {
279        let pane = ctx.add_view(|_| Pane::new(self.settings.clone()));
280        let pane_id = pane.id();
281        ctx.subscribe_to_view(&pane, move |me, _, event, ctx| {
282            me.handle_pane_event(pane_id, event, ctx)
283        });
284        self.panes.push(pane.clone());
285        self.activate_pane(pane.clone(), ctx);
286        pane
287    }
288
289    fn activate_pane(&mut self, pane: ViewHandle<Pane>, ctx: &mut ViewContext<Self>) {
290        self.active_pane = pane;
291        ctx.focus(&self.active_pane);
292        ctx.notify();
293    }
294
295    fn handle_pane_event(
296        &mut self,
297        pane_id: usize,
298        event: &pane::Event,
299        ctx: &mut ViewContext<Self>,
300    ) {
301        if let Some(pane) = self.pane(pane_id) {
302            match event {
303                pane::Event::Split(direction) => {
304                    self.split_pane(pane, *direction, ctx);
305                }
306                pane::Event::Remove => {
307                    self.remove_pane(pane, ctx);
308                }
309                pane::Event::Activate => {
310                    self.activate_pane(pane, ctx);
311                }
312            }
313        } else {
314            error!("pane {} not found", pane_id);
315        }
316    }
317
318    fn split_pane(
319        &mut self,
320        pane: ViewHandle<Pane>,
321        direction: SplitDirection,
322        ctx: &mut ViewContext<Self>,
323    ) -> ViewHandle<Pane> {
324        let new_pane = self.add_pane(ctx);
325        self.activate_pane(new_pane.clone(), ctx);
326        if let Some(item) = pane.read(ctx).active_item() {
327            if let Some(clone) = item.clone_on_split(ctx.as_mut()) {
328                self.add_item(clone, ctx);
329            }
330        }
331        self.center
332            .split(pane.id(), new_pane.id(), direction)
333            .unwrap();
334        ctx.notify();
335        new_pane
336    }
337
338    fn remove_pane(&mut self, pane: ViewHandle<Pane>, ctx: &mut ViewContext<Self>) {
339        if self.center.remove(pane.id()).unwrap() {
340            self.panes.retain(|p| p != &pane);
341            self.activate_pane(self.panes.last().unwrap().clone(), ctx);
342        }
343    }
344
345    fn pane(&self, pane_id: usize) -> Option<ViewHandle<Pane>> {
346        self.panes.iter().find(|pane| pane.id() == pane_id).cloned()
347    }
348
349    pub fn active_pane(&self) -> &ViewHandle<Pane> {
350        &self.active_pane
351    }
352
353    fn add_item(&self, item: Box<dyn ItemViewHandle>, ctx: &mut ViewContext<Self>) {
354        let active_pane = self.active_pane();
355        item.set_parent_pane(&active_pane, ctx.as_mut());
356        active_pane.update(ctx, |pane, ctx| {
357            let item_idx = pane.add_item(item, ctx);
358            pane.activate_item(item_idx, ctx);
359        });
360    }
361}
362
363impl Entity for WorkspaceView {
364    type Event = ();
365}
366
367impl View for WorkspaceView {
368    fn ui_name() -> &'static str {
369        "Workspace"
370    }
371
372    fn render(&self, _: &AppContext) -> ElementBox {
373        Container::new(
374            // self.center.render(bump)
375            Stack::new()
376                .with_child(self.center.render())
377                .with_children(self.modal.as_ref().map(|m| ChildView::new(m.id()).boxed()))
378                .boxed(),
379        )
380        .with_background_color(rgbu(0xea, 0xea, 0xeb))
381        .named("workspace")
382    }
383
384    fn on_focus(&mut self, ctx: &mut ViewContext<Self>) {
385        ctx.focus(&self.active_pane);
386    }
387}
388
389#[cfg(test)]
390mod tests {
391    use super::{pane, Workspace, WorkspaceView};
392    use crate::{settings, test::temp_tree, workspace::WorkspaceHandle as _};
393    use gpui::App;
394    use serde_json::json;
395
396    #[test]
397    fn test_open_entry() {
398        App::test_async((), |mut app| async move {
399            let dir = temp_tree(json!({
400                "a": {
401                    "aa": "aa contents",
402                    "ab": "ab contents",
403                    "ac": "ab contents",
404                },
405            }));
406
407            let settings = settings::channel(&app.font_cache()).unwrap().1;
408            let workspace = app.add_model(|ctx| Workspace::new(vec![dir.path().into()], ctx));
409            app.finish_pending_tasks().await; // Open and populate worktree.
410            let entries = app.read(|ctx| workspace.file_entries(ctx));
411
412            let (_, workspace_view) =
413                app.add_window(|ctx| WorkspaceView::new(workspace.clone(), settings, ctx));
414
415            // Open the first entry
416            workspace_view.update(&mut app, |w, ctx| w.open_entry(entries[0], ctx));
417            app.finish_pending_tasks().await;
418
419            app.read(|ctx| {
420                assert_eq!(
421                    workspace_view
422                        .read(ctx)
423                        .active_pane()
424                        .read(ctx)
425                        .items()
426                        .len(),
427                    1
428                )
429            });
430
431            // Open the second entry
432            workspace_view.update(&mut app, |w, ctx| w.open_entry(entries[1], ctx));
433            app.finish_pending_tasks().await;
434
435            app.read(|ctx| {
436                let active_pane = workspace_view.read(ctx).active_pane().read(ctx);
437                assert_eq!(active_pane.items().len(), 2);
438                assert_eq!(
439                    active_pane.active_item().unwrap().entry_id(ctx),
440                    Some(entries[1])
441                );
442            });
443
444            // Open the first entry again
445            workspace_view.update(&mut app, |w, ctx| w.open_entry(entries[0], ctx));
446            app.finish_pending_tasks().await;
447
448            app.read(|ctx| {
449                let active_pane = workspace_view.read(ctx).active_pane().read(ctx);
450                assert_eq!(active_pane.items().len(), 2);
451                assert_eq!(
452                    active_pane.active_item().unwrap().entry_id(ctx),
453                    Some(entries[0])
454                );
455            });
456
457            // Open the third entry twice concurrently
458            workspace_view.update(&mut app, |w, ctx| {
459                w.open_entry(entries[2], ctx);
460                w.open_entry(entries[2], ctx);
461            });
462            app.finish_pending_tasks().await;
463
464            app.read(|ctx| {
465                assert_eq!(
466                    workspace_view
467                        .read(ctx)
468                        .active_pane()
469                        .read(ctx)
470                        .items()
471                        .len(),
472                    3
473                );
474            });
475        });
476    }
477
478    #[test]
479    fn test_pane_actions() {
480        App::test_async((), |mut app| async move {
481            app.update(|ctx| pane::init(ctx));
482
483            let dir = temp_tree(json!({
484                "a": {
485                    "aa": "aa contents",
486                    "ab": "ab contents",
487                    "ac": "ab contents",
488                },
489            }));
490
491            let settings = settings::channel(&app.font_cache()).unwrap().1;
492            let workspace = app.add_model(|ctx| Workspace::new(vec![dir.path().into()], ctx));
493            app.finish_pending_tasks().await; // Open and populate worktree.
494            let entries = app.read(|ctx| workspace.file_entries(ctx));
495
496            let (window_id, workspace_view) =
497                app.add_window(|ctx| WorkspaceView::new(workspace.clone(), settings, ctx));
498
499            workspace_view.update(&mut app, |w, ctx| w.open_entry(entries[0], ctx));
500            app.finish_pending_tasks().await;
501
502            let pane_1 = app.read(|ctx| workspace_view.read(ctx).active_pane().clone());
503
504            app.dispatch_action(window_id, vec![pane_1.id()], "pane:split_right", ());
505            app.update(|ctx| {
506                let pane_2 = workspace_view.read(ctx).active_pane().clone();
507                assert_ne!(pane_1, pane_2);
508
509                assert_eq!(
510                    pane_2
511                        .read(ctx)
512                        .active_item()
513                        .unwrap()
514                        .entry_id(ctx.as_ref()),
515                    Some(entries[0])
516                );
517
518                ctx.dispatch_action(window_id, vec![pane_2.id()], "pane:close_active_item", ());
519
520                let w = workspace_view.read(ctx);
521                assert_eq!(w.panes.len(), 1);
522                assert_eq!(w.active_pane(), &pane_1);
523            });
524        });
525    }
526}