dock.rs

  1use collections::HashMap;
  2use gpui::{
  3    actions,
  4    elements::{ChildView, Container, Empty, MouseEventHandler, ParentElement, Side, Stack, Svg},
  5    impl_internal_actions, Border, CursorStyle, Element, ElementBox, Entity, MouseButton,
  6    MutableAppContext, RenderContext, View, ViewContext, ViewHandle, WeakViewHandle,
  7};
  8use serde::Deserialize;
  9use settings::{DockAnchor, Settings};
 10use theme::Theme;
 11
 12use crate::{
 13    handle_dropped_item, sidebar::SidebarSide, ItemHandle, Pane, StatusItemView, Workspace,
 14};
 15
 16#[derive(PartialEq, Clone, Deserialize)]
 17pub struct MoveDock(pub DockAnchor);
 18
 19#[derive(PartialEq, Clone)]
 20pub struct AddDefaultItemToDock;
 21
 22actions!(
 23    dock,
 24    [
 25        FocusDock,
 26        HideDock,
 27        AnchorDockRight,
 28        AnchorDockBottom,
 29        ExpandDock,
 30        MoveActiveItemToDock,
 31    ]
 32);
 33impl_internal_actions!(dock, [MoveDock, AddDefaultItemToDock]);
 34
 35pub fn init(cx: &mut MutableAppContext) {
 36    cx.add_action(Dock::focus_dock);
 37    cx.add_action(Dock::hide_dock);
 38    cx.add_action(Dock::move_dock);
 39    cx.add_action(
 40        |workspace: &mut Workspace, _: &AnchorDockRight, cx: &mut ViewContext<Workspace>| {
 41            Dock::move_dock(workspace, &MoveDock(DockAnchor::Right), cx)
 42        },
 43    );
 44    cx.add_action(
 45        |workspace: &mut Workspace, _: &AnchorDockBottom, cx: &mut ViewContext<Workspace>| {
 46            Dock::move_dock(workspace, &MoveDock(DockAnchor::Bottom), cx)
 47        },
 48    );
 49    cx.add_action(
 50        |workspace: &mut Workspace, _: &ExpandDock, cx: &mut ViewContext<Workspace>| {
 51            Dock::move_dock(workspace, &MoveDock(DockAnchor::Expanded), cx)
 52        },
 53    );
 54    cx.add_action(
 55        |workspace: &mut Workspace, _: &MoveActiveItemToDock, cx: &mut ViewContext<Workspace>| {
 56            if let Some(active_item) = workspace.active_item(cx) {
 57                let item_id = active_item.id();
 58
 59                let from = workspace.active_pane();
 60                let to = workspace.dock_pane();
 61                if from.id() == to.id() {
 62                    return;
 63                }
 64
 65                let destination_index = to.read(cx).items_len() + 1;
 66
 67                Pane::move_item(
 68                    workspace,
 69                    from.clone(),
 70                    to.clone(),
 71                    item_id,
 72                    destination_index,
 73                    cx,
 74                );
 75            }
 76        },
 77    );
 78}
 79
 80#[derive(Copy, Clone, PartialEq, Eq, Debug)]
 81pub enum DockPosition {
 82    Shown(DockAnchor),
 83    Hidden(DockAnchor),
 84}
 85
 86impl Default for DockPosition {
 87    fn default() -> Self {
 88        DockPosition::Hidden(Default::default())
 89    }
 90}
 91
 92pub fn icon_for_dock_anchor(anchor: DockAnchor) -> &'static str {
 93    match anchor {
 94        DockAnchor::Right => "icons/dock_right_12.svg",
 95        DockAnchor::Bottom => "icons/dock_bottom_12.svg",
 96        DockAnchor::Expanded => "icons/dock_modal_12.svg",
 97    }
 98}
 99
100impl DockPosition {
101    pub fn is_visible(&self) -> bool {
102        match self {
103            DockPosition::Shown(_) => true,
104            DockPosition::Hidden(_) => false,
105        }
106    }
107
108    pub fn anchor(&self) -> DockAnchor {
109        match self {
110            DockPosition::Shown(anchor) | DockPosition::Hidden(anchor) => *anchor,
111        }
112    }
113
114    fn hide(self) -> Self {
115        match self {
116            DockPosition::Shown(anchor) => DockPosition::Hidden(anchor),
117            DockPosition::Hidden(_) => self,
118        }
119    }
120
121    fn show(self) -> Self {
122        match self {
123            DockPosition::Hidden(anchor) => DockPosition::Shown(anchor),
124            DockPosition::Shown(_) => self,
125        }
126    }
127}
128
129pub type DefaultItemFactory =
130    fn(&mut Workspace, &mut ViewContext<Workspace>) -> Box<dyn ItemHandle>;
131
132pub struct Dock {
133    position: DockPosition,
134    panel_sizes: HashMap<DockAnchor, f32>,
135    pane: ViewHandle<Pane>,
136    default_item_factory: DefaultItemFactory,
137}
138
139impl Dock {
140    pub fn new(default_item_factory: DefaultItemFactory, cx: &mut ViewContext<Workspace>) -> Self {
141        let position = DockPosition::Hidden(cx.global::<Settings>().default_dock_anchor);
142
143        let pane = cx.add_view(|cx| Pane::new(Some(position.anchor()), cx));
144        pane.update(cx, |pane, cx| {
145            pane.set_active(false, cx);
146        });
147        let pane_id = pane.id();
148        cx.subscribe(&pane, move |workspace, _, event, cx| {
149            workspace.handle_pane_event(pane_id, event, cx);
150        })
151        .detach();
152
153        Self {
154            pane,
155            panel_sizes: Default::default(),
156            position,
157            default_item_factory,
158        }
159    }
160
161    pub fn pane(&self) -> &ViewHandle<Pane> {
162        &self.pane
163    }
164
165    pub fn visible_pane(&self) -> Option<&ViewHandle<Pane>> {
166        self.position.is_visible().then(|| self.pane())
167    }
168
169    pub fn is_anchored_at(&self, anchor: DockAnchor) -> bool {
170        self.position.is_visible() && self.position.anchor() == anchor
171    }
172
173    pub(crate) fn set_dock_position(
174        workspace: &mut Workspace,
175        new_position: DockPosition,
176        cx: &mut ViewContext<Workspace>,
177    ) {
178        workspace.dock.position = new_position;
179        // Tell the pane about the new anchor position
180        workspace.dock.pane.update(cx, |pane, cx| {
181            pane.set_docked(Some(new_position.anchor()), cx)
182        });
183
184        if workspace.dock.position.is_visible() {
185            // Close the right sidebar if the dock is on the right side and the right sidebar is open
186            if workspace.dock.position.anchor() == DockAnchor::Right {
187                if workspace.right_sidebar().read(cx).is_open() {
188                    workspace.toggle_sidebar(SidebarSide::Right, cx);
189                }
190            }
191
192            // Ensure that the pane has at least one item or construct a default item to put in it
193            let pane = workspace.dock.pane.clone();
194            if pane.read(cx).items().next().is_none() {
195                let item_to_add = (workspace.dock.default_item_factory)(workspace, cx);
196                // Adding the item focuses the pane by default
197                Pane::add_item(workspace, &pane, item_to_add, true, true, None, cx);
198            } else {
199                cx.focus(pane);
200            }
201        } else if let Some(last_active_center_pane) = workspace
202            .last_active_center_pane
203            .as_ref()
204            .and_then(|pane| pane.upgrade(cx))
205        {
206            cx.focus(last_active_center_pane);
207        }
208        cx.emit(crate::Event::DockAnchorChanged);
209        workspace.serialize_workspace(cx);
210        cx.notify();
211    }
212
213    pub fn hide(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
214        Self::set_dock_position(workspace, workspace.dock.position.hide(), cx);
215    }
216
217    pub fn show(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
218        Self::set_dock_position(workspace, workspace.dock.position.show(), cx);
219    }
220
221    pub fn hide_on_sidebar_shown(
222        workspace: &mut Workspace,
223        sidebar_side: SidebarSide,
224        cx: &mut ViewContext<Workspace>,
225    ) {
226        if (sidebar_side == SidebarSide::Right && workspace.dock.is_anchored_at(DockAnchor::Right))
227            || workspace.dock.is_anchored_at(DockAnchor::Expanded)
228        {
229            Self::hide(workspace, cx);
230        }
231    }
232
233    fn focus_dock(workspace: &mut Workspace, _: &FocusDock, cx: &mut ViewContext<Workspace>) {
234        Self::set_dock_position(workspace, workspace.dock.position.show(), cx);
235    }
236
237    fn hide_dock(workspace: &mut Workspace, _: &HideDock, cx: &mut ViewContext<Workspace>) {
238        Self::set_dock_position(workspace, workspace.dock.position.hide(), cx);
239    }
240
241    fn move_dock(
242        workspace: &mut Workspace,
243        &MoveDock(new_anchor): &MoveDock,
244        cx: &mut ViewContext<Workspace>,
245    ) {
246        Self::set_dock_position(workspace, DockPosition::Shown(new_anchor), cx);
247    }
248
249    pub fn render(
250        &self,
251        theme: &Theme,
252        anchor: DockAnchor,
253        cx: &mut RenderContext<Workspace>,
254    ) -> Option<ElementBox> {
255        let style = &theme.workspace.dock;
256
257        self.position
258            .is_visible()
259            .then(|| self.position.anchor())
260            .filter(|current_anchor| *current_anchor == anchor)
261            .map(|anchor| match anchor {
262                DockAnchor::Bottom | DockAnchor::Right => {
263                    let mut panel_style = style.panel.clone();
264                    let (resize_side, initial_size) = if anchor == DockAnchor::Bottom {
265                        panel_style.border = Border {
266                            top: true,
267                            bottom: false,
268                            left: false,
269                            right: false,
270                            ..panel_style.border
271                        };
272
273                        (Side::Top, style.initial_size_bottom)
274                    } else {
275                        panel_style.border = Border {
276                            top: false,
277                            bottom: false,
278                            left: true,
279                            right: false,
280                            ..panel_style.border
281                        };
282                        (Side::Left, style.initial_size_right)
283                    };
284
285                    enum DockResizeHandle {}
286
287                    let resizable = Container::new(ChildView::new(self.pane.clone(), cx).boxed())
288                        .with_style(panel_style)
289                        .with_resize_handle::<DockResizeHandle, _>(
290                            resize_side as usize,
291                            resize_side,
292                            4.,
293                            self.panel_sizes
294                                .get(&anchor)
295                                .copied()
296                                .unwrap_or(initial_size),
297                            cx,
298                        );
299
300                    let size = resizable.current_size();
301                    let workspace = cx.handle();
302                    cx.defer(move |cx| {
303                        if let Some(workspace) = workspace.upgrade(cx) {
304                            workspace.update(cx, |workspace, _| {
305                                workspace.dock.panel_sizes.insert(anchor, size);
306                            })
307                        }
308                    });
309
310                    resizable.flex(5., false).boxed()
311                }
312                DockAnchor::Expanded => {
313                    enum ExpandedDockWash {}
314                    enum ExpandedDockPane {}
315                    Stack::new()
316                        .with_child(
317                            // Render wash under the dock which when clicked hides it
318                            MouseEventHandler::<ExpandedDockWash>::new(0, cx, |_, _| {
319                                Empty::new()
320                                    .contained()
321                                    .with_background_color(style.wash_color)
322                                    .boxed()
323                            })
324                            .capture_all()
325                            .on_down(MouseButton::Left, |_, cx| {
326                                cx.dispatch_action(HideDock);
327                            })
328                            .with_cursor_style(CursorStyle::Arrow)
329                            .boxed(),
330                        )
331                        .with_child(
332                            MouseEventHandler::<ExpandedDockPane>::new(0, cx, |_state, cx| {
333                                ChildView::new(&self.pane, cx).boxed()
334                            })
335                            // Make sure all events directly under the dock pane
336                            // are captured
337                            .capture_all()
338                            .contained()
339                            .with_style(style.maximized)
340                            .boxed(),
341                        )
342                        .boxed()
343                }
344            })
345    }
346
347    pub fn position(&self) -> DockPosition {
348        self.position
349    }
350}
351
352pub struct ToggleDockButton {
353    workspace: WeakViewHandle<Workspace>,
354}
355
356impl ToggleDockButton {
357    pub fn new(workspace: ViewHandle<Workspace>, cx: &mut ViewContext<Self>) -> Self {
358        // When dock moves, redraw so that the icon and toggle status matches.
359        cx.subscribe(&workspace, |_, _, _, cx| cx.notify()).detach();
360
361        Self {
362            workspace: workspace.downgrade(),
363        }
364    }
365}
366
367impl Entity for ToggleDockButton {
368    type Event = ();
369}
370
371impl View for ToggleDockButton {
372    fn ui_name() -> &'static str {
373        "Dock Toggle"
374    }
375
376    fn render(&mut self, cx: &mut gpui::RenderContext<'_, Self>) -> ElementBox {
377        let workspace = self.workspace.upgrade(cx);
378
379        if workspace.is_none() {
380            return Empty::new().boxed();
381        }
382
383        let workspace = workspace.unwrap();
384        let dock_position = workspace.read(cx).dock.position;
385
386        let theme = cx.global::<Settings>().theme.clone();
387
388        let button = MouseEventHandler::<Self>::new(0, cx, {
389            let theme = theme.clone();
390            move |state, _| {
391                let style = theme
392                    .workspace
393                    .status_bar
394                    .sidebar_buttons
395                    .item
396                    .style_for(state, dock_position.is_visible());
397
398                Svg::new(icon_for_dock_anchor(dock_position.anchor()))
399                    .with_color(style.icon_color)
400                    .constrained()
401                    .with_width(style.icon_size)
402                    .with_height(style.icon_size)
403                    .contained()
404                    .with_style(style.container)
405                    .boxed()
406            }
407        })
408        .with_cursor_style(CursorStyle::PointingHand)
409        .on_up(MouseButton::Left, move |event, cx| {
410            let dock_pane = workspace.read(cx.app).dock_pane();
411            let drop_index = dock_pane.read(cx.app).items_len() + 1;
412            handle_dropped_item(event, &dock_pane.downgrade(), drop_index, false, None, cx);
413        });
414
415        if dock_position.is_visible() {
416            button
417                .on_click(MouseButton::Left, |_, cx| {
418                    cx.dispatch_action(HideDock);
419                })
420                .with_tooltip::<Self, _>(
421                    0,
422                    "Hide Dock".into(),
423                    Some(Box::new(HideDock)),
424                    theme.tooltip.clone(),
425                    cx,
426                )
427        } else {
428            button
429                .on_click(MouseButton::Left, |_, cx| {
430                    cx.dispatch_action(FocusDock);
431                })
432                .with_tooltip::<Self, _>(
433                    0,
434                    "Focus Dock".into(),
435                    Some(Box::new(FocusDock)),
436                    theme.tooltip.clone(),
437                    cx,
438                )
439        }
440        .boxed()
441    }
442}
443
444impl StatusItemView for ToggleDockButton {
445    fn set_active_pane_item(
446        &mut self,
447        _active_pane_item: Option<&dyn crate::ItemHandle>,
448        _cx: &mut ViewContext<Self>,
449    ) {
450        //Not applicable
451    }
452}
453
454#[cfg(test)]
455mod tests {
456    use std::{
457        ops::{Deref, DerefMut},
458        path::PathBuf,
459    };
460
461    use gpui::{AppContext, TestAppContext, UpdateView, ViewContext};
462    use project::{FakeFs, Project};
463    use settings::Settings;
464
465    use super::*;
466    use crate::{
467        dock,
468        item::test::TestItem,
469        persistence::model::{
470            SerializedItem, SerializedPane, SerializedPaneGroup, SerializedWorkspace,
471        },
472        register_deserializable_item,
473        sidebar::Sidebar,
474        ItemHandle, Workspace,
475    };
476
477    pub fn default_item_factory(
478        _workspace: &mut Workspace,
479        cx: &mut ViewContext<Workspace>,
480    ) -> Box<dyn ItemHandle> {
481        Box::new(cx.add_view(|_| TestItem::new()))
482    }
483
484    #[gpui::test]
485    async fn test_dock_workspace_infinite_loop(cx: &mut TestAppContext) {
486        cx.foreground().forbid_parking();
487        Settings::test_async(cx);
488
489        cx.update(|cx| {
490            register_deserializable_item::<TestItem>(cx);
491        });
492
493        let serialized_workspace = SerializedWorkspace {
494            id: 0,
495            location: Vec::<PathBuf>::new().into(),
496            dock_position: dock::DockPosition::Shown(DockAnchor::Expanded),
497            center_group: SerializedPaneGroup::Pane(SerializedPane {
498                active: false,
499                children: vec![],
500            }),
501            dock_pane: SerializedPane {
502                active: true,
503                children: vec![SerializedItem {
504                    active: true,
505                    item_id: 0,
506                    kind: "test".into(),
507                }],
508            },
509            left_sidebar_open: false,
510        };
511
512        let fs = FakeFs::new(cx.background());
513        let project = Project::test(fs, [], cx).await;
514
515        let (_, _workspace) = cx.add_window(|cx| {
516            Workspace::new(
517                Some(serialized_workspace),
518                0,
519                project.clone(),
520                default_item_factory,
521                cx,
522            )
523        });
524
525        cx.foreground().run_until_parked();
526        //Should terminate
527    }
528
529    #[gpui::test]
530    async fn test_dock_hides_when_pane_empty(cx: &mut TestAppContext) {
531        let mut cx = DockTestContext::new(cx).await;
532
533        // Closing the last item in the dock hides the dock
534        cx.move_dock(DockAnchor::Right);
535        let old_items = cx.dock_items();
536        assert!(!old_items.is_empty());
537        cx.close_dock_items().await;
538        cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Right));
539
540        // Reopening the dock adds a new item
541        cx.move_dock(DockAnchor::Right);
542        let new_items = cx.dock_items();
543        assert!(!new_items.is_empty());
544        assert!(new_items
545            .into_iter()
546            .all(|new_item| !old_items.contains(&new_item)));
547    }
548
549    #[gpui::test]
550    async fn test_dock_panel_collisions(cx: &mut TestAppContext) {
551        let mut cx = DockTestContext::new(cx).await;
552
553        // Dock closes when expanded for either panel
554        cx.move_dock(DockAnchor::Expanded);
555        cx.open_sidebar(SidebarSide::Left);
556        cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Expanded));
557        cx.close_sidebar(SidebarSide::Left);
558        cx.move_dock(DockAnchor::Expanded);
559        cx.open_sidebar(SidebarSide::Right);
560        cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Expanded));
561
562        // Dock closes in the right position if the right sidebar is opened
563        cx.move_dock(DockAnchor::Right);
564        cx.open_sidebar(SidebarSide::Left);
565        cx.assert_dock_position(DockPosition::Shown(DockAnchor::Right));
566        cx.open_sidebar(SidebarSide::Right);
567        cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Right));
568        cx.close_sidebar(SidebarSide::Right);
569
570        // Dock in bottom position ignores sidebars
571        cx.move_dock(DockAnchor::Bottom);
572        cx.open_sidebar(SidebarSide::Left);
573        cx.open_sidebar(SidebarSide::Right);
574        cx.assert_dock_position(DockPosition::Shown(DockAnchor::Bottom));
575
576        // Opening the dock in the right position closes the right sidebar
577        cx.move_dock(DockAnchor::Right);
578        cx.assert_sidebar_closed(SidebarSide::Right);
579    }
580
581    #[gpui::test]
582    async fn test_focusing_panes_shows_and_hides_dock(cx: &mut TestAppContext) {
583        let mut cx = DockTestContext::new(cx).await;
584
585        // Focusing an item not in the dock when expanded hides the dock
586        let center_item = cx.add_item_to_center_pane();
587        cx.move_dock(DockAnchor::Expanded);
588        let dock_item = cx
589            .dock_items()
590            .get(0)
591            .cloned()
592            .expect("Dock should have an item at this point");
593        center_item.update(&mut cx, |_, cx| cx.focus_self());
594        cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Expanded));
595
596        // Focusing an item not in the dock when not expanded, leaves the dock open but inactive
597        cx.move_dock(DockAnchor::Right);
598        center_item.update(&mut cx, |_, cx| cx.focus_self());
599        cx.assert_dock_position(DockPosition::Shown(DockAnchor::Right));
600        cx.assert_dock_pane_inactive();
601        cx.assert_workspace_pane_active();
602
603        // Focusing an item in the dock activates it's pane
604        dock_item.update(&mut cx, |_, cx| cx.focus_self());
605        cx.assert_dock_position(DockPosition::Shown(DockAnchor::Right));
606        cx.assert_dock_pane_active();
607        cx.assert_workspace_pane_inactive();
608    }
609
610    #[gpui::test]
611    async fn test_toggle_dock_focus(cx: &mut TestAppContext) {
612        let cx = DockTestContext::new(cx).await;
613
614        cx.move_dock(DockAnchor::Right);
615        cx.assert_dock_pane_active();
616        cx.hide_dock();
617        cx.move_dock(DockAnchor::Right);
618        cx.assert_dock_pane_active();
619    }
620
621    struct DockTestContext<'a> {
622        pub cx: &'a mut TestAppContext,
623        pub window_id: usize,
624        pub workspace: ViewHandle<Workspace>,
625    }
626
627    impl<'a> DockTestContext<'a> {
628        pub async fn new(cx: &'a mut TestAppContext) -> DockTestContext<'a> {
629            Settings::test_async(cx);
630            let fs = FakeFs::new(cx.background());
631
632            cx.update(|cx| init(cx));
633            let project = Project::test(fs, [], cx).await;
634            let (window_id, workspace) = cx.add_window(|cx| {
635                Workspace::new(Default::default(), 0, project, default_item_factory, cx)
636            });
637
638            workspace.update(cx, |workspace, cx| {
639                let left_panel = cx.add_view(|_| TestItem::new());
640                workspace.left_sidebar().update(cx, |sidebar, cx| {
641                    sidebar.add_item(
642                        "icons/folder_tree_16.svg",
643                        "Left Test Panel".to_string(),
644                        left_panel.clone(),
645                        cx,
646                    );
647                });
648
649                let right_panel = cx.add_view(|_| TestItem::new());
650                workspace.right_sidebar().update(cx, |sidebar, cx| {
651                    sidebar.add_item(
652                        "icons/folder_tree_16.svg",
653                        "Right Test Panel".to_string(),
654                        right_panel.clone(),
655                        cx,
656                    );
657                });
658            });
659
660            Self {
661                cx,
662                window_id,
663                workspace,
664            }
665        }
666
667        pub fn workspace<F, T>(&self, read: F) -> T
668        where
669            F: FnOnce(&Workspace, &AppContext) -> T,
670        {
671            self.workspace.read_with(self.cx, read)
672        }
673
674        pub fn update_workspace<F, T>(&mut self, update: F) -> T
675        where
676            F: FnOnce(&mut Workspace, &mut ViewContext<Workspace>) -> T,
677        {
678            self.workspace.update(self.cx, update)
679        }
680
681        pub fn sidebar<F, T>(&self, sidebar_side: SidebarSide, read: F) -> T
682        where
683            F: FnOnce(&Sidebar, &AppContext) -> T,
684        {
685            self.workspace(|workspace, cx| {
686                let sidebar = match sidebar_side {
687                    SidebarSide::Left => workspace.left_sidebar(),
688                    SidebarSide::Right => workspace.right_sidebar(),
689                }
690                .read(cx);
691
692                read(sidebar, cx)
693            })
694        }
695
696        pub fn center_pane_handle(&self) -> ViewHandle<Pane> {
697            self.workspace(|workspace, cx| {
698                workspace
699                    .last_active_center_pane
700                    .clone()
701                    .and_then(|pane| pane.upgrade(cx))
702                    .unwrap_or_else(|| workspace.center.panes()[0].clone())
703            })
704        }
705
706        pub fn add_item_to_center_pane(&mut self) -> ViewHandle<TestItem> {
707            self.update_workspace(|workspace, cx| {
708                let item = cx.add_view(|_| TestItem::new());
709                let pane = workspace
710                    .last_active_center_pane
711                    .clone()
712                    .and_then(|pane| pane.upgrade(cx))
713                    .unwrap_or_else(|| workspace.center.panes()[0].clone());
714                Pane::add_item(
715                    workspace,
716                    &pane,
717                    Box::new(item.clone()),
718                    true,
719                    true,
720                    None,
721                    cx,
722                );
723                item
724            })
725        }
726
727        pub fn dock_pane<F, T>(&self, read: F) -> T
728        where
729            F: FnOnce(&Pane, &AppContext) -> T,
730        {
731            self.workspace(|workspace, cx| {
732                let dock_pane = workspace.dock_pane().read(cx);
733                read(dock_pane, cx)
734            })
735        }
736
737        pub fn move_dock(&self, anchor: DockAnchor) {
738            self.cx.dispatch_action(self.window_id, MoveDock(anchor));
739        }
740
741        pub fn hide_dock(&self) {
742            self.cx.dispatch_action(self.window_id, HideDock);
743        }
744
745        pub fn open_sidebar(&mut self, sidebar_side: SidebarSide) {
746            if !self.sidebar(sidebar_side, |sidebar, _| sidebar.is_open()) {
747                self.update_workspace(|workspace, cx| workspace.toggle_sidebar(sidebar_side, cx));
748            }
749        }
750
751        pub fn close_sidebar(&mut self, sidebar_side: SidebarSide) {
752            if self.sidebar(sidebar_side, |sidebar, _| sidebar.is_open()) {
753                self.update_workspace(|workspace, cx| workspace.toggle_sidebar(sidebar_side, cx));
754            }
755        }
756
757        pub fn dock_items(&self) -> Vec<ViewHandle<TestItem>> {
758            self.dock_pane(|pane, cx| {
759                pane.items()
760                    .map(|item| {
761                        item.act_as::<TestItem>(cx)
762                            .expect("Dock Test Context uses TestItems in the dock")
763                    })
764                    .collect()
765            })
766        }
767
768        pub async fn close_dock_items(&mut self) {
769            self.update_workspace(|workspace, cx| {
770                Pane::close_items(workspace, workspace.dock_pane().clone(), cx, |_| true)
771            })
772            .await
773            .expect("Could not close dock items")
774        }
775
776        pub fn assert_dock_position(&self, expected_position: DockPosition) {
777            self.workspace(|workspace, _| assert_eq!(workspace.dock.position, expected_position));
778        }
779
780        pub fn assert_sidebar_closed(&self, sidebar_side: SidebarSide) {
781            assert!(!self.sidebar(sidebar_side, |sidebar, _| sidebar.is_open()));
782        }
783
784        pub fn assert_workspace_pane_active(&self) {
785            assert!(self
786                .center_pane_handle()
787                .read_with(self.cx, |pane, _| pane.is_active()));
788        }
789
790        pub fn assert_workspace_pane_inactive(&self) {
791            assert!(!self
792                .center_pane_handle()
793                .read_with(self.cx, |pane, _| pane.is_active()));
794        }
795
796        pub fn assert_dock_pane_active(&self) {
797            assert!(self.dock_pane(|pane, _| pane.is_active()))
798        }
799
800        pub fn assert_dock_pane_inactive(&self) {
801            assert!(!self.dock_pane(|pane, _| pane.is_active()))
802        }
803    }
804
805    impl<'a> Deref for DockTestContext<'a> {
806        type Target = gpui::TestAppContext;
807
808        fn deref(&self) -> &Self::Target {
809            self.cx
810        }
811    }
812
813    impl<'a> DerefMut for DockTestContext<'a> {
814        fn deref_mut(&mut self) -> &mut Self::Target {
815            &mut self.cx
816        }
817    }
818
819    impl<'a> UpdateView for DockTestContext<'a> {
820        fn update_view<T, S>(
821            &mut self,
822            handle: &ViewHandle<T>,
823            update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
824        ) -> S
825        where
826            T: View,
827        {
828            handle.update(self.cx, update)
829        }
830    }
831}