From 3865baefbf896b7685738c2eb3646725e2075a72 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 09:04:46 +0000 Subject: [PATCH] Measuring rect starts at the start edge under right-to-left with a finite room too A content-sized body given a shrink cap in a right-to-left scroll region measured from the region's far end, where its field took no width, so a bar too short for its members laid out differently from left-to-right. --- crates/ui-contract/src/contract/body/hooks.rs | 11 +++-- crates/ui-widgets/tests/it/bars/mod.rs | 1 + crates/ui-widgets/tests/it/bars/overflow.rs | 46 +++++++++++++++++++ 3 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 crates/ui-widgets/tests/it/bars/overflow.rs diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 1406d2dba..275494e84 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -1134,11 +1134,12 @@ impl BodyHooks<'_> { ShowAt::Flow => at.ui.available_rect_before_wrap(), }; let origin = available.min; - // Along an axis with no room limit the content grows away from its start edge: in a right-to-left - // layout that edge is the right one, so the measuring rect is as wide as a scrolling region and - // ends where the room does, as the real run's region does. - let rect = if self.room.x.is_infinite() && at.env.direction.is_rtl() && available.max.x.is_finite() { - Rect::from_min_max(Pos2::new(available.max.x - crate::contract::MAX_EXTENT, origin.y), Pos2::new(available.max.x, origin.y + self.room.y)) + // The measuring rect starts at the room's start edge, where the real run places the container: in a + // right-to-left layout that is the right edge, and the rect ends there. Along an axis with no room limit it + // is as wide as a scrolling region, since egui lays nothing out leftwards from -inf. + let rect = if at.env.direction.is_rtl() && available.max.x.is_finite() { + let width = if self.room.x.is_finite() { self.room.x } else { crate::contract::MAX_EXTENT }; + Rect::from_min_max(Pos2::new(available.max.x - width, origin.y), Pos2::new(available.max.x, origin.y + self.room.y)) } else { Rect::from_min_size(origin, self.room) }; diff --git a/crates/ui-widgets/tests/it/bars/mod.rs b/crates/ui-widgets/tests/it/bars/mod.rs index 1007c2ef1..39ac29b41 100644 --- a/crates/ui-widgets/tests/it/bars/mod.rs +++ b/crates/ui-widgets/tests/it/bars/mod.rs @@ -4,3 +4,4 @@ mod bar; mod breadcrumb; mod constraint_fields; mod items; +mod overflow; diff --git a/crates/ui-widgets/tests/it/bars/overflow.rs b/crates/ui-widgets/tests/it/bars/overflow.rs new file mode 100644 index 000000000..6c3faa74f --- /dev/null +++ b/crates/ui-widgets/tests/it/bars/overflow.rs @@ -0,0 +1,46 @@ +//! A `Bar` too short for its members: it lays out under right-to-left as the mirror of left-to-right. + +use craft_behavior::geometry::Constraint; +use craft_i18n::Direction; +use craft_ui::bars::{Bar, BarItem, BarItems, BarKind, ConstraintFields, ItemDisplay}; +use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _}; +use craft_ui::display::Divider; +use craft_ui::runtime::commands::EditCommands; +use craft_ui::testing::{Fixture, trial}; +use egui::{Rect, Vec2}; + +const SLACK: f32 = 0.5; +const ROOM: Vec2 = Vec2::new(400.0, 80.0); +const NAMES: &[&str] = &["Style", "Width", "Height", "Undo"]; + +/// An options bar with the Style, Width and Height cells, a rule and a text item strip, in `direction`. +fn options_bar(direction: Direction) -> Fixture { + let mut fixture = Fixture::new(|ui| { + let mut constraint = Constraint::Free; + Bar::new("Options", BarKind::Options).show(ui, |ui, _| { + ConstraintFields::new(Bind::bound(&mut constraint)).show(ui); + Divider::vertical().show(ui); + let items = [EditCommands::Undo, EditCommands::Redo].map(|command| BarItem::new(command).display(ItemDisplay::Text)); + BarItems::new(items.to_vec()).label("Edit").show(ui); + }); + }); + fixture.direction(direction).room(ROOM).settle(); + fixture +} + +fn rects(fixture: &Fixture) -> Vec> { + NAMES.iter().map(|name| fixture.by_label(name).map(|target| target.rect)).collect() +} + +#[trial] +fn a_bar_too_short_for_its_members_lays_out_under_rtl_as_the_mirror_of_ltr() { + let (ltr, rtl) = (rects(&options_bar(Direction::Ltr)), rects(&options_bar(Direction::Rtl))); + for ((name, a), b) in NAMES.iter().zip(<r).zip(&rtl) { + let (Some(a), Some(b)) = (a, b) else { panic!("{name}: shown in one direction only: {a:?} / {b:?}") }; + let mirror = (ROOM.x - a.max.x, ROOM.x - a.min.x); + assert!( + (b.min.x - mirror.0).abs() <= SLACK && (b.max.x - mirror.1).abs() <= SLACK, + "{name} at {a:?} under left-to-right is at {b:?} under right-to-left, not at x {mirror:?}" + ); + } +}