layout: add sidebar toggle placement options
This commit is contained in:
@@ -6,8 +6,8 @@ use gpui::{
|
||||
VisualTestContext, Window, div, point, px,
|
||||
};
|
||||
use gpui_native_data::sidebar::Sidebar;
|
||||
use gpui_native_layout::shell::{LayoutState, Shell};
|
||||
use gpui_native_theme::{ActiveTheme as _, Size, Theme};
|
||||
use gpui_native_layout::shell::{LayoutState, Shell, SidebarToggles};
|
||||
use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, Size, Theme};
|
||||
|
||||
struct Harness {
|
||||
state: Entity<LayoutState>,
|
||||
@@ -95,6 +95,11 @@ fn left_sidebar_starts_visible_and_right_hidden() {
|
||||
|
||||
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(),
|
||||
@@ -144,6 +149,14 @@ 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()));
|
||||
@@ -322,3 +335,281 @@ mod alerts {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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<Self>,
|
||||
) -> impl gpui::IntoElement {
|
||||
div().size_full()
|
||||
}
|
||||
}
|
||||
|
||||
struct DockHarness {
|
||||
state: Entity<LayoutState>,
|
||||
dock: Entity<DockState>,
|
||||
}
|
||||
|
||||
impl Render for DockHarness {
|
||||
fn render(
|
||||
&mut self,
|
||||
_window: &mut Window,
|
||||
_cx: &mut gpui::Context<Self>,
|
||||
) -> 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<LayoutState>, 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", "One", |_, cx| {
|
||||
cx.new(|_| DockPanel).into()
|
||||
}));
|
||||
for index in 0..14 {
|
||||
let tag = format!("panel-{index}");
|
||||
let id = tag.clone();
|
||||
dock.register_panel(PanelDescriptor::new(id, tag, |_, cx| {
|
||||
cx.new(|_| DockPanel).into()
|
||||
}));
|
||||
}
|
||||
});
|
||||
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<LayoutState>, 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"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user