From bd6fbd2b54c2c3c811090994cecc374a8e768fe9 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 00:21:28 +0000 Subject: [PATCH] A picture in a column sized from its content measures at its least width picture_width took the column's room where the measure offered none. A measuring run in a content column offers no room so that nothing there fills it; the picture took the room anyway, and a dialog sized from its content with a preview (Export As, a parameter dialog) took its whole share of the window, or more. The picture now takes the room it is offered and its least width where it is offered none; the real run shows it across the width the column settles on. --- crates/ui-contract/src/contract/cx/measure.rs | 10 +++-- .../ui-patterns/tests/it/form_dialogs/mod.rs | 1 + .../tests/it/form_dialogs/preview_width.rs | 42 +++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 crates/ui-patterns/tests/it/form_dialogs/preview_width.rs diff --git a/crates/ui-contract/src/contract/cx/measure.rs b/crates/ui-contract/src/contract/cx/measure.rs index b99c42f24..3a94f412c 100644 --- a/crates/ui-contract/src/contract/cx/measure.rs +++ b/crates/ui-contract/src/contract/cx/measure.rs @@ -71,11 +71,13 @@ impl MeasureCx<'_, C> { }) } - /// The width a picture sizes at (a plot, a wheel, a well): it takes the width it is given, its `column_room`, - /// down to `least` and up to `most`. At its least, or where the width is unbounded, it is `least`. A band picture - /// keeps a height of its own; a square one is as tall as this. + /// The width a picture sizes at (a plot, a wheel, a well): it takes the room it is offered, down to `least` and up + /// to `most`. At its least, where the width is unbounded, and in a measuring run of a column that sizes its width + /// from its content (which offers no room), it is `least`: a picture needs no width of its own, so it never sets + /// that column's width, and the real run shows it across the width the column settles on. A band picture keeps a + /// height of its own; a square one is as tall as this. pub fn picture_width(&self, least: f32, most: f32) -> f32 { - match self.column_room().filter(|_| self.proposal != Proposal::Min) { + match self.room.filter(|_| self.proposal != Proposal::Min) { Some(room) => room.clamp(least, most.max(least)), None => least, } diff --git a/crates/ui-patterns/tests/it/form_dialogs/mod.rs b/crates/ui-patterns/tests/it/form_dialogs/mod.rs index 7c99107e8..ed8c2e1fb 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/mod.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/mod.rs @@ -6,4 +6,5 @@ mod export; mod params; mod password; mod preview; +mod preview_width; mod size_model; diff --git a/crates/ui-patterns/tests/it/form_dialogs/preview_width.rs b/crates/ui-patterns/tests/it/form_dialogs/preview_width.rs new file mode 100644 index 000000000..9c17f5dde --- /dev/null +++ b/crates/ui-patterns/tests/it/form_dialogs/preview_width.rs @@ -0,0 +1,42 @@ +//! A picture in a dialog sized from its content needs no width of its own: the dialog does not follow the window, +//! and the picture spans the width a wider sibling sets. + +use craft_ui::contract::{Show as _, ShowContainer as _}; +use craft_ui::dialog::Dialog; +use craft_ui::display::Text; +use craft_ui::layout_components::Stack; +use craft_ui::patterns::form_dialogs::{PreviewPane, PreviewState, PreviewView}; +use craft_ui::testing::snapshot::layer_rects; +use craft_ui::testing::{Fixture, trial}; +use egui::{Rect, Vec2, vec2}; + +/// The dialog's rect and the preview's, in a window of `room`; `wide` puts a field wider than the preview's least +/// width above it. +fn shown(room: Vec2, wide: bool) -> (Rect, Rect) { + let mut fixture = Fixture::new(move |ui| { + Dialog::new("Preview").show(ui, |ui, _| { + Stack::new().show(ui, |ui, _| { + if wide { + Text::new("A caption that sets the column wider than a preview needs").show(ui); + } + PreviewPane::state("Preview", PreviewState::Pending(None), &PreviewView::fitting()).show(ui); + }); + }); + }); + fixture.room(room); + fixture.settle(); + let layers = layer_rects(fixture.ctx()); + assert_eq!(layers.len(), 1, "one dialog layer"); + (layers[0], fixture.by_label("Preview").expect("the preview").rect) +} + +#[trial] +fn a_preview_in_a_dialog_sized_from_its_content_sets_no_width() { + let (small, large) = (vec2(800.0, 600.0), vec2(1600.0, 1000.0)); + let (dialog, preview) = shown(small, false); + assert_eq!(dialog.width(), shown(large, false).0.width(), "the dialog's width does not follow the window's"); + assert!(dialog.width() < small.x * 0.8 - 1.0, "the dialog is narrower than its share of the window: {dialog:?}"); + let (wide_dialog, wide_preview) = shown(small, true); + assert!(wide_preview.width() > preview.width() + 1.0, "the preview spans the wider column: {wide_preview:?} vs {preview:?}"); + assert_eq!(wide_dialog.width(), shown(large, true).0.width()); +}