mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-21 10:01:42 +01:00
Merge pull request #2520 from aiongg/select-measure-once
Measure a select's faces once and keep them out of the layout memory
This commit is contained in:
14 files changed
+262
-32
No files matched your search
@@ -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<C: Component> 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<'_> {
|
||||
@@ -162,6 +162,9 @@ impl<C: Component> MeasureCx<'_, C> {
|
||||
/// The size `face` has when `PaintUi::face` shows it as the look of a part: steps 1, 2 and 4 of the face run
|
||||
/// under the composite's resolved `enabled`, `size` and sizing. A composite measured at its least measures the face at
|
||||
/// its least; otherwise the face has no proposal.
|
||||
/// The face is measured inside the composite's own run (measure, parts or header parts). Only the composite's
|
||||
/// measure call sizes the composite: its answer and the inputs the face read are what the layout memory keeps,
|
||||
/// and the face keeps no entry of its own.
|
||||
pub fn face<S: Show>(&self, mut face: S) -> Vec2 {
|
||||
// The face is the composite's look: a composite sized to its content sizes its face so.
|
||||
if let Some(sizing) = self.resolved.sizing {
|
||||
@@ -169,7 +172,7 @@ impl<C: Component> MeasureCx<'_, C> {
|
||||
}
|
||||
let id = self.id.with("face");
|
||||
let proposal = if self.proposal == Proposal::Min { Proposal::Min } else { Proposal::Ideal };
|
||||
crate::contract::measure_under(&mut face, self.ui, Some(id), proposal, Some(&self.under()))
|
||||
crate::contract::run::measure_face(&mut face, self.ui, id, proposal, &self.under())
|
||||
}
|
||||
|
||||
/// The icon side of the component's size.
|
||||
|
||||
@@ -29,8 +29,8 @@ use std::sync::Arc;
|
||||
mod typed;
|
||||
|
||||
pub(crate) use typed::{
|
||||
MemberNode, MemberPrep, MemberShown, measure_container, measure_under, measured_extent, member_prepare, member_show, paint_face, show_container,
|
||||
show_container_taking, show_items, show_leaf,
|
||||
MemberNode, MemberPrep, MemberShown, measure_container, measure_face, measure_under, measured_extent, member_prepare, member_show, paint_face,
|
||||
show_container, show_container_taking, show_items, show_leaf,
|
||||
};
|
||||
|
||||
/// Where a component is placed.
|
||||
@@ -1822,10 +1822,28 @@ fn scoped_env(ctx: &Context, env: Env, theme: Option<ThemeScope>) -> Env {
|
||||
}
|
||||
}
|
||||
|
||||
/// What a measure outside the component's own show is for.
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub(crate) enum MeasureFor {
|
||||
/// A component its caller shows, or sizes a rect by: a leaf of the transcript of the body it is measured in.
|
||||
Component,
|
||||
/// A composite's face (`MeasureCx::face`), measured inside the composite's own run (measure, parts or header
|
||||
/// parts). Only the composite's measure call sizes the composite, and its answer and inputs are the transcript's
|
||||
/// record, so the face keeps no leaf of its own.
|
||||
Face,
|
||||
}
|
||||
|
||||
/// Measures the component in `t` as `show` will show it, without allocating: steps 1 to 5 under the id `show` would
|
||||
/// use next (or `id`). It reads memory through a peek, registers no id and applies nothing. `under` is the
|
||||
/// composite's resolved `(enabled, size)` when it measures a child it shows with `PaintUi::child`.
|
||||
pub(super) fn measure_shape(t: &mut dyn Erased, ui: &Ui, id: Option<Id>, proposal: Proposal, under: Option<&super::cx::Under>) -> Vec2 {
|
||||
pub(super) fn measure_shape(
|
||||
t: &mut dyn Erased,
|
||||
ui: &Ui,
|
||||
id: Option<Id>,
|
||||
proposal: Proposal,
|
||||
under: Option<&super::cx::Under>,
|
||||
purpose: MeasureFor,
|
||||
) -> Vec2 {
|
||||
let facts = t.facts();
|
||||
let env = match under {
|
||||
Some(under) => super::cx::child_env(&Env::of(ui), under),
|
||||
@@ -1855,7 +1873,7 @@ pub(super) fn measure_shape(t: &mut dyn Erased, ui: &Ui, id: Option<Id>, proposa
|
||||
};
|
||||
let ask = |t: &mut dyn Erased, proposal: Proposal| measured(t, &MeasureIn { ui, env: &env, proposal, room: None }, &*style, &resolved, extent);
|
||||
let (took, inputs) = ask(&mut *t, proposal);
|
||||
if leaf && (!ui.is_sizing_pass() || crate::runtime::measure::active(ui.ctx())) {
|
||||
if leaf && purpose == MeasureFor::Component && (!ui.is_sizing_pass() || crate::runtime::measure::active(ui.ctx())) {
|
||||
let calls = &mut [Call { proposal, room: None, inputs, took }];
|
||||
crate::runtime::layout_memory::leaf_call(ui.ctx(), id, calls, |proposal, _| ask(&mut *t, proposal).0);
|
||||
}
|
||||
|
||||
@@ -520,8 +520,9 @@ fn measure_shape<C: Component>(
|
||||
proposal: Proposal,
|
||||
under: Option<&crate::contract::cx::Under>,
|
||||
hooks: &mut dyn ShapeHooks<C>,
|
||||
purpose: super::MeasureFor,
|
||||
) -> Vec2 {
|
||||
super::measure_shape(&mut Typed::new(Held::Lent(comp), hooks), ui, id, proposal, under)
|
||||
super::measure_shape(&mut Typed::new(Held::Lent(comp), hooks), ui, id, proposal, under, purpose)
|
||||
}
|
||||
|
||||
pub(crate) fn show_leaf<C: Component + Meta<Shape = crate::contract::shape::Leaf>>(
|
||||
@@ -634,7 +635,13 @@ pub(crate) fn measure_under<C: Component>(
|
||||
proposal: Proposal,
|
||||
under: Option<&crate::contract::cx::Under>,
|
||||
) -> Vec2 {
|
||||
measure_shape(comp, ui, id, proposal, under, &mut LeafHooks { scope: None })
|
||||
measure_shape(comp, ui, id, proposal, under, &mut LeafHooks { scope: None }, super::MeasureFor::Component)
|
||||
}
|
||||
|
||||
/// Measures `face` as `PaintUi::face` will show it, as part of the measure call of the composite whose `under` it
|
||||
/// takes: as [`measure_under`], with no transcript leaf of its own.
|
||||
pub(crate) fn measure_face<C: Component>(face: &mut C, ui: &Ui, id: Id, proposal: Proposal, under: &crate::contract::cx::Under) -> Vec2 {
|
||||
measure_shape(face, ui, Some(id), proposal, Some(under), &mut LeafHooks { scope: None }, super::MeasureFor::Face)
|
||||
}
|
||||
|
||||
/// Measures a container with `body` as `show` will show it: steps 1 to 5, with the body, header, footer and end
|
||||
@@ -649,7 +656,7 @@ pub(crate) fn measure_container<C: Container, R>(
|
||||
body(ui, cx);
|
||||
};
|
||||
let mut hooks = crate::contract::body::BodyHooks::of(comp, &mut call);
|
||||
measure_shape(comp, ui, None, proposal, None, &mut hooks)
|
||||
measure_shape(comp, ui, None, proposal, None, &mut hooks, super::MeasureFor::Component)
|
||||
}
|
||||
|
||||
/// A measuring run's extent: the shape's `measured_extent` under a `Cx` that senses nothing and writes only to the
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<usize> = 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]
|
||||
|
||||
@@ -1106,6 +1106,14 @@ pub fn forget_kept(ctx: &Context) {
|
||||
});
|
||||
}
|
||||
|
||||
/// How many component entries the valid kept measurements hold, each body counting only the components directly in
|
||||
/// it.
|
||||
#[cfg(feature = "testing")]
|
||||
pub fn kept_leaves(ctx: &Context) -> usize {
|
||||
let moment = Moment::of(ctx);
|
||||
with(ctx, &moment, |memory| memory.kept.values().filter(|kept| kept.valid).map(|kept| kept.transcript.leaves.len()).sum())
|
||||
}
|
||||
|
||||
/// Asks the end hook for the frame's discard pass, as a change of measured size does.
|
||||
#[track_caller]
|
||||
pub(crate) fn ask_discard(ctx: &Context) {
|
||||
|
||||
@@ -40,6 +40,8 @@ pub mod windows;
|
||||
pub use crate::contract::{Exemption, PatternKind, PatternProbe, Probe, Registration};
|
||||
#[cfg(feature = "testing")]
|
||||
pub use crate::runtime::layout_memory::forget_kept as forget_measurements;
|
||||
#[cfg(feature = "testing")]
|
||||
pub use crate::runtime::layout_memory::kept_leaves as kept_measurement_leaves;
|
||||
pub use crate::runtime::measure::{
|
||||
Settle, active as is_measuring, body_runs as measuring_runs, contributions as measured_contributions,
|
||||
last_pass_body_runs as last_pass_measuring_runs, last_pass_measure_asks, last_pass_memo_key_bytes, last_pass_runs, last_pass_settles,
|
||||
|
||||
@@ -161,7 +161,7 @@ pub(crate) fn measure<C: Component>(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()
|
||||
};
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
mod combo_list;
|
||||
mod date;
|
||||
mod select_list;
|
||||
mod select_measure;
|
||||
mod select_steps;
|
||||
mod select_width;
|
||||
mod tree_list;
|
||||
@@ -0,0 +1,164 @@
|
||||
//! 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. The measure call is the layout memory's record of the select: its faces keep no entries of their
|
||||
//! own, so a face change that leaves the select's size alone asks for no discard pass, and one that changes it shows in
|
||||
//! its frame.
|
||||
|
||||
use craft_behavior::Proposal;
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _, Sizing};
|
||||
use craft_ui::layout_components::{Row, Stack};
|
||||
use craft_ui::pickers::Select;
|
||||
use craft_ui::primitives::TextRun;
|
||||
use craft_ui::testing::{Frames, kept_measurement_leaves, last_pass_text_layouts, trial};
|
||||
use egui::Rect;
|
||||
|
||||
/// 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<Sizing>, proposal: Proposal) -> u32 {
|
||||
let entries: Vec<Entry<u8>> = 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");
|
||||
}
|
||||
|
||||
/// How far two widths may differ and still count as the same, in points.
|
||||
const SLACK: f32 = 0.5;
|
||||
/// The widest entry: wider than a field, so it sets the select's width.
|
||||
const WIDEST: &str = "Linear Dodge, the widest blend mode name of them all";
|
||||
|
||||
/// The blend modes with the widest entry at index 5.
|
||||
fn modes() -> Vec<&'static str> {
|
||||
let mut labels = MODES.to_vec();
|
||||
labels[5] = WIDEST;
|
||||
labels
|
||||
}
|
||||
|
||||
/// A content-sized stack in a row, holding a select of `labels` (or a button in its place), with a button after it.
|
||||
/// Returns the select's rect.
|
||||
fn scene(ui: &mut egui::Ui, labels: &[&'static str], select: bool) -> Rect {
|
||||
let mut rect = Rect::NOTHING;
|
||||
Row::new().show(ui, |ui, _| {
|
||||
Stack::new().show(ui, |ui, _| {
|
||||
if select {
|
||||
let entries: Vec<Entry<u8>> = labels.iter().zip(0u8..).map(|(label, at)| Entry::new(at, *label)).collect();
|
||||
rect = Select::new("Mode", Bind::given(1u8), &entries).show(ui).response.rect;
|
||||
} else {
|
||||
Button::new("Mode").show(ui);
|
||||
}
|
||||
});
|
||||
Button::new("Link").show(ui);
|
||||
});
|
||||
rect
|
||||
}
|
||||
|
||||
/// The select's width once settled with `labels`.
|
||||
fn settled_width(labels: &[&'static str]) -> f32 {
|
||||
let mut frames = Frames::new();
|
||||
let mut rect = Rect::NOTHING;
|
||||
frames.idle(30, |ui| rect = scene(ui, labels, true));
|
||||
rect.width()
|
||||
}
|
||||
|
||||
/// A settled select whose first entry widens, still narrower than its widest, takes its frame in one pass: the change
|
||||
/// leaves the select's size alone, so no kept measurement is dropped.
|
||||
#[trial]
|
||||
fn a_face_change_that_leaves_the_select_size_alone_asks_for_no_discard_pass() {
|
||||
let mut labels = modes();
|
||||
let mut frames = Frames::new();
|
||||
let mut rect = Rect::NOTHING;
|
||||
frames.idle(30, |ui| rect = scene(ui, &labels, true));
|
||||
let before = rect.width();
|
||||
// Entry 0 is the first face `Select::measure` visits: with a face leaf, the transcript kept that face's call, so
|
||||
// this edit was the one that caused a discard pass.
|
||||
labels[0] = "Normal, wider than a field yet narrower";
|
||||
let passes = frames.frame(vec![], |ui| rect = scene(ui, &labels, true));
|
||||
assert!((rect.width() - before).abs() <= SLACK, "the wider first entry leaves the select's width alone: {before} {}", rect.width());
|
||||
assert_eq!(passes, 1, "a face change that leaves the select's size alone takes one pass");
|
||||
}
|
||||
|
||||
/// A label change at any index that widens the select shows in its frame, at the width a fresh select of those labels
|
||||
/// settles at.
|
||||
#[trial]
|
||||
fn a_label_change_that_widens_the_select_shows_in_its_frame() {
|
||||
for at in [0, 7, 28] {
|
||||
let mut labels = modes();
|
||||
let mut frames = Frames::new();
|
||||
let mut rect = Rect::NOTHING;
|
||||
frames.idle(30, |ui| rect = scene(ui, &labels, true));
|
||||
let before = rect.width();
|
||||
labels[at] = "Linear Dodge, a blend mode name wider still than the widest of them all";
|
||||
frames.frame(vec![], |ui| rect = scene(ui, &labels, true));
|
||||
let fresh = settled_width(&labels);
|
||||
assert!(fresh > before + SLACK, "the new label at {at} widens the select: {before} {fresh}");
|
||||
assert!((rect.width() - fresh).abs() <= SLACK, "the label change at {at} shows in its frame: {} {fresh}", rect.width());
|
||||
}
|
||||
}
|
||||
|
||||
/// The layout memory keeps one entry for a select, as for a button in its place: its 29 faces keep none.
|
||||
#[trial]
|
||||
fn a_select_keeps_one_layout_memory_entry() {
|
||||
let leaves = |select: bool| {
|
||||
let labels = modes();
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(30, |ui| {
|
||||
scene(ui, &labels, select);
|
||||
});
|
||||
kept_measurement_leaves(&frames.ctx)
|
||||
};
|
||||
let with_button = leaves(false);
|
||||
assert!(with_button > 0, "the scene keeps measurements");
|
||||
assert_eq!(leaves(true), with_button, "a select keeps one entry, as a button does");
|
||||
}
|
||||
@@ -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<D: Show>(&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<usize>, pub rtl: bool, pub positions: Vec<f32>, pub widths: Vec<f32> }`. 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::<TextureWords>()`).
|
||||
- `craft_ui::primitives::LoadResult`: `pub type LoadResult = Result<LoadedPicture, LoadFailure>`. What a loader job returns: a `LoadedPicture`, or why there is none.
|
||||
@@ -3617,6 +3619,7 @@ Layout memory: sizes one pass measures and the next pass places by, the one disc
|
||||
|
||||
- `craft_ui::runtime::layout_memory::emit_text`: `pub fn emit_text(ctx: &Context, id: Id, text: Option<&str>)`. Hands forward a text a run found under `id` that content outside the body it runs in shows, as [`emit_record`]
|
||||
- `craft_ui::runtime::layout_memory::forget_kept`: `pub fn forget_kept(ctx: &Context)`. Drops every kept measurement, so the next pass measures each body as a first pass would.
|
||||
- `craft_ui::runtime::layout_memory::kept_leaves`: `pub fn kept_leaves(ctx: &Context) -> usize`. How many component entries the valid kept measurements hold, each body counting only the components directly in
|
||||
- `craft_ui::runtime::layout_memory::text`: `pub fn text(ctx: &Context, id: Id) -> Option<Arc<str>>`. The text a run of this pass handed forward under `id` ([`emit_text`]), or a kept transcript gave this pass.
|
||||
|
||||
## `craft_ui::runtime::memory`
|
||||
@@ -3987,6 +3990,7 @@ The fixture, the suite runner and the story helpers.
|
||||
- `craft_ui::testing::forget_measurements`: re-export of `craft_ui::runtime::layout_memory::forget_kept`.
|
||||
- `craft_ui::testing::install_spec`: re-export of `craft_ui::testing::windows::install_spec`.
|
||||
- `craft_ui::testing::is_measuring`: re-export of `craft_ui::runtime::measure::active` (defined outside these crates).
|
||||
- `craft_ui::testing::kept_measurement_leaves`: re-export of `craft_ui::runtime::layout_memory::kept_leaves`.
|
||||
- `craft_ui::testing::last_pass_measuring_runs`: re-export of `craft_ui::runtime::measure::last_pass_body_runs` (defined outside these crates).
|
||||
- `craft_ui::testing::last_pass_runs`: re-export of `craft_ui::runtime::measure::last_pass_runs` (defined outside these crates).
|
||||
- `craft_ui::testing::measured_contributions`: re-export of `craft_ui::runtime::measure::contributions` (defined outside these crates).
|
||||
|
||||
Reference in new issue
Block a user