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