ui-contract: a settled view under the pointer replays while what sensing gives it is unchanged

A memoized view replays with the pointer over it unless its pointer facts
changed: the hover, parts and pointer-over of its roots and egui widgets at the
hover point (pointer::Sensed), the modifiers while it is under the pointer, a
move in a rect it follows, a wheel share or owed ease for a taker it registered,
a tap in it, or, while a drag session runs, the pointer over it. A press held
that started in it still runs it. A move within one part replays the view; a
move onto another part runs it.
This commit is contained in:
aiongg committed 2026-11-27 01:02:01 +00:00
1 parent 27e8a77145
commit 4fe31f5dc3
4 files changed
+192 -15

No files matched your search

+53 -14
View File
@@ -281,8 +281,12 @@ struct Settled {
place: Place,
/// The rect its content took.
rect: Rect,
/// The pointer was over it, or one of its stops held focus: it runs again to show that they left.
/// A press held that started in it, or one of its stops was the target of a focus request: it runs again to show
/// that they ended.
engaged: bool,
/// What pointer sensing gave it (`pointer::Sensed`) and the hover point, screen points, when it ran.
sensed: crate::runtime::pointer::Sensed,
pointer: Option<egui::Pos2>,
/// What it ran for: its props key after the run.
key: Key,
/// The pass it ran in.
@@ -494,8 +498,14 @@ pub(crate) fn replay(ctx: &Context, id: Id) {
/// It replays when its last run was in an earlier pass, of this frame or an earlier one, and since then:
/// - nothing in it moves: no animation off its end value, no repaint asked from inside it, no texture it shows still
/// loading, no wake time reached;
/// - the pointer is not over it or an overlay layer it shows, no press started on either and none of its widgets is
/// dragged, and none of that held in its last run;
/// - what pointer sensing gives it is what its run saw (`pointer::Sensed`: the hover, part and pointer-over facts of
/// its roots and egui widgets), so a move within one part of it leaves it alone, and a move onto another part, in
/// or out runs it; while it is under the pointer, the modifiers are the ones its run saw; the pointer did not move
/// in a rect its run follows the pointer in (`pointer::follows_pointer`, which a hover position read marks), now or
/// then; no wheel share or owed ease of this pass goes to a taker it registered; no tap of this pass lies in it;
/// while a drag session runs, the pointer is not over it or an overlay layer it shows;
/// - no press that is held started on it or an overlay layer it shows, none of its widgets is dragged, and none of
/// that held in its last run;
/// - the focus is where its run left it (`replay::Held`: the same stop and egui widget of it, or none), and while the
/// focus is in it no key, text, IME, clipboard or AccessKit event came (`Input::Sent`, which such a run reads);
/// - none of its stops is the target of a focus request, now or in its last run;
@@ -595,7 +605,9 @@ pub(crate) fn settled(
// The scroll view a nested child decided by: the run's, moved with the record, while the clip moved with it; a run
// that lay wholly in its clip ran every nested child, which holds at any view.
let view = place.and_then(|_| with_clip.then(|| (settled.view.map(|ran| ran.moved(shift.offset)), view)));
if engaged(ctx, settled.place.layer, rect.intersect(shift.clip), &settled.record.facts.overlays)
let shown = rect.intersect(shift.clip);
if pressed(ctx, settled.place.layer, shown, &settled.record.facts.overlays)
|| pointer_reaches(ctx, &settled, shown, shift.offset)
|| crate::runtime::memory::ended_since(ctx, &settled.record.facts.owners, settled.lives)
|| moved(ctx, &settled, view)
{
@@ -624,16 +636,40 @@ fn moved(ctx: &Context, settled: &Settled, view: Option<(Option<View>, Option<Vi
|| crate::runtime::overlay::close_queued(ctx, &facts.layers)
}
/// Whether the pointer is over `rect` (shown) on `layer` or over one of the overlay layers `overlays` shown, or a
/// press that is held started there. Both halves ask whether the layer is the top layer at the point, so a pointer or
/// a press on an overlay above the view leaves it alone.
fn engaged(ctx: &Context, layer: Layer, rect: Rect, overlays: &[replay::ShownLayer]) -> bool {
let over = |layer: Layer, rect: Rect| {
/// Whether a press that is held started in `rect` (shown) on `layer` or in one of the overlay layers `overlays` shown,
/// with the layer the top layer at its origin, so a press on an overlay above the view leaves it alone.
fn pressed(ctx: &Context, layer: Layer, rect: Rect, overlays: &[replay::ShownLayer]) -> bool {
let Some(origin) = input_state::held_press_origin(ctx) else { return false };
let on = |layer: Layer, rect: Rect| {
let screen = ctx.layer_transform_to_global(layer).map_or(rect, |transform| transform * rect);
ctx.rect_contains_pointer(layer, rect)
|| input_state::held_press_origin(ctx).is_some_and(|origin| screen.contains(origin) && ctx.layer_id_at(origin) == Some(layer))
screen.contains(origin) && ctx.layer_id_at(origin) == Some(layer)
};
over(layer, rect) || overlays.iter().any(|shown| over(shown.layer, shown.rect))
on(layer, rect) || overlays.iter().any(|shown| on(shown.layer, shown.rect))
}
/// Whether the pointer input of this pass reaches the kept run `settled`, which replays at `rect` (shown), moved by
/// `offset`: the pointer rules of `settled`.
fn pointer_reaches(ctx: &Context, settled: &Settled, rect: Rect, offset: Vec2) -> bool {
let facts = &settled.record.facts;
let sensed = crate::runtime::pointer::sensed(ctx, &facts.roots, &settled.record.widgets);
let changed = |input: Input| inputs::generation(ctx, input) != settled.seen[input as usize];
let hover = crate::runtime::pointer::hover_point(ctx);
let follows = changed(Input::Pointer)
&& facts
.follows
.iter()
.any(|ran| hover.is_some_and(|at| ran.translate(offset).contains(at)) || settled.pointer.is_some_and(|at| ran.contains(at)));
let layer = settled.place.layer;
let over = |layer: Layer, rect: Rect| ctx.rect_contains_pointer(layer, rect);
let dragged = crate::runtime::drag::running(ctx) && (over(layer, rect) || facts.overlays.iter().any(|shown| over(shown.layer, shown.rect)));
let tapped = crate::runtime::pointer::with_frame(ctx, |frame, _| frame.and_then(|frame| frame.multi_tap))
.is_some_and(|tap| ctx.layer_transform_to_global(layer).map_or(rect, |transform| transform * rect).contains(tap.at));
sensed != settled.sensed
|| (!sensed.is_empty() && changed(Input::Modifiers))
|| follows
|| crate::runtime::wheel::reaches(ctx, &facts.takers)
|| dragged
|| tapped
}
/// Adds the last real run of the memoized view `id` to the running pass, in place of running it, at the place `shift`
@@ -702,8 +738,11 @@ pub(crate) fn ran(ctx: &Context, id: Id, record: Record, place: Place, rect: Rec
let revealed = focus::targets(ctx)[1];
let engaged_now = record.focus.stop_ids().any(|stop| revealed == Some(stop));
let lives = crate::runtime::memory::ended_generation(ctx);
let mut settled = Settled { record, frame: 0, place, rect, engaged: engaged_now, key, pass: 0, edits, output, lives, seen, view };
settled.engaged |= engaged(ctx, place.layer, rect.intersect(place.clip), &settled.record.facts.overlays);
let sensed = crate::runtime::pointer::sensed(ctx, &record.facts.roots, &record.widgets);
let pointer = crate::runtime::pointer::hover_point(ctx);
let mut settled =
Settled { record, frame: 0, place, rect, engaged: engaged_now, sensed, pointer, key, pass: 0, edits, output, lives, seen, view };
settled.engaged |= pressed(ctx, place.layer, rect.intersect(place.clip), &settled.record.facts.overlays);
with(ctx, |store, pass_nr, frame| {
settled.frame = frame;
settled.pass = pass_nr;
+59
View File
@@ -244,6 +244,9 @@ pub(crate) struct Shapes {
/// What lies under the pass's hover point, found over `last` at the begin hook with the pass's egui targets:
/// every root's hover reads it, and a press at the same point and targets reuses it.
hover: Option<(Pos2, PressTargets, Under)>,
/// The egui widgets under the pass's hover point in the last finished pass, with the click and drag targets there
/// (`Context::widgets_at`), found once at the begin hook for every view's `sensed`.
hover_widgets: Option<(Vec<Id>, Option<Id>, Option<Id>)>,
/// A press was open when the pass began: only its target hovers.
captured: bool,
/// The pass's tap went to a component.
@@ -403,8 +406,10 @@ pub(crate) fn begin(ctx: &Context, hit: HitSizes, latched: craft_keymap::Mods) {
let hover_targets = PressTargets { click: hits.0, drag: hits.1 };
let hover = hover_at.map(|at| (at, hover_targets, find_under(ctx, viewport, at, hover_targets, false)));
let captured = press_captured(ctx);
let hover_widgets = hover_at.and_then(|at| ctx.widgets_at(viewport, at));
PerViewport::<Shapes>::with(ctx, viewport, |shapes| {
shapes.hover = hover;
shapes.hover_widgets = hover_widgets;
shapes.captured = captured;
shapes.pressed_parts.clear();
});
@@ -1066,6 +1071,60 @@ pub(crate) fn with_hover<R>(ctx: &Context, viewport: ViewportId, f: impl FnOnce(
PerViewport::<Shapes>::with(ctx, viewport, |record| f(record.hover.as_ref().map(|(_, _, under)| under), record.captured))
}
/// What pointer sensing gives a memoized view's run in the running pass (`runtime::lazy`): of the arena roots and egui
/// widgets the run registered, the ones under the pass's hover point and what they win there. Equal answers in two
/// passes mean every hover, part and pointer-over fact the run's components read is the same; a hover position is
/// not part of it, as the code that reads one marks itself ([`follows_pointer`]).
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct Sensed {
/// The press winners and the shape that hovers beside them (`Under`), each as its kind, root, owner and part, of
/// the run's roots. None while a press is open: only the press's target hovers, and the press runs it.
hover: SmallVec<[(u8, Id, Id, Option<PartRef>); 3]>,
/// The run's roots with a shape under the point.
over: SmallVec<[Id; 4]>,
/// The run's egui widgets that contain the point, then its click and drag targets there.
widgets: SmallVec<[Id; 4]>,
}
impl Sensed {
/// Nothing of the run lies under the pointer.
pub(crate) fn is_empty(&self) -> bool {
self.hover.is_empty() && self.over.is_empty() && self.widgets.is_empty()
}
}
/// What pointer sensing gives the arena roots `roots` and egui widgets `widgets` of a run in the running pass
/// ([`Sensed`]): read from the pass's one resolution at the hover point.
pub(crate) fn sensed(ctx: &Context, roots: &IdSet, widgets: &IdSet) -> Sensed {
PerViewport::<Shapes>::with(ctx, pass::current_viewport(ctx), |record| {
let mut sensed = Sensed::default();
if let Some((_, _, under)) = &record.hover
&& !roots.is_empty()
{
if !record.captured {
let found = [under.press.click, under.press.drag, under.hover_top];
for (kind, (winner, root)) in (0u8..).zip(found).filter_map(|(kind, found)| Some((kind, found?))) {
if roots.contains(&root) {
sensed.hover.push((kind, root, winner.owner, winner.part));
}
}
}
sensed.over.extend(under.over.iter().copied().filter(|root| roots.contains(root)));
}
if let Some((contains, click, drag)) = &record.hover_widgets
&& !widgets.is_empty()
{
sensed.widgets.extend(contains.iter().chain(click).chain(drag).copied().filter(|widget| widgets.contains(widget)));
}
sensed
})
}
/// The running pass's hover point, screen points; `None` while the pointer is not in the window.
pub(crate) fn hover_point(ctx: &Context) -> Option<Pos2> {
with_frame(ctx, |_, hover| hover)
}
/// Whether a shape the component `id` registered shows hover in the running pass, by the pass's one resolution of
/// what lies under the pointer: it takes the press there, or it is the topmost shape and takes none. A
/// container reads it for the components in its body before they run. While a press is open it holds for none: only
+10
View File
@@ -316,6 +316,16 @@ pub(crate) fn begin(ctx: &Context) {
}
}
/// Whether this pass's wheel reaches one of `takers`: a share of it goes to one, or one's ease owes motion it pays in
/// this pass.
pub(crate) fn reaches(ctx: &Context, takers: &[Id]) -> bool {
!takers.is_empty()
&& PerViewport::<State>::with(ctx, pass::current_viewport(ctx), |state| {
state.shares.iter().any(|(owner, _)| takers.contains(owner))
|| state.eases.iter().any(|(owner, ease)| ease.owes() && takers.contains(owner))
})
}
/// The component's share of this pass's wheel, left in place.
pub(crate) fn peek(ctx: &Context, id: Id) -> Option<Wheel> {
PerViewport::<State>::with(ctx, pass::current_viewport(ctx), |state| state.shares.iter().find(|(owner, _)| *owner == id).map(|share| share.1))
@@ -1,5 +1,5 @@
//! Settled memoized views: a settled view adds its last run to the pass in place of running, and runs when one of the
//! settled rules breaks: the pointer over it, a bound value, inspection record or command state it read, its room, its
//! settled rules breaks: what pointer sensing gives it, a pointer move it follows, a bound value, inspection record or command state it read, its room, its
//! environment (theme, density, language), an animation or a repaint asked inside it. Input elsewhere in the window
//! does not run it.
@@ -182,6 +182,75 @@ fn hovering_a_settled_view_runs_it_alone() {
assert_eq!(scene.frame(&mut frames, vec![]), [0, 0], "once its hover faded, the view settles again");
}
/// A pointer that moves within one part of a settled view, once its hover has settled, leaves it settled: what
/// sensing gives the view is the same.
#[trial]
fn a_move_within_a_settled_views_part_replays_it() {
let scene = Scene::new(false, false);
let mut frames = Frames::new();
scene.settle(&mut frames);
let over = Scene::button(1).1.center();
scene.frame(&mut frames, vec![moved(over)]);
scene.settle(&mut frames);
for step in 1..=3 {
let at = over + Vec2::new(step as f32, 0.0);
assert_eq!(scene.frame(&mut frames, vec![moved(at)]), [0, 0], "move {step} within the hovered button replays both views");
}
}
/// A pointer that moves onto another part of a settled view runs that view in the frame it arrives, and only it; the
/// view settles again once its hover has.
#[trial]
fn a_move_onto_another_part_runs_the_view_once() {
let scene = Scene::new(false, false);
let mut frames = Frames::new();
scene.settle(&mut frames);
scene.frame(&mut frames, vec![moved(Scene::button(0).1.center())]);
scene.settle(&mut frames);
let [reader, other] = scene.frame(&mut frames, vec![moved(READER.center())]);
assert!(reader > 0, "the move from the button onto the scrub runs the view that shows both");
assert_eq!(other, 0, "the view beside it stays settled");
scene.settle(&mut frames);
assert_eq!(scene.frame(&mut frames, vec![]), [0, 0], "once its hover settled, the view replays again");
}
/// A view that marks a rect as following the pointer.
#[derive(MemoProps)]
struct Follower {
follows: bool,
}
fn follower(props: &mut Follower, ui: &mut Ui) {
let response = Button::new("Gamma").show(ui).response;
BUTTONS.with(|buttons| buttons[0].set((response.id, response.rect)));
if props.follows {
craft_ui::runtime::pointer::follows_pointer(ui.ctx(), response.rect);
}
}
/// A settled view whose run marked a rect as following the pointer runs on every move within it; one that marked
/// none replays on the same moves.
#[trial]
fn a_view_that_follows_the_pointer_runs_on_every_move() {
for follows in [true, false] {
let show = |ui: &mut Ui| {
in_region(ui, Rect::from_min_size(ORIGIN, ROOM), |ui| {
MemoView::new("follower", Follower { follows }, follower).show(ui);
});
};
let mut frames = Frames::new();
frames.idle(30, show);
let over = Scene::button(0).1.center();
frames.frame(vec![moved(over)], show);
frames.idle(30, show);
for step in 1..=3 {
frames.frame(vec![moved(over + Vec2::new(step as f32, 0.0))], show);
let runs = runs_of(&frames.ctx, "Follower");
assert_eq!(runs > 0, follows, "move {step}: the view runs exactly when its run follows the pointer ({follows})");
}
}
}
/// Drags the editor right in steps; returns per frame from the press on the passes, the runs of each view, and whether
/// the reader showed the dragged value.
fn drag(scene: &Scene) -> Vec<(usize, [usize; 2], bool)> {