diff --git a/crates/ui-contract/src/contract/body/scroll.rs b/crates/ui-contract/src/contract/body/scroll.rs index bf14c4e24d..e592fcd68a 100644 --- a/crates/ui-contract/src/contract/body/scroll.rs +++ b/crates/ui-contract/src/contract/body/scroll.rs @@ -199,10 +199,11 @@ impl BodyHooks<'_> { } if let Some(view) = self.view { use crate::contract::Pointer as _; - let overlay = scroll_of(props).is_some_and(|scroll| scroll.has_overlay()); + let overlay = scroll_of(props).is_some_and(|scroll| scroll.overlay_hides_at_rest()); for (bar, span) in self.scroll_geometry(props, view, rtl, tokens) { let overflow = span.content.end - span.content.start > span.visible.end - span.visible.start; - // An overlay bar shows while the pointer is over the container, or keeps the press that drags it. + // An overlay bar shows while the pointer is over the container, or keeps the press that drags it, unless + // it shows at rest. let hidden = overlay && !cx.response.contains_pointer() && !cx.part_if_declared(bar.key).is_some_and(|track| track.pressed()); if !overflow || hidden { continue; diff --git a/crates/ui-contract/src/contract/container.rs b/crates/ui-contract/src/contract/container.rs index 39148cc4f3..1561b2433a 100644 --- a/crates/ui-contract/src/contract/container.rs +++ b/crates/ui-contract/src/contract/container.rs @@ -603,7 +603,8 @@ pub enum ScrollIndicator { Strips, /// A bar that takes no room from the content, as a tool rail or an options bar shows its overflow: it lies in the /// container's end padding (its bottom padding for x), over the content where the container has none, and shows - /// only while the pointer is over the container or presses the bar. + /// only while the pointer is over the container or presses the bar, or while the content overflows when the scroll + /// shows it at rest ([`Scroll::at_rest`]). Overlay, } @@ -619,11 +620,13 @@ pub struct Scroll { pub notch: Notch, /// How the overflow shows. pub indicator: ScrollIndicator, + /// Whether an overlay bar also shows while the pointer is away from the container. + pub at_rest: bool, } impl Scroll { const fn on(axes: ScrollAxes) -> Scroll { - Scroll { axes, anchor: ScrollAnchor::Start, notch: Notch::Lines(3.0), indicator: ScrollIndicator::Bar } + Scroll { axes, anchor: ScrollAnchor::Start, notch: Notch::Lines(3.0), indicator: ScrollIndicator::Bar, at_rest: false } } /// Shows the overflow along y as scroll strips instead of a bar. @@ -649,6 +652,18 @@ impl Scroll { matches!(self.indicator, ScrollIndicator::Overlay) } + /// Shows an overlay bar whenever the content overflows, also while the pointer is away from the container. A bar of + /// another indicator already shows at rest. + pub const fn at_rest(mut self) -> Scroll { + self.at_rest = true; + self + } + + /// Whether the overlay bar shows only while the pointer is over the container or presses the bar. + pub const fn overlay_hides_at_rest(&self) -> bool { + self.has_overlay() && !self.at_rest + } + /// Scrolls along y. pub const fn vertical() -> Scroll { Scroll::on(ScrollAxes::Vertical) diff --git a/crates/ui-contract/tests/it/layout/overlay_bar.rs b/crates/ui-contract/tests/it/layout/overlay_bar.rs index 9629f591db..0f186e76ae 100644 --- a/crates/ui-contract/tests/it/layout/overlay_bar.rs +++ b/crates/ui-contract/tests/it/layout/overlay_bar.rs @@ -1,5 +1,6 @@ //! A scroll body with an overlay bar (`Scroll::overlay`): the content keeps the whole view, the bar lies in the -//! container's end padding, and it shows only while the pointer is over the container. +//! container's end padding, and it shows only while the pointer is over the container, or also at rest with +//! `Scroll::at_rest`. use craft_ui::contract::{ContainerProps, Pad, Props as _, Scroll, Show as _, ShowContainer as _, Sizing}; use craft_ui::runtime::env::{Direction, with_env}; @@ -18,10 +19,10 @@ const AT: Rect = Rect { min: Pos2::new(20.0, 20.0), max: Pos2::new(180.0, 120.0) /// A point outside the container. const AWAY: Pos2 = Pos2::new(300.0, 300.0); -/// The padded container that scrolls along y under an overlay bar, holding a probe that fills its width and is three -/// times its height. -fn overlay(ui: &mut Ui, seen: &RefCell) { - let props = ContainerProps::new().pad(Pad::Control).sizing(Sizing::Fill).scroll(Scroll::vertical().overlay()); +/// The padded container that scrolls along y under `scroll`'s overlay bar, holding a probe that fills its width and is +/// three times its height. +fn overlay(ui: &mut Ui, seen: &RefCell, scroll: Scroll) { + let props = ContainerProps::new().pad(Pad::Control).sizing(Sizing::Fill).scroll(scroll); Slab::new(props).id_salt("overlay").show_at(ui, AT, |ui, _| { Probe::new(seen).size(Vec2::new(40.0, AT.height() * 3.0)).sizing(Sizing::FillWidth).id_salt("content").show(ui); }); @@ -32,21 +33,26 @@ fn paints_in(shapes: &[Shape], band: Rect) -> bool { shapes.iter().map(Shape::visual_bounding_rect).any(|bounds| bounds.is_positive() && band.expand(SLACK).contains_rect(bounds)) } +/// The end padding beside `content`, the band an overlay bar lies in. +fn end_band(direction: Direction, content: Rect) -> Rect { + match direction { + Direction::Rtl => Rect::from_x_y_ranges(AT.min.x..=content.min.x, content.min.y.max(AT.min.y)..=AT.max.y), + _ => Rect::from_x_y_ranges(content.max.x..=AT.max.x, content.min.y.max(AT.min.y)..=AT.max.y), + } +} + #[trial] fn an_overlay_bar_takes_no_room_lies_in_the_end_padding_and_shows_only_under_the_pointer() { for direction in [Direction::Ltr, Direction::Rtl] { let seen = RefCell::new(Seen::default()); - let mut show = |ui: &mut Ui| with_env(ui, |o| o.direction = Some(direction), |ui| overlay(ui, &seen)); + let mut show = |ui: &mut Ui| with_env(ui, |o| o.direction = Some(direction), |ui| overlay(ui, &seen, Scroll::vertical().overlay())); let mut frames = Frames::new(); frames.frame(vec![pointer::moved(AWAY)], &mut show); frames.idle(4, &mut show); let content = seen.borrow().rect; let (start, end) = (content.min.x - AT.min.x, AT.max.x - content.max.x); assert!(start > 0.0 && (start - end).abs() < SLACK, "{direction:?}: the content keeps the whole view between equal pads: {content:?}"); - let band = match direction { - Direction::Rtl => Rect::from_x_y_ranges(AT.min.x..=content.min.x, content.min.y.max(AT.min.y)..=AT.max.y), - _ => Rect::from_x_y_ranges(content.max.x..=AT.max.x, content.min.y.max(AT.min.y)..=AT.max.y), - }; + let band = end_band(direction, content); let away = frames.frame_shapes(&mut show); assert!(!paints_in(&away, band), "{direction:?}: no bar shows while the pointer is away"); frames.frame(vec![pointer::moved(Pos2::new(content.center().x, AT.center().y))], &mut show); @@ -55,3 +61,20 @@ fn an_overlay_bar_takes_no_room_lies_in_the_end_padding_and_shows_only_under_the assert!(paints_in(&over, band), "{direction:?}: the bar shows in the end padding while the pointer is over the container"); } } + +#[trial] +fn an_overlay_bar_at_rest_shows_in_the_end_padding_while_the_pointer_is_away() { + for direction in [Direction::Ltr, Direction::Rtl] { + let seen = RefCell::new(Seen::default()); + let scroll = Scroll::vertical().overlay().at_rest(); + let mut show = |ui: &mut Ui| with_env(ui, |o| o.direction = Some(direction), |ui| overlay(ui, &seen, scroll)); + let mut frames = Frames::new(); + frames.frame(vec![pointer::moved(AWAY)], &mut show); + frames.idle(4, &mut show); + let content = seen.borrow().rect; + let (start, end) = (content.min.x - AT.min.x, AT.max.x - content.max.x); + assert!(start > 0.0 && (start - end).abs() < SLACK, "{direction:?}: the bar at rest takes no room either: {content:?}"); + let away = frames.frame_shapes(&mut show); + assert!(paints_in(&away, end_band(direction, content)), "{direction:?}: the bar shows in the end padding while the pointer is away"); + } +} diff --git a/crates/ui-panels/src/rails/tool.rs b/crates/ui-panels/src/rails/tool.rs index ecd19c1854..54d207493a 100644 --- a/crates/ui-panels/src/rails/tool.rs +++ b/crates/ui-panels/src/rails/tool.rs @@ -130,7 +130,8 @@ type FooterFn<'a> = Box; /// and the slots fill the rows in reading order. The control padding surrounds the buttons; a slot that ends a group /// leaves `space.gap.inline` after its row; slots within a group sit `space.gap.tight` apart. The Edit Toolbar button /// follows the last slot; the host's footer stays in view under the slots, which scroll when the room is short under -/// an overlay bar in the end padding that shows only under the pointer (`Scroll::overlay`), and its +/// an overlay bar in the end padding that shows only under the pointer (`Scroll::overlay`), or at rest too with +/// [`bar_at_rest`](ToolRail::bar_at_rest), and its /// buttons show the rail's glyphs. The rail is exactly as tall as its rows and footer at the width it gets /// ([`measure_rail`](ToolRail::measure_rail)). The buttons are one roving group: arrow keys move between them by the /// grid. While the toolbar is edited the buttons are dragged and the rail takes drops, and reports the drop in the @@ -245,6 +246,14 @@ impl<'a, T: Value + MemoProps + PlainData + 'static> ToolRail<'a, T> { self } + /// The overlay bar of a rail too short for its rows also shows while the pointer is away from the rail + /// ([`Scroll::at_rest`]). + #[must_use] + pub fn bar_at_rest(mut self) -> Self { + self.props = std::mem::take(&mut self.props).scroll(Scroll::vertical().overlay().at_rest()); + self + } + /// The cells the rail lays out: its visible slots and the Edit Toolbar button. fn cells(&self) -> usize { visible_slots(self.slots, hidden_in(self.hidden)).len() + usize::from(self.edit_toolbar.is_some()) diff --git a/crates/ui-panels/src/rails/tool/trials.rs b/crates/ui-panels/src/rails/tool/trials.rs index a5067f8bdb..b3a56bbe9c 100644 --- a/crates/ui-panels/src/rails/tool/trials.rs +++ b/crates/ui-panels/src/rails/tool/trials.rs @@ -156,6 +156,32 @@ fn in_a_short_room_the_rows_scroll_above_the_footer_that_stays_in_view() { } } +/// A rail too short for its rows shows its bar in the end padding beside the buttons while the pointer is away only +/// with `bar_at_rest`. +#[trial] +fn a_short_rail_shows_its_bar_at_rest_only_with_bar_at_rest() { + let grid = DockGrid::rail(LABELS.len()).resolved(&DockSizes::of(Fixture::new(|_| {}).ctx())); + for at_rest in [false, true] { + let (slots, last) = (Tracked::new(slots()), Tracked::new(LastUsed::default())); + let mut fixture = Fixture::new(move |ui| { + let rail = ToolRail::new("Tools", &slots, &0, &last); + let rail = if at_rest { rail.bar_at_rest() } else { rail }; + rail.show_rail(ui, |ui, _| { + Button::new("Foot").show(ui); + }); + }); + fixture.room(Vec2::new(grid.width(1), 100.0)).settle(); + let (rail, first, foot) = (rect(&fixture, "Tools"), rect(&fixture, "Move"), rect(&fixture, "Foot")); + assert!(rect(&fixture, "Clone Stamp").bottom() > foot.top(), "at rest {at_rest}: the rows do not fit the room"); + let band = Rect::from_x_y_ranges(first.right()..=rail.right(), rail.top()..=foot.top()); + fixture.frame(vec![moved(rail.right_bottom() + Vec2::splat(200.0))]); + fixture.settle(); + let shapes = fixture.frame_shapes(); + let paints = shapes.iter().map(egui::Shape::visual_bounding_rect).any(|b| b.is_positive() && band.expand(0.5).contains_rect(b)); + assert_eq!(paints, at_rest, "at rest {at_rest}: a bar shows in the end padding with the pointer away: {band:?}"); + } +} + /// The tool rail's buttons are larger than a lone icon button's and rest in a quieter ink than they show under the /// pointer. #[trial]