dock.rs

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