dock.rs

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