From 6225657f480b33afa5e981631eaa153dcd4a5ff8 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 13:15:04 +0000 Subject: [PATCH] A rule in a bar shows only with the member after it A component declares itself a rule with Component::RULE; Divider does. In a bar's line the pass holds the shapes a rule painted until the next member decides whether the view shows it: a member shown keeps them, a member the bar hides takes them back, and so does the end of the line for a rule with nothing after it. A settling region's record is taken after that, so a replay shows the line as the run did. A divider was left behind at the bar's end when the members after it were hidden. --- crates/ui-contract/src/contract/body/hooks.rs | 3 + crates/ui-contract/src/contract/container.rs | 4 +- crates/ui-contract/src/contract/mod.rs | 4 + crates/ui-contract/src/contract/run.rs | 76 +++++++++++++++++- crates/ui-contract/src/runtime/pass_state.rs | 1 + crates/ui-widgets/src/bars/bar.rs | 2 +- crates/ui-widgets/src/display/divider.rs | 5 +- crates/ui-widgets/tests/it/bars/overflow.rs | 80 ++++++++++++++++++- 8 files changed, 165 insertions(+), 10 deletions(-) diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 0559163f1..8393137f9 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -852,6 +852,9 @@ impl ShapeHooks for BodyHooks<'_> { shared_labels(ui, id, columns, room.x, self.settle.clone(), &mut labels, |ui| body(ui, bcx)) }); Env::restore(&ctx, region_env); + if lays_out { + crate::contract::run::close_bar_line(&ctx, id); + } let extent = region_ui.min_rect().size(); if lays_out { let placed = crate::runtime::lazy::placed(&ctx, id); diff --git a/crates/ui-contract/src/contract/container.rs b/crates/ui-contract/src/contract/container.rs index 344d475fa..331541362 100644 --- a/crates/ui-contract/src/contract/container.rs +++ b/crates/ui-contract/src/contract/container.rs @@ -644,8 +644,8 @@ pub enum Gap { } /// Set by a bar for its body: its fields put `space.gap.item` between a caption and the control, a vertical divider -/// takes a box wider than its line, a select fits its widest entry up to a field length, and a -/// row too long for its room narrows its last units first. +/// takes a box wider than its line, a select fits its widest entry up to a field length, a rule shows only with the +/// member after it, and a row too long for its room narrows its last units first. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] #[doc(hidden)] pub struct InBar(pub bool); diff --git a/crates/ui-contract/src/contract/mod.rs b/crates/ui-contract/src/contract/mod.rs index b43eb01be..462f41d75 100644 --- a/crates/ui-contract/src/contract/mod.rs +++ b/crates/ui-contract/src/contract/mod.rs @@ -114,6 +114,7 @@ pub use crate::runtime::drag::{DragPreview, DropMark, DropTarget, OsFile, OsFile pub(crate) use body::MOVE_BAND; pub use craft_behavior::ColumnKey; pub use craft_behavior::DropOperation; +pub(crate) use run::BarRules; pub(crate) use run::measure_under; #[cfg(feature = "testing")] pub(crate) use steps::queue_reveal; @@ -352,6 +353,9 @@ pub trait Component: Meta + Sized { const BINDS_VIEW: bool = false; /// How a disabled instance looks: token states (the default), or its enabled look faded. const DISABLED: DisabledLook = DisabledLook::States; + /// Whether the component is a rule between groups of members (a divider). In a bar's line a rule shows only when + /// the member after it shows: a rule before a member the bar hides, or with no member after it, is taken back. + const RULE: bool = false; /// The style type. type Style: ComponentStyle; diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index ae71850d9..f17c8256c 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -430,6 +430,8 @@ pub(crate) struct TypeFacts { pub binds_view: bool, /// `DISABLED` is `Faded`. pub faded: bool, + /// `RULE`. + pub rule: bool, /// The style key's type, which `Auto` ids are counted per. pub style_key: TypeId, } @@ -448,6 +450,7 @@ impl TypeFacts { labelled_after: C::LABELLED_AFTER, binds_view: C::BINDS_VIEW, faded: C::DISABLED == super::DisabledLook::Faded, + rule: C::RULE, style_key: TypeId::of::(), } } @@ -903,10 +906,17 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option, h // scroll brings it in whole: it keeps its stop, so Tab reaches it and the reveal scrolls it in. Only a member in // flow is judged: a part shown at a rect, and a control in a label scope, belong to the member around them. let shown_clip = ui.clip_rect(); - let cut = e.member() && matches!(at, ShowAt::Flow) && !scope::in_label(&ctx) && cut_by_bar_view(env, hit); + let line = if e.member() && matches!(at, ShowAt::Flow) && !scope::in_label(&ctx) { bar_line(env) } else { None }; + let cut = line.is_some() && cut_by_bar_view(env, hit); + // Rules wait for their member only in the run that paints. + let line = line.filter(|_| !crate::runtime::measure::active(&ctx)); if cut { ui.set_clip_rect(Rect::NOTHING); } + // The rules before a member show with it, or are taken back with it. + if let Some(line) = line.filter(|_| !facts.rule) { + settle_rules(&ctx, line, cut); + } let sensing::RootResult { mut response, parts: results, press, activation, activated_member, sense: root_sense } = sense_root(ui, member, RootSpec { id, rect: hit, own_sense: own, own: decls, enabled }, arena); if label.is_some() { @@ -1073,6 +1083,7 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option, h let mut values = AppValues::default(); e.content_env(&mut values); let unfaded = steps::fade(ui, e.core()); + let rule_from = line.filter(|_| facts.rule && !cut).map(|line| (line, next_shape(ui))); steps::paint_surface(ui, env, e.core_ref(), style); steps::paint_part_layers(ui, env, e.core_ref()); // Paint and content stay inside the rect (grown by the type's declared outset), however small the room is. @@ -1083,6 +1094,10 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option, h ui.set_opacity(unfaded); ui.set_clip_rect(outer_clip); steps::paint_focus(ui, e.core_ref(), style); + // A rule a bar's view shows waits for the member after it. + if let Some((line, from)) = rule_from { + hold_rule(ui, line, from); + } ui.set_clip_rect(inside); // 14 content, then `paint_over` if p.trace.mark(Step::Content) { @@ -1155,6 +1170,65 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option, h } } +/// The bar line a component in `env` sits in: the scrolling body of a bar, by its id. +fn bar_line(env: &Env) -> Option { + if !env.values.get::().0 { + return None; + } + env.lazy.filter(|view| view.axes.0).map(|view| view.owner) +} + +/// The rules a bar line shows this pass that wait for the member after them: the shapes each painted, by line. +#[derive(Default)] +pub(crate) struct BarRules(HashMap>); + +/// A rule's paint: `shapes` of the paint list of `layer`, in the pass `pass`. +struct HeldRule { + layer: egui::LayerId, + shapes: std::ops::Range, + pass: u64, +} + +/// The index the next shape `ui` paints gets in its layer's paint list. +fn next_shape(ui: &Ui) -> usize { + ui.ctx().graphics(|layers| layers.get(ui.layer_id()).map_or(0, |list| list.next_idx().0)) +} + +/// Holds the shapes a rule of `line` painted since `from` until the member after it settles them. +fn hold_rule(ui: &Ui, line: Id, from: usize) { + let rule = HeldRule { layer: ui.layer_id(), shapes: from..next_shape(ui), pass: ui.ctx().cumulative_pass_nr() }; + crate::runtime::pass_state::with::(ui.ctx(), |rules| rules.0.entry(line).or_default().push(rule)); +} + +/// Settles the rules of `line` that wait for a member: a member shown keeps them, a member `cut` takes them back. +fn settle_rules(ctx: &Context, line: Id, cut: bool) { + let Some(held) = crate::runtime::pass_state::with::(ctx, |rules| rules.0.remove(&line)) else { return }; + if cut { + take_back(ctx, &held); + } +} + +/// Closes the line of the bar body `line`: a rule no member came after is taken back. +pub(crate) fn close_bar_line(ctx: &Context, line: Id) { + if let Some(held) = crate::runtime::pass_state::with::(ctx, |rules| rules.0.remove(&line)) { + take_back(ctx, &held); + } +} + +/// Empties the shapes of `held` painted this pass. The run record of a settling region is taken when it closes, after +/// this, so a replay repeats the line as it shows. +fn take_back(ctx: &Context, held: &[HeldRule]) { + let pass = ctx.cumulative_pass_nr(); + ctx.graphics_mut(|layers| { + for rule in held.iter().filter(|rule| rule.pass == pass) { + let list = layers.entry(rule.layer); + for at in rule.shapes.clone() { + list.reset_shape(egui::layers::ShapeIdx(at)); + } + } + }); +} + /// 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. pub(crate) fn cut_by_bar_view(env: &Env, rect: Rect) -> bool { diff --git a/crates/ui-contract/src/runtime/pass_state.rs b/crates/ui-contract/src/runtime/pass_state.rs index bcd9d1240..cb8eb44c0 100644 --- a/crates/ui-contract/src/runtime/pass_state.rs +++ b/crates/ui-contract/src/runtime/pass_state.rs @@ -90,6 +90,7 @@ pass_state! { jumps: crate::contract::body::scroll::Jumps, shrink_reports: crate::layout::body::ShrinkReports, least_reports: crate::layout::body::LeastReports, + bar_rules: crate::contract::BarRules, fill_counts: crate::layout::body::FillCounts, cursors: crate::layout::body::Cursors, column_groups: crate::layout::column_group::Open, diff --git a/crates/ui-widgets/src/bars/bar.rs b/crates/ui-widgets/src/bars/bar.rs index 5c836f165..4bb623507 100644 --- a/crates/ui-widgets/src/bars/bar.rs +++ b/crates/ui-widgets/src/bars/bar.rs @@ -83,7 +83,7 @@ impl Outcome for BarOutput { /// 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 select in a bar fits its widest entry, up to a field length. A rule between groups of members is a -/// `Divider::vertical()`. A bar with `commit_cancel` ends with a +/// `Divider::vertical()`; it shows only with the member after it. 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)] diff --git a/crates/ui-widgets/src/display/divider.rs b/crates/ui-widgets/src/display/divider.rs index 1b9a06526..a99eebac8 100644 --- a/crates/ui-widgets/src/display/divider.rs +++ b/crates/ui-widgets/src/display/divider.rs @@ -32,8 +32,8 @@ enum Axis { } /// A separator line in the divider colour, snapped to the pixel grid. A horizontal line takes the width it is -/// offered (at least one control height; in a content-sized column it never widens the column) and a vertical one the height of its row, at least a control's height. Decoration: it is not in the -/// AccessKit tree. +/// offered (at least one control height; in a content-sized column it never widens the column) and a vertical one the height of its row, at least a control's height. In a bar it +/// shows only with the member after it. Decoration: it is not in the AccessKit tree. #[derive(crate::contract::Component)] pub struct Divider { #[common] @@ -60,6 +60,7 @@ impl Component for Divider { const INPUT: craft_behavior::Input = craft_behavior::Input::Display; const SHOWS_LABEL: bool = false; const OVERLAYS: bool = false; + const RULE: bool = true; type Style = DividerStyle; type Output = Response; diff --git a/crates/ui-widgets/tests/it/bars/overflow.rs b/crates/ui-widgets/tests/it/bars/overflow.rs index cfad59ed7..bcd0b16e3 100644 --- a/crates/ui-widgets/tests/it/bars/overflow.rs +++ b/crates/ui-widgets/tests/it/bars/overflow.rs @@ -1,6 +1,6 @@ //! A `Bar` too short for its members: it lays out under right-to-left as the mirror of left-to-right, and its end //! region shares the members' line; a member that fills takes the room the bar leaves; a select fits its widest entry -//! up to a field length. +//! up to a field length; a rule before a member the bar hides is not drawn, and a settled bar replays it so. use craft_behavior::IconId; use craft_behavior::geometry::Constraint; @@ -8,14 +8,15 @@ use craft_i18n::Direction; use craft_theme::ControlSize; use craft_ui::bars::{Bar, BarItem, BarItems, BarKind, ConstraintFields, ItemDisplay}; use craft_ui::buttons::{Button, IconButton}; -use craft_ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _}; +use craft_ui::contract::{Bind, Entry, Lazy, Props as _, Show as _, ShowContainer as _}; use craft_ui::display::{Divider, Text}; use craft_ui::form::Field; use craft_ui::pickers::Select; use craft_ui::runtime::commands::EditCommands; -use craft_ui::testing::{Fixture, trial}; +use craft_ui::testing::layout::in_region; +use craft_ui::testing::{Fixture, Frames, trial}; use craft_ui::value_drag::Slider; -use egui::{Rect, Ui, Vec2}; +use egui::{Pos2, Rect, Shape, Ui, Vec2}; use std::cell::Cell; use std::rc::Rc; @@ -180,3 +181,74 @@ fn a_select_in_a_bar_fits_its_widest_entry_up_to_the_field_length() { assert!(long_bar <= short_plain + SLACK, "a long entry widens the select in a bar past the field length: {long_bar} against {short_plain}"); assert!(long_plain > long_bar + SLACK, "outside a bar the long entry sets the width: {long_plain}, in a bar {long_bar}"); } + +/// A bar `width` wide holding a button, a rule and a wider button; `rects` gets the two buttons' rects. +fn rule_bar(ui: &mut Ui, width: f32, rects: &std::cell::Cell<(Rect, Rect)>) { + in_region(ui, Rect::from_min_size(Pos2::ZERO, Vec2::new(width, 80.0)), |ui| { + Bar::new("Options", BarKind::Options).show(ui, |ui, _| { + let first = Button::new("First").show(ui).response.rect; + Divider::vertical().show(ui); + let second = Button::new("A second, wider button").show(ui).response.rect; + rects.set((first, second)); + }); + }); +} + +/// Whether `shapes` draw anything between the two buttons of `rects`: the rule, there being nothing else. +fn draws_rule(shapes: &[Shape], (first, second): (Rect, Rect)) -> bool { + let between = Rect::from_min_max(Pos2::new(first.max.x, first.min.y - 4.0), Pos2::new(second.min.x, first.max.y + 4.0)); + shapes.iter().map(Shape::visual_bounding_rect).any(|bounds| bounds.is_positive() && between.contains_rect(bounds)) +} + +/// The shapes a bar `width` wide paints once settled, and its buttons' rects. +fn rule_bar_shapes(width: f32) -> (Vec, (Rect, Rect)) { + let rects = std::cell::Cell::new((Rect::NOTHING, Rect::NOTHING)); + let mut frames = Frames::new(); + frames.idle(4, |ui| rule_bar(ui, width, &rects)); + let shapes = frames.frame_shapes(|ui| rule_bar(ui, width, &rects)); + (shapes, rects.get()) +} + +/// A width at which the bar shows the second button only in part: it starts inside the bar and ends past it. +fn cutting_width() -> f32 { + (180..=520) + .step_by(11) + .map(|width| width as f32) + .find(|&width| { + let (_, (_, second)) = rule_bar_shapes(width); + second.min.x + 2.0 < width - 20.0 && second.max.x > width && second.width() < width - 40.0 + }) + .expect("no width put the second button across the bar's end") +} + +#[trial] +fn a_rule_before_a_member_the_bar_hides_is_not_drawn() { + let (shown, rects) = rule_bar_shapes(900.0); + assert!(draws_rule(&shown, rects), "with both buttons whole the rule between them is drawn"); + let (cut, rects) = rule_bar_shapes(cutting_width()); + assert!(!draws_rule(&cut, rects), "the rule before the hidden button is drawn"); +} + +/// A settled region holding a bar whose rule was taken back replays the bar without the rule. +#[trial] +fn a_settled_bar_replays_the_rule_it_took_back() { + let width = cutting_width(); + let runs = std::cell::Cell::new(0); + let rects = std::cell::Cell::new((Rect::NOTHING, Rect::NOTHING)); + let scene = |ui: &mut Ui| { + Lazy::new("bar", 0.0).settles(true).show(ui, |ui| { + runs.set(runs.get() + 1); + rule_bar(ui, width, &rects); + }); + }; + let mut frames = Frames::new(); + frames.frame(vec![], scene); + assert!(runs.get() > 0, "the first frame runs the region"); + frames.idle(30, scene); + for frame in 0..4 { + runs.set(0); + let shapes = frames.frame_shapes(scene); + assert_eq!(runs.get(), 0, "settled frame {frame} ran the region"); + assert!(!draws_rule(&shapes, rects.get()), "settled frame {frame} replays the rule the run took back"); + } +}