From ef1a365006aa451644f4e764e8fcbe9c024d4385 Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 20 Nov 2026 08:05:28 +0000 Subject: [PATCH] Lay out a text measured at its least once TextRun::least (MeasureCx::least) returns the run laid out whole with its least width, so a text measured at Proposal::Min no longer lays the whole line out a second time for its height. A fitted select face takes its cap from the same call. A select of 29 blend modes plus the mixed word lays out 56 texts per Min ask instead of 86. --- crates/ui-contract/src/contract/cx/measure.rs | 10 +-- crates/ui-contract/src/primitives/mod.rs | 2 +- crates/ui-contract/src/primitives/text.rs | 35 +++++++--- crates/ui-widgets/src/buttons/face.rs | 2 +- crates/ui-widgets/src/buttons/link.rs | 3 +- crates/ui-widgets/src/display/text.rs | 2 +- crates/ui-widgets/src/pickers/face.rs | 15 ++-- crates/ui-widgets/tests/it/pickers/mod.rs | 1 + .../tests/it/pickers/select_measure.rs | 70 +++++++++++++++++++ docs/contract-api.md | 4 +- 10 files changed, 120 insertions(+), 24 deletions(-) create mode 100644 crates/ui-widgets/tests/it/pickers/select_measure.rs diff --git a/crates/ui-contract/src/contract/cx/measure.rs b/crates/ui-contract/src/contract/cx/measure.rs index 701f2643e..73f57a540 100644 --- a/crates/ui-contract/src/contract/cx/measure.rs +++ b/crates/ui-contract/src/contract/cx/measure.rs @@ -1,7 +1,7 @@ //! `MeasureCx`: step 5's read view of a component. use crate::contract::{Component, Resolved, Show, ValueOf}; -use crate::primitives::{TextEnv, TextLayout, TextRun}; +use crate::primitives::{LeastLayout, TextEnv, TextLayout, TextRun}; use crate::runtime::commands::{self, CommandRef, ResolvedCommand}; use crate::runtime::env::{EnvView, Label}; use crate::runtime::layout_memory; @@ -112,10 +112,10 @@ impl MeasureCx<'_, C> { run.layout(self.ui.ctx(), &self.text_env(), width) } - /// The width of `run`, a run that elides, at its readable least ([`TextRun::READABLE_CHARACTERS`]): the least - /// width an elidable text reports at `Proposal::Min`. - pub fn least_width(&self, run: &TextRun<'_>) -> f32 { - run.least_width(self.ui.ctx(), &self.text_env()) + /// `run`, a run that elides, laid out whole and its width at its readable least + /// ([`TextRun::READABLE_CHARACTERS`]): what an elidable text reports at `Proposal::Min`. Each is laid out once. + pub fn least(&self, run: &TextRun<'_>) -> LeastLayout { + run.least(self.ui.ctx(), &self.text_env()) } fn text_env(&self) -> TextEnv<'_> { diff --git a/crates/ui-contract/src/primitives/mod.rs b/crates/ui-contract/src/primitives/mod.rs index d302024b4..4547fd3af 100644 --- a/crates/ui-contract/src/primitives/mod.rs +++ b/crates/ui-contract/src/primitives/mod.rs @@ -54,7 +54,7 @@ pub use surface::Surface; pub(crate) use surface::SurfaceSlot; pub use surround::paint_surround; pub use svg_set::{AppIcons, register_app_icons}; -pub use text::{ChordText, HAlign, LayoutRun, ReadingMeasure, Span, TextLayout, TextRun, Wrap}; +pub use text::{ChordText, HAlign, LayoutRun, LeastLayout, ReadingMeasure, Span, TextLayout, TextRun, Wrap}; pub use text_input::{Completer, Completion, EditorLine, EditorPurpose, EditorRun, EditorState, TextKey, TextOptions}; pub use textures::TextureCache; pub use tool::ToolIcon; diff --git a/crates/ui-contract/src/primitives/text.rs b/crates/ui-contract/src/primitives/text.rs index bee717e76..835a23ecc 100644 --- a/crates/ui-contract/src/primitives/text.rs +++ b/crates/ui-contract/src/primitives/text.rs @@ -221,7 +221,7 @@ impl TextRun<'_> { /// `Line` run ignores it. pub(crate) fn layout(&self, ctx: &Context, env: &TextEnv<'_>, max_width: f32) -> TextLayout { let full = self.full_text(); - // Every cut goes through the one cut that `least_width` measures, so a run shown at its least keeps its + // Every cut goes through the one cut that `least` measures, so a run shown at its least keeps its // readable characters. let galley = match self.wrap { Wrap::Elide | Wrap::ElideStart | Wrap::ElideMiddle | Wrap::ElidePath => self.layout_cut(ctx, env, &full, max_width), @@ -233,15 +233,16 @@ impl TextRun<'_> { layout } - /// The width of the run at its readable least: cut where its wrap says, keeping - /// [`READABLE_CHARACTERS`](Self::READABLE_CHARACTERS) and the ellipsis, or whole when it has no more than one - /// character beyond them. - pub(crate) fn least_width(&self, ctx: &Context, env: &TextEnv<'_>) -> f32 { + /// The run laid out whole in unbounded room, and its width at its readable least: cut where its wrap says, + /// keeping [`READABLE_CHARACTERS`](Self::READABLE_CHARACTERS) and the ellipsis, or whole when it has no more than + /// one character beyond them. + pub(crate) fn least(&self, ctx: &Context, env: &TextEnv<'_>) -> LeastLayout { let full = self.full_text(); - let whole = text_layout::layout(ctx, self.job(env, self.spans.iter().map(|s| (s, s.text.as_ref())), f32::INFINITY)).size().x; + let laid = self.layout(ctx, env, f32::INFINITY); + let whole = laid.size().x; let ends: Vec = full.char_indices().map(|(at, _)| at).chain([full.len()]).collect(); if ends.len() - 1 <= Self::READABLE_CHARACTERS + 1 { - return whole; + return LeastLayout { width: whole, whole: laid }; } let rtl_text = !self.is_rtl(env) && craft_i18n::base_direction(&full) == Some(Direction::Rtl); let width = |pieces: &[Piece]| text_layout::layout(ctx, self.job_cut(env, &full, pieces, rtl_text)).size().x; @@ -260,7 +261,7 @@ impl TextRun<'_> { } else { width(&cut_pieces(&full, &ends, 0..full.len(), self.wrap, Self::READABLE_CHARACTERS)) }; - least.min(whole) + LeastLayout { width: least.min(whole), whole: laid } } /// Whether the paragraph runs right to left: the env's direction, unless the run is fixed left to right. @@ -434,6 +435,24 @@ pub(crate) fn note_painted(ctx: &Context, layout: &TextLayout) { } } +/// A run laid out whole on its line, and its width at its readable least: what a text that elides measures at +/// `Proposal::Min`. +#[derive(Clone)] +#[non_exhaustive] +pub struct LeastLayout { + /// The run laid out whole, as a layout in unbounded room lays it out. + pub whole: TextLayout, + /// The width at its readable least; never wider than `whole`. + pub width: f32, +} + +impl LeastLayout { + /// The size the run takes at its least: its least width and its whole line's height. + pub fn size(&self) -> egui::Vec2 { + egui::Vec2::new(self.width, self.whole.size().y) + } +} + /// A laid-out run. #[derive(Clone)] #[non_exhaustive] diff --git a/crates/ui-widgets/src/buttons/face.rs b/crates/ui-widgets/src/buttons/face.rs index 16494c2f0..af7bb4781 100644 --- a/crates/ui-widgets/src/buttons/face.rs +++ b/crates/ui-widgets/src/buttons/face.rs @@ -161,7 +161,7 @@ pub(crate) fn measure(cx: &MeasureCx<'_, C>, spec: &FaceSpec<'_>) Vec2::ZERO } else if cx.proposal == Proposal::Min { let run = spec.run(); - Vec2::new(cx.least_width(&run), cx.layout_at(&run, f32::INFINITY).size().y) + cx.least(&run).size() } else { cx.layout_at(&spec.run(), cx.room.map_or(f32::INFINITY, |room| (room - fixed).max(0.0))).size() }; diff --git a/crates/ui-widgets/src/buttons/link.rs b/crates/ui-widgets/src/buttons/link.rs index 7547049da..a4093c40c 100644 --- a/crates/ui-widgets/src/buttons/link.rs +++ b/crates/ui-widgets/src/buttons/link.rs @@ -88,7 +88,8 @@ impl Component for Link { let run = self.run(cx.resolved.label); // At its least a link that elides is at its readable least. if cx.proposal == Proposal::Min && self.cut_first() { - return Vec2::new(cx.least_width(&run), painted_height(&cx.layout_at(&run, f32::INFINITY))); + let least = cx.least(&run); + return Vec2::new(least.width, painted_height(&least.whole)); } let layout = cx.layout_at(&run, cx.room.unwrap_or(f32::INFINITY)); Vec2::new(layout.size().x, painted_height(&layout)) diff --git a/crates/ui-widgets/src/display/text.rs b/crates/ui-widgets/src/display/text.rs index 666161ab6..eb9c610db 100644 --- a/crates/ui-widgets/src/display/text.rs +++ b/crates/ui-widgets/src/display/text.rs @@ -164,7 +164,7 @@ impl Component for Text { // At its least an elided line is at its readable least. if cx.proposal == Proposal::Min { let run = self.run(role, cx.resolved.label); - return Vec2::new(cx.least_width(&run), cx.layout_at(&run, f32::INFINITY).size().y); + return cx.least(&run).size(); } let band = wrap.unwrap_or(f32::INFINITY); let mut layout = cx.layout_at(&self.run(role, cx.resolved.label), band); diff --git a/crates/ui-widgets/src/pickers/face.rs b/crates/ui-widgets/src/pickers/face.rs index fd8022fb5..998f8222b 100644 --- a/crates/ui-widgets/src/pickers/face.rs +++ b/crates/ui-widgets/src/pickers/face.rs @@ -190,11 +190,14 @@ impl Component for SelectFace<'_> { fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 { // At its least the face elides its name to its readable least. let run = TextRun::new(cx.resolved.label).role(cx.style.text.role).elide(); - let least = || cx.least_width(&run); - let mut text = match (self.name, cx.proposal) { - (true, Proposal::Min) => Vec2::new(least(), cx.layout_at(&run, f32::INFINITY).size().y), - (true, _) => cx.layout_at(&run, f32::INFINITY).size(), - (false, _) => Vec2::ZERO, + // A fitted box also needs the least to cap its text at. + let (mut text, least) = if !self.name { + (Vec2::ZERO, 0.0) + } else if cx.proposal == Proposal::Min || self.fit { + let least = cx.least(&run); + (if cx.proposal == Proposal::Min { least.size() } else { least.whole.size() }, least.width) + } else { + (cx.layout_at(&run, f32::INFINITY).size(), 0.0) }; let caption = self.caption_run(&cx.style.caption).map_or(Vec2::ZERO, |run| cx.layout(&run).size()); let picture = match (&self.thumb, caption.y > 0.0) { @@ -208,7 +211,7 @@ impl Component for SelectFace<'_> { // least. let field = cx.style.field.metrics(); let room = field.ideal_width - cx.style.metrics.padding.start - cx.style.metrics.padding.end - beside; - text.x = text.x.min(room.max(least())); + text.x = text.x.min(room.max(least)); } Vec2::new(beside + text.x, text.y.max(picture)) } diff --git a/crates/ui-widgets/tests/it/pickers/mod.rs b/crates/ui-widgets/tests/it/pickers/mod.rs index 8b2fd8a6b..9d7251e60 100644 --- a/crates/ui-widgets/tests/it/pickers/mod.rs +++ b/crates/ui-widgets/tests/it/pickers/mod.rs @@ -3,6 +3,7 @@ mod combo_list; mod date; mod select_list; +mod select_measure; mod select_steps; mod select_width; mod tree_list; diff --git a/crates/ui-widgets/tests/it/pickers/select_measure.rs b/crates/ui-widgets/tests/it/pickers/select_measure.rs new file mode 100644 index 000000000..52a2b2c21 --- /dev/null +++ b/crates/ui-widgets/tests/it/pickers/select_measure.rs @@ -0,0 +1,70 @@ +//! A select's measure lays out each face's text once per ask: the whole line, and at its least or in a fitted box the +//! cut it elides to. + +use craft_behavior::Proposal; +use craft_ui::contract::{Bind, Entry, Props as _, Show as _, Sizing}; +use craft_ui::pickers::Select; +use craft_ui::primitives::TextRun; +use craft_ui::testing::{Frames, last_pass_text_layouts, trial}; + +/// The blend modes of a brush's Mode select. +const MODES: [&str; 29] = [ + "Normal", + "Dissolve", + "Behind", + "Clear", + "Darken", + "Multiply", + "Color Burn", + "Linear Burn", + "Darker Color", + "Lighten", + "Screen", + "Color Dodge", + "Linear Dodge", + "Lighter Color", + "Overlay", + "Soft Light", + "Hard Light", + "Vivid Light", + "Linear Light", + "Pin Light", + "Hard Mix", + "Difference", + "Exclusion", + "Subtract", + "Divide", + "Hue", + "Saturation", + "Color", + "Luminosity", +]; + +/// The text layouts of one measure ask of a select of `labels` at `proposal`. +fn layouts(labels: &[&'static str], sizing: Option, proposal: Proposal) -> u32 { + let entries: Vec> = labels.iter().zip(0u8..).map(|(label, at)| Entry::new(at, *label)).collect(); + let mut frames = Frames::new(); + let passes = frames.frame(vec![], |ui| { + let select = Select::new("Mode", Bind::given(0u8), &entries); + let mut select = match sizing { + Some(sizing) => select.sizing(sizing), + None => select, + }; + select.measure_in(ui, proposal); + }); + assert_eq!(passes, 1, "a frame that only measures takes one pass"); + last_pass_text_layouts(&frames.ctx) +} + +/// With 29 entries, one ask lays out each entry's face once more than with 3: its whole line at `Ideal`, and at +/// `Min` or in a fitted box its whole line and, for a label longer than its readable least, the cut. +#[trial] +fn a_select_measure_lays_out_each_face_once() { + let added = &MODES[3..]; + let cuts = added.iter().filter(|label| label.chars().count() > TextRun::READABLE_CHARACTERS + 1).count() as u32; + let lines = added.len() as u32; + let more = |sizing, proposal| layouts(&MODES, sizing, proposal) - layouts(&MODES[..3], sizing, proposal); + assert_eq!(more(None, Proposal::Ideal), lines, "an Ideal ask lays out each face's line once"); + assert_eq!(more(None, Proposal::Min), lines + cuts, "a Min ask lays out each face's line and its cut once"); + assert_eq!(more(Some(Sizing::Content), Proposal::Ideal), lines + cuts, "a fitted box lays out each face's line and its cut once"); +} diff --git a/docs/contract-api.md b/docs/contract-api.md index b69e2aac7..050caf281 100644 --- a/docs/contract-api.md +++ b/docs/contract-api.md @@ -2141,7 +2141,7 @@ The component traits, context types, `PaintUi`, `Looks`, the pipeline and its st - `pub fn cell_width(&self, column: &craft_behavior::ColumnKey, width: f32)`. Reports, from the `Ideal` measure of a collection's item, the width its content in `column` takes unclipped, - `pub fn layout(&self, run: &TextRun<'_>) -> TextLayout`. Lays `run` out in the content's room, as `Draw::text` will paint it. - `pub fn layout_at(&self, run: &TextRun<'_>, width: f32) -> TextLayout`. Lays `run` out `width` wide, for a text that paints in a band narrower than the room. - - `pub fn least_width(&self, run: &TextRun<'_>) -> f32`. The width of `run`, a run that elides, at its readable least ([`TextRun::READABLE_CHARACTERS`]): the least + - `pub fn least(&self, run: &TextRun<'_>) -> LeastLayout`. `run`, a run that elides, laid out whole and its width at its readable least - `pub fn field_text(&self, run: &TextRun<'_>) -> Vec2`. The room an editable text in a control sized by its text needs for `run`: the laid-out text and the caret the - `pub fn layout_doc(&self, text: &crate::primitives::DocText<'_>) -> TextLayout`. Lays document `text` out `text.wrap` wide, as `ContentPainterCx::layout` will paint it: the galley a content - `pub fn child(&self, key: impl Hash + Debug, child: &mut D, proposal: Proposal) -> Vec2`. Measures a child under the composite's resolved `enabled` and `size`, as `PaintUi::child` will show it. @@ -2727,6 +2727,8 @@ Painters, `Draw`, the sensing core, the text adapter and `ImeSurface`. - `pub fn badge(mut self, color: DataColor) -> Self`. Adds a dot of `color` at the bottom-trailing corner of an SVG or glyph icon, ringed in the icon's colour: the - `craft_ui::primitives::ImeSurface`: `pub struct ImeSurface`. The handle of an editor part's focus widget. - `craft_ui::primitives::LayoutRun`: `pub struct LayoutRun { pub bytes: Range, pub rtl: bool, pub positions: Vec, pub widths: Vec }`. One direction run of a single-row layout, owned. +- `craft_ui::primitives::LeastLayout`: `pub struct LeastLayout { pub whole: TextLayout, pub width: f32 }`. A run laid out whole on its line, and its width at its readable least: what a text that elides measures at + - `pub fn size(&self) -> egui::Vec2`. The size the run takes at its least: its least width and its whole line's height. - `craft_ui::primitives::LoadFailure`: `pub enum LoadFailure { Missing, Unreadable, Panicked, Message(String) }`. Why a job made no picture. A job carries no text: the state carries this value and the painter words it at run - `pub fn text<'a>(&'a self, words: &'a TextureWords) -> &'a str`. The failure tooltip in the words of the scope (`cx.words::()`). - `craft_ui::primitives::LoadResult`: `pub type LoadResult = Result`. What a loader job returns: a `LoadedPicture`, or why there is none.