diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index a562b93ce..ea646110b 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -727,6 +727,7 @@ impl ShapeHooks for BodyHooks<'_> { let line = shared_line(&props, beside); // B2 label: the control gets the rect the label leaves; the label column adds to the container's extent. let mut body_rect = regions.body; + let mut label_rect = Rect::NOTHING; let mut label_input = None; if let Some(side) = props.label.as_ref() { let resolved = cx.core.resolved; @@ -751,10 +752,11 @@ impl ShapeHooks for BodyHooks<'_> { claims: false, trailing: Vec2::ZERO, }; - body_rect = label::run(ui, env, &input, None).control; + let placed = label::run(ui, env, &input, None); + (body_rect, label_rect) = (placed.control, placed.label); label_input = Some(input); } - let label_extra = label_extra_of(regions.body, body_rect); + let label_extra = label_extra_of(regions.body, body_rect, label_rect, rtl); let body_env = line_env.derive_local(|scoped| { let label_width = label_extra.x; self.scope_body(scoped, &props, id, BodyScope { room, real, sizing, content_column, scroll, label_width }); @@ -915,8 +917,9 @@ impl ShapeHooks for BodyHooks<'_> { input.control_min = scope::open_control_min(&ctx, id); // The footer starts under the control column this decision gives, not the one before the control // reported its minimum. - body_rect = label::control_after(ui, env, input); - label_extra = label_extra_of(regions.body, body_rect); + let placed = label::control_after(ui, env, input); + (body_rect, label_rect) = (placed.control, placed.label); + label_extra = label_extra_of(regions.body, body_rect, label_rect, rtl); } if let Some(size) = &mut self.body_size { // A body beside its label in a content-sized column of rows reports how far it reaches past the label diff --git a/crates/ui-contract/src/contract/body/regions.rs b/crates/ui-contract/src/contract/body/regions.rs index aa448d682..6888d9d24 100644 --- a/crates/ui-contract/src/contract/body/regions.rs +++ b/crates/ui-contract/src/contract/body/regions.rs @@ -417,11 +417,17 @@ impl<'b> BodyHooks<'b> { } } -/// The extent a label takes from `body` by placing the control at `control`. The label takes its column either by -/// narrowing the control's rect or by shifting it along the row, which a content-sized row (unbounded to the end) -/// does; the extent grows by whichever of the two is finite. -pub(super) fn label_extra_of(body: Rect, control: Rect) -> Vec2 { +/// The extent a label placed at `label` takes from `body` by placing the control at `control`. A label beside or above +/// takes its column either by narrowing the control's rect or by shifting it along the row, which a content-sized row +/// (unbounded to the end) does; the extent grows by whichever of the two is finite. A label after the control takes +/// the gap and its own width past the control's end edge, not the rest of the row. +pub(super) fn label_extra_of(body: Rect, control: Rect, label: Rect, rtl: bool) -> Vec2 { let taken = |amount: f32| if amount.is_finite() { amount.max(0.0) } else { 0.0 }; + let after = label.is_positive() && if rtl { label.max.x <= control.min.x } else { label.min.x >= control.max.x }; + if after { + let reach = control.union(label); + return Vec2::new(reach.width() - control.width(), taken(reach.height() - control.height())); + } (body.size() - control.size()).max(Vec2::ZERO).max(Vec2::new( taken(control.min.x - body.min.x).max(taken(body.max.x - control.max.x)), taken(control.min.y - body.min.y).max(taken(body.max.y - control.max.y)), diff --git a/crates/ui-contract/src/contract/label/place.rs b/crates/ui-contract/src/contract/label/place.rs index 40c3065fe..059a10d97 100644 --- a/crates/ui-contract/src/contract/label/place.rs +++ b/crates/ui-contract/src/contract/label/place.rs @@ -148,11 +148,11 @@ pub(crate) fn label_gap(env: &Env) -> f32 { /// Where the control sits once `input.control_min` is known: the decision `run` takes, without showing the label. A /// measuring run calls it after the body ran, with the control minimum that run recorded. -pub(crate) fn control_after(ui: &mut Ui, env: &Env, input: &LabelIn<'_>) -> Rect { +pub(crate) fn control_after(ui: &mut Ui, env: &Env, input: &LabelIn<'_>) -> Placed { let ctx = ui.ctx().clone(); let flow = Flow::new(input.row, env.direction.is_rtl()); if input.side.kind == LabelKind::Hidden { - return hidden_control(&ctx, input, flow, label_gap(env)); + return Placed { label: Rect::NOTHING, control: hidden_control(&ctx, input, flow, label_gap(env)), trailing: Rect::NOTHING }; } let reset = scope::last_reset(&ctx, input.scope); let slot = if reset.is_some() && input.side.reset_dot { env.tokens.size.handle.sm } else { 0.0 }; @@ -172,5 +172,5 @@ pub(crate) fn control_after(ui: &mut Ui, env: &Env, input: &LabelIn<'_>) -> Rect let installed = Env::install_for(&kids, crate::contract::cx::child_env(env, input.enabled, input.size)); let placed = decide(&ctx, env, input, &kids, &mut leaf, glyph, flow); Env::restore(&ctx, installed); - placed.control + placed } diff --git a/crates/ui-panels/tests/it/body/after_label.rs b/crates/ui-panels/tests/it/body/after_label.rs new file mode 100644 index 000000000..8370cd113 --- /dev/null +++ b/crates/ui-panels/tests/it/body/after_label.rs @@ -0,0 +1,35 @@ +//! A label after its control adds the gap and its own width to the container, not the rest of the row. + +use craft_ui::choice::Checkbox; +use craft_ui::contract::{Bind, FieldLayout, Show as _, ShowContainer as _}; +use craft_ui::dialog::Dialog; +use craft_ui::form::Field; +use craft_ui::testing::snapshot::layer_rects; +use craft_ui::testing::{Fixture, trial}; +use egui::{Vec2, vec2}; + +/// The width of a content-sized dialog holding one checkbox field laid out as `layout`, in a window of `room`. +fn dialog_width(layout: FieldLayout, room: Vec2) -> f32 { + let mut fixture = Fixture::new(move |ui| { + Dialog::new("Grid").show(ui, |ui, _| { + Field::new("Snap to grid").layout(layout).show(ui, |ui, _| { + Checkbox::new(Bind::given(true)).show(ui); + }); + }); + }); + fixture.room(room); + fixture.idle(20); + let layers = layer_rects(fixture.ctx()); + assert_eq!(layers.len(), 1, "one dialog layer"); + layers[0].width() +} + +#[trial] +fn a_dialog_with_a_label_after_its_checkbox_is_as_wide_as_its_content() { + let (small, large) = (vec2(800.0, 600.0), vec2(1600.0, 1000.0)); + let after = dialog_width(FieldLayout::After, small); + assert_eq!(after, dialog_width(FieldLayout::After, large), "the dialog's width does not follow the window's"); + // Beside, the label column and the gap come before the control; after, the gap and the label come past it. + let beside = dialog_width(FieldLayout::Beside, small); + assert!((after - beside).abs() <= 8.0, "after {after}, beside {beside}"); +} diff --git a/crates/ui-panels/tests/it/body/mod.rs b/crates/ui-panels/tests/it/body/mod.rs index f22af3786..b8af0f714 100644 --- a/crates/ui-panels/tests/it/body/mod.rs +++ b/crates/ui-panels/tests/it/body/mod.rs @@ -1,3 +1,4 @@ //! The body step and the mount host: regions, scrolling, move and resize, and viewport mounts. +mod after_label; mod footer_reserve;