dock.rs

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