From 51c7fc31177a2a166beafa66e57a0e369370da17 Mon Sep 17 00:00:00 2001 From: aiongg Date: Wed, 11 Nov 2026 16:50:22 +0000 Subject: [PATCH] A component's shown copy, last drawn pass, last state and state tweens are one shown slot --- crates/ui-contract/src/contract/run.rs | 4 +- crates/ui-contract/src/contract/steps.rs | 4 +- .../ui-contract/src/contract/steps/state.rs | 23 ++++---- .../ui-contract/src/contract/steps/value.rs | 32 ++++++----- crates/ui-contract/src/runtime/motion.rs | 53 ++++++------------- 5 files changed, 47 insertions(+), 69 deletions(-) diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index 5e0675392..0ba16e466 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -641,7 +641,7 @@ fn run_in_host(p: &mut Pass<'_, '_, C>, ui: &mut Ui, host_at: Opti // A component that declares no parts skips the step; a debug build runs it anyway to check the declaration. let declares_parts = C::PARTS || p.hooks.declares_parts(); if !inert && (declares_parts || cfg!(debug_assertions)) { - let last = *cx.core.memory.shown::(sub(id, "last_state")); + let last = cx.core.memory.shown::>>(id).last_state; let input = MeasureIn { ui: &*ui, env, proposal: Proposal::Ideal, room: p.room }; let measure = steps::measure_cx::(&input, p.style, resolved, cx.shown.as_deref()); let mut pcx = PartsCx { @@ -651,7 +651,7 @@ fn run_in_host(p: &mut Pass<'_, '_, C>, ui: &mut Ui, host_at: Opti rovings: &mut parts.rovings, scrolls: &mut parts.scrolls, text: &mut parts.text, - last_state: (last.0, last.1), + last_state: last, measure, items: p.hooks.geometry(comp, flow.inset(metrics.padding), rtl), }; diff --git a/crates/ui-contract/src/contract/steps.rs b/crates/ui-contract/src/contract/steps.rs index 774b3d069..e27b41c8a 100644 --- a/crates/ui-contract/src/contract/steps.rs +++ b/crates/ui-contract/src/contract/steps.rs @@ -39,11 +39,11 @@ pub(crate) use scroll_owner::queue_reveal; pub(crate) use scroll_owner::{peek_reveal, peek_scroll_moves, take_reveal, take_scroll_moves}; pub(crate) use sense::{part_state, register_wheel}; pub(crate) use settle::settle; -pub(crate) use state::{LastState, register_reset, state}; +pub(crate) use state::{register_reset, state}; pub(crate) use stops::{StopsIn, body_group, join_group, member_group, register_stops, takes_focus}; pub(crate) use style::{style, style_cx}; pub(crate) use update::{after_update, edit_intents, gesture_gate, key_activation, record_context, takes_activation, update_inputs}; -pub(crate) use value::{peek_value, value}; +pub(crate) use value::{Shown, peek_value, value}; /// The 17 steps of the pipeline, in order. #[derive(Clone, Copy, Debug, PartialEq, Eq)] diff --git a/crates/ui-contract/src/contract/steps/state.rs b/crates/ui-contract/src/contract/steps/state.rs index 19ebbbcf0..90c04fc9a 100644 --- a/crates/ui-contract/src/contract/steps/state.rs +++ b/crates/ui-contract/src/contract/steps/state.rs @@ -1,10 +1,10 @@ //! Step 12, state: flags, eased state and the reset a control registers with its label. use super::paint::surface_ink; +use super::value::Shown; use crate::contract::cx::CxCore; -use crate::contract::{Component, Cx, Validity}; +use crate::contract::{Component, Cx, Validity, ValueOf}; use crate::runtime::access::scope; -use crate::runtime::memory::sub; use crate::runtime::motion::{Drives as _, spec}; use crate::style::{ComponentStyle, Flags, Ink, StateSet}; use craft_behavior::Input; @@ -20,18 +20,16 @@ pub(crate) fn register_reset(comp: &mut C, cx: &Cx<'_, C>) { scope::register_reset(&cx.core.ctx, cx.core.id, differs); } -/// The `(hovered, focus_visible)` a component showed last pass, which `PartsCx::hovered` and `focus_visible` read. -#[derive(Clone, Copy, Default)] -pub(crate) struct LastState(pub bool, pub bool); - -/// The eased hover, press and on values of a component; a face shows its targets. -fn ease(core: &CxCore<'_>, targets: [bool; 3]) -> StateMix { +/// The eased hover, press and on values of a component, kept in its `Shown` record; a face shows its targets. +fn ease(core: &mut CxCore<'_>, targets: [bool; 3]) -> StateMix { let [hover, press, on] = targets; if core.face_mode { return StateMix::new(f32::from(u8::from(hover)), f32::from(u8::from(press)), f32::from(u8::from(on))); } - let driver = core.driver(); - let [hover, press, on] = driver.state_anims([hover, press, on], [spec::HOVER, spec::HOVER, spec::CHECK]); + let id = core.id; + let mut tweens = core.memory.shown::>>(id).tweens.take(); + let [hover, press, on] = core.driver().state_anims(&mut tweens, [hover, press, on], [spec::HOVER, spec::HOVER, spec::CHECK]); + core.memory.shown::>>(id).tweens = tweens; StateMix::new(hover, press, on) } @@ -79,8 +77,7 @@ pub(crate) fn state(comp: &C, cx: &mut Cx<'_, C>, open: bool, expa if visible { flags |= Flags::FOCUS_VISIBLE; } - let id = core.id; - *core.memory.shown::(sub(id, "last_state")) = LastState(hovered, visible); + core.memory.shown::>>(core.id).last_state = (hovered, visible); let state = if !core.resolved.enabled { State::Disabled } else if pressed || flags.contains(Flags::OPEN) { @@ -90,7 +87,7 @@ pub(crate) fn state(comp: &C, cx: &mut Cx<'_, C>, open: bool, expa } else { State::Rest }; - let t = ease(core, [hovered, pressed, flags.intersects(Flags::ON)]); + let t = ease::(core, [hovered, pressed, flags.intersects(Flags::ON)]); core.state = StateSet::new(state, flags, t); core.ink = surface_ink(cx.style.surface(), &core.state, inherited); core.stated = true; diff --git a/crates/ui-contract/src/contract/steps/value.rs b/crates/ui-contract/src/contract/steps/value.rs index 0db9deee9..7283bd95d 100644 --- a/crates/ui-contract/src/contract/steps/value.rs +++ b/crates/ui-contract/src/contract/steps/value.rs @@ -5,22 +5,30 @@ use crate::contract::values::reuse_arc; use crate::contract::{Component, ValueOf}; use crate::runtime::commands::{CommandKind, ResolvedCommand}; use crate::runtime::memory::MemoryStage; +use crate::runtime::motion::StateTweens; use craft_behavior::{CancelReason, EditEvent, Gesture, Mixed, Value}; use egui::{Context, Id}; use std::sync::Arc; -/// The shown copy of a component's value, in shown memory. -pub(crate) struct ShownCopy(pub Option>); +/// What a component showed last pass, in one shown slot under its id: the shown copy of its value, the pass its value +/// step last ran in, the `(hovered, focus_visible)` that `PartsCx::hovered` and `focus_visible` read, and its eased +/// hover, press and on. +pub(crate) struct Shown { + pub copy: Option>, + pub drawn: u64, + pub last_state: (bool, bool), + pub tweens: Option, +} -impl Clone for ShownCopy { +impl Clone for Shown { fn clone(&self) -> Self { - ShownCopy(self.0.clone()) + Shown { copy: self.copy.clone(), drawn: self.drawn, last_state: self.last_state, tweens: self.tweens.clone() } } } -impl Default for ShownCopy { +impl Default for Shown { fn default() -> Self { - ShownCopy(None) + Shown { copy: None, drawn: 0, last_state: (false, false), tweens: None } } } @@ -47,23 +55,19 @@ fn command_given(command: Option<&ResolvedCommand>) -> Option { (&checked as &dyn std::any::Any).downcast_ref::().cloned() } -/// The pass a component's value step last ran in. -#[derive(Clone, Default)] -struct LastDrawn(u64); - /// Step 4, value: the shown value is the running gesture's proposal, else the binding's value. The one shown copy is /// replaced only when the binding differs from it. A gesture ends here when `cancel_all` ran, or when the app changed the /// value under it. pub(crate) fn value(comp: &mut C, stage: &mut MemoryStage, id: Id, command: Option<&ResolvedCommand>) -> ValueStep> { type V = ValueOf; - let held = stage.shown::>>(id).0.clone(); + let held = stage.shown::>>(id).copy.clone(); let slot = gesture_key(id); let (serial, all_reason) = crate::runtime::edit::latest(stage.ctx()); let mut cancelled = None; // A component not drawn for a pass loses its press at the end hook; its running gesture ends here, the first pass it is // drawn again. let pass_nr = crate::runtime::pointer::pass_of(stage.ctx()); - let removed = std::mem::replace(&mut stage.shown::(slot).0, pass_nr).saturating_add(1) < pass_nr; + let removed = std::mem::replace(&mut stage.shown::>>(id).drawn, pass_nr).saturating_add(1) < pass_nr; if stage.shown::>>(slot).is_running() { // `(given, value)` of a single-valued binding the app owns. let host = comp.bind().and_then(|bind| match bind.get() { @@ -119,7 +123,7 @@ pub(crate) fn value(comp: &mut C, stage: &mut MemoryStage, id: Id, } (None, None) => out.value = command_given::>(command).map(|value| reuse_arc(&held, &value)), } - let shown = &mut stage.shown::>>(id).0; + let shown = &mut stage.shown::>>(id).copy; let same = match (&*shown, &out.value) { (Some(a), Some(b)) => Arc::ptr_eq(a, b), (None, None) => true, @@ -135,7 +139,7 @@ pub(crate) fn value(comp: &mut C, stage: &mut MemoryStage, id: Id, /// shown memory. /// Returns the value and whether the binding is mixed. pub(crate) fn peek_value(comp: &mut C, ctx: &Context, id: Id, command: Option<&ResolvedCommand>) -> (Option>>, bool) { - let held = MemoryStage::peek::>>(ctx, id).0; + let held = MemoryStage::peek::>>(ctx, id).copy; let Some(bind) = comp.bind() else { let given = command_given::>(command).map(|value| reuse_arc(&held, &value)); return (given.or(held), false); diff --git a/crates/ui-contract/src/runtime/motion.rs b/crates/ui-contract/src/runtime/motion.rs index 6e9b12f1c..19697ac2a 100644 --- a/crates/ui-contract/src/runtime/motion.rs +++ b/crates/ui-contract/src/runtime/motion.rs @@ -261,7 +261,7 @@ pub(crate) fn layer_presence(ctx: &Context, base: Id, owner: Id, policy: MotionP /// The hover, press and on tweens of one component. #[derive(Clone)] -struct StateTweens([Tween; 3]); +pub(crate) struct StateTweens([Tween; 3]); /// Everything an `Animate` call needs from the context type that makes it. pub(crate) struct Driver<'a> { @@ -270,43 +270,23 @@ pub(crate) struct Driver<'a> { pub(crate) policy: MotionPolicy, } -/// Whose animation a slot is: a component's own `anim` names, or the pipeline's state mix. The lanes never share -/// a slot, whatever the names. -#[derive(Clone, Copy, Debug)] -enum Lane { - Author, - Pipeline, -} - impl Driver<'_> { - fn slot(&self, lane: Lane, name: &'static str) -> Id { - match lane { - Lane::Author => memory::sub(self.base, ("anim", name)), - Lane::Pipeline => memory::sub(self.base, ("state-anim", name)), - } + /// The slot of a component's own `anim` name. + fn slot(&self, name: &'static str) -> Id { + memory::sub(self.base, ("anim", name)) } pub(crate) fn anim(&self, name: &'static str, target: T, spec: MotionSpec) -> T { - self.run(Lane::Author, name, target, spec) + self.run(name, target, spec) } - /// The pipeline's state values (`hover`, `press`, `on`) in one slot of the pipeline lane: one clock read and one - /// context round trip for the three tweens. - pub(crate) fn state_anims(&self, targets: [bool; 3], specs: [MotionSpec; 3]) -> [f32; 3] { + /// The pipeline's state values (`hover`, `press`, `on`), stepped in `tweens`, which the component's `Shown` + /// record keeps; `None` the first pass, which starts at the targets. A sizing pass's stage keeps nothing, so it + /// steps a copy. + pub(crate) fn state_anims(&self, tweens: &mut Option, targets: [bool; 3], specs: [MotionSpec; 3]) -> [f32; 3] { let ctx = self.stage.ctx(); - let id = self.slot(Lane::Pipeline, "state"); - let sizing = self.stage.is_peek(); let now = frame::clock(ctx); - let existing = if sizing { store::peek_clone::(ctx, id) } else { store::take::(ctx, id) }; - let mut tweens = match existing { - Some(tweens) => tweens, - None => { - if !sizing { - memory::adopt::(ctx, self.stage.owner(), id); - } - StateTweens(targets.map(|on| Tween::at(f32::from(u8::from(on))))) - } - }; + let tweens = tweens.get_or_insert_with(|| StateTweens(targets.map(|on| Tween::at(f32::from(u8::from(on)))))); let mut shown = [0.0; 3]; for (at, tween) in tweens.0.iter_mut().enumerate() { let target = f32::from(u8::from(targets[at])); @@ -320,20 +300,17 @@ impl Driver<'_> { } shown[at] = tween.shown(); } - if !sizing { - if tweens.0.iter().any(|tween| !tween.is_settled()) { - note_unsettled(ctx); - } - store::put(ctx, id, tweens); + if !self.stage.is_peek() && tweens.0.iter().any(|tween| !tween.is_settled()) { + note_unsettled(ctx); } shown } /// A sizing pass evaluates a copy and stores nothing: it must not retarget a running tween or adopt a slot /// for an owner that registers no pass. - fn run(&self, lane: Lane, name: &'static str, target: T, spec: MotionSpec) -> T { + fn run(&self, name: &'static str, target: T, spec: MotionSpec) -> T { let ctx = self.stage.ctx(); - let id = self.slot(lane, name); + let id = self.slot(name); let sizing = self.stage.is_peek(); let now = frame::clock(ctx); let existing = if sizing { store::peek_clone::>(ctx, id) } else { store::take::>(ctx, id) }; @@ -379,7 +356,7 @@ impl Driver<'_> { return; } let ctx = self.stage.ctx(); - let id = self.slot(Lane::Author, name); + let id = self.slot(name); if store::peek_clone::>(ctx, id).is_none() { memory::adopt::>(ctx, self.stage.owner(), id); }