mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 04:22:38 +01:00
Reuse prepared steps for adjacent labelled leaves
This commit is contained in:
1 parent
e840877d93
commit
db604faefb
8 files changed
+288
-17
No files matched your search
@@ -166,11 +166,18 @@ impl PaintUi<'_> {
|
||||
layout_memory::boundary(&ctx, true, || {
|
||||
let placed = crate::layout::ColumnGroup::field(&mut *self.ui, |ui| label::leaf(ui, &env, &at, &mut child, None));
|
||||
let mut control = placed.control;
|
||||
let mut prepared = None;
|
||||
if child.fills() != Some(craft_behavior::Axis::Y) {
|
||||
let ideal = run::measure_under(&mut child, &*self.ui, Some(id), craft_behavior::Proposal::Ideal, Some(&under));
|
||||
let ideal = if self.fade.is_none() {
|
||||
let (ideal, kept) = run::measure_under_prepared(&mut child, &*self.ui, id, craft_behavior::Proposal::Ideal, &under);
|
||||
prepared = kept;
|
||||
ideal
|
||||
} else {
|
||||
run::measure_under(&mut child, &*self.ui, Some(id), craft_behavior::Proposal::Ideal, Some(&under))
|
||||
};
|
||||
control.max.y = control.max.y.min(control.min.y + ideal.y);
|
||||
}
|
||||
let output = self.child_with(id, control, child, None, under.inside.clone());
|
||||
let output = self.child_with_prepared(id, control, child, None, under.inside.clone(), prepared);
|
||||
placed.finish(&ctx);
|
||||
output
|
||||
})
|
||||
@@ -179,6 +186,18 @@ impl PaintUi<'_> {
|
||||
/// Shows `child` at `rect` under the composite's `enabled` and `size`, and `inside`, the inside label of the side
|
||||
/// label the child is the control of.
|
||||
fn child_with<C: Show>(&mut self, id: Id, rect: Rect, child: C, tip: Option<TipFn<'_>>, inside: Option<Arc<InsideLabel>>) -> C::Output {
|
||||
self.child_with_prepared(id, rect, child, tip, inside, None)
|
||||
}
|
||||
|
||||
fn child_with_prepared<C: Show>(
|
||||
&mut self,
|
||||
id: Id,
|
||||
rect: Rect,
|
||||
child: C,
|
||||
tip: Option<TipFn<'_>>,
|
||||
inside: Option<Arc<InsideLabel>>,
|
||||
prepared: Option<run::PreparedLeaf<C>>,
|
||||
) -> C::Output {
|
||||
let scope = sub(self.id, "children");
|
||||
let parent = &mut *self.ui;
|
||||
let kids = self.kids.get_or_insert_with(|| {
|
||||
@@ -186,7 +205,10 @@ impl PaintUi<'_> {
|
||||
alloc::child(parent, access::with_parent(builder, self.id))
|
||||
});
|
||||
let ctx = kids.ctx().clone();
|
||||
let env = super::cx::child_env(&Env::of(kids), &super::cx::Under { enabled: self.enabled, size: self.size, inside });
|
||||
let source = Env::of(kids);
|
||||
let child = super::Props::id(child, id);
|
||||
let prepared = prepared.filter(|prepared| self.fade.is_none() && prepared.matches(&child, &source));
|
||||
let env = super::cx::child_env(&source, &super::cx::Under { enabled: self.enabled, size: self.size, inside });
|
||||
// A child shown while the owner's fade runs lies under the owner's veil, so a faded child adds none of its own.
|
||||
let env = match self.fade {
|
||||
Some(_) => {
|
||||
@@ -200,7 +222,10 @@ impl PaintUi<'_> {
|
||||
let installed = Env::install_for(kids, env);
|
||||
// The child sits in a rect its owner chose, and the owner's own measure answers for it: no body's transcript
|
||||
// holds it.
|
||||
let output = layout_memory::boundary(&ctx, true, || run::show_leaf(&mut Some(super::Props::id(child, id)), kids, ShowAt::Rect(rect), tip));
|
||||
let output = layout_memory::boundary(&ctx, true, || match prepared {
|
||||
Some(prepared) => run::show_leaf_prepared(&mut Some(child), kids, ShowAt::Rect(rect), tip, prepared),
|
||||
None => run::show_leaf(&mut Some(child), kids, ShowAt::Rect(rect), tip),
|
||||
});
|
||||
Env::restore(&ctx, installed);
|
||||
output
|
||||
}
|
||||
|
||||
@@ -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, PreparedLeaf, measure_container, measure_under, measure_under_prepared, measured_extent, member_prepare,
|
||||
member_show, paint_face, show_container, show_container_taking, show_items, show_leaf, show_leaf_prepared,
|
||||
};
|
||||
|
||||
/// Where a component is placed.
|
||||
@@ -1387,22 +1387,31 @@ fn measured(t: &mut dyn Erased, input: &MeasureIn<'_>, style: &dyn StyleView, re
|
||||
|
||||
/// The pipeline: steps 1 to 17 for one component, whatever its type and shape. A component that takes a cell of a
|
||||
/// `Grid` runs under the column group of its cell's column (`ColumnGroup::cell`).
|
||||
pub(super) struct PreparedStep {
|
||||
step1: steps::EnvStep,
|
||||
style: Arc<dyn StyleView>,
|
||||
}
|
||||
|
||||
pub(super) fn run(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, tip: Option<TipFn<'_>>) {
|
||||
run_with(t, ui, at, tip, None);
|
||||
}
|
||||
|
||||
fn run_with(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, tip: Option<TipFn<'_>>, prepared: Option<PreparedStep>) {
|
||||
let cell = (matches!(at, ShowAt::Flow) && t.in_flow()).then(|| crate::layout::grid_cell(ui)).flatten();
|
||||
match cell {
|
||||
Some((grid, column, sized)) => {
|
||||
let ctx = ui.ctx().clone();
|
||||
crate::layout::ColumnGroup::cell(&ctx, grid, column, sized, || run_steps(t, ui, at, tip));
|
||||
crate::layout::ColumnGroup::cell(&ctx, grid, column, sized, || run_steps(t, ui, at, tip, prepared));
|
||||
}
|
||||
None => run_steps(t, ui, at, tip),
|
||||
None => run_steps(t, ui, at, tip, prepared),
|
||||
}
|
||||
}
|
||||
|
||||
/// Steps 1 to 17 of `run`. A component's children run inside step 5 (a body's measuring run) and inside the passes
|
||||
/// of steps 6 to 16, so the frames on that path keep only what the run reads after them: each step runs in its own
|
||||
/// function, and `run_steps` holds what steps 1 to 5 found.
|
||||
fn run_steps(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, tip: Option<TipFn<'_>>) {
|
||||
let begun = begin(t, ui);
|
||||
fn run_steps(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, tip: Option<TipFn<'_>>, prepared: Option<PreparedStep>) {
|
||||
let begun = begin(t, ui, prepared);
|
||||
let mut stage = begun.open_stage(t);
|
||||
let mixed = value_step(t, &begun, &mut stage);
|
||||
let resolved = begun.resolved(t);
|
||||
@@ -1467,7 +1476,7 @@ impl Begun {
|
||||
/// Steps 1 to 3: env, style and id.
|
||||
// Kept out of the recursive frame above it: opt-level 2 inlines it there, so its locals stay on the stack while children run.
|
||||
#[inline(never)]
|
||||
fn begin(t: &mut dyn Erased, ui: &Ui) -> Begun {
|
||||
fn begin(t: &mut dyn Erased, ui: &Ui, prepared: Option<PreparedStep>) -> Begun {
|
||||
let ctx = ui.ctx().clone();
|
||||
let facts = t.facts();
|
||||
crate::runtime::measure::count_run(&ctx, facts.name);
|
||||
@@ -1484,9 +1493,14 @@ fn begin(t: &mut dyn Erased, ui: &Ui) -> Begun {
|
||||
// scope, and everything it shows inherits it.
|
||||
let env = scoped_env(&ctx, Env::of(ui), t.theme_scope());
|
||||
let common: Common = t.take_common();
|
||||
let step1 = steps::env(&ctx, &env, &common, t.variants(common.variant));
|
||||
// 2 style
|
||||
let style = t.style(&ctx, &steps::style_cx(&ctx, &env, &step1));
|
||||
let (step1, style) = match prepared {
|
||||
Some(prepared) => (prepared.step1, prepared.style),
|
||||
None => {
|
||||
let step1 = steps::env(&ctx, &env, &common, t.variants(common.variant));
|
||||
let style = t.style(&ctx, &steps::style_cx(&ctx, &env, &step1));
|
||||
(step1, style)
|
||||
}
|
||||
};
|
||||
// 3 id
|
||||
let id = steps::id(ui, &common.id, !sizing, true, facts.style_key, facts.name);
|
||||
if recording {
|
||||
@@ -1843,6 +1857,10 @@ fn scoped_env(ctx: &Context, env: Env, theme: Option<ThemeScope>) -> Env {
|
||||
/// 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 {
|
||||
measure_shape_with(t, ui, id, proposal, under).0
|
||||
}
|
||||
|
||||
fn measure_shape_with(t: &mut dyn Erased, ui: &Ui, id: Option<Id>, proposal: Proposal, under: Option<&super::cx::Under>) -> (Vec2, steps::EnvStep) {
|
||||
let facts = t.facts();
|
||||
let env = match under {
|
||||
Some(under) => super::cx::child_env(&Env::of(ui), under),
|
||||
@@ -1879,7 +1897,7 @@ pub(super) fn measure_shape(t: &mut dyn Erased, ui: &Ui, id: Option<Id>, proposa
|
||||
if let Some(key) = t.bind_key(id) {
|
||||
reads::note(ui.ctx(), key, id, true, &[Sample { proposal, room: None, took }]);
|
||||
}
|
||||
took
|
||||
(took, step1)
|
||||
}
|
||||
|
||||
/// The flags a host passes to its faces: the validity of its value. The rest (focus, selection, open) belong to the
|
||||
|
||||
@@ -12,7 +12,7 @@ use crate::contract::{
|
||||
use crate::primitives::{FaceRecord, face_id};
|
||||
use crate::runtime::access::{Politeness, SetPosition};
|
||||
use crate::runtime::commands::{self, ResolvedCommand};
|
||||
use crate::runtime::env::{AppValues, Env, Message, ThemeScope};
|
||||
use crate::runtime::env::{AppValues, Env, Label, Message, ThemeScope};
|
||||
use crate::runtime::focus;
|
||||
use crate::runtime::layout_memory::Inputs;
|
||||
use crate::runtime::memory::{MemoryStage, sub};
|
||||
@@ -529,6 +529,100 @@ fn measure_shape<C: Component>(
|
||||
super::measure_shape(&mut Typed::new(Held::Lent(comp), hooks), ui, id, proposal, under)
|
||||
}
|
||||
|
||||
/// Step 1 and style from a measure immediately followed by a show of the same leaf.
|
||||
pub(crate) struct PreparedLeaf<C: Component> {
|
||||
source: Env,
|
||||
inputs: StepInputs,
|
||||
style_key: C::StyleKey,
|
||||
step1: steps::EnvStep,
|
||||
style: Arc<C::Style>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
struct StepInputs {
|
||||
label: Label,
|
||||
enabled: Option<bool>,
|
||||
read_only: Option<bool>,
|
||||
size: Option<craft_theme::ControlSize>,
|
||||
glyphs: Option<crate::style::GlyphRole>,
|
||||
variant: Option<Variant>,
|
||||
}
|
||||
|
||||
impl StepInputs {
|
||||
fn of(common: &Common) -> Option<Self> {
|
||||
// A command or catalogue word can change independently during measure; disabled children get a different
|
||||
// inherited env in the label step. Those cases take the ordinary path.
|
||||
if common.command.is_some() || matches!(&common.label, Label::Word(_)) || common.enabled == Some(false) {
|
||||
return None;
|
||||
}
|
||||
Some(Self {
|
||||
label: common.label.clone(),
|
||||
enabled: common.enabled,
|
||||
read_only: common.read_only,
|
||||
size: common.size,
|
||||
glyphs: common.glyphs,
|
||||
variant: common.variant,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Component> PreparedLeaf<C> {
|
||||
pub(crate) fn matches(&self, child: &C, source: &Env) -> bool {
|
||||
self.source.same_source(source)
|
||||
&& StepInputs::of(child.common(Internal::new())).as_ref() == Some(&self.inputs)
|
||||
&& child.style_key() == self.style_key
|
||||
}
|
||||
}
|
||||
|
||||
/// Measures a leaf and keeps its steps only when the same component and scope are still present afterwards.
|
||||
pub(crate) fn measure_under_prepared<C: Show>(
|
||||
comp: &mut C,
|
||||
ui: &Ui,
|
||||
id: Id,
|
||||
proposal: Proposal,
|
||||
under: &crate::contract::cx::Under,
|
||||
) -> (Vec2, Option<PreparedLeaf<C>>) {
|
||||
let Some(inputs) = StepInputs::of(comp.common(Internal::new())) else {
|
||||
return (measure_under(comp, ui, Some(id), proposal, Some(under)), None);
|
||||
};
|
||||
let source = Env::of(ui);
|
||||
let key = comp.style_key();
|
||||
let mut hooks = LeafHooks { scope: None };
|
||||
let mut typed = Typed::new(Held::Lent(comp), &mut hooks);
|
||||
let (size, step1) = super::measure_shape_with(&mut typed, ui, Some(id), proposal, Some(under));
|
||||
let style = typed.style.clone();
|
||||
drop(typed);
|
||||
let prepared = style.and_then(|style| {
|
||||
(source.same_source(&Env::of(ui)) && StepInputs::of(comp.common(Internal::new())).as_ref() == Some(&inputs) && comp.style_key() == key)
|
||||
.then_some(PreparedLeaf { source, inputs, style_key: key, step1, style })
|
||||
});
|
||||
(size, prepared)
|
||||
}
|
||||
|
||||
/// Shows the same leaf with steps kept by `measure_under_prepared`.
|
||||
pub(crate) fn show_leaf_prepared<C: Show>(
|
||||
slot: &mut Option<C>,
|
||||
ui: &mut Ui,
|
||||
at: ShowAt,
|
||||
tip: Option<TipFn<'_>>,
|
||||
prepared: PreparedLeaf<C>,
|
||||
) -> C::Output {
|
||||
let PreparedLeaf { step1, style, .. } = prepared;
|
||||
#[cfg(feature = "testing")]
|
||||
{
|
||||
crate::runtime::measure::count_prepared_leaf(ui.ctx());
|
||||
let env = Env::of(ui);
|
||||
let scx = steps::style_cx(ui.ctx(), &env, &step1);
|
||||
crate::testing::style::record::<C>(ui.ctx(), &scx, held(slot).style_key(), &style);
|
||||
}
|
||||
let mut hooks = LeafHooks { scope: held(slot).common(Internal::new()).command_scope };
|
||||
let mut typed = Typed::owned(slot, &mut hooks);
|
||||
typed.style = Some(Arc::clone(&style));
|
||||
let style: Arc<dyn StyleView> = style;
|
||||
super::run_with(&mut typed, ui, at, tip, Some(super::PreparedStep { step1, style }));
|
||||
typed.output()
|
||||
}
|
||||
|
||||
pub(crate) fn show_leaf<C: Component + Meta<Shape = crate::contract::shape::Leaf>>(
|
||||
slot: &mut Option<C>,
|
||||
ui: &mut Ui,
|
||||
|
||||
@@ -416,6 +416,11 @@ impl EnvCapture {
|
||||
}
|
||||
|
||||
impl Env {
|
||||
/// Whether both views come from the same immutable scope. A derived scope has a different source.
|
||||
pub(crate) fn same_source(&self, other: &Env) -> bool {
|
||||
Arc::ptr_eq(&self.scoped, &other.scoped)
|
||||
}
|
||||
|
||||
/// The word set `L` in the scope's language.
|
||||
pub fn words<L: WordSet + Send + Sync + 'static>(&self) -> Arc<L> {
|
||||
self.words.set::<L>()
|
||||
|
||||
@@ -214,6 +214,35 @@ pub fn body_runs(ctx: &Context) -> Vec<(Id, u32)> {
|
||||
store::with::<BodyRuns, _>(ctx, |log| log.runs.clone())
|
||||
}
|
||||
|
||||
/// Adjacent leaf measures whose environment and style steps were reused in the current pass.
|
||||
#[cfg(feature = "testing")]
|
||||
#[derive(Default)]
|
||||
struct PreparedLeafCounts {
|
||||
pass: u64,
|
||||
count: u32,
|
||||
}
|
||||
|
||||
#[cfg(feature = "testing")]
|
||||
pub(crate) fn count_prepared_leaf(ctx: &Context) {
|
||||
if active(ctx) {
|
||||
return;
|
||||
}
|
||||
let pass = ctx.cumulative_pass_nr();
|
||||
store::with::<PreparedLeafCounts, _>(ctx, |counts| {
|
||||
if counts.pass != pass {
|
||||
*counts = PreparedLeafCounts { pass, count: 0 };
|
||||
}
|
||||
counts.count += 1;
|
||||
});
|
||||
}
|
||||
|
||||
/// Adjacent leaf measures whose steps were reused in the last pass.
|
||||
#[cfg(feature = "testing")]
|
||||
pub fn last_pass_prepared_leaves(ctx: &Context) -> u32 {
|
||||
let last = ctx.cumulative_pass_nr().saturating_sub(1);
|
||||
store::with::<PreparedLeafCounts, _>(ctx, |counts| if counts.pass == last { counts.count } else { 0 })
|
||||
}
|
||||
|
||||
/// Counts a run of a component of the type `name` in this pass, measuring or real. Counts nothing without `testing`.
|
||||
pub(crate) fn count_run(ctx: &Context, name: &'static str) {
|
||||
#[cfg(feature = "testing")]
|
||||
|
||||
@@ -42,7 +42,7 @@ pub use crate::contract::{Exemption, PatternKind, PatternProbe, Probe, Registrat
|
||||
pub use crate::runtime::layout_memory::forget_kept as forget_measurements;
|
||||
pub use crate::runtime::measure::{
|
||||
active as is_measuring, body_runs as measuring_runs, contributions as measured_contributions, last_pass_body_runs as last_pass_measuring_runs,
|
||||
last_pass_runs, least_runs as shrink_least_runs, row_measures, run as measuring_run, take_effects, widest_runs,
|
||||
last_pass_prepared_leaves, last_pass_runs, least_runs as shrink_least_runs, row_measures, run as measuring_run, take_effects, widest_runs,
|
||||
};
|
||||
pub use craft_ui_macros::{story, trial};
|
||||
pub use fixture::{Fixture, Impure, Room, Target};
|
||||
|
||||
@@ -5,5 +5,6 @@ mod gesture;
|
||||
mod hit_order;
|
||||
mod pages;
|
||||
mod pointer;
|
||||
mod prepared;
|
||||
mod style;
|
||||
mod visited_type;
|
||||
@@ -0,0 +1,99 @@
|
||||
//! Adjacent measurement and show reuse only unchanged environment and style inputs.
|
||||
|
||||
use craft_behavior::{Proposal, Sense};
|
||||
use craft_ui::contract::{Common, Component, Cx, MeasureCx, PaintUi, Props as _, Role, Show as _, SideLabel};
|
||||
use craft_ui::style::{ComponentStyle as _, StyleCx};
|
||||
use craft_ui::testing::{Fixture, last_pass_prepared_leaves, trial};
|
||||
use craft_ui_test_kit::contract::PlainStyle;
|
||||
use egui::{Response, Vec2};
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Component)]
|
||||
struct Child {
|
||||
#[common]
|
||||
common: Common,
|
||||
#[style_key]
|
||||
changed: bool,
|
||||
mutate: bool,
|
||||
seen: Rc<Cell<f32>>,
|
||||
}
|
||||
|
||||
impl Component for Child {
|
||||
const ROLE: Role = Role::GROUP;
|
||||
type Style = PlainStyle;
|
||||
type Output = Response;
|
||||
|
||||
fn style(_: &StyleCx<'_>, key: ChildStyleKey) -> PlainStyle {
|
||||
PlainStyle::min(Vec2::new(if key.changed { 40.0 } else { 20.0 }, 20.0))
|
||||
}
|
||||
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
if self.mutate && cx.proposal == Proposal::Ideal {
|
||||
self.changed = true;
|
||||
}
|
||||
Vec2::new(10.0, 20.0)
|
||||
}
|
||||
|
||||
fn sense(&self) -> Sense {
|
||||
Sense::empty()
|
||||
}
|
||||
|
||||
fn paint(&mut self, _: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
|
||||
self.seen.set(cx.style.metrics().min_size.x);
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> Response {
|
||||
cx.response.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct Owner {
|
||||
#[common]
|
||||
common: Common,
|
||||
mutate: bool,
|
||||
seen: Rc<Cell<f32>>,
|
||||
}
|
||||
|
||||
impl Component for Owner {
|
||||
const ROLE: Role = Role::GROUP;
|
||||
type Style = PlainStyle;
|
||||
type Output = Response;
|
||||
|
||||
fn style(_: &StyleCx<'_>, _: OwnerStyleKey) -> PlainStyle {
|
||||
PlainStyle::min(Vec2::new(200.0, 50.0))
|
||||
}
|
||||
fn sense(&self) -> Sense {
|
||||
Sense::empty()
|
||||
}
|
||||
|
||||
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
|
||||
let child = Child { common: Common::new(), changed: false, mutate: self.mutate, seen: self.seen.clone() };
|
||||
ui.labelled("child", cx.rect, "Caption".into(), SideLabel::new(), child);
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> Response {
|
||||
cx.response.clone()
|
||||
}
|
||||
}
|
||||
|
||||
fn show(mutate: bool) -> (u32, f32) {
|
||||
let seen = Rc::new(Cell::new(0.0));
|
||||
let output = seen.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
Owner { common: Common::new(), mutate, seen: output.clone() }.id_salt("owner").show(ui);
|
||||
});
|
||||
fixture.frame(Vec::new());
|
||||
(last_pass_prepared_leaves(fixture.ctx()), seen.get())
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn adjacent_leaf_reuses_steps_only_while_its_style_key_is_unchanged() {
|
||||
let (reused, unchanged) = show(false);
|
||||
assert!(reused > 0, "unchanged child reuses its adjacent measure steps");
|
||||
assert_eq!(unchanged, 20.0);
|
||||
let (reused, changed) = show(true);
|
||||
assert_eq!(reused, 0, "a changed style key uses the ordinary path");
|
||||
assert_eq!(changed, 40.0, "the shown child receives its changed style");
|
||||
}
|
||||
Reference in new issue
Block a user