dock.rs

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