refactor: add panel zoom and close toolbar
Rust / build (macos-latest, stable) (push) Waiting to run
Rust / build (ubuntu-latest, stable) (push) Waiting to run
Rust / build (windows-latest, stable) (push) Waiting to run
Rust / build (macos-latest, stable) (pull_request) Waiting to run
Rust / build (ubuntu-latest, stable) (pull_request) Waiting to run
Rust / build (windows-latest, stable) (pull_request) Waiting to run

This commit is contained in:
2026-11-24 10:35:57 +07:00
parent 3f9e4e1c12
commit 875e7f3582
7 changed files with 391 additions and 53 deletions
@@ -509,8 +509,9 @@ impl PullRequestDetailView {
.px_4() .px_4()
.py_2() .py_2()
.gap_2() .gap_2()
// The top right corner is the panel toolbar's.
.child( .child(
h_flex().gap_2().child(status_badge(status)).child( h_flex().gap_2().pr_16().child(status_badge(status)).child(
div() div()
.flex_1() .flex_1()
.min_w_0() .min_w_0()
@@ -583,6 +584,10 @@ impl Panel for PullRequestDetailView {
fn loading(&self, _cx: &App) -> bool { fn loading(&self, _cx: &App) -> bool {
self.loading self.loading
} }
fn toolbar(&self, _cx: &App) -> bool {
true
}
} }
impl Render for PullRequestDetailView { impl Render for PullRequestDetailView {
@@ -774,9 +774,11 @@ impl CommitDiffView {
.gap_2() .gap_2()
.border_b_1() .border_b_1()
.border_color(cx.theme().colors.border) .border_color(cx.theme().colors.border)
// The top right corner is the panel toolbar's.
.child( .child(
div() div()
.min_w_0() .min_w_0()
.pr_16()
.font_semibold() .font_semibold()
.truncate() .truncate()
.text_ellipsis() .text_ellipsis()
@@ -882,6 +884,10 @@ impl Panel for CommitDiffView {
fn loading(&self, _cx: &App) -> bool { fn loading(&self, _cx: &App) -> bool {
self.loading self.loading
} }
fn toolbar(&self, _cx: &App) -> bool {
true
}
} }
impl Render for CommitDiffView { impl Render for CommitDiffView {
+3
View File
@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none">
<path d="M10.25 3.75H3.75V10.25" stroke="black" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/><path d="M20.25 13.75V20.25H13.75" stroke="black" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/><path d="M4.52344 4.52344L10.25 10.25" stroke="black" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/><path d="M13.75 13.75L19.4766 19.4766" stroke="black" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
</svg>

After

Width:  |  Height:  |  Size: 555 B

+3
View File
@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none">
<path d="M10.25 3.75V10.25M10.25 10.25H3.75M10.25 10.25L3.75 3.75" stroke="black" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/><path d="M13.75 20.25V13.75M13.75 13.75H20.25M13.75 13.75L20.25 20.25" stroke="black" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
</svg>

After

Width:  |  Height:  |  Size: 383 B

+4
View File
@@ -62,6 +62,8 @@ pub enum IconRole {
PanelLeft, PanelLeft,
UnfoldVertical, UnfoldVertical,
Download, Download,
Maximize,
Minimize,
} }
impl IconRole { impl IconRole {
@@ -112,6 +114,8 @@ impl IconRole {
Self::PanelLeft => "panel-left", Self::PanelLeft => "panel-left",
Self::UnfoldVertical => "unfold-vertical", Self::UnfoldVertical => "unfold-vertical",
Self::Download => "download", Self::Download => "download",
Self::Maximize => "maximize",
Self::Minimize => "minimize",
} }
} }
+200 -52
View File
@@ -6,6 +6,8 @@
//! ([`PanelStack::insert_split_at`]), with a drag divider between the panes; a //! ([`PanelStack::insert_split_at`]), with a drag divider between the panes; a
//! double-click restores half. While no main panel is open the stack shows the //! double-click restores half. While no main panel is open the stack shows the
//! empty state its owner set ([`PanelStack::set_empty`]). //! empty state its owner set ([`PanelStack::set_empty`]).
//! A panel can ask for a zoom and close toolbar over its pane's top right
//! corner ([`Panel::toolbar`]); zooming shows that panel alone until toggled back.
use std::sync::Arc; use std::sync::Arc;
@@ -15,8 +17,11 @@ use gpui::{
ParentElement as _, Pixels, Render, SharedString, StyleRefinement, Styled as _, Window, div, ParentElement as _, Pixels, Render, SharedString, StyleRefinement, Styled as _, Window, div,
}; };
use crate::components::common::{ResultLogErr as _, v_flex}; use crate::components::button::{Button, ButtonVariant};
use crate::components::common::{ResultLogErr as _, Sizable as _, h_flex, v_flex};
use crate::components::tabs::{Tab, Tabs, TabsVariant}; use crate::components::tabs::{Tab, Tabs, TabsVariant};
use crate::components::tooltip::Tooltip;
use crate::icons::{Icon, IconRole};
use crate::layout::resizable::{Resizable, ResizableState}; use crate::layout::resizable::{Resizable, ResizableState};
use crate::theme::{ActiveTheme as _, PANE_MIN_SIZE, RESIZE_HANDLE_WIDTH}; use crate::theme::{ActiveTheme as _, PANE_MIN_SIZE, RESIZE_HANDLE_WIDTH};
@@ -40,6 +45,11 @@ pub trait Panel: Render + 'static {
fn loading(&self, cx: &App) -> bool { fn loading(&self, cx: &App) -> bool {
false false
} }
/// Whether the panel shows the zoom and close toolbar while a split is on screen.
fn toolbar(&self, cx: &App) -> bool {
false
}
} }
/// An object-safe handle to a panel, so apps can hold panels by kind. /// An object-safe handle to a panel, so apps can hold panels by kind.
@@ -66,6 +76,11 @@ pub trait PanelView: 'static {
false false
} }
/// Whether the panel shows the zoom and close toolbar.
fn toolbar(&self, cx: &App) -> bool {
false
}
/// Builds the panel's tab from its title, prefix, and loading state. /// Builds the panel's tab from its title, prefix, and loading state.
fn tab(&self, key: ElementId, cx: &App) -> Tab { fn tab(&self, key: ElementId, cx: &App) -> Tab {
let mut tab = Tab::new(key, self.title(cx)).busy(self.loading(cx)); let mut tab = Tab::new(key, self.title(cx)).busy(self.loading(cx));
@@ -96,6 +111,10 @@ impl<T: Panel> PanelView for Entity<T> {
fn loading(&self, cx: &App) -> bool { fn loading(&self, cx: &App) -> bool {
self.read(cx).loading(cx) self.read(cx).loading(cx)
} }
fn toolbar(&self, cx: &App) -> bool {
self.read(cx).toolbar(cx)
}
} }
impl<P: PanelView + ?Sized> PanelView for Arc<P> { impl<P: PanelView + ?Sized> PanelView for Arc<P> {
@@ -118,6 +137,10 @@ impl<P: PanelView + ?Sized> PanelView for Arc<P> {
fn loading(&self, cx: &App) -> bool { fn loading(&self, cx: &App) -> bool {
(**self).loading(cx) (**self).loading(cx)
} }
fn toolbar(&self, cx: &App) -> bool {
(**self).toolbar(cx)
}
} }
/// Which stack, main or split, a panel opens into. /// Which stack, main or split, a panel opens into.
@@ -232,6 +255,8 @@ pub struct PanelStack {
split_side: SplitSide, split_side: SplitSide,
/// The main pane width the divider was last seeded at, to tell a drag from a window resize. /// The main pane width the divider was last seeded at, to tell a drag from a window resize.
opened_at: Option<Pixels>, opened_at: Option<Pixels>,
/// The panel shown alone while it is zoomed.
zoomed: Option<SharedString>,
/// The divider position, kept only while a split is on screen. /// The divider position, kept only while a split is on screen.
sizes: Entity<ResizableState>, sizes: Entity<ResizableState>,
} }
@@ -270,6 +295,7 @@ impl PanelStack {
split_width: None, split_width: None,
split_side: SplitSide::Trailing, split_side: SplitSide::Trailing,
opened_at: None, opened_at: None,
zoomed: None,
sizes: ResizableState::new(cx), 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<Self>) {
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<Self>) {
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<Self>) { fn close_at(&mut self, slot: PanelSlot, index: usize, cx: &mut Context<Self>) {
let was_shown = self.stack(slot).selected() == Some(index); let was_shown = self.stack(slot).selected() == Some(index);
if !self.stack_mut(slot).remove(index) { if !self.stack_mut(slot).remove(index) {
@@ -478,7 +521,7 @@ impl PanelStack {
.is_some_and(|empty| empty.split.is_some()) .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<Self>) { fn forget_divider(&mut self, cx: &mut Context<Self>) {
if self.is_split_shown() { if self.is_split_shown() {
return; return;
@@ -487,6 +530,7 @@ impl PanelStack {
self.split_width = None; self.split_width = None;
self.split_side = SplitSide::Trailing; self.split_side = SplitSide::Trailing;
self.opened_at = None; self.opened_at = None;
self.zoomed = None;
if !self.sizes.read(cx).sizes().is_empty() { if !self.sizes.read(cx).sizes().is_empty() {
self.sizes.update(cx, |state, cx| state.set_sizes([], cx)); self.sizes.update(cx, |state, cx| state.set_sizes([], cx));
@@ -538,6 +582,87 @@ impl PanelStack {
) )
.into_any_element() .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<Self>) -> Option<AnyElement> {
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<AnyElement>, cx: &App) -> Div {
island(
div()
.size_full()
.relative()
.child(content)
.children(toolbar),
cx,
)
}
} }
impl Render for PanelStack { 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| { div().flex_1().min_w_0().h_full().p_2().pt_1().map(|this| {
match split { match split {
Some(split) => { Some(split) => match zoomed {
let main = island(div().size_full().child(main), cx); // A zoomed panel fills the row; its toolbar is the way back.
let split = island(div().size_full().child(split), cx); Some(slot) => {
let (first, last) = if leading { let content = match slot {
(split, main) PanelSlot::Main => main,
} else { PanelSlot::Split => split,
(main, split) };
}; this.child(div().w_full().h_full().child(self.pane(
let first_width = width.unwrap_or((extent - gutter) / 2.0).max(PANE_MIN_SIZE); content,
self.toolbar(slot, cx),
if !self.resizable { cx,
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 => {
None => this.child( let main = self.pane(main, self.toolbar(PanelSlot::Main, cx), cx);
div() let split = self.pane(split, self.toolbar(PanelSlot::Split, cx), cx);
.w_full() let (first, last) = if leading {
.h_full() (split, main)
.child(island(div().size_full().child(main), cx)), } 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,
))),
} }
}) })
} }
+169
View File
@@ -20,12 +20,22 @@ const WINDOW_HEIGHT: f32 = 768.0;
/// A full-size marker the tests measure through its debug selector. /// A full-size marker the tests measure through its debug selector.
struct TagView { struct TagView {
selector: SharedString, selector: SharedString,
/// Whether the marker asks for the zoom and close toolbar.
toolbar: bool,
} }
impl TagView { impl TagView {
fn new(selector: &'static str) -> Self { fn new(selector: &'static str) -> Self {
Self { Self {
selector: SharedString::from(selector), 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 { fn title(&self, _cx: &App) -> SharedString {
self.selector.clone() self.selector.clone()
} }
fn toolbar(&self, _cx: &App) -> bool {
self.toolbar
}
} }
struct PanelHarness { struct PanelHarness {
@@ -60,6 +74,11 @@ fn tag(cx: &mut App, selector: &'static str) -> Entity<TagView> {
cx.new(|_| TagView::new(selector)) 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<TagView> {
cx.new(|_| TagView::with_toolbar(selector))
}
/// Opens the test window over a stack seeded by `seed`. /// Opens the test window over a stack seeded by `seed`.
fn stack_window( fn stack_window(
cx: &mut TestAppContext, 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" "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"
);
}