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