PanelView for Arc {
@@ -118,6 +137,10 @@ impl PanelView for Arc {
fn loading(&self, cx: &App) -> bool {
(**self).loading(cx)
}
+
+ fn toolbar(&self, cx: &App) -> bool {
+ (**self).toolbar(cx)
+ }
}
/// Which stack, main or split, a panel opens into.
@@ -232,6 +255,8 @@ pub struct PanelStack {
split_side: SplitSide,
/// The main pane width the divider was last seeded at, to tell a drag from a window resize.
opened_at: Option,
+ /// The panel shown alone while it is zoomed.
+ zoomed: Option,
/// The divider position, kept only while a split is on screen.
sizes: Entity,
}
@@ -270,6 +295,7 @@ impl PanelStack {
split_width: None,
split_side: SplitSide::Trailing,
opened_at: None,
+ zoomed: None,
sizes: ResizableState::new(cx),
}
}
@@ -435,6 +461,23 @@ impl PanelStack {
}
}
+ /// Shows the slot's shown panel alone, or the split again when it already is.
+ fn toggle_zoom(&mut self, slot: PanelSlot, cx: &mut Context) {
+ let active = self.stack(slot).active.clone();
+ self.zoomed = if self.is_split_shown() && self.zoomed != active {
+ active
+ } else {
+ None
+ };
+ cx.notify();
+ }
+
+ fn close_active(&mut self, slot: PanelSlot, cx: &mut Context) {
+ if let Some(index) = self.stack(slot).selected() {
+ self.close_at(slot, index, cx);
+ }
+ }
+
fn close_at(&mut self, slot: PanelSlot, index: usize, cx: &mut Context) {
let was_shown = self.stack(slot).selected() == Some(index);
if !self.stack_mut(slot).remove(index) {
@@ -478,7 +521,7 @@ impl PanelStack {
.is_some_and(|empty| empty.split.is_some())
}
- /// Drops the divider position, width, and side while no split is on screen, so the next split opens at its start width.
+ /// Drops the divider position, width, side, and zoom while no split is on screen, so the next split opens at its start width.
fn forget_divider(&mut self, cx: &mut Context) {
if self.is_split_shown() {
return;
@@ -487,6 +530,7 @@ impl PanelStack {
self.split_width = None;
self.split_side = SplitSide::Trailing;
self.opened_at = None;
+ self.zoomed = None;
if !self.sizes.read(cx).sizes().is_empty() {
self.sizes.update(cx, |state, cx| state.set_sizes([], cx));
@@ -538,6 +582,87 @@ impl PanelStack {
)
.into_any_element()
}
+
+ /// The zoom and close buttons over a pane's top right corner, when its shown panel asks for them.
+ fn toolbar(&self, slot: PanelSlot, cx: &Context) -> Option {
+ if !self.stack(slot).active()?.toolbar(cx) {
+ return None;
+ }
+
+ let zoomed = self.stack(slot).active == self.zoomed;
+ let enabled = self.is_split_shown();
+
+ let (zoom_id, close_id, pane) = match slot {
+ PanelSlot::Main => ("zoom-main", "close-main", "main"),
+ PanelSlot::Split => ("zoom-split", "close-split", "split"),
+ };
+
+ let change = cx.weak_entity();
+ let close = change.clone();
+ let zoom_prefix = self.id.clone();
+ let close_prefix = zoom_prefix.clone();
+
+ Some(
+ h_flex()
+ .absolute()
+ .top_2()
+ .right_2()
+ .gap_1()
+ .child(
+ Button::new((self.id.clone(), zoom_id))
+ .icon(Icon::named(if zoomed {
+ IconRole::Minimize
+ } else {
+ IconRole::Maximize
+ }))
+ .variant(ButtonVariant::Ghost)
+ .xsmall()
+ .fully_rounded(true)
+ .tab_stop(false)
+ .disabled(!enabled)
+ .tooltip(Tooltip::text(if zoomed {
+ "Unzoom panel"
+ } else {
+ "Zoom panel"
+ }))
+ .debug_selector(move || format!("{zoom_prefix}-zoom-{pane}"))
+ .on_click(move |_, _, cx| {
+ change
+ .update(cx, |stack, cx| stack.toggle_zoom(slot, cx))
+ .log_err();
+ }),
+ )
+ .child(
+ Button::new((self.id.clone(), close_id))
+ .icon(IconRole::Close)
+ .variant(ButtonVariant::Ghost)
+ .xsmall()
+ .fully_rounded(true)
+ .tab_stop(false)
+ .disabled(!enabled)
+ .tooltip(Tooltip::text("Close panel"))
+ .debug_selector(move || format!("{close_prefix}-close-{pane}"))
+ .on_click(move |_, _, cx| {
+ close
+ .update(cx, |stack, cx| stack.close_active(slot, cx))
+ .log_err();
+ }),
+ )
+ .into_any_element(),
+ )
+ }
+
+ /// The island around a pane's content, with its toolbar over the top right corner.
+ fn pane(&self, content: AnyElement, toolbar: Option, cx: &App) -> Div {
+ island(
+ div()
+ .size_full()
+ .relative()
+ .child(content)
+ .children(toolbar),
+ cx,
+ )
+ }
}
impl Render for PanelStack {
@@ -576,59 +701,82 @@ impl Render for PanelStack {
},
};
+ // The pane shown alone while its panel is zoomed.
+ let zoomed = self.zoomed.as_ref().and_then(|zoomed| {
+ [PanelSlot::Main, PanelSlot::Split]
+ .into_iter()
+ .find(|slot| split.is_some() && self.stack(*slot).active.as_ref() == Some(zoomed))
+ });
+
div().flex_1().min_w_0().h_full().p_2().pt_1().map(|this| {
match split {
- Some(split) => {
- let main = island(div().size_full().child(main), cx);
- let split = island(div().size_full().child(split), cx);
- let (first, last) = if leading {
- (split, main)
- } else {
- (main, split)
- };
- let first_width = width.unwrap_or((extent - gutter) / 2.0).max(PANE_MIN_SIZE);
-
- if !self.resizable {
- this.child(
- div()
- .w_full()
- .h_full()
- .flex()
- .flex_row()
- .child(div().flex_none().w(first_width).h_full().child(first))
- .child(div().flex_none().w(gutter))
- .child(div().flex_1().min_w_0().h_full().child(last)),
- )
- } else {
- // A split opened at a width keeps it across window resizes until the divider is dragged.
- let current = self.sizes.read(cx).sizes().first().copied();
- let keeps_width = self.split_width.is_some() && current == self.opened_at;
-
- if current.is_none() || (keeps_width && self.opened_at != Some(first_width))
- {
- self.sizes.update(cx, |state, cx| {
- state.set_pane_width(0, first_width, cx);
- });
- self.opened_at = Some(first_width);
- }
-
- this.child(
- div().w_full().h_full().child(
- Resizable::new((self.id.clone(), "split"), self.sizes.clone())
- .axis(Axis::Horizontal)
- .pane(first)
- .pane(last)
- .h_full(),
- ),
- )
+ Some(split) => match zoomed {
+ // A zoomed panel fills the row; its toolbar is the way back.
+ Some(slot) => {
+ let content = match slot {
+ PanelSlot::Main => main,
+ PanelSlot::Split => split,
+ };
+ this.child(div().w_full().h_full().child(self.pane(
+ content,
+ self.toolbar(slot, cx),
+ cx,
+ )))
}
- }
- None => this.child(
- div()
- .w_full()
- .h_full()
- .child(island(div().size_full().child(main), cx)),
- ),
+ None => {
+ let main = self.pane(main, self.toolbar(PanelSlot::Main, cx), cx);
+ let split = self.pane(split, self.toolbar(PanelSlot::Split, cx), cx);
+ let (first, last) = if leading {
+ (split, main)
+ } else {
+ (main, split)
+ };
+ let first_width =
+ width.unwrap_or((extent - gutter) / 2.0).max(PANE_MIN_SIZE);
+
+ if !self.resizable {
+ this.child(
+ div()
+ .w_full()
+ .h_full()
+ .flex()
+ .flex_row()
+ .child(div().flex_none().w(first_width).h_full().child(first))
+ .child(div().flex_none().w(gutter))
+ .child(div().flex_1().min_w_0().h_full().child(last)),
+ )
+ } else {
+ // A split opened at a width keeps it across window resizes until the divider is dragged.
+ let current = self.sizes.read(cx).sizes().first().copied();
+ let keeps_width =
+ self.split_width.is_some() && current == self.opened_at;
+
+ if current.is_none()
+ || (keeps_width && self.opened_at != Some(first_width))
+ {
+ self.sizes.update(cx, |state, cx| {
+ state.set_pane_width(0, first_width, cx);
+ });
+ self.opened_at = Some(first_width);
+ }
+
+ this.child(
+ div().w_full().h_full().child(
+ Resizable::new((self.id.clone(), "split"), self.sizes.clone())
+ .axis(Axis::Horizontal)
+ .pane(first)
+ .pane(last)
+ .h_full(),
+ ),
+ )
+ }
+ }
+ },
+ None => this.child(div().w_full().h_full().child(self.pane(
+ main,
+ self.toolbar(PanelSlot::Main, cx),
+ cx,
+ ))),
}
})
}
diff --git a/crates/ui/tests/panel.rs b/crates/ui/tests/panel.rs
index 2709fe1..97156c3 100644
--- a/crates/ui/tests/panel.rs
+++ b/crates/ui/tests/panel.rs
@@ -20,12 +20,22 @@ const WINDOW_HEIGHT: f32 = 768.0;
/// A full-size marker the tests measure through its debug selector.
struct TagView {
selector: SharedString,
+ /// Whether the marker asks for the zoom and close toolbar.
+ toolbar: bool,
}
impl TagView {
fn new(selector: &'static str) -> Self {
Self {
selector: SharedString::from(selector),
+ toolbar: false,
+ }
+ }
+
+ fn with_toolbar(selector: &'static str) -> Self {
+ Self {
+ toolbar: true,
+ ..Self::new(selector)
}
}
}
@@ -43,6 +53,10 @@ impl Panel for TagView {
fn title(&self, _cx: &App) -> SharedString {
self.selector.clone()
}
+
+ fn toolbar(&self, _cx: &App) -> bool {
+ self.toolbar
+ }
}
struct PanelHarness {
@@ -60,6 +74,11 @@ fn tag(cx: &mut App, selector: &'static str) -> Entity {
cx.new(|_| TagView::new(selector))
}
+/// A marker view that shows the zoom and close toolbar.
+fn toolbar_tag(cx: &mut App, selector: &'static str) -> Entity {
+ cx.new(|_| TagView::with_toolbar(selector))
+}
+
/// Opens the test window over a stack seeded by `seed`.
fn stack_window(
cx: &mut TestAppContext,
@@ -820,3 +839,153 @@ fn a_split_after_a_leading_one_opens_after_the_main_panel(cx: &mut TestAppContex
"the split opens after the main panel again"
);
}
+
+/// A panel's toolbar zooms it to fill the row and back, and closes it.
+#[gpui::test]
+fn the_toolbar_zooms_its_panel_and_closes_it(cx: &mut TestAppContext) {
+ let (mut cx, _stack) = stack_window(cx, |stack, cx| {
+ main_only(stack, cx);
+ stack.insert(
+ PanelSlot::Split,
+ "split",
+ Arc::new(toolbar_tag(cx, "panel-test-split")),
+ cx,
+ );
+ });
+
+ let pane = cx
+ .debug_bounds("panel-test-split")
+ .expect("split panel laid out");
+ let zoom = cx
+ .debug_bounds("panel-harness-zoom-split")
+ .expect("the split pane shows a zoom button");
+ assert!(
+ zoom.right() <= pane.right() && zoom.top() >= pane.top(),
+ "the toolbar sits over the pane's top right: {:?}",
+ zoom
+ );
+ assert!(
+ cx.debug_bounds("panel-harness-zoom-main").is_none(),
+ "a panel without the toolbar shows none"
+ );
+
+ cx.simulate_click(zoom.center(), Modifiers::none());
+ cx.run_until_parked();
+ let zoomed = cx
+ .debug_bounds("panel-test-split")
+ .expect("split panel laid out zoomed");
+ assert!(
+ cx.debug_bounds("panel-test-main").is_none(),
+ "zooming hides the main pane"
+ );
+ assert!(
+ cx.debug_bounds("resize-handle").is_none(),
+ "and the divider"
+ );
+ assert!(zoomed.size.width >= px(WINDOW_WIDTH - 40.0));
+
+ cx.simulate_click(zoom.center(), Modifiers::none());
+ cx.run_until_parked();
+ let restored = cx
+ .debug_bounds("panel-test-split")
+ .expect("split panel laid out restored");
+ assert!(
+ cx.debug_bounds("panel-test-main").is_some(),
+ "unzooming brings the main pane back"
+ );
+ assert!(restored.size.width < px(WINDOW_WIDTH / 2.0));
+
+ let close = cx
+ .debug_bounds("panel-harness-close-split")
+ .expect("the split pane shows a close button");
+ cx.simulate_click(close.center(), Modifiers::none());
+ cx.run_until_parked();
+ assert!(
+ cx.debug_bounds("panel-test-split").is_none(),
+ "close closes the panel"
+ );
+ assert!(
+ cx.debug_bounds("panel-harness-zoom-split").is_none(),
+ "and its toolbar leaves with it"
+ );
+}
+
+/// Without a split on screen, the toolbar's buttons do nothing.
+#[gpui::test]
+fn toolbar_buttons_do_nothing_without_a_split(cx: &mut TestAppContext) {
+ let (mut cx, stack) = stack_window(cx, |stack, cx| {
+ stack.insert(
+ PanelSlot::Main,
+ "main",
+ Arc::new(toolbar_tag(cx, "panel-test-main")),
+ cx,
+ );
+ });
+ let before = cx
+ .debug_bounds("panel-test-main")
+ .expect("main panel laid out");
+ let zoom = cx
+ .debug_bounds("panel-harness-zoom-main")
+ .expect("the main pane shows a zoom button");
+ let close = cx
+ .debug_bounds("panel-harness-close-main")
+ .expect("the main pane shows a close button");
+
+ cx.simulate_click(zoom.center(), Modifiers::none());
+ cx.simulate_click(close.center(), Modifiers::none());
+ cx.run_until_parked();
+
+ assert_eq!(
+ cx.debug_bounds("panel-test-main")
+ .expect("main panel laid out"),
+ before,
+ "the buttons are disabled without a split"
+ );
+ cx.update(|_, cx| {
+ assert!(
+ stack
+ .read(cx)
+ .is_active(PanelSlot::Main, &SharedString::from("main"))
+ );
+ });
+}
+
+/// Zooming belongs to the panel: showing another split panel ends it.
+#[gpui::test]
+fn zoom_ends_when_another_split_panel_is_shown(cx: &mut TestAppContext) {
+ let (mut cx, _stack) = stack_window(cx, |stack, cx| {
+ main_only(stack, cx);
+ stack.insert(
+ PanelSlot::Split,
+ "second",
+ Arc::new(tag(cx, "panel-test-second")),
+ cx,
+ );
+ stack.insert(
+ PanelSlot::Split,
+ "first",
+ Arc::new(toolbar_tag(cx, "panel-test-first")),
+ cx,
+ );
+ });
+ let zoom = cx
+ .debug_bounds("panel-harness-zoom-split")
+ .expect("zoom button");
+ cx.simulate_click(zoom.center(), Modifiers::none());
+ cx.run_until_parked();
+ assert!(
+ cx.debug_bounds("panel-test-main").is_none(),
+ "the first panel is zoomed"
+ );
+
+ let second = cx
+ .debug_bounds("panel-harness-split-tabs-tab-panel-test-second")
+ .expect("the second panel's pill tab");
+ cx.simulate_click(second.center(), Modifiers::none());
+ cx.run_until_parked();
+ assert!(cx.debug_bounds("panel-test-second").is_some());
+ assert!(
+ cx.debug_bounds("panel-test-main").is_some(),
+ "the split is back once another panel is shown"
+ );
+}