From e461247ce0bc11bb9400ba5fcf7d1f1eaee5ef34 Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Wed, 18 Nov 2026 21:23:01 +0000 Subject: [PATCH 1/2] Levelled row units measure each level apart and in a finite region A ribbon group's levels show the same bodies under the same ids, so in the row's counting sweep each narrower level reused the record the full level left, every level reported one width and the plan collapsed every group; the sweep at the given levels reused it too, so each later pass measured every group again. Each level now measures as a layout probe. The level measure ran in an infinite-width sizing region placed at the parent's cursor, which egui starts at negative infinity in a right-to-left row, giving a NaN region. Sizing regions now start at the rest of the parent's region, and a level measures at the row's width with its room unbounded along x. DIAG prints for the level plan and the collapsed group's popover. --- crates/menu/src/ribbon/group.rs | 9 +++++++++ crates/ui-contract/src/layout/body.rs | 15 +++++++++++++-- crates/ui-contract/src/runtime/egui/sizing.rs | 6 ++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/crates/menu/src/ribbon/group.rs b/crates/menu/src/ribbon/group.rs index 64b67c08d..7a96562af 100644 --- a/crates/menu/src/ribbon/group.rs +++ b/crates/menu/src/ribbon/group.rs @@ -70,7 +70,16 @@ impl<'a> RibbonGroup<'a> { /// popup the content opens does not close the popover: the overlay family keeps it open. fn collapsed(&self, ui: &mut Ui, add: &mut dyn FnMut(&mut Ui)) { let button = Button::new(self.title.clone()).icon(self.icon.clone()).arrangement(Arrangement::Stacked).show(ui); + if craft_ui::contract::clicked(&button.response) { + eprintln!( + "DIAG a_collapsed_groups_popover_stays_open_while_a_child_popup_is_used: collapsed button {:?} clicked at {:?}, sizing {}", + button.response.id, + button.response.rect, + ui.is_sizing_pass() + ); + } Popover::new(self.title.clone(), Bind::kept()).overlay(|overlay| overlay.opened_by(&button.response, Trigger::Click)).show(ui, |ui, _| { + eprintln!("DIAG a_collapsed_groups_popover_stays_open_while_a_child_popup_is_used: popover body of {:?} runs", self.title.as_str()); with_env( ui, |overrides| { diff --git a/crates/ui-contract/src/layout/body.rs b/crates/ui-contract/src/layout/body.rs index 1774db396..0186ccb85 100644 --- a/crates/ui-contract/src/layout/body.rs +++ b/crates/ui-contract/src/layout/body.rs @@ -364,9 +364,17 @@ pub fn levelled_unit(ui: &mut Ui, key: Id, levels: usize, mut show: impl FnMut(& let rtl = Env::of(ui).direction.is_rtl(); let mut widths = Vec::with_capacity(levels); let mut widest = Vec2::ZERO; + // Each level measures at what it takes in no room: unbounded along x, in a region as wide as the row's. + // An infinite egui region would put right-to-left content's start edge at infinity. + let no_room = own_room.derive_local(|scoped| scoped.unbounded.0 = true); + let width = ui.max_rect().width(); for level in 0..levels.max(1) { - let installed = Env::install_for(ui, own_room.clone()); - let (_, took) = crate::runtime::egui::sizing::run_sized(ui, f32::INFINITY, child::column_layout(rtl), |ui| show(ui, level)); + let installed = Env::install_for(ui, no_room.clone()); + // Every level shows the same bodies under the same ids: each measures as a probe, so no level reuses a + // record another level left, and none serves the sweep at the levels given, which records its own. + let (_, took) = layout_memory::probing(&ctx, || { + crate::runtime::egui::sizing::run_sized(ui, width, child::column_layout(rtl), |ui| show(ui, level)) + }); Env::restore(&ctx, installed); if level == 0 { widest = took; @@ -605,6 +613,9 @@ fn run_shrinking(ui: &mut Ui, id: Id, mut body: impl FnMut(&mut Ui) -> R) -> } else { vec![0; levelled.len()] }; + if !levelled.is_empty() { + eprintln!("DIAG groups_with_the_same_title_keep_their_own_level: row {id:?} room {room} fixed {fixed} level widths {level_widths:?} levels {levels:?}"); + } let width_at = |leaf: &Reported, level: usize| leaf.levels.get(level).copied().unwrap_or_default(); let held: f32 = levelled.iter().zip(&levels).map(|(leaf, level)| width_at(*leaf, *level)).sum(); let widths = if room.is_finite() { diff --git a/crates/ui-contract/src/runtime/egui/sizing.rs b/crates/ui-contract/src/runtime/egui/sizing.rs index b1748c492..785d8dff9 100644 --- a/crates/ui-contract/src/runtime/egui/sizing.rs +++ b/crates/ui-contract/src/runtime/egui/sizing.rs @@ -83,10 +83,12 @@ pub(crate) fn discardable(ctx: &Context) -> bool { ctx.current_pass_index() + 1 < ctx.options(|options| options.max_passes.get()) } -/// Runs `f` in an invisible sizing pass `width` wide, from the parent's cursor, and returns what it took. +/// Runs `f` in an invisible sizing pass `width` wide, at the start of the rest of the parent's region, and returns +/// what it took. /// The parent allocates nothing. pub(crate) fn run_sized(ui: &mut Ui, width: f32, layout: Layout, f: impl FnOnce(&mut Ui) -> R) -> (R, Vec2) { - let room = Rect::from_min_size(ui.cursor().min, Vec2::new(width, f32::INFINITY)); + // The rest of the parent's region, not its cursor: a right-to-left cursor starts at negative infinity. + let room = Rect::from_min_size(ui.available_rect_before_wrap().min, Vec2::new(width, f32::INFINITY)); let mut child = ui.new_child(UiBuilder::new().max_rect(room).layout(layout).sizing_pass().invisible()); let inner = f(&mut child); (inner, child.min_rect().size()) From d6021060f921d7653b8082d5f0a4b6ba78efadc5 Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Wed, 18 Nov 2026 21:23:03 +0000 Subject: [PATCH 2/2] Ribbon slide trial reads where the group is painted The row slides through its container's layer transform, and access bounds stay in that layer's points, so the trial read the same x on every frame. It now reads the group's rect from the ribbon's inspect record, which maps each element through its layer. DIAG prints for the slide and the run counts. --- crates/menu/src/ribbon.rs | 5 ++++ crates/menu/tests/it/ribbon.rs | 48 ++++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/crates/menu/src/ribbon.rs b/crates/menu/src/ribbon.rs index 8a5705ca9..b0663e4cd 100644 --- a/crates/menu/src/ribbon.rs +++ b/crates/menu/src/ribbon.rs @@ -163,6 +163,11 @@ fn show_row( let side = if cx.env.direction().is_rtl() { -1.0 } else { 1.0 }; let distance = cx.style::().map_or(0.0, |style| style.slide); let lead = gap.clamp(-1.0, 1.0) * side * distance; + if gap != 0.0 { + eprintln!( + "DIAG a_tab_change_slides_the_row_in_from_the_new_tabs_side_and_shows_at_rest_under_reduced_motion: place {place} gap {gap} distance {distance} lead {lead}" + ); + } let chevron = cx.style::().map_or(0.0, |style| style.chevron); // The row moves through the slide container's own offset, which places its body on a layer of its own. let shown = Region::slide(lead) diff --git a/crates/menu/tests/it/ribbon.rs b/crates/menu/tests/it/ribbon.rs index 227cdd8cc..3aa52ddaa 100644 --- a/crates/menu/tests/it/ribbon.rs +++ b/crates/menu/tests/it/ribbon.rs @@ -3,6 +3,7 @@ use std::cell::{Cell, RefCell}; use std::collections::HashMap; use std::rc::Rc; +use std::sync::Arc; use craft_behavior::{IconId, MotionMode}; use craft_keymap::CommandRef; @@ -10,6 +11,7 @@ use craft_menu::{GroupLevel, GroupWidths, Ribbon, RibbonGroup, RibbonRow, Ribbon use craft_ui::buttons::{Arrangement, Button, SplitButton}; use craft_ui::contract::{Bind, Entry, Props as _, Show as _}; use craft_ui::runtime::env::{Direction, Env}; +use craft_ui::runtime::inspect::{Element, Recorded}; use craft_ui::testing::{Fixture, is_measuring, last_pass_measuring_runs, trial}; use egui::{Event, Key, Modifiers, PointerButton, Pos2, vec2}; @@ -142,6 +144,13 @@ fn a_collapsed_groups_popover_stays_open_while_a_child_popup_is_used() { assert!(fixture.by_label("Paste").is_none(), "the group is collapsed"); let button = fixture.by_label("Clipboard").expect("the collapsed group's button"); fixture.click(&button).idle(3); + eprintln!( + "DIAG a_collapsed_groups_popover_stays_open_while_a_child_popup_is_used: clicked {:?}; after: Clipboard {:?} Paste {:?} Wide {:?}", + button.rect, + fixture.by_label("Clipboard").map(|target| target.rect), + fixture.by_label("Paste").map(|target| target.rect), + fixture.by_label("Wide").map(|target| target.rect) + ); assert!(fixture.by_label("Paste").is_some(), "the button opens the group's popover"); fixture.key(Key::ArrowDown, Modifiers::ALT).idle(3); fixture.key(Key::Enter, Modifiers::NONE).idle(3); @@ -151,14 +160,22 @@ fn a_collapsed_groups_popover_stays_open_while_a_child_popup_is_used() { assert!(fixture.by_label("Paste").is_none(), "a press outside closes the popover"); } -/// The painted x of the group titled `title`, once the fixture has run a frame. -fn group_x(fixture: &Fixture, title: &str) -> f32 { - fixture.by_label(title).map_or(f32::NAN, |target| target.rect.left()) +/// The ribbon's inspected record of the latest real run. +type Controls = Rc>>>; + +/// The x at which the latest frame painted the group titled `title`: its rect in the ribbon's record, which maps each +/// element through the layer it shows on (the slide's), unlike its access bounds, which stay in that layer's points. +fn group_x(controls: &Controls, title: &str) -> f32 { + let recorded = controls.borrow(); + let named = |element: &&Element| element.part.is_none() && element.facts.as_ref().and_then(|facts| facts.name.as_deref()) == Some(title); + let elements: &[Element] = recorded.as_deref().map_or(&[], |recorded| recorded.elements.as_slice()); + elements.iter().filter(named).min_by_key(|element| element.depth).map_or(f32::NAN, |element| element.rect.left()) } -/// A ribbon of three tabs with one group each, showing the tab `shown` holds. -fn slide_rig(shown: &Rc>, direction: Direction, motion: MotionMode) -> Fixture { +/// A ribbon of three tabs with one group each, showing the tab `shown` holds; `controls` takes its record. +fn slide_rig(shown: &Rc>, controls: &Controls, direction: Direction, motion: MotionMode) -> Fixture { let state = shown.clone(); + let record = controls.clone(); let mut fixture = Fixture::new(move |ui| { let tabs = [RibbonTab::new(0_usize, "One"), RibbonTab::new(1, "Two"), RibbonTab::new(2, "Three")]; let out = Ribbon::new("Ribbon", &tabs, Bind::given(state.get())).show(ui, |row, tab| match tab { @@ -169,6 +186,9 @@ fn slide_rig(shown: &Rc>, direction: Direction, motion: MotionMode) if let Some(next) = out.shown { state.set(next); } + if let Some(controls) = out.controls { + *record.borrow_mut() = Some(controls); + } }); fixture.direction(direction).motion(motion).room(vec2(600.0, 300.0)); fixture.settle(); @@ -180,14 +200,21 @@ fn slide_rig(shown: &Rc>, direction: Direction, motion: MotionMode) /// the release (the click's idle frame) is the first that shows the new row. fn slide(direction: Direction, motion: MotionMode, from: usize, to: usize) -> (f32, f32) { let shown = Rc::new(Cell::new(from)); - let mut fixture = slide_rig(&shown, direction, motion); + let controls: Controls = Rc::new(RefCell::new(None)); + let mut fixture = slide_rig(&shown, &controls, direction, motion); let tab = fixture.by_label(["One", "Two", "Three"][to]).expect("the tab to change to"); fixture.click(&tab); assert_eq!(shown.get(), to, "the click on the tab showed it"); let title = ["Alpha", "Beta", "Gamma"][to]; - let first = group_x(&fixture, title); + let first = group_x(&controls, title); fixture.idle(120); - (first, group_x(&fixture, title)) + let rest = group_x(&controls, title); + eprintln!( + "DIAG a_tab_change_slides_the_row_in_from_the_new_tabs_side_and_shows_at_rest_under_reduced_motion: {from}->{to} rtl {} first {first} rest {rest} access {:?}", + direction.is_rtl(), + fixture.by_label(title).map(|target| target.rect) + ); + (first, rest) } /// A tab change slides the new group row in from the side of the new tab (mirrored in right-to-left) and reaches @@ -269,6 +296,11 @@ fn a_settled_ribbon_runs_no_group_body_again_and_a_level_change_runs_only_that_g assert!(KEYS.iter().all(|key| level(&runs, key) == GroupLevel::Full), "every group shows in full in a wide room"); clear(&runs); fixture.idle(1); + eprintln!( + "DIAG a_settled_ribbon_runs_no_group_body_again_and_a_level_change_runs_only_that_group: idle pass runs {:?}, measuring bodies {:?}", + runs.borrow(), + last_pass_measuring_runs(fixture.ctx()) + ); assert!(KEYS.iter().all(|key| measured(&runs, key) == 0), "a pass with unchanged inputs measures no group: {:?}", runs.borrow()); assert!(last_pass_measuring_runs(fixture.ctx()).is_empty(), "a pass with unchanged inputs runs no measuring run");