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,
 11    platform::{CursorStyle, MouseButton},
 12    AppContext, Border, Drawable, 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<Element<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 = Container::new(ChildView::new(&self.pane, cx).boxed())
352                        .with_style(panel_style)
353                        .with_resize_handle::<DockResizeHandle>(
354                            resize_side as usize,
355                            resize_side,
356                            4.,
357                            self.panel_sizes
358                                .get(&anchor)
359                                .copied()
360                                .unwrap_or(initial_size),
361                            cx,
362                        );
363
364                    let size = resizable.current_size();
365                    cx.defer(move |workspace, _| {
366                        workspace.dock.panel_sizes.insert(anchor, size);
367                    });
368
369                    if anchor == DockAnchor::Right {
370                        resizable
371                            .constrained()
372                            .dynamically(|constraint, _, cx| {
373                                SizeConstraint::new(
374                                    Vector2F::new(20., constraint.min.y()),
375                                    Vector2F::new(cx.window_size().x() * 0.8, constraint.max.y()),
376                                )
377                            })
378                            .boxed()
379                    } else {
380                        resizable
381                            .constrained()
382                            .dynamically(|constraint, _, cx| {
383                                SizeConstraint::new(
384                                    Vector2F::new(constraint.min.x(), 50.),
385                                    Vector2F::new(constraint.max.x(), cx.window_size().y() * 0.8),
386                                )
387                            })
388                            .boxed()
389                    }
390                }
391                DockAnchor::Expanded => {
392                    enum ExpandedDockWash {}
393                    enum ExpandedDockPane {}
394                    Stack::new()
395                        .with_child(
396                            // Render wash under the dock which when clicked hides it
397                            MouseEventHandler::<ExpandedDockWash, _>::new(0, cx, |_, _| {
398                                Empty::new()
399                                    .contained()
400                                    .with_background_color(style.wash_color)
401                                    .boxed()
402                            })
403                            .capture_all()
404                            .on_down(MouseButton::Left, |_, _, cx| {
405                                cx.dispatch_action(HideDock);
406                            })
407                            .with_cursor_style(CursorStyle::Arrow)
408                            .boxed(),
409                        )
410                        .with_child(
411                            MouseEventHandler::<ExpandedDockPane, _>::new(0, cx, |_state, cx| {
412                                ChildView::new(&self.pane, cx).boxed()
413                            })
414                            // Make sure all events directly under the dock pane
415                            // are captured
416                            .capture_all()
417                            .contained()
418                            .with_style(style.maximized)
419                            .boxed(),
420                        )
421                        .boxed()
422                }
423            })
424    }
425
426    pub fn position(&self) -> DockPosition {
427        self.position
428    }
429}
430
431#[cfg(test)]
432mod tests {
433    use std::{
434        ops::{Deref, DerefMut},
435        path::PathBuf,
436    };
437
438    use gpui::{AppContext, TestAppContext, UpdateView, View, ViewContext};
439    use project::{FakeFs, Project};
440    use settings::Settings;
441
442    use super::*;
443    use crate::{
444        dock,
445        item::{self, test::TestItem},
446        persistence::model::{
447            SerializedItem, SerializedPane, SerializedPaneGroup, SerializedWorkspace,
448        },
449        register_deserializable_item,
450        sidebar::Sidebar,
451        ItemHandle, Workspace,
452    };
453
454    pub fn default_item_factory(
455        _workspace: &mut Workspace,
456        cx: &mut ViewContext<Workspace>,
457    ) -> Option<Box<dyn ItemHandle>> {
458        Some(Box::new(cx.add_view(|_| TestItem::new())))
459    }
460
461    #[gpui::test]
462    async fn test_dock_workspace_infinite_loop(cx: &mut TestAppContext) {
463        cx.foreground().forbid_parking();
464        Settings::test_async(cx);
465
466        cx.update(|cx| {
467            register_deserializable_item::<item::test::TestItem>(cx);
468        });
469
470        let serialized_workspace = SerializedWorkspace {
471            id: 0,
472            location: Vec::<PathBuf>::new().into(),
473            dock_position: dock::DockPosition::Shown(DockAnchor::Expanded),
474            center_group: SerializedPaneGroup::Pane(SerializedPane {
475                active: false,
476                children: vec![],
477            }),
478            dock_pane: SerializedPane {
479                active: true,
480                children: vec![SerializedItem {
481                    active: true,
482                    item_id: 0,
483                    kind: "TestItem".into(),
484                }],
485            },
486            left_sidebar_open: false,
487            bounds: Default::default(),
488            display: Default::default(),
489        };
490
491        let fs = FakeFs::new(cx.background());
492        let project = Project::test(fs, [], cx).await;
493
494        let (_, _workspace) = cx.add_window(|cx| {
495            Workspace::new(
496                Some(serialized_workspace),
497                0,
498                project.clone(),
499                default_item_factory,
500                || &[],
501                cx,
502            )
503        });
504
505        cx.foreground().run_until_parked();
506        //Should terminate
507    }
508
509    #[gpui::test]
510    async fn test_dock_hides_when_pane_empty(cx: &mut TestAppContext) {
511        let mut cx = DockTestContext::new(cx).await;
512
513        // Closing the last item in the dock hides the dock
514        cx.move_dock(DockAnchor::Right);
515        let old_items = cx.dock_items();
516        assert!(!old_items.is_empty());
517        cx.close_dock_items().await;
518        cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Right));
519
520        // Reopening the dock adds a new item
521        cx.move_dock(DockAnchor::Right);
522        let new_items = cx.dock_items();
523        assert!(!new_items.is_empty());
524        assert!(new_items
525            .into_iter()
526            .all(|new_item| !old_items.contains(&new_item)));
527    }
528
529    #[gpui::test]
530    async fn test_dock_panel_collisions(cx: &mut TestAppContext) {
531        let mut cx = DockTestContext::new(cx).await;
532
533        // Dock closes when expanded for either panel
534        cx.move_dock(DockAnchor::Expanded);
535        cx.open_sidebar(SidebarSide::Left);
536        cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Expanded));
537        cx.close_sidebar(SidebarSide::Left);
538        cx.move_dock(DockAnchor::Expanded);
539        cx.open_sidebar(SidebarSide::Right);
540        cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Expanded));
541
542        // Dock closes in the right position if the right sidebar is opened
543        cx.move_dock(DockAnchor::Right);
544        cx.open_sidebar(SidebarSide::Left);
545        cx.assert_dock_position(DockPosition::Shown(DockAnchor::Right));
546        cx.open_sidebar(SidebarSide::Right);
547        cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Right));
548        cx.close_sidebar(SidebarSide::Right);
549
550        // Dock in bottom position ignores sidebars
551        cx.move_dock(DockAnchor::Bottom);
552        cx.open_sidebar(SidebarSide::Left);
553        cx.open_sidebar(SidebarSide::Right);
554        cx.assert_dock_position(DockPosition::Shown(DockAnchor::Bottom));
555
556        // Opening the dock in the right position closes the right sidebar
557        cx.move_dock(DockAnchor::Right);
558        cx.assert_sidebar_closed(SidebarSide::Right);
559    }
560
561    #[gpui::test]
562    async fn test_focusing_panes_shows_and_hides_dock(cx: &mut TestAppContext) {
563        let mut cx = DockTestContext::new(cx).await;
564
565        // Focusing an item not in the dock when expanded hides the dock
566        let center_item = cx.add_item_to_center_pane();
567        cx.move_dock(DockAnchor::Expanded);
568        let dock_item = cx
569            .dock_items()
570            .get(0)
571            .cloned()
572            .expect("Dock should have an item at this point");
573        center_item.update(&mut cx, |_, cx| cx.focus_self());
574        cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Expanded));
575
576        // Focusing an item not in the dock when not expanded, leaves the dock open but inactive
577        cx.move_dock(DockAnchor::Right);
578        center_item.update(&mut cx, |_, cx| cx.focus_self());
579        cx.assert_dock_position(DockPosition::Shown(DockAnchor::Right));
580        cx.assert_dock_pane_inactive();
581        cx.assert_workspace_pane_active();
582
583        // Focusing an item in the dock activates it's pane
584        dock_item.update(&mut cx, |_, cx| cx.focus_self());
585        cx.assert_dock_position(DockPosition::Shown(DockAnchor::Right));
586        cx.assert_dock_pane_active();
587        cx.assert_workspace_pane_inactive();
588    }
589
590    #[gpui::test]
591    async fn test_toggle_dock_focus(cx: &mut TestAppContext) {
592        let cx = DockTestContext::new(cx).await;
593
594        cx.move_dock(DockAnchor::Right);
595        cx.assert_dock_pane_active();
596        cx.hide_dock();
597        cx.move_dock(DockAnchor::Right);
598        cx.assert_dock_pane_active();
599    }
600
601    #[gpui::test]
602    async fn test_activate_next_and_prev_pane(cx: &mut TestAppContext) {
603        let mut cx = DockTestContext::new(cx).await;
604
605        cx.move_dock(DockAnchor::Right);
606        cx.assert_dock_pane_active();
607
608        cx.update_workspace(|workspace, cx| workspace.activate_next_pane(cx));
609        cx.assert_dock_pane_active();
610
611        cx.update_workspace(|workspace, cx| workspace.activate_previous_pane(cx));
612        cx.assert_dock_pane_active();
613    }
614
615    struct DockTestContext<'a> {
616        pub cx: &'a mut TestAppContext,
617        pub window_id: usize,
618        pub workspace: ViewHandle<Workspace>,
619    }
620
621    impl<'a> DockTestContext<'a> {
622        pub async fn new(cx: &'a mut TestAppContext) -> DockTestContext<'a> {
623            Settings::test_async(cx);
624            let fs = FakeFs::new(cx.background());
625
626            cx.update(|cx| init(cx));
627            let project = Project::test(fs, [], cx).await;
628            let (window_id, workspace) = cx.add_window(|cx| {
629                Workspace::new(
630                    Default::default(),
631                    0,
632                    project,
633                    default_item_factory,
634                    || &[],
635                    cx,
636                )
637            });
638
639            workspace.update(cx, |workspace, cx| {
640                let left_panel = cx.add_view(|_| TestItem::new());
641                workspace.left_sidebar().update(cx, |sidebar, cx| {
642                    sidebar.add_item(
643                        "icons/folder_tree_16.svg",
644                        "Left Test Panel".to_string(),
645                        left_panel.clone(),
646                        cx,
647                    );
648                });
649
650                let right_panel = cx.add_view(|_| TestItem::new());
651                workspace.right_sidebar().update(cx, |sidebar, cx| {
652                    sidebar.add_item(
653                        "icons/folder_tree_16.svg",
654                        "Right Test Panel".to_string(),
655                        right_panel.clone(),
656                        cx,
657                    );
658                });
659            });
660
661            Self {
662                cx,
663                window_id,
664                workspace,
665            }
666        }
667
668        pub fn workspace<F, T>(&self, read: F) -> T
669        where
670            F: FnOnce(&Workspace, &AppContext) -> T,
671        {
672            self.workspace.read_with(self.cx, read)
673        }
674
675        pub fn update_workspace<F, T>(&mut self, update: F) -> T
676        where
677            F: FnOnce(&mut Workspace, &mut ViewContext<Workspace>) -> T,
678        {
679            self.workspace.update(self.cx, update)
680        }
681
682        pub fn sidebar<F, T>(&self, sidebar_side: SidebarSide, read: F) -> T
683        where
684            F: FnOnce(&Sidebar, &AppContext) -> T,
685        {
686            self.workspace(|workspace, cx| {
687                let sidebar = match sidebar_side {
688                    SidebarSide::Left => workspace.left_sidebar(),
689                    SidebarSide::Right => workspace.right_sidebar(),
690                }
691                .read(cx);
692
693                read(sidebar, cx)
694            })
695        }
696
697        pub fn center_pane_handle(&self) -> ViewHandle<Pane> {
698            self.workspace(|workspace, cx| {
699                workspace
700                    .last_active_center_pane
701                    .clone()
702                    .and_then(|pane| pane.upgrade(cx))
703                    .unwrap_or_else(|| workspace.center.panes()[0].clone())
704            })
705        }
706
707        pub fn add_item_to_center_pane(&mut self) -> ViewHandle<TestItem> {
708            self.update_workspace(|workspace, cx| {
709                let item = cx.add_view(|_| TestItem::new());
710                let pane = workspace
711                    .last_active_center_pane
712                    .clone()
713                    .and_then(|pane| pane.upgrade(cx))
714                    .unwrap_or_else(|| workspace.center.panes()[0].clone());
715                Pane::add_item(
716                    workspace,
717                    &pane,
718                    Box::new(item.clone()),
719                    true,
720                    true,
721                    None,
722                    cx,
723                );
724                item
725            })
726        }
727
728        pub fn dock_pane<F, T>(&self, read: F) -> T
729        where
730            F: FnOnce(&Pane, &AppContext) -> T,
731        {
732            self.workspace(|workspace, cx| {
733                let dock_pane = workspace.dock_pane().read(cx);
734                read(dock_pane, cx)
735            })
736        }
737
738        pub fn move_dock(&self, anchor: DockAnchor) {
739            self.cx.dispatch_action(self.window_id, MoveDock(anchor));
740        }
741
742        pub fn hide_dock(&self) {
743            self.cx.dispatch_action(self.window_id, HideDock);
744        }
745
746        pub fn open_sidebar(&mut self, sidebar_side: SidebarSide) {
747            if !self.sidebar(sidebar_side, |sidebar, _| sidebar.is_open()) {
748                self.update_workspace(|workspace, cx| workspace.toggle_sidebar(sidebar_side, cx));
749            }
750        }
751
752        pub fn close_sidebar(&mut self, sidebar_side: SidebarSide) {
753            if self.sidebar(sidebar_side, |sidebar, _| sidebar.is_open()) {
754                self.update_workspace(|workspace, cx| workspace.toggle_sidebar(sidebar_side, cx));
755            }
756        }
757
758        pub fn dock_items(&self) -> Vec<ViewHandle<TestItem>> {
759            self.dock_pane(|pane, cx| {
760                pane.items()
761                    .map(|item| {
762                        item.act_as::<TestItem>(cx)
763                            .expect("Dock Test Context uses TestItems in the dock")
764                    })
765                    .collect()
766            })
767        }
768
769        pub async fn close_dock_items(&mut self) {
770            self.update_workspace(|workspace, cx| {
771                Pane::close_items(workspace, workspace.dock_pane().clone(), cx, |_| true)
772            })
773            .await
774            .expect("Could not close dock items")
775        }
776
777        pub fn assert_dock_position(&self, expected_position: DockPosition) {
778            self.workspace(|workspace, _| assert_eq!(workspace.dock.position, expected_position));
779        }
780
781        pub fn assert_sidebar_closed(&self, sidebar_side: SidebarSide) {
782            assert!(!self.sidebar(sidebar_side, |sidebar, _| sidebar.is_open()));
783        }
784
785        pub fn assert_workspace_pane_active(&self) {
786            assert!(self
787                .center_pane_handle()
788                .read_with(self.cx, |pane, _| pane.is_active()));
789        }
790
791        pub fn assert_workspace_pane_inactive(&self) {
792            assert!(!self
793                .center_pane_handle()
794                .read_with(self.cx, |pane, _| pane.is_active()));
795        }
796
797        pub fn assert_dock_pane_active(&self) {
798            assert!(self.dock_pane(|pane, _| pane.is_active()))
799        }
800
801        pub fn assert_dock_pane_inactive(&self) {
802            assert!(!self.dock_pane(|pane, _| pane.is_active()))
803        }
804    }
805
806    impl<'a> Deref for DockTestContext<'a> {
807        type Target = gpui::TestAppContext;
808
809        fn deref(&self) -> &Self::Target {
810            self.cx
811        }
812    }
813
814    impl<'a> DerefMut for DockTestContext<'a> {
815        fn deref_mut(&mut self) -> &mut Self::Target {
816            &mut self.cx
817        }
818    }
819
820    impl<'a> UpdateView for DockTestContext<'a> {
821        type Output<S> = S;
822
823        fn update_view<T, S>(
824            &mut self,
825            handle: &ViewHandle<T>,
826            update: &mut dyn FnMut(&mut T, &mut ViewContext<T>) -> S,
827        ) -> S
828        where
829            T: View,
830        {
831            handle.update(self.cx, update)
832        }
833    }
834}