mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
A memoized view's key is its content's version: bodies inside measure afresh in a run with a new key (FW-365)
A container's kept transcript keyed on its props, room and env only, so a view that ran with new props (a shell slot's new version) took its bodies' old measures, found the change only in the real run and moved a content slot in the next frame. The view's key, when it follows every input, now reaches the measure key (Scoped::view_version, EnvFacts::view); a key fed through changes_every_pass gives none (MemoKey::follows). A toolbar resize runs its view once, in one pass.
This commit is contained in:
1 parent
6e11c7efd6
commit
7f4311f96f
7 files changed
+60
-16
No files matched your search
@@ -15,8 +15,9 @@ use craft_behavior::layout_memory::measure_tolerance;
|
||||
use egui::{Context, Id, Rect, Ui, Vec2};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// The inputs a body's measuring run measures under. A later pass offering the same takes the body's kept
|
||||
/// transcript (`layout_memory::kept_serves`); its real run checks that the content is the same.
|
||||
/// The inputs a body's measuring run measures under, the content version of the memoized view it runs in among them
|
||||
/// (`EnvFacts::view`). A later pass offering the same takes the body's kept transcript (`layout_memory::kept_serves`);
|
||||
/// its real run checks that the content is the same.
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub(super) struct MeasureKey {
|
||||
props: MeasuredProps,
|
||||
@@ -63,6 +64,8 @@ pub(crate) struct EnvFacts {
|
||||
measure: Option<f32>,
|
||||
grid: Option<Id>,
|
||||
egui_cell: bool,
|
||||
/// The content version of the memoized view the body runs in (`Scoped::view_version`).
|
||||
view: Option<u128>,
|
||||
}
|
||||
|
||||
impl EnvFacts {
|
||||
@@ -84,6 +87,7 @@ impl EnvFacts {
|
||||
measure: env.measure,
|
||||
grid: env.grid,
|
||||
egui_cell: env.egui_cell,
|
||||
view: env.view_version,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -376,6 +376,8 @@ pub struct MemoKey {
|
||||
/// The context of the view that takes the key: framework values whose writes the context counts (kept and saved
|
||||
/// bindings) feed that count.
|
||||
context: Option<egui::Context>,
|
||||
/// An input the key cannot follow was fed (`changes_every_pass`).
|
||||
every_pass: bool,
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for MemoKey {
|
||||
@@ -419,9 +421,20 @@ impl MemoKey {
|
||||
if let Some(ctx) = &self.context {
|
||||
let pass = ctx.cumulative_pass_nr();
|
||||
self.write(&pass.to_le_bytes());
|
||||
self.every_pass = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the key follows every input fed into it: none was fed through `changes_every_pass`.
|
||||
pub(crate) const fn follows(&self) -> bool {
|
||||
!self.every_pass
|
||||
}
|
||||
|
||||
/// Takes the inputs `child` (from `child`) fed that the key cannot follow, once its hash is fed.
|
||||
pub(crate) fn absorb(&mut self, child: &MemoKey) {
|
||||
self.every_pass |= child.every_pass;
|
||||
}
|
||||
|
||||
/// Feeds `bytes`.
|
||||
pub fn write(&mut self, bytes: &[u8]) {
|
||||
self.hasher.write(bytes);
|
||||
|
||||
@@ -59,6 +59,11 @@ use std::sync::Arc;
|
||||
/// - the clock, read through `frame::stepped_clock`: the view runs at the next step;
|
||||
/// - commands sent to a command scope instance inside it: it runs while one waits.
|
||||
///
|
||||
/// Its key is also its content's version: a container inside measures under it, so a run with a new key measures its
|
||||
/// bodies afresh, in the first pass, instead of taking what they measured under the last key. A run with the same key
|
||||
/// (input reached it) takes those measures. A key that holds an input it cannot follow (`changes_every_pass`) gives
|
||||
/// no version: the containers inside measure as outside any view.
|
||||
///
|
||||
/// Its content is a layout room of its own, as a pane's is: the flow of the body around it (a column's sweep, a grid's
|
||||
/// cursor) does not reach in, so the room it is given reaches it only through its place. To the body
|
||||
/// around it the view is one unit, sized by its content's measure. In a shrinking `Row` it is a unit the row narrows
|
||||
@@ -133,13 +138,20 @@ impl<P: MemoProps, R: Clone + PartialEq + Shift + Send + Sync + 'static> MemoVie
|
||||
(env, None)
|
||||
};
|
||||
let id = lazy.id(ui, &env);
|
||||
let key = Self::key(&ctx, &env, &props, show);
|
||||
let (key, follows) = Self::key(&ctx, &env, &props, show);
|
||||
let shown = lazy.run_keyed((ui, env.clone()), std::any::type_name::<P>(), crate::runtime::lazy::Key::Memo(key), |ui, run| {
|
||||
// The content is a layout room of its own: the flow of the body around it does not reach in. It keeps how
|
||||
// that body sizes from its content, whether its leaves fit their room and which axis it shares among the
|
||||
// children that fill it (`Scoped::start_view_room`), so the view takes what its content used there, as the
|
||||
// same content shown without the view would.
|
||||
let installed = Env::install_for(ui, env.derive_local(|scoped| scoped.start_view_room(id)));
|
||||
// Its key is its content's version: a body inside measures under it (`Scoped::view_version`).
|
||||
let installed = Env::install_for(
|
||||
ui,
|
||||
env.derive_local(|scoped| {
|
||||
scoped.start_view_room(id);
|
||||
scoped.view_version = follows.then_some(key);
|
||||
}),
|
||||
);
|
||||
let out = show(&mut props, ui);
|
||||
Env::restore(&ctx, installed);
|
||||
run.output = Some(Arc::new(out.clone()));
|
||||
@@ -164,16 +176,18 @@ impl<P: MemoProps, R: Clone + PartialEq + Shift + Send + Sync + 'static> MemoVie
|
||||
|
||||
/// The key of `props` shown through `show` in the scope `env`: the props type, the `fn`'s address, the props' inputs
|
||||
/// and what the scope hands down beyond its style inputs (`Scoped::scope_key`). Two `fn`s the compiler merged into
|
||||
/// one address differ in their props type or show the same.
|
||||
fn key(ctx: &egui::Context, env: &Env, props: &P, show: fn(&mut P, &mut Ui) -> R) -> u128 {
|
||||
/// one address differ in their props type or show the same. The flag says whether the key follows every input it
|
||||
/// holds (`MemoKey::follows`).
|
||||
fn key(ctx: &egui::Context, env: &Env, props: &P, show: fn(&mut P, &mut Ui) -> R) -> (u128, bool) {
|
||||
let mut key = MemoKey::of_view(ctx);
|
||||
key.feed_hash(&P::identity());
|
||||
key.write(&(show as usize).to_le_bytes());
|
||||
key.write(&env.scope_key().to_le_bytes());
|
||||
let (scope, scope_follows) = env.scope_key();
|
||||
key.write(&scope.to_le_bytes());
|
||||
props.key(&mut key);
|
||||
#[cfg(feature = "testing")]
|
||||
crate::runtime::measure::count_memo_key_bytes(ctx, key.bytes());
|
||||
key.finish()
|
||||
(key.finish(), scope_follows && key.follows())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -112,7 +112,11 @@ pub struct Scoped {
|
||||
#[doc(hidden)]
|
||||
pub key: EnvKey,
|
||||
/// The key of what a memoized view shown in the scope inherits beyond `key` (`scope_key`), once a view asked.
|
||||
scope_key: std::sync::OnceLock<u128>,
|
||||
scope_key: std::sync::OnceLock<(u128, bool)>,
|
||||
/// The key of the memoized view whose content the scope runs in, when that key follows every input of the view
|
||||
/// (`MemoKey::follows`): its content version. A body inside measures under it (`EnvFacts`), so a run of the view
|
||||
/// with other inputs measures afresh instead of taking a transcript its last inputs left.
|
||||
pub(crate) view_version: Option<u128>,
|
||||
/// Ambient control size; the root default is `Md`.
|
||||
pub size: ControlSize,
|
||||
/// How the icon-only buttons of the scope show their glyph; the root default and a layer's is `Control`.
|
||||
@@ -566,8 +570,9 @@ impl Scoped {
|
||||
/// the flow facts, which its place compares): the scope's state (enabled, read-only, size, glyphs, ink, surface,
|
||||
/// wheel, directions), the containers around it (arena, roving group, transform, collection item and row, inside
|
||||
/// label, footer slot, line length, scroll owner), the command chain, motion, the host's formats and date, and every
|
||||
/// app value. Computed once per scope, on the first view that asks; the bytes it fed are counted then.
|
||||
pub(crate) fn scope_key(&self) -> u128 {
|
||||
/// app value. Computed once per scope, on the first view that asks; the bytes it fed are counted then. The flag says
|
||||
/// whether the key follows every input it holds (`MemoKey::follows`).
|
||||
pub(crate) fn scope_key(&self) -> (u128, bool) {
|
||||
*self.scope_key.get_or_init(|| {
|
||||
use crate::contract::{MemoKey, MemoProps as _};
|
||||
let mut key = MemoKey::of_view(&self.ctx);
|
||||
@@ -597,7 +602,7 @@ impl Scoped {
|
||||
crate::runtime::measure::count_memo_key_bytes(&self.ctx, key.bytes() + value_bytes);
|
||||
#[cfg(not(feature = "testing"))]
|
||||
let _ = value_bytes;
|
||||
key.finish()
|
||||
(key.finish(), key.follows())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -728,6 +733,7 @@ impl Env {
|
||||
}),
|
||||
key,
|
||||
scope_key: std::sync::OnceLock::new(),
|
||||
view_version: None,
|
||||
ink: Ink::default(),
|
||||
surface: SurfaceKind::Background,
|
||||
size: ControlSize::Md,
|
||||
|
||||
@@ -102,6 +102,7 @@ impl AppValues {
|
||||
keyer(&**value, &mut one);
|
||||
sum = sum.wrapping_add(one.finish());
|
||||
bytes += one.bytes();
|
||||
key.absorb(&one);
|
||||
}
|
||||
key.write(&sum.to_le_bytes());
|
||||
bytes
|
||||
|
||||
@@ -214,7 +214,7 @@ fn a_slot_sized_by_its_content_grows_to_a_child_that_fills_its_height() {
|
||||
|
||||
/// A settled frame runs a content slot's content once, in one pass: its measure is reused while its inputs are
|
||||
/// unchanged. A frame where the content grows (a roomier density makes every button taller) lays the window out again
|
||||
/// at most once, shows the new extent in that frame and leaves the next frame settled.
|
||||
/// at most once, shows the new extent with the body under it in that frame and leaves the next frame settled.
|
||||
#[trial]
|
||||
fn a_content_slot_runs_its_content_once_per_settled_frame_and_grows_in_one_frame() {
|
||||
let runs = Rc::new(Cell::new(0_u32));
|
||||
@@ -244,9 +244,11 @@ fn a_content_slot_runs_its_content_once_per_settled_frame_and_grows_in_one_frame
|
||||
fixture.density(Size::Comfortable).frame(Vec::new());
|
||||
assert!(fixture.passes() <= 2, "the frame the content grows in lays the window out again at most once: {} passes", fixture.passes());
|
||||
let slot = regions.borrow().as_ref().and_then(|regions| regions.slots.first().map(|(_, rect)| *rect)).expect("the slot shows");
|
||||
let body = regions.borrow().as_ref().map(|regions| regions.body).expect("the body shows");
|
||||
let grown = seen.borrow().0;
|
||||
assert!(grown.height() > before.height() + 1.0, "the roomier density makes the column taller: {before:?} to {grown:?}");
|
||||
assert!(slot.expand(1.0).contains_rect(grown), "in the frame it grows in, the slot {slot:?} holds the grown column {grown:?}");
|
||||
assert!((body.top() - slot.bottom()).abs() <= 1.0, "in the frame it grows in, the body {body:?} starts under the grown slot {slot:?}");
|
||||
runs.set(0);
|
||||
fixture.frame(Vec::new());
|
||||
assert_eq!(fixture.passes(), 1, "the frame after the grow is settled: one pass");
|
||||
@@ -624,6 +626,8 @@ fn a_versioned_content_slot_keeps_its_extent_while_it_replays() {
|
||||
let (ran, grown) = frame(&mut frames);
|
||||
assert!(ran.contains(&"ribbon"), "a new version runs the slot");
|
||||
assert!(grown.top() > settled.top(), "the grown slot pushes the body down in the frame it grew: {grown:?} after {settled:?}");
|
||||
let (_, next) = frame(&mut frames);
|
||||
assert_eq!(next, grown, "the frame after the grow keeps the body where the grow frame put it");
|
||||
}
|
||||
|
||||
/// A versioned slot that registers a revealed rect registers it again when it replays: the shell reads the same rects
|
||||
|
||||
@@ -170,13 +170,15 @@ fn a_tool_change_runs_only_the_toolbar() {
|
||||
assert_eq!(ran(&fixture, "ToolbarProps"), 0, "then it replays");
|
||||
}
|
||||
|
||||
/// A change that resizes the toolbar asks for a discard pass, and the view that asked runs again in it: once per pass.
|
||||
/// A change that resizes the toolbar runs its view once: the run's new props are its content's version, so the bar
|
||||
/// inside measures afresh and takes its new size in that pass, with no discard pass.
|
||||
#[trial]
|
||||
fn a_change_that_resizes_the_toolbar_runs_it_in_the_discard_pass_too() {
|
||||
fn a_change_that_resizes_the_toolbar_runs_it_once() {
|
||||
let (mut fixture, state) = scene();
|
||||
state.borrow_mut().width = "a much longer caption".to_owned();
|
||||
fixture.frame(vec![]);
|
||||
assert_eq!((ran(&fixture, "ToolbarProps"), ran(&fixture, "StatusProps")), (2, 0), "{:?}", last_frame_memo_runs(fixture.ctx()));
|
||||
assert_eq!((ran(&fixture, "ToolbarProps"), ran(&fixture, "StatusProps")), (1, 0), "{:?}", last_frame_memo_runs(fixture.ctx()));
|
||||
assert_eq!(fixture.passes(), 1, "the frame runs one pass");
|
||||
fixture.frame(vec![]);
|
||||
assert_eq!(ran(&fixture, "ToolbarProps"), 0, "then it replays");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user