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