dock.rs

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