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, BackgroundActions, 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(
43 |workspace: &mut Workspace, &MoveDock(dock_anchor), cx: &mut ViewContext<Workspace>| {
44 Dock::move_dock(workspace, dock_anchor, true, cx);
45 },
46 );
47 cx.add_action(
48 |workspace: &mut Workspace, _: &AnchorDockRight, cx: &mut ViewContext<Workspace>| {
49 Dock::move_dock(workspace, DockAnchor::Right, true, cx);
50 },
51 );
52 cx.add_action(
53 |workspace: &mut Workspace, _: &AnchorDockBottom, cx: &mut ViewContext<Workspace>| {
54 Dock::move_dock(workspace, DockAnchor::Bottom, true, cx)
55 },
56 );
57 cx.add_action(
58 |workspace: &mut Workspace, _: &ExpandDock, cx: &mut ViewContext<Workspace>| {
59 Dock::move_dock(workspace, DockAnchor::Expanded, true, cx)
60 },
61 );
62 cx.add_action(
63 |workspace: &mut Workspace, _: &AddTabToDock, cx: &mut ViewContext<Workspace>| {
64 if let Some(active_item) = workspace.active_item(cx) {
65 let item_id = active_item.id();
66
67 let from = workspace.active_pane();
68 let to = workspace.dock_pane();
69 if from.id() == to.id() {
70 return;
71 }
72
73 let destination_index = to.read(cx).items_len() + 1;
74
75 Pane::move_item(
76 workspace,
77 from.clone(),
78 to.clone(),
79 item_id,
80 destination_index,
81 cx,
82 );
83 }
84 },
85 );
86 cx.add_action(
87 |workspace: &mut Workspace, _: &RemoveTabFromDock, cx: &mut ViewContext<Workspace>| {
88 if let Some(active_item) = workspace.active_item(cx) {
89 let item_id = active_item.id();
90
91 let from = workspace.dock_pane();
92 let to = workspace
93 .last_active_center_pane
94 .as_ref()
95 .and_then(|pane| pane.upgrade(cx))
96 .unwrap_or_else(|| {
97 workspace
98 .panes
99 .first()
100 .expect("There must be a pane")
101 .clone()
102 });
103
104 if from.id() == to.id() {
105 return;
106 }
107
108 let destination_index = to.read(cx).items_len() + 1;
109
110 Pane::move_item(
111 workspace,
112 from.clone(),
113 to.clone(),
114 item_id,
115 destination_index,
116 cx,
117 );
118 }
119 },
120 );
121}
122
123#[derive(Copy, Clone, PartialEq, Eq, Debug)]
124pub enum DockPosition {
125 Shown(DockAnchor),
126 Hidden(DockAnchor),
127}
128
129impl Default for DockPosition {
130 fn default() -> Self {
131 DockPosition::Hidden(Default::default())
132 }
133}
134
135pub fn icon_for_dock_anchor(anchor: DockAnchor) -> &'static str {
136 match anchor {
137 DockAnchor::Right => "icons/dock_right_12.svg",
138 DockAnchor::Bottom => "icons/dock_bottom_12.svg",
139 DockAnchor::Expanded => "icons/dock_modal_12.svg",
140 }
141}
142
143impl DockPosition {
144 pub fn is_visible(&self) -> bool {
145 match self {
146 DockPosition::Shown(_) => true,
147 DockPosition::Hidden(_) => false,
148 }
149 }
150
151 pub fn anchor(&self) -> DockAnchor {
152 match self {
153 DockPosition::Shown(anchor) | DockPosition::Hidden(anchor) => *anchor,
154 }
155 }
156
157 fn hide(self) -> Self {
158 match self {
159 DockPosition::Shown(anchor) => DockPosition::Hidden(anchor),
160 DockPosition::Hidden(_) => self,
161 }
162 }
163
164 fn show(self) -> Self {
165 match self {
166 DockPosition::Hidden(anchor) => DockPosition::Shown(anchor),
167 DockPosition::Shown(_) => self,
168 }
169 }
170}
171
172pub type DockDefaultItemFactory =
173 fn(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> Option<Box<dyn ItemHandle>>;
174
175pub struct Dock {
176 position: DockPosition,
177 panel_sizes: HashMap<DockAnchor, f32>,
178 pane: ViewHandle<Pane>,
179 default_item_factory: DockDefaultItemFactory,
180}
181
182impl Dock {
183 pub fn new(
184 workspace_id: usize,
185 default_item_factory: DockDefaultItemFactory,
186 background_actions: BackgroundActions,
187 cx: &mut ViewContext<Workspace>,
188 ) -> Self {
189 let position = DockPosition::Hidden(cx.global::<Settings>().default_dock_anchor);
190
191 let pane = cx.add_view(|cx| {
192 Pane::new(
193 workspace_id,
194 Some(position.anchor()),
195 background_actions,
196 cx,
197 )
198 });
199 pane.update(cx, |pane, cx| {
200 pane.set_active(false, cx);
201 });
202 let pane_id = pane.id();
203 cx.subscribe(&pane, move |workspace, _, event, cx| {
204 workspace.handle_pane_event(pane_id, event, cx);
205 })
206 .detach();
207
208 Self {
209 pane,
210 panel_sizes: Default::default(),
211 position,
212 default_item_factory,
213 }
214 }
215
216 pub fn pane(&self) -> &ViewHandle<Pane> {
217 &self.pane
218 }
219
220 pub fn visible_pane(&self) -> Option<&ViewHandle<Pane>> {
221 self.position.is_visible().then(|| self.pane())
222 }
223
224 pub fn is_anchored_at(&self, anchor: DockAnchor) -> bool {
225 self.position.is_visible() && self.position.anchor() == anchor
226 }
227
228 pub(crate) fn set_dock_position(
229 workspace: &mut Workspace,
230 new_position: DockPosition,
231 focus: bool,
232 cx: &mut ViewContext<Workspace>,
233 ) {
234 workspace.dock.position = new_position;
235 // Tell the pane about the new anchor position
236 workspace.dock.pane.update(cx, |pane, cx| {
237 pane.set_docked(Some(new_position.anchor()), cx)
238 });
239
240 if workspace.dock.position.is_visible() {
241 // Close the right sidebar if the dock is on the right side and the right sidebar is open
242 if workspace.dock.position.anchor() == DockAnchor::Right {
243 if workspace.right_sidebar().read(cx).is_open() {
244 workspace.toggle_sidebar(SidebarSide::Right, cx);
245 }
246 }
247
248 // Ensure that the pane has at least one item or construct a default item to put in it
249 let pane = workspace.dock.pane.clone();
250 if pane.read(cx).items().next().is_none() {
251 if let Some(item_to_add) = (workspace.dock.default_item_factory)(workspace, cx) {
252 Pane::add_item(workspace, &pane, item_to_add, focus, focus, None, cx);
253 } else {
254 workspace.dock.position = workspace.dock.position.hide();
255 }
256 } else {
257 if focus {
258 cx.focus(pane);
259 }
260 }
261 } else if let Some(last_active_center_pane) = workspace
262 .last_active_center_pane
263 .as_ref()
264 .and_then(|pane| pane.upgrade(cx))
265 {
266 if focus {
267 cx.focus(last_active_center_pane);
268 }
269 }
270 cx.emit(crate::Event::DockAnchorChanged);
271 workspace.serialize_workspace(cx);
272 cx.notify();
273 }
274
275 pub fn hide(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
276 Self::set_dock_position(workspace, workspace.dock.position.hide(), true, cx);
277 }
278
279 pub fn show(workspace: &mut Workspace, focus: bool, cx: &mut ViewContext<Workspace>) {
280 Self::set_dock_position(workspace, workspace.dock.position.show(), focus, cx);
281 }
282
283 pub fn hide_on_sidebar_shown(
284 workspace: &mut Workspace,
285 sidebar_side: SidebarSide,
286 cx: &mut ViewContext<Workspace>,
287 ) {
288 if (sidebar_side == SidebarSide::Right && workspace.dock.is_anchored_at(DockAnchor::Right))
289 || workspace.dock.is_anchored_at(DockAnchor::Expanded)
290 {
291 Self::hide(workspace, cx);
292 }
293 }
294
295 fn focus_dock(workspace: &mut Workspace, _: &FocusDock, cx: &mut ViewContext<Workspace>) {
296 Self::set_dock_position(workspace, workspace.dock.position.show(), true, cx);
297 }
298
299 fn hide_dock(workspace: &mut Workspace, _: &HideDock, cx: &mut ViewContext<Workspace>) {
300 Self::set_dock_position(workspace, workspace.dock.position.hide(), true, cx);
301 }
302
303 pub fn move_dock(
304 workspace: &mut Workspace,
305 new_anchor: DockAnchor,
306 focus: bool,
307 cx: &mut ViewContext<Workspace>,
308 ) {
309 Self::set_dock_position(workspace, DockPosition::Shown(new_anchor), focus, cx);
310 }
311
312 pub fn render(
313 &self,
314 theme: &Theme,
315 anchor: DockAnchor,
316 cx: &mut RenderContext<Workspace>,
317 ) -> Option<ElementBox> {
318 let style = &theme.workspace.dock;
319
320 self.position
321 .is_visible()
322 .then(|| self.position.anchor())
323 .filter(|current_anchor| *current_anchor == anchor)
324 .map(|anchor| match anchor {
325 DockAnchor::Bottom | DockAnchor::Right => {
326 let mut panel_style = style.panel.clone();
327 let (resize_side, initial_size) = if anchor == DockAnchor::Bottom {
328 panel_style.border = Border {
329 top: true,
330 bottom: false,
331 left: false,
332 right: false,
333 ..panel_style.border
334 };
335
336 (Side::Top, style.initial_size_bottom)
337 } else {
338 panel_style.border = Border {
339 top: false,
340 bottom: false,
341 left: true,
342 right: false,
343 ..panel_style.border
344 };
345 (Side::Left, style.initial_size_right)
346 };
347
348 enum DockResizeHandle {}
349
350 let resizable = Container::new(ChildView::new(self.pane.clone(), cx).boxed())
351 .with_style(panel_style)
352 .with_resize_handle::<DockResizeHandle, _>(
353 resize_side as usize,
354 resize_side,
355 4.,
356 self.panel_sizes
357 .get(&anchor)
358 .copied()
359 .unwrap_or(initial_size),
360 cx,
361 );
362
363 let size = resizable.current_size();
364 let workspace = cx.handle();
365 cx.defer(move |cx| {
366 if let Some(workspace) = workspace.upgrade(cx) {
367 workspace.update(cx, |workspace, _| {
368 workspace.dock.panel_sizes.insert(anchor, size);
369 })
370 }
371 });
372
373 if anchor == DockAnchor::Right {
374 resizable
375 .constrained()
376 .dynamically(|constraint, cx| {
377 SizeConstraint::new(
378 Vector2F::new(20., constraint.min.y()),
379 Vector2F::new(cx.window_size.x() * 0.8, constraint.max.y()),
380 )
381 })
382 .boxed()
383 } else {
384 resizable
385 .constrained()
386 .dynamically(|constraint, cx| {
387 SizeConstraint::new(
388 Vector2F::new(constraint.min.x(), 50.),
389 Vector2F::new(constraint.max.x(), cx.window_size.y() * 0.8),
390 )
391 })
392 .boxed()
393 }
394 }
395 DockAnchor::Expanded => {
396 enum ExpandedDockWash {}
397 enum ExpandedDockPane {}
398 Stack::new()
399 .with_child(
400 // Render wash under the dock which when clicked hides it
401 MouseEventHandler::<ExpandedDockWash>::new(0, cx, |_, _| {
402 Empty::new()
403 .contained()
404 .with_background_color(style.wash_color)
405 .boxed()
406 })
407 .capture_all()
408 .on_down(MouseButton::Left, |_, cx| {
409 cx.dispatch_action(HideDock);
410 })
411 .with_cursor_style(CursorStyle::Arrow)
412 .boxed(),
413 )
414 .with_child(
415 MouseEventHandler::<ExpandedDockPane>::new(0, cx, |_state, cx| {
416 ChildView::new(&self.pane, cx).boxed()
417 })
418 // Make sure all events directly under the dock pane
419 // are captured
420 .capture_all()
421 .contained()
422 .with_style(style.maximized)
423 .boxed(),
424 )
425 .boxed()
426 }
427 })
428 }
429
430 pub fn position(&self) -> DockPosition {
431 self.position
432 }
433}
434
435#[cfg(test)]
436mod tests {
437 use std::{
438 ops::{Deref, DerefMut},
439 path::PathBuf,
440 };
441
442 use gpui::{AppContext, TestAppContext, UpdateView, View, ViewContext};
443 use project::{FakeFs, Project};
444 use settings::Settings;
445
446 use super::*;
447 use crate::{
448 dock,
449 item::{self, test::TestItem},
450 persistence::model::{
451 SerializedItem, SerializedPane, SerializedPaneGroup, SerializedWorkspace,
452 },
453 register_deserializable_item,
454 sidebar::Sidebar,
455 ItemHandle, Workspace,
456 };
457
458 pub fn default_item_factory(
459 _workspace: &mut Workspace,
460 cx: &mut ViewContext<Workspace>,
461 ) -> Option<Box<dyn ItemHandle>> {
462 Some(Box::new(cx.add_view(|_| TestItem::new())))
463 }
464
465 #[gpui::test]
466 async fn test_dock_workspace_infinite_loop(cx: &mut TestAppContext) {
467 cx.foreground().forbid_parking();
468 Settings::test_async(cx);
469
470 cx.update(|cx| {
471 register_deserializable_item::<item::test::TestItem>(cx);
472 });
473
474 let serialized_workspace = SerializedWorkspace {
475 id: 0,
476 location: Vec::<PathBuf>::new().into(),
477 dock_position: dock::DockPosition::Shown(DockAnchor::Expanded),
478 center_group: SerializedPaneGroup::Pane(SerializedPane {
479 active: false,
480 children: vec![],
481 }),
482 dock_pane: SerializedPane {
483 active: true,
484 children: vec![SerializedItem {
485 active: true,
486 item_id: 0,
487 kind: "TestItem".into(),
488 }],
489 },
490 left_sidebar_open: false,
491 bounds: Default::default(),
492 display: Default::default(),
493 };
494
495 let fs = FakeFs::new(cx.background());
496 let project = Project::test(fs, [], cx).await;
497
498 let (_, _workspace) = cx.add_window(|cx| {
499 Workspace::new(
500 Some(serialized_workspace),
501 0,
502 project.clone(),
503 default_item_factory,
504 || &[],
505 cx,
506 )
507 });
508
509 cx.foreground().run_until_parked();
510 //Should terminate
511 }
512
513 #[gpui::test]
514 async fn test_dock_hides_when_pane_empty(cx: &mut TestAppContext) {
515 let mut cx = DockTestContext::new(cx).await;
516
517 // Closing the last item in the dock hides the dock
518 cx.move_dock(DockAnchor::Right);
519 let old_items = cx.dock_items();
520 assert!(!old_items.is_empty());
521 cx.close_dock_items().await;
522 cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Right));
523
524 // Reopening the dock adds a new item
525 cx.move_dock(DockAnchor::Right);
526 let new_items = cx.dock_items();
527 assert!(!new_items.is_empty());
528 assert!(new_items
529 .into_iter()
530 .all(|new_item| !old_items.contains(&new_item)));
531 }
532
533 #[gpui::test]
534 async fn test_dock_panel_collisions(cx: &mut TestAppContext) {
535 let mut cx = DockTestContext::new(cx).await;
536
537 // Dock closes when expanded for either panel
538 cx.move_dock(DockAnchor::Expanded);
539 cx.open_sidebar(SidebarSide::Left);
540 cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Expanded));
541 cx.close_sidebar(SidebarSide::Left);
542 cx.move_dock(DockAnchor::Expanded);
543 cx.open_sidebar(SidebarSide::Right);
544 cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Expanded));
545
546 // Dock closes in the right position if the right sidebar is opened
547 cx.move_dock(DockAnchor::Right);
548 cx.open_sidebar(SidebarSide::Left);
549 cx.assert_dock_position(DockPosition::Shown(DockAnchor::Right));
550 cx.open_sidebar(SidebarSide::Right);
551 cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Right));
552 cx.close_sidebar(SidebarSide::Right);
553
554 // Dock in bottom position ignores sidebars
555 cx.move_dock(DockAnchor::Bottom);
556 cx.open_sidebar(SidebarSide::Left);
557 cx.open_sidebar(SidebarSide::Right);
558 cx.assert_dock_position(DockPosition::Shown(DockAnchor::Bottom));
559
560 // Opening the dock in the right position closes the right sidebar
561 cx.move_dock(DockAnchor::Right);
562 cx.assert_sidebar_closed(SidebarSide::Right);
563 }
564
565 #[gpui::test]
566 async fn test_focusing_panes_shows_and_hides_dock(cx: &mut TestAppContext) {
567 let mut cx = DockTestContext::new(cx).await;
568
569 // Focusing an item not in the dock when expanded hides the dock
570 let center_item = cx.add_item_to_center_pane();
571 cx.move_dock(DockAnchor::Expanded);
572 let dock_item = cx
573 .dock_items()
574 .get(0)
575 .cloned()
576 .expect("Dock should have an item at this point");
577 center_item.update(&mut cx, |_, cx| cx.focus_self());
578 cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Expanded));
579
580 // Focusing an item not in the dock when not expanded, leaves the dock open but inactive
581 cx.move_dock(DockAnchor::Right);
582 center_item.update(&mut cx, |_, cx| cx.focus_self());
583 cx.assert_dock_position(DockPosition::Shown(DockAnchor::Right));
584 cx.assert_dock_pane_inactive();
585 cx.assert_workspace_pane_active();
586
587 // Focusing an item in the dock activates it's pane
588 dock_item.update(&mut cx, |_, cx| cx.focus_self());
589 cx.assert_dock_position(DockPosition::Shown(DockAnchor::Right));
590 cx.assert_dock_pane_active();
591 cx.assert_workspace_pane_inactive();
592 }
593
594 #[gpui::test]
595 async fn test_toggle_dock_focus(cx: &mut TestAppContext) {
596 let cx = DockTestContext::new(cx).await;
597
598 cx.move_dock(DockAnchor::Right);
599 cx.assert_dock_pane_active();
600 cx.hide_dock();
601 cx.move_dock(DockAnchor::Right);
602 cx.assert_dock_pane_active();
603 }
604
605 #[gpui::test]
606 async fn test_activate_next_and_prev_pane(cx: &mut TestAppContext) {
607 let mut cx = DockTestContext::new(cx).await;
608
609 cx.move_dock(DockAnchor::Right);
610 cx.assert_dock_pane_active();
611
612 cx.update_workspace(|workspace, cx| workspace.activate_next_pane(cx));
613 cx.assert_dock_pane_active();
614
615 cx.update_workspace(|workspace, cx| workspace.activate_previous_pane(cx));
616 cx.assert_dock_pane_active();
617 }
618
619 struct DockTestContext<'a> {
620 pub cx: &'a mut TestAppContext,
621 pub window_id: usize,
622 pub workspace: ViewHandle<Workspace>,
623 }
624
625 impl<'a> DockTestContext<'a> {
626 pub async fn new(cx: &'a mut TestAppContext) -> DockTestContext<'a> {
627 Settings::test_async(cx);
628 let fs = FakeFs::new(cx.background());
629
630 cx.update(|cx| init(cx));
631 let project = Project::test(fs, [], cx).await;
632 let (window_id, workspace) = cx.add_window(|cx| {
633 Workspace::new(
634 Default::default(),
635 0,
636 project,
637 default_item_factory,
638 || &[],
639 cx,
640 )
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}