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