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