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.
This commit is contained in:
aiongg
2026-11-12 14:32:49 +00:00
parent 4a25bebc19
commit 3865baefbf
3 changed files with 53 additions and 5 deletions
@@ -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)
};
+1
View File
@@ -4,3 +4,4 @@ mod bar;
mod breadcrumb;
mod constraint_fields;
mod items;
mod overflow;
@@ -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<Option<Rect>> {
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(&ltr).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:?}"
);
}
}