diff --git a/crates/ui-contract/src/contract/mod.rs b/crates/ui-contract/src/contract/mod.rs index 6c4877339c..29206c8c43 100644 --- a/crates/ui-contract/src/contract/mod.rs +++ b/crates/ui-contract/src/contract/mod.rs @@ -411,6 +411,19 @@ pub trait Component: Meta + Sized { Vec2::ZERO } + /// Set when `measure_pair` may answer a paired request. The default avoids calling the hook for legacy components. + const PAIRED_MEASURE: bool = false; + + /// Optionally answers an ideal and a least content measure together. The answers must match `measure` under + /// the two supplied contexts, including the external inputs each reads, and leave the component as those calls + /// would. The component's fill declaration and label-scope reads must not change during either measure. Both + /// answers are replayed under the union of those inputs, so use this only when that preserves each answer. Return + /// `None` without reading either context or changing state to keep the ordinary separate calls; + /// that is the default. + fn measure_pair(&mut self, _ideal: &MeasureCx<'_, Self>, _least: &MeasureCx<'_, Self>) -> Option<(Vec2, Vec2)> { + None + } + /// The axis along which the component takes all the room the flow offers when that is more than it measured, /// as a container with `Sizing::Fill` does: the width when its caller set `Sizing::FillWidth` (a search field /// before a count) or `Sizing::Fill` (which takes the height as well). A component that fills by what it is diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index fa0d9923d2..4671f66a70 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -438,6 +438,7 @@ pub(crate) struct TypeFacts { pub canvas_control: bool, pub labelled_after: bool, pub binds_view: bool, + pub paired_measure: bool, /// `DISABLED` is `Faded`. pub faded: bool, /// `RULE`. @@ -459,6 +460,7 @@ impl TypeFacts { canvas_control: C::CANVAS_CONTROL, labelled_after: C::LABELLED_AFTER, binds_view: C::BINDS_VIEW, + paired_measure: C::PAIRED_MEASURE, faded: C::DISABLED == super::DisabledLook::Faded, rule: C::RULE, style_key: TypeId::of::(), @@ -497,6 +499,7 @@ pub(crate) trait Erased { fn cut_first(&self) -> bool; /// What `C::measure` gives for the shown value. fn measure_content(&mut self, input: &MeasureIn<'_>, resolved: &Resolved<'_>) -> (Vec2, Inputs); + fn measure_content_pair(&mut self, ideal: &MeasureIn<'_>, least: &MeasureIn<'_>, resolved: &Resolved<'_>) -> Option<((Vec2, Vec2), Inputs)>; fn measured_extent(&mut self, at: &Measuring<'_, '_>, stage: &mut MemoryStage) -> Vec2; fn pass_extent(&mut self, at: &Measuring<'_, '_>) -> Vec2; /// Builds the component's `Cx` over `core` and `style` (what `style` gave), runs `rest` with it, then `finish`. @@ -1609,8 +1612,8 @@ fn measure_step( let ask = |t: &mut dyn Erased, proposal: Proposal, room: Option, extent: Vec2| { measured(t, &MeasureIn { ui: &*ui, env, proposal, room }, style, resolved, extent) }; - let (size, inputs) = ask(&mut *t, Proposal::Ideal, room, extent); - let ideal = Call { proposal: Proposal::Ideal, room, inputs, took: size }; + // Legacy components keep their original ordering: measure before asking whether they fill. + let initial = (!facts.paired_measure).then(|| ask(&mut *t, Proposal::Ideal, room, extent)); // A control in a label scope reports its narrowest width, in a measuring run too: the real run's label step reads // it to decide whether the row fits the label beside it. let in_label = shape == ShapeKind::Leaf && scope::in_label(ctx); @@ -1621,10 +1624,36 @@ fn measure_step( let tells_least = shape == ShapeKind::Leaf && matches!(at, ShowAt::Flow) && [craft_behavior::Axis::X, craft_behavior::Axis::Y].into_iter().any(|axis| fills(axis) && crate::layout::counting_fills(ui, axis)); - let min = (in_label || tells_least).then(|| { - let (took, inputs) = ask(&mut *t, Proposal::Min, None, Vec2::ZERO); - Call { proposal: Proposal::Min, room: None, inputs, took } - }); + let pair = if facts.paired_measure + && (in_label || tells_least || (shape == ShapeKind::Leaf && matches!(shrink, Some(crate::layout::Shrink::Count(_))))) + { + let ideal_input = MeasureIn { ui, env, proposal: Proposal::Ideal, room }; + let least_input = MeasureIn { ui, env, proposal: Proposal::Min, room: None }; + t.measure_content_pair(&ideal_input, &least_input, resolved).map(|((ideal, least), inputs)| { + let size = steps::measure(style, &ideal_input, ideal, extent, resolved); + let least = steps::measure(style, &least_input, least, Vec2::ZERO, resolved); + (size, inputs, least, ideal) + }) + } else { + None + }; + let paired = pair.as_ref().map(|(_, _, least, content)| (*least, *content)); + let (size, ideal, min) = match pair { + Some((size, inputs, least, _)) => { + let ideal = Call { proposal: Proposal::Ideal, room, inputs: inputs.clone(), took: size }; + let min = Call { proposal: Proposal::Min, room: None, inputs, took: least }; + (size, ideal, Some(min)) + } + None => { + let (size, inputs) = initial.unwrap_or_else(|| ask(&mut *t, Proposal::Ideal, room, extent)); + let ideal = Call { proposal: Proposal::Ideal, room, inputs, took: size }; + let min = (in_label || tells_least).then(|| { + let (took, inputs) = ask(&mut *t, Proposal::Min, None, Vec2::ZERO); + Call { proposal: Proposal::Min, room: None, inputs, took } + }); + (size, ideal, min) + } + }; let min_took = min.as_ref().filter(|_| in_label).map(|min| min.took); // A leaf's measure calls are what its body's layout takes from it: a measuring run keeps them in the body's // transcript, and a real run its kept transcript serves checks them against this run's. A leaf shown at a rect @@ -1684,10 +1713,13 @@ fn measure_step( Some(crate::layout::Shrink::Count(row)) if shape == ShapeKind::Leaf => { // The `Ideal` size in no room, and the least width at which its content shows whole. let input = MeasureIn { ui, env, proposal: Proposal::Ideal, room: None }; - let (content, _) = t.measure_content(&input, resolved); + let content = match paired.filter(|_| room.is_none()) { + Some((_, content)) => content, + None => t.measure_content(&input, resolved).0, + }; let ideal = steps::measure(style, &input, content, Vec2::ZERO, resolved).x; let whole = steps::whole(style, content, Vec2::ZERO, resolved); - let least = ask(&mut *t, Proposal::Min, None, Vec2::ZERO).0.x; + let least = paired.map_or_else(|| ask(&mut *t, Proposal::Min, None, Vec2::ZERO).0.x, |(least, _)| least.x); crate::layout::report_shrink(ctx, row, id, size.x, ideal, Some((whole, least)), t.cut_first()); } Some(crate::layout::Shrink::Count(row)) => crate::layout::report_shrink(ctx, row, id, size.x, size.x, None, false), diff --git a/crates/ui-contract/src/contract/run/typed.rs b/crates/ui-contract/src/contract/run/typed.rs index 838a651f1f..fd8695ecbc 100644 --- a/crates/ui-contract/src/contract/run/typed.rs +++ b/crates/ui-contract/src/contract/run/typed.rs @@ -176,6 +176,11 @@ impl Erased for Typed<'_, '_, C> { steps::measure_content(self.comp.get(), input, style, resolved, self.shown.as_deref()) } + fn measure_content_pair(&mut self, ideal: &MeasureIn<'_>, least: &MeasureIn<'_>, resolved: &Resolved<'_>) -> Option<((Vec2, Vec2), Inputs)> { + let style = step_style(&self.style); + steps::measure_content_pair(self.comp.get(), ideal, least, style, resolved, self.shown.as_deref()) + } + fn measured_extent(&mut self, at: &Measuring<'_, '_>, stage: &mut MemoryStage) -> Vec2 { let style = step_style(&self.style); let (extent, shown) = measured_extent(self.comp.get(), &mut *self.hooks, at, stage, style, self.shown.take()); diff --git a/crates/ui-contract/src/contract/steps.rs b/crates/ui-contract/src/contract/steps.rs index 3d4f3551e4..c4aeec092a 100644 --- a/crates/ui-contract/src/contract/steps.rs +++ b/crates/ui-contract/src/contract/steps.rs @@ -29,7 +29,7 @@ pub(crate) use finish::{before_finish, inert_response, record_body_size, record_ pub(super) use id::measuring; pub(crate) use id::{claim_id, draft, id, keeping_ids}; pub(crate) use label::scrub_by_label; -pub(crate) use measure::{MeasureIn, guard, measure, measure_content, measure_cx, padding, whole}; +pub(crate) use measure::{MeasureIn, guard, measure, measure_content, measure_content_pair, measure_cx, padding, whole}; pub(crate) use paint::{Fade, UnderVeil, fade, next_shape, paint_focus, paint_part_layers, paint_surface, surface_ink, unfade}; pub(crate) use parts::{PartsStep, check_parts, declares_none}; pub(crate) use place::place; diff --git a/crates/ui-contract/src/contract/steps/measure.rs b/crates/ui-contract/src/contract/steps/measure.rs index 6293bebde2..41f0bbf289 100644 --- a/crates/ui-contract/src/contract/steps/measure.rs +++ b/crates/ui-contract/src/contract/steps/measure.rs @@ -30,6 +30,22 @@ pub(crate) fn measure_content( layout_memory::measure_call(input.ui.ctx(), || comp.measure(&cx)) } +/// Asks a component that supports it for both measures in one call. Both answers share the inputs read by the call, +/// so replay checks each answer under all of those inputs. +pub(crate) fn measure_content_pair( + comp: &mut C, + ideal: &MeasureIn<'_>, + least: &MeasureIn<'_>, + style: &C::Style, + resolved: &Resolved<'_>, + value: Option<&ValueOf>, +) -> Option<((Vec2, Vec2), Inputs)> { + let ideal_cx = measure_cx::(ideal, style, resolved, value); + let least_cx = measure_cx::(least, style, resolved, value); + let (pair, inputs) = layout_memory::measure_call(ideal.ui.ctx(), || comp.measure_pair(&ideal_cx, &least_cx)); + pair.map(|pair| (pair, inputs)) +} + /// Step 5, measure: the `content` size plus padding, then `max(min_size)`, and for any proposal but `Min` at least the /// ideal width, up to the room, unless the component is sized to its content (`sizing`, a leaf's `Sizing::Content`). /// At `Min` a least width the style sets takes the place of `min_size`'s width. `extent` is what the shape adds to the diff --git a/crates/ui-contract/tests/it/layout/fill.rs b/crates/ui-contract/tests/it/layout/fill.rs index 635f450d5b..0648551271 100644 --- a/crates/ui-contract/tests/it/layout/fill.rs +++ b/crates/ui-contract/tests/it/layout/fill.rs @@ -127,6 +127,7 @@ const LINE: f32 = 20.0; struct Asked { ideal: Cell, min: Cell, + pair: Cell, } /// A leaf `IDEAL` wide that narrows to `LEAST`, whatever its room; it counts the measures it answers in measuring runs. @@ -136,11 +137,13 @@ struct Stretch<'a> { common: Common, seen: &'a RefCell, asked: &'a Asked, + paired: bool, + track_input: bool, } impl<'a> Stretch<'a> { fn new(seen: &'a RefCell, asked: &'a Asked) -> Self { - Stretch { common: Common::new(), seen, asked } + Stretch { common: Common::new(), seen, asked, paired: false, track_input: false } } } @@ -154,6 +157,9 @@ impl Component for Stretch<'_> { } fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 { + if self.track_input { + let _ = cx.column_room(); + } let (count, width) = if cx.proposal == Proposal::Min { (&self.asked.min, LEAST) } else { (&self.asked.ideal, IDEAL) }; if is_measuring(cx.ui.ctx()) { count.set(count.get() + 1); @@ -161,6 +167,24 @@ impl Component for Stretch<'_> { Vec2::new(width, LINE) } + const PAIRED_MEASURE: bool = true; + + fn measure_pair(&mut self, ideal: &MeasureCx<'_, Self>, least: &MeasureCx<'_, Self>) -> Option<(Vec2, Vec2)> { + if !self.paired { + return None; + } + assert_eq!(ideal.proposal, Proposal::Ideal); + assert_eq!(least.proposal, Proposal::Min); + if self.track_input { + let _ = ideal.column_room(); + let _ = least.column_room(); + } + if is_measuring(ideal.ui.ctx()) { + self.asked.pair.set(self.asked.pair.get() + 1); + } + Some((Vec2::new(IDEAL, LINE), Vec2::new(LEAST, LINE))) + } + fn sense(&self) -> Sense { Sense::empty() } @@ -174,6 +198,44 @@ impl Component for Stretch<'_> { } } +/// Compare the paired path with the legacy component calls through a nested filling body and its kept transcript. +fn nested_pair(paired: bool) -> (Rect, Rect, usize, usize, usize) { + let seen = Rc::new(RefCell::new(Seen::default())); + let asked = Rc::new(Asked::default()); + let (shown, counts) = (Rc::clone(&seen), Rc::clone(&asked)); + let mut fixture = Fixture::new(move |ui| { + Panel::new().layout(BodyLayout::row()).sizing(Sizing::FillWidth).id_salt("outer").show(ui, |ui, _| { + Panel::new().layout(BodyLayout::row()).sizing(Sizing::FillWidth).id_salt("inner").show(ui, |ui, _| { + let mut child = Stretch::new(&shown, &counts); + child.paired = paired; + child.track_input = true; + child.sizing(Sizing::FillWidth).id_salt("child").show(ui); + }); + }); + }); + fixture.room(Vec2::new(200.0, 100.0)).settle(); + let rect = seen.borrow().rect; + let pairs = asked.pair.get(); + let separate_calls = asked.ideal.get() + asked.min.get(); + fixture.idle(3); + let after_idle = asked.pair.get(); + fixture.room(Vec2::new(140.0, 100.0)).settle(); + let resized = seen.borrow().rect; + (rect, resized, pairs, after_idle, separate_calls) +} + +#[trial] +fn paired_measure_matches_separate_calls_through_a_nested_body_and_replay() { + let (separate, separate_resized, _, _, separate_calls) = nested_pair(false); + let (paired, paired_resized, before_idle, after_idle, paired_calls) = nested_pair(true); + assert!(paired.width() > 0.0); + assert!((paired.size() - separate.size()).abs().max_elem() < 0.5, "paired {paired:?}, separate {separate:?}"); + assert!((paired_resized.size() - separate_resized.size()).abs().max_elem() < 0.5, "paired {paired_resized:?}, separate {separate_resized:?}"); + assert!(before_idle > 0, "the nested filling leaf used the paired path"); + assert!(paired_calls < separate_calls, "paired calls {paired_calls}, separate calls {separate_calls}"); + assert_eq!(before_idle, after_idle, "the kept transcript needs no new paired measure on idle frames"); +} + /// The rect of a row that fills a room `width` wide, scrolling along x when `scrolls`, and of the `Stretch` that fills /// its width alone in it. fn stretched(width: f32, scrolls: bool) -> (Rect, Rect) { diff --git a/crates/ui-widgets/src/buttons/button.rs b/crates/ui-widgets/src/buttons/button.rs index e4ea9fdcda..a208b05872 100644 --- a/crates/ui-widgets/src/buttons/button.rs +++ b/crates/ui-widgets/src/buttons/button.rs @@ -210,6 +210,12 @@ impl Component for Button<'_> { face::measure(cx, &self.spec(cx.resolved.label)) } + const PAIRED_MEASURE: bool = true; + + fn measure_pair(&mut self, ideal: &MeasureCx<'_, Self>, least: &MeasureCx<'_, Self>) -> Option<(Vec2, Vec2)> { + Some(face::measure_pair(ideal, least, &self.spec(ideal.resolved.label))) + } + fn sense(&self) -> Sense { if self.loading { Sense::FOCUS } else { Sense::CLICK | Sense::FOCUS } } diff --git a/crates/ui-widgets/src/buttons/face.rs b/crates/ui-widgets/src/buttons/face.rs index 16494c2f00..27bc12e755 100644 --- a/crates/ui-widgets/src/buttons/face.rs +++ b/crates/ui-widgets/src/buttons/face.rs @@ -168,6 +168,22 @@ pub(crate) fn measure(cx: &MeasureCx<'_, C>, spec: &FaceSpec<'_>) Vec2::new(text.x + fixed, text.y.max(icon)) } +/// The ideal and least face sizes from one paired component measure. +pub(crate) fn measure_pair(ideal: &MeasureCx<'_, C>, least: &MeasureCx<'_, C>, spec: &FaceSpec<'_>) -> (Vec2, Vec2) { + let icon = ideal.icon(); + let fixed = icon * spec.icons() as f32 + ideal.gap() * spec.gaps() as f32; + let (whole, narrow) = if spec.label.is_empty() { + (Vec2::ZERO, Vec2::ZERO) + } else { + let run = spec.run(); + let whole = ideal.layout_at(&run, ideal.room.map_or(f32::INFINITY, |room| (room - fixed).max(0.0))).size(); + let width = least.least_width(&run); + let height = least.layout_at(&run, f32::INFINITY).size().y; + (whole, Vec2::new(width, height)) + }; + (Vec2::new(whole.x + fixed, whole.y.max(icon)), Vec2::new(narrow.x + fixed, narrow.y.max(icon))) +} + /// `icon` in `color`, unless the spec names a colour of its own. pub(super) fn tinted<'a>(icon: &IconSpec<'a>, color: StyleColor) -> IconSpec<'a> { let icon = icon.clone(); diff --git a/crates/ui-widgets/src/buttons/link.rs b/crates/ui-widgets/src/buttons/link.rs index 7547049da0..4c4b7c1d4a 100644 --- a/crates/ui-widgets/src/buttons/link.rs +++ b/crates/ui-widgets/src/buttons/link.rs @@ -94,6 +94,20 @@ impl Component for Link { Vec2::new(layout.size().x, painted_height(&layout)) } + const PAIRED_MEASURE: bool = true; + + fn measure_pair(&mut self, ideal: &MeasureCx<'_, Self>, least: &MeasureCx<'_, Self>) -> Option<(Vec2, Vec2)> { + if !self.cut_first() { + return None; + } + let run = self.run(ideal.resolved.label); + let layout = ideal.layout_at(&run, ideal.room.unwrap_or(f32::INFINITY)); + let ideal_size = Vec2::new(layout.size().x, painted_height(&layout)); + let width = least.least_width(&run); + let whole = least.layout_at(&run, f32::INFINITY); + Some((ideal_size, Vec2::new(width, painted_height(&whole)))) + } + fn update(&mut self, cx: &mut Cx<'_, Self>) { let middle_click = match cx.press().phase { PressPhase::Release { start, click: true, .. } => start.button == PointerButton::Middle, diff --git a/crates/ui-widgets/src/buttons/split_button.rs b/crates/ui-widgets/src/buttons/split_button.rs index bf8fdd0836..abd9e5b1df 100644 --- a/crates/ui-widgets/src/buttons/split_button.rs +++ b/crates/ui-widgets/src/buttons/split_button.rs @@ -143,6 +143,15 @@ impl Component for SplitButton<'_, T> { Vec2::new(face.x + cx.icon(), face.y) } + const PAIRED_MEASURE: bool = true; + + fn measure_pair(&mut self, ideal: &MeasureCx<'_, Self>, least: &MeasureCx<'_, Self>) -> Option<(Vec2, Vec2)> { + let (mut whole, mut narrow) = face::measure_pair(ideal, least, &self.spec(ideal.resolved.label)); + whole.x += ideal.icon(); + narrow.x += least.icon(); + Some((whole, narrow)) + } + fn sense(&self) -> Sense { Sense::CLICK | Sense::FOCUS } diff --git a/crates/ui-widgets/src/buttons/toggle_button.rs b/crates/ui-widgets/src/buttons/toggle_button.rs index bf4ee21b08..936e47df0e 100644 --- a/crates/ui-widgets/src/buttons/toggle_button.rs +++ b/crates/ui-widgets/src/buttons/toggle_button.rs @@ -160,6 +160,12 @@ impl Component for ToggleButton<'_> { face::measure(cx, &self.spec(cx.resolved.label, true)) } + const PAIRED_MEASURE: bool = true; + + fn measure_pair(&mut self, ideal: &MeasureCx<'_, Self>, least: &MeasureCx<'_, Self>) -> Option<(Vec2, Vec2)> { + Some(face::measure_pair(ideal, least, &self.spec(ideal.resolved.label, true))) + } + fn sense(&self) -> Sense { Sense::CLICK | Sense::FOCUS } diff --git a/crates/ui-widgets/src/display/text.rs b/crates/ui-widgets/src/display/text.rs index 666161ab61..b80feac11c 100644 --- a/crates/ui-widgets/src/display/text.rs +++ b/crates/ui-widgets/src/display/text.rs @@ -183,6 +183,27 @@ impl Component for Text { } } + const PAIRED_MEASURE: bool = true; + + fn measure_pair(&mut self, ideal: &MeasureCx<'_, Self>, least: &MeasureCx<'_, Self>) -> Option<(Vec2, Vec2)> { + // Wrapped and document text need their own least-measure logic; keep the separate path for them. + if !self.spans.is_empty() || self.elide == Elide::Off { + return None; + } + let band = ideal.column_room().unwrap_or(f32::INFINITY); + let run = self.run(ideal.style.text.role, ideal.resolved.label); + let layout = ideal.layout_at(&run, band); + let ideal_size = match ideal.room { + Some(room) if layout.elided => Vec2::new(room, layout.size().y), + _ => layout.size(), + }; + // `measure` reads the column room even in its least branch. + let _ = least.column_room(); + let width = least.least_width(&run); + let height = least.layout_at(&run, f32::INFINITY).size().y; + Some((ideal_size, Vec2::new(width, height))) + } + fn cut_first(&self) -> bool { self.elide != Elide::Off } diff --git a/crates/ui-widgets/src/pickers/face.rs b/crates/ui-widgets/src/pickers/face.rs index c4afb34fb7..0e5fbf64c9 100644 --- a/crates/ui-widgets/src/pickers/face.rs +++ b/crates/ui-widgets/src/pickers/face.rs @@ -209,6 +209,33 @@ impl Component for SelectFace<'_> { Vec2::new(beside + text.x, text.y.max(picture)) } + const PAIRED_MEASURE: bool = true; + + fn measure_pair(&mut self, ideal: &MeasureCx<'_, Self>, least: &MeasureCx<'_, Self>) -> Option<(Vec2, Vec2)> { + // A caption may wrap differently under two rooms, so keep the separate path when the ideal has a room. + if ideal.room.is_some() { + return None; + } + let run = TextRun::new(ideal.resolved.label).role(ideal.style.text.role).elide(); + let whole = if self.name { ideal.layout_at(&run, f32::INFINITY).size() } else { Vec2::ZERO }; + let width = if self.name { least.least_width(&run) } else { 0.0 }; + let caption = self.caption_run(&ideal.style.caption).map_or(Vec2::ZERO, |run| ideal.layout(&run).size()); + let picture = match (&self.thumb, caption.y > 0.0) { + (None, _) => 0.0, + (Some(_), false) => ideal.style.thumb, + (Some(_), true) => ideal.style.thumb + ideal.style.caption_gap + caption.y, + }; + let beside = self.lead(ideal.style, caption.x) + reserve(&ideal.style.field, &self.leading_glyphs(), &[Self::chevron()]); + let mut ideal_width = whole.x; + if self.fit && self.name { + let field = ideal.style.field.metrics(); + let room = field.ideal_width - ideal.style.metrics.padding.start - ideal.style.metrics.padding.end - beside; + ideal_width = ideal_width.min(room.max(width)); + } + let height = whole.y.max(picture); + Some((Vec2::new(beside + ideal_width, height), Vec2::new(beside + width, height))) + } + fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { let chevron = Self::chevron(); let rtl = cx.flow.is_rtl();