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