diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 9cda37695..2e46f888b 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -1288,6 +1288,7 @@ impl BodyHooks<'_> { plan.regions.body.width() }, content_row: false, + unbounded: false, text: resolved.label_value, side, help: props.help.as_ref(), diff --git a/crates/ui-contract/src/contract/label/place.rs b/crates/ui-contract/src/contract/label/place.rs index f86ab6e67..ca9c88259 100644 --- a/crates/ui-contract/src/contract/label/place.rs +++ b/crates/ui-contract/src/contract/label/place.rs @@ -87,7 +87,7 @@ pub(super) fn decide(ctx: &egui::Context, env: &Env, input: &LabelIn<'_>, kids: let gap = label_gap(env); let indent = after_indent(ctx, env, input, gap); let mut natural = leaf.measure_in(kids, craft_behavior::Proposal::Ideal); - if input.side.layout == FieldLayout::After { + if input.side.layout == FieldLayout::After && !input.unbounded { // The label takes the room the control and the gap leave, and its detail wraps there. let room = (input.row.width() - indent - input.control_min - gap).max(0.0); if natural.x > room { @@ -96,7 +96,9 @@ pub(super) fn decide(ctx: &egui::Context, env: &Env, input: &LabelIn<'_>, kids: } let baseline = leaf.baseline.get(); if input.side.layout == FieldLayout::Line { - let (cell, rest) = flow.take_end(env.control(input.size).number); + let number = env.control(input.size).number; + let flow = if input.unbounded { widened(flow, natural.x + gap + number) } else { flow }; + let (cell, rest) = flow.take_end(number); let width = natural.x.min((rest.rect().width() - gap).max(0.0)); let drop = (cell.height() - natural.y).max(0.0) / 2.0; let label = Flow::new(rest.rect(), rtl).align(Vec2::new(width, natural.y), Align::Min, Align::Min).translate(Vec2::new(0.0, drop)); @@ -124,20 +126,19 @@ pub(super) fn decide(ctx: &egui::Context, env: &Env, input: &LabelIn<'_>, kids: let caption = if matches!(input.side.layout, FieldLayout::Above | FieldLayout::Caption(_)) { column } else { 0.0 }; // The room decides; a beside label gets the width it needs even where last frame's content was narrower. A label // that never sits beside stacks in an unbounded room as well. - let stacks = (input.room < stack_below || stack_below == f32::INFINITY) && !(input.content_row && input.side.layout == FieldLayout::Beside); - // A stacked label has the row's width at most; its name elides there. - let column = if stacks { column.min(input.row.width()) } else { column }; - let flow = if stacks || input.row.width() >= stack_below { - flow - } else { - let mut wide = input.row; - if rtl { - wide.min.x = wide.max.x - stack_below; - } else { - wide.max.x = wide.min.x + stack_below; - } - Flow::new(wide, rtl) + let stacks = (input.room < stack_below || stack_below == f32::INFINITY) + && !((input.content_row || input.unbounded) && input.side.layout == FieldLayout::Beside); + // A stacked label has the row's width at most, where its name elides; in a row without an end it keeps its own. + let column = if stacks && !input.unbounded { column.min(input.row.width()) } else { column }; + // A beside label that does not stack takes its column, the gap and the control's least, past the row's end where + // the row is narrower. A row without an end is as wide as its label and control need, whatever the layout. + let need = match input.side.layout { + FieldLayout::After if input.unbounded => indent + input.control_min + gap + natural.x, + _ if input.unbounded && stacks => column.max(input.control_min), + _ if stacks => 0.0, + _ => stack_below, }; + let flow = widened(flow, need); if input.side.layout == FieldLayout::After { let flow = if indent > 0.0 { flow.take_start(indent).1 } else { flow }; @@ -162,6 +163,20 @@ pub(super) fn decide(ctx: &egui::Context, env: &Env, input: &LabelIn<'_>, kids: } } +/// `flow` widened from its start edge to `need` when it is narrower: its end edge moves, to the left under RTL. +fn widened(flow: Flow, need: f32) -> Flow { + let mut wide = flow.rect(); + if wide.width() >= need { + return flow; + } + if flow.is_rtl() { + wide.min.x = wide.max.x - need; + } else { + wide.max.x = wide.min.x + need; + } + Flow::new(wide, flow.is_rtl()) +} + /// The gap between a label and its control: `space.gap.inline`, `space.gap.item` inside a bar. pub(crate) fn label_gap(env: &Env) -> f32 { let gaps = &env.tokens.space.gap; diff --git a/crates/ui-contract/src/contract/label/step.rs b/crates/ui-contract/src/contract/label/step.rs index e9126f28b..c186c28c6 100644 --- a/crates/ui-contract/src/contract/label/step.rs +++ b/crates/ui-contract/src/contract/label/step.rs @@ -32,6 +32,9 @@ pub(crate) struct LabelIn<'a> { /// The row sits in a column that takes its width from its content: a beside label never stacks for want of room, /// since the column is as wide as the label, the gap and the control need. pub content_row: bool, + /// The row's flow sets no end to it (`LeafLabel::unbounded`): the row is as wide as its label and control need, so + /// the label neither wraps nor stacks for want of room. + pub unbounded: bool, pub text: &'a Label, pub side: &'a SideLabel, /// The help line the container's footer shows; it joins the control's description with the label's detail. @@ -82,6 +85,9 @@ pub(crate) struct LeafLabel<'a> { pub parent: Option, /// The row the label and the child share. pub row: Rect, + /// The row's flow sets no end to it: egui lays it out and extends text there (an `egui::Grid` row, `ui.horizontal`). + /// `row` then gives only the row's start edge, top and height. + pub unbounded: bool, pub text: &'a Label, pub side: &'a SideLabel, /// What the child inherits (`leaf_under`). @@ -148,6 +154,7 @@ fn leaf_input<'a>(env: &Env, at: &LeafLabel<'a>, row: Rect, facts: LeafFacts, tr row, room: row.width(), content_row: env.content.0, + unbounded: at.unbounded, text: at.text, side: at.side, help: None, @@ -392,7 +399,12 @@ fn flow_leaf_row( // grid's cell or a horizontal layout. The row spans the room's own edges: a room that scrolls along x is unbounded // at its start edge, the left one under RTL. let row = Rect::from_min_max(avail.min, egui::pos2(avail.max.x, avail.min.y + ideal.y)); - let at = LeafLabel { id, owner, parent: None, row, text: &label, side: &side, under: &under }; + // A flow egui lays out (no body installed its env for `ui`) in which egui extends text rather than wrapping it + // (`ui.horizontal`, an `egui::Grid` without a max column width) offers the row no end: the label is decided at + // its own width. An `egui::Grid` offers a column with no width from the last frame its least cell width, where + // the label would wrap and keep the column that narrow. + let unbounded = !Env::installed_for(ui) && ui.wrap_mode() == egui::TextWrapMode::Extend; + let at = LeafLabel { id, owner, parent: None, row, unbounded, text: &label, side: &side, under: &under }; let decided = decide_leaf(ui, &env, &at, &mut child, trailing.as_ref().map_or(Vec2::ZERO, |trailing| trailing.size)); // In a column that sizes from its content the row is the label plus the control, never the room around them. // A row of a horizontal flow is as wide as its label and control: the room it has there is unbounded. diff --git a/crates/ui-contract/src/contract/paint_ui.rs b/crates/ui-contract/src/contract/paint_ui.rs index f60372b59..1e61f4d8f 100644 --- a/crates/ui-contract/src/contract/paint_ui.rs +++ b/crates/ui-contract/src/contract/paint_ui.rs @@ -163,7 +163,7 @@ impl PaintUi<'_> { let env = Env::of(self.ui); let mut child = child; let under = label::leaf_under(&env, &side, &child, (self.enabled, self.size)); - let at = LeafLabel { id, owner: self.id, parent: Some(self.id), row: rect, text: &label, side: &side, under: &under }; + let at = LeafLabel { id, owner: self.id, parent: Some(self.id), row: rect, unbounded: false, text: &label, side: &side, under: &under }; let ctx = self.ui.ctx().clone(); layout_memory::boundary(&ctx, true, || { let placed = crate::layout::ColumnGroup::field(&mut *self.ui, |ui| label::leaf(ui, &env, &at, &mut child, None)); diff --git a/crates/ui-widgets/tests/it/layout/row_line.rs b/crates/ui-widgets/tests/it/layout/row_line.rs index b77106fb8..b198a9369 100644 --- a/crates/ui-widgets/tests/it/layout/row_line.rs +++ b/crates/ui-widgets/tests/it/layout/row_line.rs @@ -174,18 +174,18 @@ fn a_labelled_check_box_sits_with_its_box_on_a_buttons_line_in_a_row_and_a_grid_ fn check_boxes_in_an_egui_grid_in_a_row_keep_their_own_rows() { const ROWS: usize = 3; let shown = |in_row: bool| { - let seen = Rc::new(Cell::new(([Rect::NOTHING; ROWS], Rect::NOTHING))); + let seen = Rc::new(Cell::new(([(Rect::NOTHING, Rect::NOTHING); ROWS], Rect::NOTHING))); let out = Rc::clone(&seen); let _fixture = settled(move |ui| { let room = Rect::from_min_size(ui.available_rect_before_wrap().min, egui::vec2(300.0, 600.0)); let out = Rc::clone(&out); Stack::new().fill().show_at(ui, room, move |ui, _| { - let mut boxes = [Rect::NOTHING; ROWS]; + let mut boxes = [(Rect::NOTHING, Rect::NOTHING); ROWS]; let mut grid = |ui: &mut egui::Ui| { egui::Grid::new("boxes").num_columns(2).spacing([24.0, 6.0]).show(ui, |ui| { for (row, rect) in boxes.iter_mut().enumerate() { - *rect = Checkbox::new(Bind::given(true)).label(format!("Left {row}")).show(ui).response.rect; - Checkbox::new(Bind::given(false)).label(format!("Right {row}")).show(ui); + rect.0 = Checkbox::new(Bind::given(true)).label(format!("Left {row}")).show(ui).response.rect; + rect.1 = Checkbox::new(Bind::given(false)).label(format!("Right {row}")).show(ui).response.rect; ui.end_row(); } }); @@ -203,9 +203,12 @@ fn check_boxes_in_an_egui_grid_in_a_row_keep_their_own_rows() { }; let (alone, _) = shown(false); let (boxes, after) = shown(true); - let pitch = |boxes: &[Rect; ROWS]| boxes[1].top() - boxes[0].top(); + for (left, right) in boxes { + assert!((left.center().y - right.center().y).abs() <= ROUNDING, "the two columns share each row: {left:?} {right:?}"); + } + let pitch = |boxes: &[(Rect, Rect); ROWS]| boxes[1].0.top() - boxes[0].0.top(); assert!((pitch(&boxes) - pitch(&alone)).abs() <= ROUNDING, "the grid rows in a row {boxes:?} are as high as outside one {alone:?}"); - assert!(after.top() >= boxes[ROWS - 1].bottom() - ROUNDING, "the button {after:?} is under the grid {boxes:?}"); + assert!(after.top() >= boxes[ROWS - 1].0.bottom() - ROUNDING, "the button {after:?} is under the grid {boxes:?}"); } /// Check boxes in a `ui.horizontal` an app shows in a `Grid` body are laid out by it: side by side on one line, taking @@ -232,3 +235,30 @@ fn check_boxes_in_an_egui_container_in_a_grid_take_no_cells() { let (one, two) = seen.get(); assert!((one.center().y - two.center().y).abs() <= ROUNDING && two.left() >= one.right() - ROUNDING, "One {one:?}, then Two {two:?} on its line"); } + +/// Under right-to-left, a check box in a `ui.horizontal` too narrow for its label keeps the label on one line: the +/// row is as wide as its box and label, and the label runs on from its box towards the left, past the room's end. +#[trial] +fn a_check_box_in_a_narrow_horizontal_keeps_its_label_on_one_line_under_rtl() { + const LONG: &str = "Second option with a long name"; + let seen = Rc::new(Cell::new((Rect::NOTHING, Rect::NOTHING))); + let out = Rc::clone(&seen); + let mut fixture = Fixture::new(move |ui| { + let out = Rc::clone(&out); + Stack::new().fill().show(ui, move |ui, _| { + let pair = ui.horizontal(|ui| { + let one = Checkbox::new(Bind::given(true)).label("One").show(ui).response.rect; + let two = Checkbox::new(Bind::given(false)).label(LONG).show(ui).response.rect; + (one, two) + }); + out.set(pair.inner); + }); + }); + fixture.direction(craft_i18n::Direction::Rtl).room(egui::vec2(160.0, 200.0)).settle(); + fixture.idle(3); + let (one, two) = seen.get(); + let label = text(&fixture, LONG); + assert!(two.right() <= one.left() + ROUNDING, "Two {two:?} follows One {one:?} towards the left"); + assert!(label.height() < two.height() * 1.5, "the label {label:?} stays on one line beside its box {two:?}"); + assert!(label.right() <= two.left() + ROUNDING, "the label {label:?} runs on from its box {two:?} towards the left"); +}