#![cfg(feature = "shell")] use gpui::{ Entity, InteractiveElement as _, Modifiers, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement as _, Pixels, Render, Styled as _, TestAppContext, VisualTestContext, Window, div, point, px, }; use gpui_native_data::sidebar::Sidebar; use gpui_native_layout::shell::{LayoutState, Shell, SidebarToggles}; use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, Size, Theme}; struct Harness { state: Entity, } impl Render for Harness { fn render( &mut self, _window: &mut Window, _cx: &mut gpui::Context, ) -> impl gpui::IntoElement { div().size_full().child( Shell::new("test-layout", self.state.clone()) .left_sidebar(Sidebar::new("test-left")) .center( div() .size_full() .debug_selector(|| "test-center".to_string()), ) .right_sidebar( div() .size_full() .debug_selector(|| "test-right".to_string()), ), ) } } fn layout_window(cx: &mut TestAppContext) -> (Entity, VisualTestContext) { cx.update(Theme::init); let state = cx.update(LayoutState::new); let state_for_window = state.clone(); let window = cx.add_window(move |_, _| Harness { state: state_for_window, }); let mut visual = VisualTestContext::from_window(window.into(), cx); redraw(&mut visual); (state, visual) } fn redraw(visual: &mut VisualTestContext) { visual.update(|window, cx| { window.draw(cx).clear(cx); }); } fn bounds(visual: &mut VisualTestContext, selector: &'static str) -> Option> { visual.debug_bounds(selector) } fn center_of(bounds: gpui::Bounds) -> gpui::Point { point(bounds.center().x, bounds.center().y) } fn press(visual: &mut VisualTestContext, at: gpui::Point, click_count: usize) { visual.simulate_event(MouseDownEvent { position: at, click_count, modifiers: Modifiers::default(), button: MouseButton::Left, first_mouse: false, }); visual.simulate_event(MouseUpEvent { position: at, click_count, modifiers: Modifiers::default(), button: MouseButton::Left, }); } fn drag(visual: &mut VisualTestContext, from: gpui::Point, to: gpui::Point) { visual.simulate_mouse_down(from, MouseButton::Left, Modifiers::default()); visual.simulate_event(MouseMoveEvent { position: to, pressed_button: Some(MouseButton::Left), modifiers: Modifiers::default(), }); visual.simulate_mouse_up(to, MouseButton::Left, Modifiers::default()); } #[test] fn left_sidebar_starts_visible_and_right_hidden() { let mut cx = TestAppContext::single(); let (state, mut visual) = layout_window(&mut cx); assert!(state.read_with(&cx, |state, _| state.is_left_sidebar_visible())); assert!(!state.read_with(&cx, |state, _| state.is_right_sidebar_visible())); assert_eq!( state.read_with(&cx, |state, _| state.sidebar_toggles()), SidebarToggles::Tabbar, "the toggles default to the dock's tab bar" ); assert!( bounds(&mut visual, "sidebar-test-left").is_some(), "left region missing" ); assert!( bounds(&mut visual, "test-center").is_some(), "center missing" ); assert!( bounds(&mut visual, "test-right").is_none(), "right region must be hidden by default" ); } #[test] fn toggling_a_sidebar_shows_and_hides_its_region() { let mut cx = TestAppContext::single(); let (state, mut visual) = layout_window(&mut cx); cx.update(|cx| { state.update(cx, |state, cx| state.set_right_sidebar_visible(true, cx)); }); redraw(&mut visual); assert!( bounds(&mut visual, "test-right").is_some(), "right region missing" ); cx.update(|cx| { state.update(cx, |state, cx| state.set_left_sidebar_visible(false, cx)); }); redraw(&mut visual); assert!( bounds(&mut visual, "sidebar-test-left").is_none(), "left region lingering" ); // The center keeps filling the shell. assert!( bounds(&mut visual, "test-center").is_some(), "center missing" ); } #[test] fn the_header_toggle_hides_its_sidebar() { let mut cx = TestAppContext::single(); let (state, mut visual) = layout_window(&mut cx); // The strip carries the toggle only under the strips placement. cx.update(|cx| { state.update(cx, |state, cx| { state.set_sidebar_toggles(SidebarToggles::Strips, cx) }); }); redraw(&mut visual); // The toggle sits at the trailing end of the sidebar's header strip, an // icon-only small button: derive its center from the theme metrics. let metrics = cx.update(|cx| cx.theme().sizes.resolve(Size::default())); let small = cx.update(|cx| cx.theme().sizes.resolve(Size::Small)); let strip_height = cx.update(|cx| cx.theme().titlebar_height); let region = bounds(&mut visual, "sidebar-test-left").expect("left sidebar missing"); // The Sidebar fills the content area below the strip; the toggle sits at // the strip's trailing end, one strip height above the sidebar's top. let toggle = point( region.right() - metrics.dense_padding_x - small.control_height / 2.0, region.top() - strip_height / 2.0, ); press(&mut visual, toggle, 1); redraw(&mut visual); assert!( !state.read_with(&cx, |state, _| state.is_left_sidebar_visible()), "the toggle should hide the left sidebar" ); assert!(bounds(&mut visual, "sidebar-test-left").is_none()); } #[test] fn dragging_the_seam_resizes_and_clamps() { let mut cx = TestAppContext::single(); let (state, mut visual) = layout_window(&mut cx); let before = state.read_with(&cx, |state, _| state.left_sidebar_width()); let seam = bounds(&mut visual, "resize-handle").expect("left seam missing"); drag( &mut visual, center_of(seam), center_of(seam) + point(px(50.), px(0.)), ); assert_eq!( state.read_with(&cx, |state, _| state.left_sidebar_width()), before + px(50.) ); // A huge drag clamps instead of collapsing the sidebar. let seam = bounds(&mut visual, "resize-handle").expect("seam missing"); drag( &mut visual, center_of(seam), center_of(seam) + point(px(2000.), px(0.)), ); assert_eq!( state.read_with(&cx, |state, _| state.left_sidebar_width()), px(480.) ); // Dragging back left shrinks it to the low clamp, no drift from the first. let seam = bounds(&mut visual, "resize-handle").expect("seam missing"); drag( &mut visual, center_of(seam), center_of(seam) - point(px(2000.), px(0.)), ); assert_eq!( state.read_with(&cx, |state, _| state.left_sidebar_width()), px(160.) ); } #[test] fn the_right_seam_grows_the_right_sidebar_leftwards() { let mut cx = TestAppContext::single(); let (state, mut visual) = layout_window(&mut cx); // Hide the left sidebar so the shell has exactly one seam. cx.update(|cx| { state.update(cx, |state, cx| state.set_left_sidebar_visible(false, cx)); state.update(cx, |state, cx| state.set_right_sidebar_visible(true, cx)); }); redraw(&mut visual); let before = state.read_with(&cx, |state, _| state.right_sidebar_width()); let seam = bounds(&mut visual, "resize-handle").expect("right seam missing"); // Dragging left (negative) grows the right sidebar. drag( &mut visual, center_of(seam), center_of(seam) - point(px(30.), px(0.)), ); let grown = state.read_with(&cx, |state, _| state.right_sidebar_width()); assert_eq!(grown, before + px(30.)); // The right region's width follows the state. let region = bounds(&mut visual, "test-right").expect("right region missing"); assert_eq!(region.size.width, grown); } /// Toasts and alerts live on the shell's [`LayoutState`]; pushing one must /// show up in the rendered shell with no observing or manual overlay. #[cfg(feature = "toast")] mod toasts { use super::*; use gpui_native_core::toast::{Toast, ToastAction}; use std::time::Duration; #[test] fn a_pushed_toast_renders_in_the_shell() { let mut cx = TestAppContext::single(); let (state, mut visual) = layout_window(&mut cx); let id = cx.update(|cx| { state.update(cx, |state, cx| { state.push_toast( Toast::new("Draft deleted").action(ToastAction::new("Undo", |_| { // A real app restores the draft here. })), cx, ) }) }); // No redraw, no observation: the shell repaints its own overlay. cx.run_until_parked(); assert!( bounds(&mut visual, "toast-pill").is_some(), "the pushed toast must appear in the shell's overlay" ); cx.update(|cx| { state.update(cx, |state, cx| state.dismiss_toast(id, cx)); }); cx.run_until_parked(); // The fade-out keeps the pill mounted briefly. assert!( bounds(&mut visual, "toast-pill").is_some(), "the dismissed toast must stay mounted through its fade-out" ); cx.executor().advance_clock(Duration::from_secs(2)); cx.run_until_parked(); assert!( bounds(&mut visual, "toast-pill").is_none(), "the dismissed toast must leave the overlay once faded" ); } } #[cfg(feature = "alert")] mod alerts { use super::*; use gpui_native_core::alert::{Alert, AlertSeverity}; #[test] fn a_pushed_alert_renders_in_the_shell() { let mut cx = TestAppContext::single(); let (state, mut visual) = layout_window(&mut cx); cx.update(|cx| { state.update(cx, |state, cx| { state.push_alert( Alert::new("shell-alert") .title("Backed up") .severity(AlertSeverity::Info), cx, ); }) }); cx.run_until_parked(); assert!( bounds(&mut visual, "alert-entry").is_some(), "the pushed alert must appear in the shell's overlay" ); cx.update(|cx| { state.update(cx, |state, cx| state.clear_alerts(cx)); }); cx.run_until_parked(); assert!( bounds(&mut visual, "alert-entry").is_none(), "cleared alerts must leave the overlay" ); } } /// Toggles placed in the dock's tab bar need both the shell and the dock. #[cfg(all(feature = "shell", feature = "dock"))] mod tabbar_toggles { use super::*; use gpui::{AppContext as _, ScrollDelta, ScrollEvent}; use gpui_native_layout::dock::{Dock, DockState, PanelDescriptor}; use gpui_native_layout::shell::SidebarToggles; struct DockPanel; impl Render for DockPanel { fn render( &mut self, _window: &mut Window, _cx: &mut gpui::Context, ) -> impl gpui::IntoElement { div().size_full() } } struct DockHarness { state: Entity, dock: Entity, } impl Render for DockHarness { fn render( &mut self, _window: &mut Window, _cx: &mut gpui::Context, ) -> impl gpui::IntoElement { div().size_full().child( Shell::new("test-layout", self.state.clone()) .left_sidebar(Sidebar::new("test-left")) .center( Dock::new("test-dock", self.dock.clone()) .sidebar_toggles(self.state.clone()), ) .right_sidebar( div() .size_full() .debug_selector(|| "test-right".to_string()), ), ) } } fn tabbar_window( cx: &mut TestAppContext, open: &[&str], ) -> (Entity, VisualTestContext) { cx.update(Theme::init); let state = cx.update(LayoutState::new); let dock = cx.update(|cx| { let dock = DockState::new(cx); dock.update(cx, |dock, _| { dock.register_panel( PanelDescriptor::new("one", |_, cx| cx.new(|_| DockPanel).into()).title("One"), ); for index in 0..14 { let tag = format!("panel-{index}"); let id = tag.clone(); dock.register_panel( PanelDescriptor::new(id, |_, cx| cx.new(|_| DockPanel).into()).title(tag), ); } }); dock }); let state_for_window = state.clone(); let dock_for_window = dock.clone(); let window = cx.add_window(move |_, _| DockHarness { state: state_for_window, dock: dock_for_window, }); let mut visual = VisualTestContext::from_window(window.into(), cx); visual.update(|window, cx| { for id in open { dock.update(cx, |dock, cx| dock.open_panel(id, window, cx)); } }); redraw(&mut visual); (state, visual) } fn place_toggles_in_tabbar(state: &Entity, cx: &mut TestAppContext) { cx.update(|cx| { state.update(cx, |state, cx| { state.set_sidebar_toggles(SidebarToggles::Tabbar, cx) }); }); } #[test] fn tabbar_toggles_sit_at_the_bar_edges() { let mut cx = TestAppContext::single(); let (state, mut visual) = tabbar_window(&mut cx, &["one"]); place_toggles_in_tabbar(&state, &mut cx); redraw(&mut visual); let metrics = cx.update(|cx| cx.theme().sizes.resolve(Size::default())); let small = cx.update(|cx| cx.theme().sizes.resolve(Size::Small)); let bar = bounds(&mut visual, "test-dock-pane-0-bar").expect("tab bar missing"); let left = bounds(&mut visual, "sidebar-toggle-left").expect("left toggle missing"); let right = bounds(&mut visual, "sidebar-toggle-right").expect("right toggle missing"); let scroll = bounds(&mut visual, "test-dock-pane-0-tabs-scroll") .expect("tab scroll container missing"); // The left toggle leads the bar after its leading pad; the right // toggle trails it at the trailing pad. The button's outer width is // its borders plus dense padding around the icon. let toggle_width = BORDER_WIDTH * 2.0 + small.dense_padding_x * 2.0 + small.icon_size; let left_center = bar.left() + metrics.dense_padding_x + toggle_width / 2.0; let right_center = bar.right() - metrics.dense_padding_x - toggle_width / 2.0; assert!( (left.center().x - left_center).abs() < px(1.0), "the left toggle must lead the tab bar" ); assert!( (right.center().x - right_center).abs() < px(1.0), "the right toggle must trail the tab bar" ); assert!( left.center().y > bar.top() && left.center().y < bar.bottom(), "the toggles must sit inside the tab bar" ); let gap = scroll.left() - left.right(); assert!( (gap - px(4.0)).abs() < px(0.5), "the toggle and the tabs must sit one tab gap apart" ); } #[test] fn an_overflowing_tab_bar_keeps_the_trailing_toggle_pinned() { let mut cx = TestAppContext::single(); let open = [ "one", "panel-0", "panel-1", "panel-2", "panel-3", "panel-4", "panel-5", "panel-6", "panel-7", "panel-8", "panel-9", "panel-10", "panel-11", "panel-12", "panel-13", ]; let (state, mut visual) = tabbar_window(&mut cx, &open); place_toggles_in_tabbar(&state, &mut cx); redraw(&mut visual); let metrics = cx.update(|cx| cx.theme().sizes.resolve(Size::default())); let small = cx.update(|cx| cx.theme().sizes.resolve(Size::Small)); let bar = bounds(&mut visual, "test-dock-pane-0-bar").expect("tab bar missing"); let left = bounds(&mut visual, "sidebar-toggle-left").expect("left toggle missing"); let right = bounds(&mut visual, "sidebar-toggle-right").expect("right toggle missing"); let scroll = bounds(&mut visual, "test-dock-pane-0-tabs-scroll") .expect("tab scroll container missing"); // The tabs outgrow the bar: the drag strip collapses, the scroll // viewport spans between the toggles, and the trailing toggle stays // pinned inside the bar instead of being pushed out of the window. let leading_gap = scroll.left() - left.right(); assert!((leading_gap - px(4.0)).abs() < px(0.5)); let trailing_gap = right.left() - scroll.right(); assert!( (trailing_gap - px(4.0)).abs() < px(0.5), "the viewport must reach the trailing toggle" ); assert!( right.right() <= bar.right() + px(1.0), "the trailing toggle must stay inside the bar" ); let toggle_width = BORDER_WIDTH * 2.0 + small.dense_padding_x * 2.0 + small.icon_size; let right_center = bar.right() - metrics.dense_padding_x - toggle_width / 2.0; assert!( (right.center().x - right_center).abs() < px(1.0), "the trailing toggle must stay at the bar's trailing pad" ); } #[test] fn an_overflowing_tab_bar_scrolls_with_the_wheel() { let mut cx = TestAppContext::single(); let open = [ "one", "panel-0", "panel-1", "panel-2", "panel-3", "panel-4", "panel-5", "panel-6", "panel-7", "panel-8", "panel-9", "panel-10", "panel-11", "panel-12", "panel-13", ]; let (state, mut visual) = tabbar_window(&mut cx, &open); place_toggles_in_tabbar(&state, &mut cx); redraw(&mut visual); let scroll = bounds(&mut visual, "test-dock-pane-0-tabs-scroll") .expect("tab scroll container missing"); let fit = bounds(&mut visual, "test-dock-pane-0-tabs-fit").expect("tab strip missing"); // The pills row is wider than the viewport, so the bar can scroll. assert!( fit.size.width > scroll.size.width + px(1.0), "the tab strip must overflow the bar" ); // A plain vertical wheel over the bar scrolls the strip toward its // end: the bar only scrolls on the x axis, so gpui remaps the delta. visual.simulate_event(ScrollEvent { position: scroll.center(), delta: ScrollDelta::Pixels(point(px(0.), px(-60.))), modifiers: Modifiers::default(), ..Default::default() }); redraw(&mut visual); let fit_after = bounds(&mut visual, "test-dock-pane-0-tabs-fit").expect("tab strip missing"); assert!( fit_after.left() < fit.left() - px(10.0), "the wheel must scroll the tab strip" ); } #[test] fn the_tabbar_toggle_shows_and_hides_the_left_sidebar() { let mut cx = TestAppContext::single(); let (state, mut visual) = tabbar_window(&mut cx, &["one"]); place_toggles_in_tabbar(&state, &mut cx); redraw(&mut visual); let toggle = bounds(&mut visual, "sidebar-toggle-left").expect("left toggle missing"); press(&mut visual, center_of(toggle), 1); redraw(&mut visual); assert!(!state.read_with(&cx, |state, _| state.is_left_sidebar_visible())); assert!(bounds(&mut visual, "sidebar-test-left").is_none()); // The toggle outlives its hidden sidebar and brings it back. let toggle = bounds(&mut visual, "sidebar-toggle-left") .expect("left toggle missing while the sidebar is hidden"); press(&mut visual, center_of(toggle), 1); redraw(&mut visual); assert!(state.read_with(&cx, |state, _| state.is_left_sidebar_visible())); assert!(bounds(&mut visual, "sidebar-test-left").is_some()); } #[test] fn the_tabbar_right_toggle_opens_the_right_sidebar() { let mut cx = TestAppContext::single(); let (state, mut visual) = tabbar_window(&mut cx, &["one"]); place_toggles_in_tabbar(&state, &mut cx); redraw(&mut visual); assert!(bounds(&mut visual, "test-right").is_none()); let toggle = bounds(&mut visual, "sidebar-toggle-right").expect("right toggle missing"); press(&mut visual, center_of(toggle), 1); redraw(&mut visual); assert!(state.read_with(&cx, |state, _| state.is_right_sidebar_visible())); assert!(bounds(&mut visual, "test-right").is_some()); let toggle = bounds(&mut visual, "sidebar-toggle-right").expect("right toggle missing"); press(&mut visual, center_of(toggle), 1); redraw(&mut visual); assert!(!state.read_with(&cx, |state, _| state.is_right_sidebar_visible())); assert!(bounds(&mut visual, "test-right").is_none()); } #[test] fn hidden_placement_renders_no_toggles() { let mut cx = TestAppContext::single(); let (state, mut visual) = layout_window(&mut cx); cx.update(|cx| { state.update(cx, |state, cx| { state.set_sidebar_toggles(SidebarToggles::Hidden, cx) }); }); redraw(&mut visual); assert!( bounds(&mut visual, "sidebar-toggle-left").is_none(), "the strip must not carry a toggle when they are hidden" ); assert!( bounds(&mut visual, "sidebar-test-left").is_some(), "hiding the toggles must not hide the sidebar itself" ); } }