From 66bb57a029e911da64133ee6d3ac3064ffdc4f29 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 09:16:01 +0000 Subject: [PATCH] A bar shows each member whole or not at all A leaf or a captioned field in a bar's scrolling body that the view shows only in part gets an empty clip: it neither paints nor takes the pointer until a scroll brings it in whole, and it keeps its Tab stop, so the reveal scrolls it in. Fields were cut at the bar's scroll edge. --- crates/ui-contract/src/contract/body/hooks.rs | 4 ++ crates/ui-contract/src/contract/run.rs | 29 +++++++++++++ crates/ui-contract/src/contract/run/typed.rs | 4 ++ crates/ui-widgets/src/bars/bar.rs | 11 +++-- crates/ui-widgets/tests/it/bars/overflow.rs | 42 +++++++++++++++++++ 5 files changed, 86 insertions(+), 4 deletions(-) diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index e64dcf929..aaa0e6095 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -503,6 +503,10 @@ impl ShapeHooks for BodyHooks<'_> { matches!(comp.props(Internal::new()).layout, layout::BodyLayout::Row { wrap: false, .. }) } + fn member(&self, comp: &C) -> bool { + comp.props(Internal::new()).label.is_some() + } + /// The slots of the container's own header parts this pass, for `PartsCx::header_slot`. fn geometry( &mut self, diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index 572207b11..9e4c090f6 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -143,6 +143,12 @@ pub(crate) trait ShapeHooks { true } + /// Whether the root is one member of a bar's line, shown whole or not at all: a leaf is; a body is when it has a + /// side label (a captioned field), and otherwise only lays out the members it holds. + fn member(&self, _comp: &C) -> bool { + true + } + /// Whether the root fills the width alone: a root that fills both axes takes the room of a `Content` grid column /// and sets its width. fn fills_width_only(&self, comp: &C) -> bool { @@ -552,6 +558,7 @@ pub(crate) trait ErasedPass<'a> { fn sized(&self, ui: &Ui, id: Id, size: Vec2) -> Vec2; fn fills_width(&self) -> bool; fn on_line(&self) -> bool; + fn member(&self) -> bool; fn fills_width_only(&self) -> bool; fn arena(&self) -> bool; fn key_role(&self) -> KeyRole; @@ -842,6 +849,13 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option, h return; } p.real = true; + // A member of a bar's line that its view shows only in part is not shown until a scroll brings it in whole: it + // keeps its stop, so Tab reaches it and the reveal scrolls it in. + let shown_clip = ui.clip_rect(); + let cut = p.p.member() && cut_by_bar_view(env, rect); + if cut { + ui.set_clip_rect(Rect::NOTHING); + } let inspected = p.trace.recording() && inspect::open_element(ui, id, facts.name); let e = &mut *p.p; // 7 parts @@ -1133,6 +1147,21 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option, h let core = e.core_ref(); inspect::close_element(&ctx, id, core.rect, core.content_rect); } + if cut { + ui.set_clip_rect(shown_clip); + } +} + +/// Whether `rect`, in the scrolling body of a bar, lies partly outside the body's view along x. A member wider than the +/// view shows in part, since no scroll shows it whole. +fn cut_by_bar_view(env: &Env, rect: Rect) -> bool { + if !env.values.get::().0 { + return false; + } + let Some(view) = env.lazy.filter(|view| view.axes.0) else { return false }; + // Half a point: less than a pixel shows at any scale the box runs. + let outside = rect.min.x < view.rect.min.x - 0.5 || rect.max.x > view.rect.max.x + 0.5; + outside && rect.width() <= view.rect.width() + 0.5 } /// Step 5's size for `input`: what the component measures plus `extent`, padded and bounded by its style. diff --git a/crates/ui-contract/src/contract/run/typed.rs b/crates/ui-contract/src/contract/run/typed.rs index 5cb2236b7..8482a9cb1 100644 --- a/crates/ui-contract/src/contract/run/typed.rs +++ b/crates/ui-contract/src/contract/run/typed.rs @@ -383,6 +383,10 @@ impl<'a, C: Component> ErasedPass<'a> for TypedPass<'_, 'a, C> { self.hooks.on_line(self.comp) } + fn member(&self) -> bool { + self.hooks.member(self.comp) + } + fn fills_width_only(&self) -> bool { self.hooks.fills_width_only(self.comp) } diff --git a/crates/ui-widgets/src/bars/bar.rs b/crates/ui-widgets/src/bars/bar.rs index 5b89716f7..452f8072e 100644 --- a/crates/ui-widgets/src/bars/bar.rs +++ b/crates/ui-widgets/src/bars/bar.rs @@ -78,10 +78,13 @@ impl Outcome for BarOutput { /// size, an options or status bar's at the small one. They are one roving group unless the bar is made of /// [`tab_stops`](Bar::tab_stops). The bar is as tall as its tallest member plus the control padding, and takes the /// width it is offered. A bar of one row lays its body out as a row; a bar of several rows as a stack whose rows the -/// caller makes with `Row`. A row too long for the bar first narrows its selects and elidable text, the last first, -/// then scrolls, so a member past the edge is reached by Tab and revealed. A rule between groups of members is a -/// `Divider::vertical()`. A bar with `commit_cancel` ends with a cancel and a commit button in its end region, outside -/// the scroll and `space.gap.stack` from the body; they run `BarCommands`. +/// caller makes with `Row`. A member that fills (a `Slider::fill_width`, a fill-width `Row`) takes the room the view +/// leaves. A row too long for the bar first narrows its selects and elidable text, the last first, then scrolls, so a +/// member past the edge is reached by Tab and revealed. A member the view shows only in part (a control, or a captioned +/// field with its caption) is not shown until a scroll brings it in whole. Under right-to-left the bar is the mirror of +/// left-to-right. A rule between groups of members is a `Divider::vertical()`. A bar with `commit_cancel` ends with a +/// cancel and a commit button in its end region, outside the scroll, `space.gap.stack` from the body and on the +/// members' line; they run `BarCommands`. #[derive(crate::contract::Component)] #[component(container)] pub struct Bar<'a> { diff --git a/crates/ui-widgets/tests/it/bars/overflow.rs b/crates/ui-widgets/tests/it/bars/overflow.rs index 9fc19c31f..6d6d01bee 100644 --- a/crates/ui-widgets/tests/it/bars/overflow.rs +++ b/crates/ui-widgets/tests/it/bars/overflow.rs @@ -122,3 +122,45 @@ fn a_member_that_fills_takes_the_room_its_bar_leaves() { ); assert!(wide.2.width() - narrow.2.width() >= grown, "the search field grew from {:?} to {:?} in a 300 pt wider bar", narrow.2, wide.2); } + +/// An options bar `width` wide with eight buttons, and the clicks each button reported. +fn buttons(width: f32) -> (Fixture, Rc>) { + let clicks = Rc::new(Cell::new([0u32; 8])); + let seen = Rc::clone(&clicks); + let mut fixture = Fixture::new(move |ui| { + Bar::new("Options", BarKind::Options).show(ui, |ui, _| { + for at in 0..8 { + if Button::new(format!("Button {at}")).show(ui).clicked() { + let mut counts = seen.get(); + counts[at] += 1; + seen.set(counts); + } + } + }); + }); + fixture.room(Vec2::new(width, 80.0)).settle(); + (fixture, clicks) +} + +#[trial] +fn a_member_the_bar_shows_in_part_is_not_shown_until_it_is_whole() { + let mut checked = 0; + for width in (200..=420).step_by(9) { + let width = width as f32; + let (fixture, _) = buttons(width); + // A button that starts well inside the bar and ends past it. + let straddles = (0..8).find_map(|at| { + let rect = fixture.by_label(&format!("Button {at}"))?.rect; + (rect.min.x + 2.0 < width - 20.0 && rect.max.x > width).then_some((at, rect)) + }); + let Some((at, rect)) = straddles else { continue }; + let (mut fixture, clicks) = buttons(width); + fixture.click_at(egui::pos2(rect.min.x + 2.0, rect.center().y)); + assert_eq!(clicks.get()[at], 0, "at {width}: Button {at} at {rect:?}, shown in part, took a click"); + let first = fixture.by_label("Button 0").expect("the first button"); + fixture.click(&first); + assert_eq!(clicks.get()[0], 1, "at {width}: the first button, shown whole, took no click"); + checked += 1; + } + assert!(checked > 0, "no width put a button across the bar's end"); +}