A component's shown copy, last drawn pass, last state and state tweens are one shown slot

This commit is contained in:
aiongg
2026-11-12 14:32:39 +00:00
parent 5835939df3
commit 51c7fc3117
5 changed files with 47 additions and 69 deletions
+2 -2
View File
@@ -641,7 +641,7 @@ fn run_in_host<C: Component>(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::<steps::LastState>(sub(id, "last_state"));
let last = cx.core.memory.shown::<steps::Shown<ValueOf<C>>>(id).last_state;
let input = MeasureIn { ui: &*ui, env, proposal: Proposal::Ideal, room: p.room };
let measure = steps::measure_cx::<C>(&input, p.style, resolved, cx.shown.as_deref());
let mut pcx = PartsCx {
@@ -651,7 +651,7 @@ fn run_in_host<C: Component>(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),
};
+2 -2
View File
@@ -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)]
+10 -13
View File
@@ -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<C: Component>(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<C: Component>(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::<Shown<ValueOf<C>>>(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::<Shown<ValueOf<C>>>(id).tweens = tweens;
StateMix::new(hover, press, on)
}
@@ -79,8 +77,7 @@ pub(crate) fn state<C: Component>(comp: &C, cx: &mut Cx<'_, C>, open: bool, expa
if visible {
flags |= Flags::FOCUS_VISIBLE;
}
let id = core.id;
*core.memory.shown::<LastState>(sub(id, "last_state")) = LastState(hovered, visible);
core.memory.shown::<Shown<ValueOf<C>>>(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<C: Component>(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::<C>(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;
+18 -14
View File
@@ -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<V>(pub Option<Arc<V>>);
/// 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<V> {
pub copy: Option<Arc<V>>,
pub drawn: u64,
pub last_state: (bool, bool),
pub tweens: Option<StateTweens>,
}
impl<V> Clone for ShownCopy<V> {
impl<V> Clone for Shown<V> {
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<V> Default for ShownCopy<V> {
impl<V> Default for Shown<V> {
fn default() -> Self {
ShownCopy(None)
Shown { copy: None, drawn: 0, last_state: (false, false), tweens: None }
}
}
@@ -47,23 +55,19 @@ fn command_given<V: Value>(command: Option<&ResolvedCommand>) -> Option<V> {
(&checked as &dyn std::any::Any).downcast_ref::<V>().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<C: Component>(comp: &mut C, stage: &mut MemoryStage, id: Id, command: Option<&ResolvedCommand>) -> ValueStep<ValueOf<C>> {
type V<C> = ValueOf<C>;
let held = stage.shown::<ShownCopy<V<C>>>(id).0.clone();
let held = stage.shown::<Shown<V<C>>>(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::<LastDrawn>(slot).0, pass_nr).saturating_add(1) < pass_nr;
let removed = std::mem::replace(&mut stage.shown::<Shown<V<C>>>(id).drawn, pass_nr).saturating_add(1) < pass_nr;
if stage.shown::<Gesture<V<C>>>(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<C: Component>(comp: &mut C, stage: &mut MemoryStage, id: Id,
}
(None, None) => out.value = command_given::<V<C>>(command).map(|value| reuse_arc(&held, &value)),
}
let shown = &mut stage.shown::<ShownCopy<V<C>>>(id).0;
let shown = &mut stage.shown::<Shown<V<C>>>(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<C: Component>(comp: &mut C, stage: &mut MemoryStage, id: Id,
/// shown memory.
/// Returns the value and whether the binding is mixed.
pub(crate) fn peek_value<C: Component>(comp: &mut C, ctx: &Context, id: Id, command: Option<&ResolvedCommand>) -> (Option<Arc<ValueOf<C>>>, bool) {
let held = MemoryStage::peek::<ShownCopy<ValueOf<C>>>(ctx, id).0;
let held = MemoryStage::peek::<Shown<ValueOf<C>>>(ctx, id).copy;
let Some(bind) = comp.bind() else {
let given = command_given::<ValueOf<C>>(command).map(|value| reuse_arc(&held, &value));
return (given.or(held), false);
+15 -38
View File
@@ -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<f32>; 3]);
pub(crate) struct StateTweens([Tween<f32>; 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<T: Interpolate>(&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<StateTweens>, 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::<StateTweens>(ctx, id) } else { store::take::<StateTweens>(ctx, id) };
let mut tweens = match existing {
Some(tweens) => tweens,
None => {
if !sizing {
memory::adopt::<StateTweens>(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<T: Interpolate>(&self, lane: Lane, name: &'static str, target: T, spec: MotionSpec) -> T {
fn run<T: Interpolate>(&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::<Tween<T>>(ctx, id) } else { store::take::<Tween<T>>(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::<Tween<T>>(ctx, id).is_none() {
memory::adopt::<Tween<T>>(ctx, self.stage.owner(), id);
}