diff --git a/crates/imaging/src/ui/histogram/plot.rs b/crates/imaging/src/ui/histogram/plot.rs index 728d27deb..4b3017d8d 100644 --- a/crates/imaging/src/ui/histogram/plot.rs +++ b/crates/imaging/src/ui/histogram/plot.rs @@ -310,7 +310,8 @@ impl Component for Histogram<'_> { let accent = super::data::color32(colors.data.selection); let warn = cx.tone(craft_theme::variant::Tone::Warn); HistogramStyle { - metrics: Metrics::new().min_size(Vec2::new(cx.tokens.size.plot.width, tall)), + // The width's floor is `measure`'s (`picture_width`), which a narrow room may take below the plot width. + metrics: Metrics::new().min_size(Vec2::new(0.0, tall)), focus: cx.focus_look(), caption: TextLook::new(TypeRole::Caption, Stateful::new(cx.color(colors.text.normal))), stale: IconLook::new(cx.tokens.size.icon.sm, Stateful::new(warn)), diff --git a/crates/pages/src/ui/reading.rs b/crates/pages/src/ui/reading.rs index efab6a379..8a0c96210 100644 --- a/crates/pages/src/ui/reading.rs +++ b/crates/pages/src/ui/reading.rs @@ -290,7 +290,8 @@ impl Component for ReadingView<'_> { fn style(cx: &StyleCx<'_>, _: ReadingViewStyleKey) -> ReadingViewStyle { ReadingViewStyle { - metrics: Metrics::new().min_size(Vec2::new(cx.tokens.size.plot.width, cx.tokens.size.plot.height)), + // The width's floor is `measure`'s (`picture_width`), which a narrow room may take below the plot width. + metrics: Metrics::new().min_size(Vec2::new(0.0, cx.tokens.size.plot.height)), focus: cx.focus_look(), gap: cx.tokens.space.gap.section, reach: cx.tokens.size.hit.target, diff --git a/crates/ui-canvas/src/canvas/navigator/preview.rs b/crates/ui-canvas/src/canvas/navigator/preview.rs index 78f1e7ab6..8f1b9d299 100644 --- a/crates/ui-canvas/src/canvas/navigator/preview.rs +++ b/crates/ui-canvas/src/canvas/navigator/preview.rs @@ -287,7 +287,8 @@ impl Component for Navigator<'_> { fn style(cx: &StyleCx<'_>, _: NavigatorStyleKey) -> NavigatorStyle { NavigatorStyle { - metrics: Metrics::new().min_size(Vec2::new(cx.tokens.size.plot.width, cx.tokens.size.plot.height)), + // The width's floor is `measure`'s (`picture_width`), which a narrow room may take below the plot width. + metrics: Metrics::new().min_size(Vec2::new(0.0, cx.tokens.size.plot.height)), focus: cx.focus_look(), inset: cx.tokens.space.gap.inline, radius: UiTokens::corner(cx.tokens.shape.radius.sm), diff --git a/crates/ui-contract/src/contract/cx/measure.rs b/crates/ui-contract/src/contract/cx/measure.rs index e0ba7cf6f..338156873 100644 --- a/crates/ui-contract/src/contract/cx/measure.rs +++ b/crates/ui-contract/src/contract/cx/measure.rs @@ -84,13 +84,14 @@ impl MeasureCx<'_, C> { } /// The width a band picture sizes at (a plot, a strip), one whose height is its own whatever its width: 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. + /// the room it is offered, 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. Offered less room than `least` (a panel at its narrowest width), it takes that room, down to the plot's + /// least height (`size.plot.height`, or `least` when that is smaller), so it stays inside its container. pub fn picture_width(&self, least: f32, most: f32) -> f32 { match self.room.filter(|_| self.proposal != Proposal::Min) { - Some(room) => room.clamp(least, most.max(least)), + Some(room) => room.clamp(least.min(self.tokens.size.plot.height), most.max(least)), None => least, } } diff --git a/crates/ui-contract/tests/it/layout/body.rs b/crates/ui-contract/tests/it/layout/body.rs index 3cea18b40..c9ef1431c 100644 --- a/crates/ui-contract/tests/it/layout/body.rs +++ b/crates/ui-contract/tests/it/layout/body.rs @@ -377,6 +377,37 @@ fn a_content_column_sweeps_again_only_for_a_square_picture_narrower_than_it() { } } +/// The window's region and the rect of a `Picture` of `least` shown straight in a window `room` wide. +fn picture_in_room(least: f32, square: bool, room: f32) -> (Rect, Rect) { + let (seen, region) = (Rc::new(RefCell::new(Seen::default())), Rc::new(Cell::new(Rect::NOTHING))); + let (shown, shown_region) = (Rc::clone(&seen), Rc::clone(®ion)); + let measures = Rc::new(Measures::default()); + let mut fixture = Fixture::new(move |ui| { + shown_region.set(ui.available_rect_before_wrap()); + Picture { common: Common::new(), least, square, seen: &shown, measures: &measures }.id_salt("picture").show(ui); + }); + fixture.room(Vec2::new(room, 800.0)).settle(); + let picture = seen.borrow().rect; + (region.get(), picture) +} + +// A picture offered less room than its least (a panel at its narrowest) takes that room and stays inside it; offered +// more, it shows across the room. +#[trial] +fn a_picture_offered_less_room_than_its_least_stays_inside_it() { + for square in [false, true] { + let (region, narrow) = picture_in_room(400.0, square, 300.0); + let dump = format!("square {square}, region {region:?}, picture {narrow:?}"); + assert!(narrow.right() <= region.right() + SLACK, "the picture stays inside the room it is offered: {dump}"); + assert!(narrow.width() > 2.0 * PICTURE_PAD + SLACK, "the picture still shows: {dump}"); + if square { + assert!((narrow.width() - 2.0 * PICTURE_PAD - narrow.height()).abs() < SLACK, "a square picture stays square: {dump}"); + } + let (region, wide) = picture_in_room(400.0, square, 700.0); + assert!(wide.width() > 400.0 + 2.0 * PICTURE_PAD + SLACK, "offered more than its least, it shows across the room: {wide:?} in {region:?}"); + } +} + #[trial] fn a_space_between_child_leaves_room_for_the_siblings_after_it() { let (first, last) = (Rc::new(RefCell::new(Seen::default())), Rc::new(RefCell::new(Seen::default()))); diff --git a/crates/ui-patterns/src/patterns/form_dialogs/preview.rs b/crates/ui-patterns/src/patterns/form_dialogs/preview.rs index 12ffd5a6e..0e3ab7627 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/preview.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/preview.rs @@ -456,7 +456,8 @@ impl Component for PreviewPane<'_> { metrics: if key.fixed { Metrics::new() } else { - Metrics::new().min_size(Vec2::new(cx.tokens.size.plot.width, cx.tokens.size.plot.height)) + // The width's floor is `measure`'s (`picture_width`), which a narrow room may take below the plot width. + Metrics::new().min_size(Vec2::new(0.0, cx.tokens.size.plot.height)) }, focus: cx.focus_look(), } diff --git a/crates/ui-patterns/tests/it/form_dialogs/preview_width.rs b/crates/ui-patterns/tests/it/form_dialogs/preview_width.rs index 9c17f5dde..37c5ed2f1 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/preview_width.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/preview_width.rs @@ -1,14 +1,16 @@ //! 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. +//! and the picture spans the width a wider sibling sets. In a panel narrower than the plot's least width, it takes +//! the panel's room. use craft_ui::contract::{Show as _, ShowContainer as _}; use craft_ui::dialog::Dialog; use craft_ui::display::Text; +use craft_ui::docking::{DockGroup, PanelKey, PanelTitle, Titles}; 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}; +use egui::{Pos2, 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. @@ -30,6 +32,35 @@ fn shown(room: Vec2, wide: bool) -> (Rect, Rect) { (layers[0], fixture.by_label("Preview").expect("the preview").rect) } +/// The preview's rect in a docked panel group `width` wide. +fn in_group(width: f32) -> Rect { + let panels = vec![PanelTitle::new(PanelKey::new(1), "Flattening")]; + let mut fixture = Fixture::new(move |ui| { + let group = DockGroup::new(1usize, Titles::from_content(panels.clone()), panels[0].key); + group.show_at(ui, Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::new(width, 400.0)), |ui, _| { + PreviewPane::state("Preview", PreviewState::Pending(None), &PreviewView::fitting()).show(ui); + }); + }); + fixture.room(vec2(800.0, 600.0)); + fixture.settle(); + fixture.by_label("Preview").expect("the preview").rect +} + +// A panel at its narrowest offers its body less room than the plot's least width; the preview takes that room and +// stays inside the panel. A wider panel's preview spans its body as before, wider than the preview of a dialog sized +// from its content, which takes the plot's least width. +#[trial] +fn a_preview_in_a_narrow_panel_stays_inside_it() { + let narrow = 220.0; + let preview = in_group(narrow); + assert!(preview.right() <= 10.0 + narrow + 0.5, "the preview stays inside a panel {narrow} wide: {preview:?}"); + assert!(preview.width() > 0.0, "the preview still shows: {preview:?}"); + let (_, least) = shown(vec2(800.0, 600.0), false); + let wide = in_group(500.0); + assert!(wide.width() > least.width() + 1.0, "a wider panel's preview spans its body: {wide:?} vs {least:?}"); + assert!(wide.right() <= 510.5, "and stays inside it: {wide:?}"); +} + #[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));