From fcdd810bbcf6295583dd4bc7d0d030a7b60d027b Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 05:38:57 +0000 Subject: [PATCH] Text: a wrapping text is never narrower than its widest word, so a row breaks it between words, not inside one --- crates/ui-widgets/src/display/text.rs | 17 ++++++-- crates/ui-widgets/tests/it/layout/mod.rs | 1 + .../tests/it/layout/shrink_order.rs | 43 +++++++++++++++++++ 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 crates/ui-widgets/tests/it/layout/shrink_order.rs diff --git a/crates/ui-widgets/src/display/text.rs b/crates/ui-widgets/src/display/text.rs index 09f9f903e..8185a8b96 100644 --- a/crates/ui-widgets/src/display/text.rs +++ b/crates/ui-widgets/src/display/text.rs @@ -154,15 +154,24 @@ impl Component for Text { }; } let role = cx.style.text.role; + // A line breaks between words, never inside one: a wrapped text is never narrower than its widest word. + let widest = + || cx.resolved.label.split_whitespace().map(|word| cx.layout_at(&self.run(role, word), f32::INFINITY).size().x).fold(0.0, f32::max); if cx.proposal == Proposal::Min && self.elide == Elide::Off { - // At its least a wrapped text is its widest word: a line breaks between words, never inside one. - let widest = - cx.resolved.label.split_whitespace().map(|word| cx.layout_at(&self.run(role, word), f32::INFINITY).size().x).fold(0.0, f32::max); + let widest = widest(); return Vec2::new(widest, cx.layout_at(&self.run(role, cx.resolved.label), widest).size().y); } // At its least an elided line is the ellipsis. let band = if cx.proposal == Proposal::Min { 0.0 } else { wrap.unwrap_or(f32::INFINITY) }; - let layout = cx.layout_at(&self.run(role, cx.resolved.label), band); + let mut layout = cx.layout_at(&self.run(role, cx.resolved.label), band); + // In a row the room is what the earlier siblings left, which can be less than a word; the row's shrink step + // then makes room for the word. + if self.elide == Elide::Off && layout.galley.rows.len() > 1 { + let widest = widest(); + if widest > band { + layout = cx.layout_at(&self.run(role, cx.resolved.label), widest); + } + } // A cut line takes the whole room: its width must not follow where the last glyph happened to fit. match cx.room { Some(room) if layout.elided => Vec2::new(room, layout.size().y), diff --git a/crates/ui-widgets/tests/it/layout/mod.rs b/crates/ui-widgets/tests/it/layout/mod.rs index 9b129811e..16755d95e 100644 --- a/crates/ui-widgets/tests/it/layout/mod.rs +++ b/crates/ui-widgets/tests/it/layout/mod.rs @@ -7,3 +7,4 @@ mod room_height; mod row_room; mod rtl_scroll; mod scroll_region; +mod shrink_order; diff --git a/crates/ui-widgets/tests/it/layout/shrink_order.rs b/crates/ui-widgets/tests/it/layout/shrink_order.rs new file mode 100644 index 000000000..4d69b275b --- /dev/null +++ b/crates/ui-widgets/tests/it/layout/shrink_order.rs @@ -0,0 +1,43 @@ +//! How a row too short for its children shares the shortfall: a wrapping text keeps its words whole. + +use craft_ui::buttons::Button; +use craft_ui::contract::{Show as _, ShowContainer as _}; +use craft_ui::display::Text; +use craft_ui::layout_components::Row; +use craft_ui::testing::{Fixture, trial}; +use egui::{Rect, Vec2}; + +const ROOM: Vec2 = Vec2::new(800.0, 600.0); +const SLACK: f32 = 0.5; + +/// The rect of the widget labelled `label` in a row of `children`, in a room `width` wide. +fn rect_in_row(width: f32, label: &'static str, children: impl Fn(&mut egui::Ui) + 'static) -> Rect { + let mut fixture = Fixture::new(move |ui| { + Row::new().show(ui, |ui, _| children(ui)); + }); + fixture.room(Vec2::new(width, ROOM.y)).settle(); + fixture.by_label(label).map_or(Rect::NOTHING, |target| target.rect) +} + +/// A wrapping text after a wide sibling, which leaves it less room than its widest word, breaks between words only: +/// it is at least as wide as that word, and the row narrows the sibling to make the room. +#[trial] +fn a_wrapping_text_in_a_short_row_is_never_narrower_than_its_widest_word() { + const WORD: &str = "following"; + const TEXT: &str = "of the following rules:"; + const BUTTON: &str = "A button with a long label"; + let word = rect_in_row(ROOM.x, WORD, |ui| { + Text::new(WORD).show(ui); + }); + let button = rect_in_row(ROOM.x, BUTTON, |ui| { + Button::new(BUTTON).show(ui); + }); + assert!(word.width() > 0.0 && button.width() > 0.0, "both measure: {word:?}, {button:?}"); + // The button takes all but half the word's width. + let width = button.width() + word.width() / 2.0; + let text = rect_in_row(width, TEXT, |ui| { + Button::new(BUTTON).show(ui); + Text::new(TEXT).show(ui); + }); + assert!(text.width() >= word.width() - SLACK, "the text {text:?} is narrower than its widest word {word:?}"); +}