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