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.
This commit is contained in:
aiongg
2026-11-12 14:32:49 +00:00
parent e15353918a
commit 66bb57a029
5 changed files with 86 additions and 4 deletions
@@ -503,6 +503,10 @@ impl<C: Container> ShapeHooks<C> 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,
+29
View File
@@ -143,6 +143,12 @@ pub(crate) trait ShapeHooks<C: Component> {
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<ShowAt>, 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<ShowAt>, 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::<crate::contract::InBar>().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.
@@ -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)
}
+7 -4
View File
@@ -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> {
@@ -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<Cell<[u32; 8]>>) {
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");
}