diff --git a/crates/signed_app/src/pull_requests/detail.rs b/crates/signed_app/src/pull_requests/detail.rs index ec94902..22e9ad1 100644 --- a/crates/signed_app/src/pull_requests/detail.rs +++ b/crates/signed_app/src/pull_requests/detail.rs @@ -509,8 +509,9 @@ impl PullRequestDetailView { .px_4() .py_2() .gap_2() + // The top right corner is the panel toolbar's. .child( - h_flex().gap_2().child(status_badge(status)).child( + h_flex().gap_2().pr_16().child(status_badge(status)).child( div() .flex_1() .min_w_0() @@ -583,6 +584,10 @@ impl Panel for PullRequestDetailView { fn loading(&self, _cx: &App) -> bool { self.loading } + + fn toolbar(&self, _cx: &App) -> bool { + true + } } impl Render for PullRequestDetailView { diff --git a/crates/signed_app/src/repo/commit_diff.rs b/crates/signed_app/src/repo/commit_diff.rs index 7a43530..02e1bcf 100644 --- a/crates/signed_app/src/repo/commit_diff.rs +++ b/crates/signed_app/src/repo/commit_diff.rs @@ -774,9 +774,11 @@ impl CommitDiffView { .gap_2() .border_b_1() .border_color(cx.theme().colors.border) + // The top right corner is the panel toolbar's. .child( div() .min_w_0() + .pr_16() .font_semibold() .truncate() .text_ellipsis() @@ -882,6 +884,10 @@ impl Panel for CommitDiffView { fn loading(&self, _cx: &App) -> bool { self.loading } + + fn toolbar(&self, _cx: &App) -> bool { + true + } } impl Render for CommitDiffView { diff --git a/crates/ui/src/icons/assets/maximize.svg b/crates/ui/src/icons/assets/maximize.svg new file mode 100644 index 0000000..4e7be8a --- /dev/null +++ b/crates/ui/src/icons/assets/maximize.svg @@ -0,0 +1,3 @@ + + + diff --git a/crates/ui/src/icons/assets/minimize.svg b/crates/ui/src/icons/assets/minimize.svg new file mode 100644 index 0000000..57fa22b --- /dev/null +++ b/crates/ui/src/icons/assets/minimize.svg @@ -0,0 +1,3 @@ + + + diff --git a/crates/ui/src/icons/mod.rs b/crates/ui/src/icons/mod.rs index 053b9f6..1dfc866 100644 --- a/crates/ui/src/icons/mod.rs +++ b/crates/ui/src/icons/mod.rs @@ -62,6 +62,8 @@ pub enum IconRole { PanelLeft, UnfoldVertical, Download, + Maximize, + Minimize, } impl IconRole { @@ -112,6 +114,8 @@ impl IconRole { Self::PanelLeft => "panel-left", Self::UnfoldVertical => "unfold-vertical", Self::Download => "download", + Self::Maximize => "maximize", + Self::Minimize => "minimize", } } diff --git a/crates/ui/src/layout/panel.rs b/crates/ui/src/layout/panel.rs index 374d08e..27c4712 100644 --- a/crates/ui/src/layout/panel.rs +++ b/crates/ui/src/layout/panel.rs @@ -6,6 +6,8 @@ //! ([`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 //! 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; @@ -15,8 +17,11 @@ use gpui::{ 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::tooltip::Tooltip; +use crate::icons::{Icon, IconRole}; use crate::layout::resizable::{Resizable, ResizableState}; 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 { 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. @@ -66,6 +76,11 @@ pub trait PanelView: 'static { 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. fn tab(&self, key: ElementId, cx: &App) -> Tab { let mut tab = Tab::new(key, self.title(cx)).busy(self.loading(cx)); @@ -96,6 +111,10 @@ impl PanelView for Entity { fn loading(&self, cx: &App) -> bool { self.read(cx).loading(cx) } + + fn toolbar(&self, cx: &App) -> bool { + self.read(cx).toolbar(cx) + } } impl 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" + ); +}