Keep captured parts available through release passes

This commit is contained in:
Dylan M. Taylor
2026-11-20 11:34:46 -05:00
parent 119a633c10
commit 8eacb8d7a2
3 changed files with 46 additions and 6 deletions
+32 -2
View File
@@ -46,6 +46,8 @@ struct Config {
snap: bool,
/// The box is `TransformBox::text_frame`; `rules`, `zone`, `inside_moves` and `reference_point` are then unused.
text_frame: bool,
/// Show the frame only while hovered or while its part owns the pointer.
hover_frame: bool,
overflow: bool,
/// The canvas's rulers on both axes.
rulers: Option<RulerText>,
@@ -95,6 +97,7 @@ fn defaults() -> Config {
guides: false,
snap: false,
text_frame: false,
hover_frame: false,
overflow: false,
rulers: None,
f32_host: false,
@@ -169,6 +172,12 @@ fn harness(state: BoxState, setup: impl FnOnce(&mut Config)) -> Harness {
let output = Guides::new("Guides", canvas_id, Bind::bound(&mut guides)).interactive(true).show_at(ui, body.rect);
guide_edits = usize::from(!output.edit.events.is_empty());
}
let frame_id = egui::Id::unique("hover frame");
let frame_rect = egui::Rect::from_two_pos(view.to_screen(RECT.origin()), view.to_screen(Point::new(RECT.x1, RECT.y1))).expand(8.0);
let hovered = ui.ctx().pointer_hover_pos().is_some_and(|pos| frame_rect.contains(pos));
if config.hover_frame && !hovered && craft_ui::runtime::pointer::active_part(ui.ctx(), frame_id).is_none() {
return None;
}
let mut control = if config.text_frame {
TransformBox::text_frame("Free Transform", Bind::given(state), RECT).overflow(config.overflow)
} else {
@@ -185,9 +194,12 @@ fn harness(state: BoxState, setup: impl FnOnce(&mut Config)) -> Harness {
if let Some(line) = config.line {
control = control.line(line);
}
(control.show_at(ui, body.rect), guide_edits)
if config.hover_frame {
control = control.id(frame_id);
}
Some((control.show_at(ui, body.rect), guide_edits))
});
if let Some((output, guide_edits)) = canvas.inner {
if let Some(Some((output, guide_edits))) = canvas.inner {
let mut host = shown_host.borrow_mut();
host.shown = output.edit.shown.as_ref().and_then(|value| value.same()).map(|value| **value);
host.guide_events += guide_edits;
@@ -474,6 +486,24 @@ fn a_text_frame_fed_back_through_an_f32_host_follows_every_move_to_the_commit()
assert!((h.state().quad[2].y - 280.0).abs() < 0.01 && events.last() == Some(&Seen::Commit), "the commit holds the last move");
}
/// A hover-revealed frame remains declared through release after its drag leaves the original box.
#[trial]
fn a_hover_frame_commits_a_drag_released_outside() {
let mut h = harness(BoxState::from_rect(RECT), |config| {
config.text_frame = true;
config.hover_frame = true;
});
let start = h.screen(Point::new(300.0, 150.0));
let end = start + by(120.0, 0.0);
frame(&mut h, vec![pointer::moved(start)]);
frame(&mut h, vec![pointer::press(start)]);
frame(&mut h, vec![pointer::moved(end)]);
frame(&mut h, vec![pointer::release(end)]);
frame(&mut h, Vec::new());
let events = &h.host.borrow().events;
assert!(events.contains(&Seen::Commit), "the host receives the release: {events:?}");
}
#[trial]
fn transform_box_shows_each_drag_proposal_in_the_same_frame() {
let mut h = upright();
+2 -2
View File
@@ -998,9 +998,9 @@ fn any_press_open(ctx: &Context) -> bool {
PerViewport::<Arenas>::with(ctx, pass::current_viewport(ctx), |arenas| arenas.by_root.values().any(|state| state.machine.target().is_some()))
}
pub(crate) fn pressed_part(ctx: &Context, owner: Id) -> Option<PartRef> {
pub(crate) fn pressed_parts(ctx: &Context) -> Vec<(Id, PartRef)> {
PerViewport::<Arenas>::with(ctx, pass::current_viewport(ctx), |arenas| {
arenas.by_root.values().find_map(|state| state.machine.target().filter(|hit| hit.owner == owner).and_then(|hit| hit.part))
arenas.by_root.values().filter_map(|state| state.machine.target().and_then(|hit| hit.part.map(|part| (hit.owner, part)))).collect()
})
}
+12 -2
View File
@@ -149,6 +149,8 @@ pub(crate) struct Shapes {
hover: Option<(Pos2, PressTargets, Under)>,
/// A press was open when the pass began: only its target hovers.
captured: bool,
/// Parts holding a press at the start of this pass, before enclosing arenas process its release.
pressed_parts: Vec<(Id, PartRef)>,
/// What lies under another point a press asked for in the pass, with its targets: every root asks for the same
/// press.
winner: Option<(Pos2, PressTargets, Under)>,
@@ -241,9 +243,11 @@ 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 pressed_parts = crate::primitives::sensing::pressed_parts(ctx);
PerViewport::<Shapes>::with(ctx, viewport, |shapes| {
shapes.hover = hover;
shapes.captured = captured;
shapes.pressed_parts = pressed_parts;
});
}
@@ -919,12 +923,18 @@ pub fn shapes_at(ctx: &Context, at: Pos2) -> Vec<(Id, Option<PartRef>)> {
/// The part of `owner` holding the pointer's hover or an open press in this viewport.
///
/// An open press keeps its part while the pointer moves away, so callers can keep declaring it until release.
/// An open press keeps its part while the pointer moves away. A press open at the start of the pass keeps its part
/// for that whole pass, so a child can still declare it after an enclosing arena has processed the release.
pub fn active_part(ctx: &Context, owner: Id) -> Option<PartRef> {
let viewport = pass::current_viewport(ctx);
let pressed =
PerViewport::<Shapes>::with(ctx, viewport, |record| record.pressed_parts.iter().find_map(|(id, part)| (*id == owner).then_some(*part)));
if pressed.is_some() {
return pressed;
}
with_hover(ctx, viewport, |under, captured| {
if captured {
return crate::primitives::sensing::pressed_part(ctx, owner);
return None;
}
let under = under?;
[under.press.click, under.press.drag, under.hover_top]