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 cx.subscribe(&pane, Workspace::handle_pane_event).detach();
182
183 Self {
184 pane,
185 panel_sizes: Default::default(),
186 position,
187 default_item_factory,
188 }
189 }
190
191 pub fn pane(&self) -> &ViewHandle<Pane> {
192 &self.pane
193 }
194
195 pub fn visible_pane(&self) -> Option<&ViewHandle<Pane>> {
196 self.position.is_visible().then(|| self.pane())
197 }
198
199 pub fn is_anchored_at(&self, anchor: DockAnchor) -> bool {
200 self.position.is_visible() && self.position.anchor() == anchor
201 }
202
203 pub(crate) fn set_dock_position(
204 workspace: &mut Workspace,
205 new_position: DockPosition,
206 focus: bool,
207 cx: &mut ViewContext<Workspace>,
208 ) {
209 workspace.dock.position = new_position;
210 // Tell the pane about the new anchor position
211 workspace.dock.pane.update(cx, |pane, cx| {
212 pane.set_docked(Some(new_position.anchor()), cx)
213 });
214
215 if workspace.dock.position.is_visible() {
216 // Close the right sidebar if the dock is on the right side and the right sidebar is open
217 if workspace.dock.position.anchor() == DockAnchor::Right {
218 if workspace.right_sidebar().read(cx).is_open() {
219 workspace.toggle_sidebar(SidebarSide::Right, cx);
220 }
221 }
222
223 // Ensure that the pane has at least one item or construct a default item to put in it
224 let pane = workspace.dock.pane.clone();
225 if pane.read(cx).items().next().is_none() {
226 if let Some(item_to_add) = (workspace.dock.default_item_factory)(workspace, cx) {
227 Pane::add_item(workspace, &pane, item_to_add, focus, focus, None, cx);
228 } else {
229 workspace.dock.position = workspace.dock.position.hide();
230 }
231 } else {
232 if focus {
233 cx.focus(&pane);
234 }
235 }
236 } else if let Some(last_active_center_pane) = workspace
237 .last_active_center_pane
238 .as_ref()
239 .and_then(|pane| pane.upgrade(cx))
240 {
241 if focus {
242 cx.focus(&last_active_center_pane);
243 }
244 }
245 cx.emit(crate::Event::DockAnchorChanged);
246 workspace.serialize_workspace(cx);
247 cx.notify();
248 }
249
250 pub fn hide(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
251 Self::set_dock_position(workspace, workspace.dock.position.hide(), true, cx);
252 }
253
254 pub fn show(workspace: &mut Workspace, focus: bool, cx: &mut ViewContext<Workspace>) {
255 Self::set_dock_position(workspace, workspace.dock.position.show(), focus, cx);
256 }
257
258 pub fn hide_on_sidebar_shown(
259 workspace: &mut Workspace,
260 sidebar_side: SidebarSide,
261 cx: &mut ViewContext<Workspace>,
262 ) {
263 if (sidebar_side == SidebarSide::Right && workspace.dock.is_anchored_at(DockAnchor::Right))
264 || workspace.dock.is_anchored_at(DockAnchor::Expanded)
265 {
266 Self::hide(workspace, cx);
267 }
268 }
269
270 pub fn focus_dock(workspace: &mut Workspace, _: &FocusDock, cx: &mut ViewContext<Workspace>) {
271 Self::set_dock_position(workspace, workspace.dock.position.show(), true, cx);
272 }
273
274 pub fn hide_dock(workspace: &mut Workspace, _: &HideDock, cx: &mut ViewContext<Workspace>) {
275 Self::set_dock_position(workspace, workspace.dock.position.hide(), true, cx);
276 }
277
278 pub fn move_dock(
279 workspace: &mut Workspace,
280 new_anchor: DockAnchor,
281 focus: bool,
282 cx: &mut ViewContext<Workspace>,
283 ) {
284 Self::set_dock_position(workspace, DockPosition::Shown(new_anchor), focus, cx);
285 }
286
287 pub fn render(
288 &self,
289 theme: &Theme,
290 anchor: DockAnchor,
291 cx: &mut ViewContext<Workspace>,
292 ) -> Option<AnyElement<Workspace>> {
293 let style = &theme.workspace.dock;
294
295 self.position
296 .is_visible()
297 .then(|| self.position.anchor())
298 .filter(|current_anchor| *current_anchor == anchor)
299 .map(|anchor| match anchor {
300 DockAnchor::Bottom | DockAnchor::Right => {
301 let mut panel_style = style.panel.clone();
302 let (resize_side, initial_size) = if anchor == DockAnchor::Bottom {
303 panel_style.border = Border {
304 top: true,
305 bottom: false,
306 left: false,
307 right: false,
308 ..panel_style.border
309 };
310
311 (Side::Top, style.initial_size_bottom)
312 } else {
313 panel_style.border = Border {
314 top: false,
315 bottom: false,
316 left: true,
317 right: false,
318 ..panel_style.border
319 };
320 (Side::Left, style.initial_size_right)
321 };
322
323 enum DockResizeHandle {}
324
325 let resizable = ChildView::new(&self.pane, cx)
326 .contained()
327 .with_style(panel_style)
328 .with_resize_handle::<DockResizeHandle>(
329 resize_side as usize,
330 resize_side,
331 4.,
332 self.panel_sizes
333 .get(&anchor)
334 .copied()
335 .unwrap_or(initial_size),
336 cx,
337 );
338
339 let size = resizable.current_size();
340 cx.defer(move |workspace, _| {
341 workspace.dock.panel_sizes.insert(anchor, size);
342 });
343
344 if anchor == DockAnchor::Right {
345 resizable.constrained().dynamically(|constraint, _, cx| {
346 SizeConstraint::new(
347 Vector2F::new(20., constraint.min.y()),
348 Vector2F::new(cx.window_size().x() * 0.8, constraint.max.y()),
349 )
350 })
351 } else {
352 resizable.constrained().dynamically(|constraint, _, cx| {
353 SizeConstraint::new(
354 Vector2F::new(constraint.min.x(), 50.),
355 Vector2F::new(constraint.max.x(), cx.window_size().y() * 0.8),
356 )
357 })
358 }
359 .into_any()
360 }
361 DockAnchor::Expanded => {
362 enum ExpandedDockWash {}
363 enum ExpandedDockPane {}
364 Stack::new()
365 .with_child(
366 // Render wash under the dock which when clicked hides it
367 MouseEventHandler::<ExpandedDockWash, _>::new(0, cx, |_, _| {
368 Empty::new()
369 .contained()
370 .with_background_color(style.wash_color)
371 })
372 .capture_all()
373 .on_down(MouseButton::Left, |_, workspace, cx| {
374 Dock::hide_dock(workspace, &Default::default(), cx)
375 })
376 .with_cursor_style(CursorStyle::Arrow),
377 )
378 .with_child(
379 MouseEventHandler::<ExpandedDockPane, _>::new(0, cx, |_state, cx| {
380 ChildView::new(&self.pane, cx)
381 })
382 // Make sure all events directly under the dock pane
383 // are captured
384 .capture_all()
385 .contained()
386 .with_style(style.maximized),
387 )
388 .into_any()
389 }
390 })
391 }
392
393 pub fn position(&self) -> DockPosition {
394 self.position
395 }
396}
397
398#[cfg(test)]
399mod tests {
400 use std::{
401 ops::{Deref, DerefMut},
402 path::PathBuf,
403 sync::Arc,
404 };
405
406 use gpui::{AppContext, BorrowWindowContext, TestAppContext, ViewContext, WindowContext};
407 use project::{FakeFs, Project};
408 use settings::Settings;
409 use theme::ThemeRegistry;
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 AppState, ItemHandle, Workspace,
421 };
422
423 pub fn default_item_factory(
424 _workspace: &mut Workspace,
425 cx: &mut ViewContext<Workspace>,
426 ) -> Option<Box<dyn ItemHandle>> {
427 Some(Box::new(cx.add_view(|_| TestItem::new())))
428 }
429
430 #[gpui::test]
431 async fn test_dock_workspace_infinite_loop(cx: &mut TestAppContext) {
432 cx.foreground().forbid_parking();
433 Settings::test_async(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 Some(serialized_workspace),
466 0,
467 project.clone(),
468 Arc::new(AppState {
469 languages: project.read(cx).languages().clone(),
470 themes: ThemeRegistry::new((), cx.font_cache().clone()),
471 client: project.read(cx).client(),
472 user_store: project.read(cx).user_store(),
473 fs: project.read(cx).fs().clone(),
474 build_window_options: |_, _, _| Default::default(),
475 initialize_workspace: |_, _, _| {},
476 dock_default_item_factory: default_item_factory,
477 background_actions: || &[],
478 }),
479 cx,
480 )
481 });
482
483 cx.foreground().run_until_parked();
484 //Should terminate
485 }
486
487 #[gpui::test]
488 async fn test_dock_hides_when_pane_empty(cx: &mut TestAppContext) {
489 let mut cx = DockTestContext::new(cx).await;
490
491 // Closing the last item in the dock hides the dock
492 cx.move_dock(DockAnchor::Right);
493 let old_items = cx.dock_items();
494 assert!(!old_items.is_empty());
495 cx.close_dock_items().await;
496 cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Right));
497
498 // Reopening the dock adds a new item
499 cx.move_dock(DockAnchor::Right);
500 let new_items = cx.dock_items();
501 assert!(!new_items.is_empty());
502 assert!(new_items
503 .into_iter()
504 .all(|new_item| !old_items.contains(&new_item)));
505 }
506
507 #[gpui::test]
508 async fn test_dock_panel_collisions(cx: &mut TestAppContext) {
509 let mut cx = DockTestContext::new(cx).await;
510
511 // Dock closes when expanded for either panel
512 cx.move_dock(DockAnchor::Expanded);
513 cx.open_sidebar(SidebarSide::Left);
514 cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Expanded));
515 cx.close_sidebar(SidebarSide::Left);
516 cx.move_dock(DockAnchor::Expanded);
517 cx.open_sidebar(SidebarSide::Right);
518 cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Expanded));
519
520 // Dock closes in the right position if the right sidebar is opened
521 cx.move_dock(DockAnchor::Right);
522 cx.open_sidebar(SidebarSide::Left);
523 cx.assert_dock_position(DockPosition::Shown(DockAnchor::Right));
524 cx.open_sidebar(SidebarSide::Right);
525 cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Right));
526 cx.close_sidebar(SidebarSide::Right);
527
528 // Dock in bottom position ignores sidebars
529 cx.move_dock(DockAnchor::Bottom);
530 cx.open_sidebar(SidebarSide::Left);
531 cx.open_sidebar(SidebarSide::Right);
532 cx.assert_dock_position(DockPosition::Shown(DockAnchor::Bottom));
533
534 // Opening the dock in the right position closes the right sidebar
535 cx.move_dock(DockAnchor::Right);
536 cx.assert_sidebar_closed(SidebarSide::Right);
537 }
538
539 #[gpui::test]
540 async fn test_focusing_panes_shows_and_hides_dock(cx: &mut TestAppContext) {
541 let mut cx = DockTestContext::new(cx).await;
542
543 // Focusing an item not in the dock when expanded hides the dock
544 let center_item = cx.add_item_to_center_pane();
545 cx.move_dock(DockAnchor::Expanded);
546 let dock_item = cx
547 .dock_items()
548 .get(0)
549 .cloned()
550 .expect("Dock should have an item at this point");
551 center_item.update(&mut cx, |_, cx| cx.focus_self());
552 cx.assert_dock_position(DockPosition::Hidden(DockAnchor::Expanded));
553
554 // Focusing an item not in the dock when not expanded, leaves the dock open but inactive
555 cx.move_dock(DockAnchor::Right);
556 center_item.update(&mut cx, |_, cx| cx.focus_self());
557 cx.assert_dock_position(DockPosition::Shown(DockAnchor::Right));
558 cx.assert_dock_pane_inactive();
559 cx.assert_workspace_pane_active();
560
561 // Focusing an item in the dock activates it's pane
562 dock_item.update(&mut cx, |_, cx| cx.focus_self());
563 cx.assert_dock_position(DockPosition::Shown(DockAnchor::Right));
564 cx.assert_dock_pane_active();
565 cx.assert_workspace_pane_inactive();
566 }
567
568 #[gpui::test]
569 async fn test_toggle_dock_focus(cx: &mut TestAppContext) {
570 let mut cx = DockTestContext::new(cx).await;
571
572 cx.move_dock(DockAnchor::Right);
573 cx.assert_dock_pane_active();
574 cx.hide_dock();
575 cx.move_dock(DockAnchor::Right);
576 cx.assert_dock_pane_active();
577 }
578
579 #[gpui::test]
580 async fn test_activate_next_and_prev_pane(cx: &mut TestAppContext) {
581 let mut cx = DockTestContext::new(cx).await;
582
583 cx.move_dock(DockAnchor::Right);
584 cx.assert_dock_pane_active();
585
586 cx.update_workspace(|workspace, cx| workspace.activate_next_pane(cx));
587 cx.assert_dock_pane_active();
588
589 cx.update_workspace(|workspace, cx| workspace.activate_previous_pane(cx));
590 cx.assert_dock_pane_active();
591 }
592
593 struct DockTestContext<'a> {
594 pub cx: &'a mut TestAppContext,
595 pub window_id: usize,
596 pub workspace: ViewHandle<Workspace>,
597 }
598
599 impl<'a> DockTestContext<'a> {
600 pub async fn new(cx: &'a mut TestAppContext) -> DockTestContext<'a> {
601 Settings::test_async(cx);
602 let fs = FakeFs::new(cx.background());
603
604 cx.update(|cx| init(cx));
605 let project = Project::test(fs, [], cx).await;
606 let (window_id, workspace) = cx.add_window(|cx| {
607 Workspace::new(
608 None,
609 0,
610 project.clone(),
611 Arc::new(AppState {
612 languages: project.read(cx).languages().clone(),
613 themes: ThemeRegistry::new((), cx.font_cache().clone()),
614 client: project.read(cx).client(),
615 user_store: project.read(cx).user_store(),
616 fs: project.read(cx).fs().clone(),
617 build_window_options: |_, _, _| Default::default(),
618 initialize_workspace: |_, _, _| {},
619 dock_default_item_factory: default_item_factory,
620 background_actions: || &[],
621 }),
622 cx,
623 )
624 });
625
626 workspace.update(cx, |workspace, cx| {
627 let left_panel = cx.add_view(|_| TestItem::new());
628 workspace.left_sidebar().update(cx, |sidebar, cx| {
629 sidebar.add_item(
630 "icons/folder_tree_16.svg",
631 "Left Test Panel".to_string(),
632 left_panel.clone(),
633 cx,
634 );
635 });
636
637 let right_panel = cx.add_view(|_| TestItem::new());
638 workspace.right_sidebar().update(cx, |sidebar, cx| {
639 sidebar.add_item(
640 "icons/folder_tree_16.svg",
641 "Right Test Panel".to_string(),
642 right_panel.clone(),
643 cx,
644 );
645 });
646 });
647
648 Self {
649 cx,
650 window_id,
651 workspace,
652 }
653 }
654
655 pub fn workspace<F, T>(&self, read: F) -> T
656 where
657 F: FnOnce(&Workspace, &ViewContext<Workspace>) -> T,
658 {
659 self.workspace.read_with(self.cx, read)
660 }
661
662 pub fn update_workspace<F, T>(&mut self, update: F) -> T
663 where
664 F: FnOnce(&mut Workspace, &mut ViewContext<Workspace>) -> T,
665 {
666 self.workspace.update(self.cx, update)
667 }
668
669 pub fn sidebar<F, T>(&self, sidebar_side: SidebarSide, read: F) -> T
670 where
671 F: FnOnce(&Sidebar, &AppContext) -> T,
672 {
673 self.workspace(|workspace, cx| {
674 let sidebar = match sidebar_side {
675 SidebarSide::Left => workspace.left_sidebar(),
676 SidebarSide::Right => workspace.right_sidebar(),
677 }
678 .read(cx);
679
680 read(sidebar, cx)
681 })
682 }
683
684 pub fn center_pane_handle(&self) -> ViewHandle<Pane> {
685 self.workspace(|workspace, cx| {
686 workspace
687 .last_active_center_pane
688 .clone()
689 .and_then(|pane| pane.upgrade(cx))
690 .unwrap_or_else(|| workspace.center.panes()[0].clone())
691 })
692 }
693
694 pub fn add_item_to_center_pane(&mut self) -> ViewHandle<TestItem> {
695 self.update_workspace(|workspace, cx| {
696 let item = cx.add_view(|_| TestItem::new());
697 let pane = workspace
698 .last_active_center_pane
699 .clone()
700 .and_then(|pane| pane.upgrade(cx))
701 .unwrap_or_else(|| workspace.center.panes()[0].clone());
702 Pane::add_item(
703 workspace,
704 &pane,
705 Box::new(item.clone()),
706 true,
707 true,
708 None,
709 cx,
710 );
711 item
712 })
713 }
714
715 pub fn dock_pane<F, T>(&self, read: F) -> T
716 where
717 F: FnOnce(&Pane, &AppContext) -> T,
718 {
719 self.workspace(|workspace, cx| {
720 let dock_pane = workspace.dock_pane().read(cx);
721 read(dock_pane, cx)
722 })
723 }
724
725 pub fn move_dock(&mut self, anchor: DockAnchor) {
726 self.update_workspace(|workspace, cx| Dock::move_dock(workspace, anchor, true, cx));
727 }
728
729 pub fn hide_dock(&mut self) {
730 self.cx.dispatch_action(self.window_id, HideDock);
731 }
732
733 pub fn open_sidebar(&mut self, sidebar_side: SidebarSide) {
734 if !self.sidebar(sidebar_side, |sidebar, _| sidebar.is_open()) {
735 self.update_workspace(|workspace, cx| workspace.toggle_sidebar(sidebar_side, cx));
736 }
737 }
738
739 pub fn close_sidebar(&mut self, sidebar_side: SidebarSide) {
740 if self.sidebar(sidebar_side, |sidebar, _| sidebar.is_open()) {
741 self.update_workspace(|workspace, cx| workspace.toggle_sidebar(sidebar_side, cx));
742 }
743 }
744
745 pub fn dock_items(&self) -> Vec<ViewHandle<TestItem>> {
746 self.dock_pane(|pane, cx| {
747 pane.items()
748 .map(|item| {
749 item.act_as::<TestItem>(cx)
750 .expect("Dock Test Context uses TestItems in the dock")
751 })
752 .collect()
753 })
754 }
755
756 pub async fn close_dock_items(&mut self) {
757 self.update_workspace(|workspace, cx| {
758 Pane::close_items(workspace, workspace.dock_pane().clone(), cx, |_| true)
759 })
760 .await
761 .expect("Could not close dock items")
762 }
763
764 pub fn assert_dock_position(&self, expected_position: DockPosition) {
765 self.workspace(|workspace, _| assert_eq!(workspace.dock.position, expected_position));
766 }
767
768 pub fn assert_sidebar_closed(&self, sidebar_side: SidebarSide) {
769 assert!(!self.sidebar(sidebar_side, |sidebar, _| sidebar.is_open()));
770 }
771
772 pub fn assert_workspace_pane_active(&self) {
773 assert!(self
774 .center_pane_handle()
775 .read_with(self.cx, |pane, _| pane.is_active()));
776 }
777
778 pub fn assert_workspace_pane_inactive(&self) {
779 assert!(!self
780 .center_pane_handle()
781 .read_with(self.cx, |pane, _| pane.is_active()));
782 }
783
784 pub fn assert_dock_pane_active(&self) {
785 assert!(self.dock_pane(|pane, _| pane.is_active()))
786 }
787
788 pub fn assert_dock_pane_inactive(&self) {
789 assert!(!self.dock_pane(|pane, _| pane.is_active()))
790 }
791 }
792
793 impl<'a> Deref for DockTestContext<'a> {
794 type Target = gpui::TestAppContext;
795
796 fn deref(&self) -> &Self::Target {
797 self.cx
798 }
799 }
800
801 impl<'a> DerefMut for DockTestContext<'a> {
802 fn deref_mut(&mut self) -> &mut Self::Target {
803 &mut self.cx
804 }
805 }
806
807 impl BorrowWindowContext for DockTestContext<'_> {
808 fn read_with<T, F: FnOnce(&WindowContext) -> T>(&self, window_id: usize, f: F) -> T {
809 BorrowWindowContext::read_with(self.cx, window_id, f)
810 }
811
812 fn update<T, F: FnOnce(&mut WindowContext) -> T>(&mut self, window_id: usize, f: F) -> T {
813 BorrowWindowContext::update(self.cx, window_id, f)
814 }
815 }
816}