mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 00:34:25 +00:00
Text: a wrapping text is never narrower than its widest word, so a row breaks it between words, not inside one
This commit is contained in:
@@ -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),
|
||||
|
||||
@@ -7,3 +7,4 @@ mod room_height;
|
||||
mod row_room;
|
||||
mod rtl_scroll;
|
||||
mod scroll_region;
|
||||
mod shrink_order;
|
||||
|
||||
@@ -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:?}");
|
||||
}
|
||||
Reference in New Issue
Block a user