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