mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 00:34:25 +00:00
Press machine: a press reports its phases in order, one per pass
A release in a pass that still owes the press's Down or DragStart is held for the next pass, with its click state taken when the button went up. Two clicks in one frame, which the one-press rule splits into [P1], [R1, P2], [R2], report Down and Activate for both clicks.
This commit is contained in:
@@ -443,9 +443,24 @@ struct Open<T> {
|
||||
abandoned: bool,
|
||||
/// A pass reported the press: later passes within slop report `Held`, not `Down`.
|
||||
reported: bool,
|
||||
/// Where the button went up, when it did in the pass the drag started. That pass reports `DragStart` and the
|
||||
/// next one the release, so every part reads a drag's start before its end.
|
||||
released_at: Option<Pos2>,
|
||||
/// A pass reported the drag's start: later passes report `Drag`.
|
||||
drag_reported: bool,
|
||||
/// Where the button went up and whether that was a click, when it did in a pass that still owed the press's `Down`
|
||||
/// or `DragStart`. That pass reports the owed phase and a later one the release, so every part reads a press's
|
||||
/// phases in order, one per pass.
|
||||
released_at: Option<(Pos2, bool)>,
|
||||
}
|
||||
|
||||
impl<T> Open<T> {
|
||||
/// A phase before the release is still to be reported.
|
||||
fn owes_phase(&self) -> bool {
|
||||
!self.reported || (self.dragging && !self.drag_reported)
|
||||
}
|
||||
|
||||
/// A release now is a click.
|
||||
fn clicks(&self) -> bool {
|
||||
!self.dragging && !self.abandoned && self.sense.contains(Sense::CLICK)
|
||||
}
|
||||
}
|
||||
|
||||
/// The press lifecycle of one root: the press winner it picked, kept until release or cancel.
|
||||
@@ -485,13 +500,13 @@ impl<T: Copy> PressMachine<T> {
|
||||
/// when no shape of this root takes the press.
|
||||
pub fn step(&mut self, input: PassInput<'_>, mut pick: impl FnMut(Pos2) -> Option<(T, Sense)>) -> PressStep<T> {
|
||||
let mut pos = input.pos;
|
||||
let mut drag_started = false;
|
||||
let mut released: Option<(T, PressStart, Pos2, bool, bool)> = None;
|
||||
let mut cancelled: Option<(T, PressStart, PressCancel)> = None;
|
||||
if let Some(open) = self.open
|
||||
&& let Some(at) = open.released_at
|
||||
&& let Some((at, click)) = open.released_at
|
||||
&& !open.owes_phase()
|
||||
{
|
||||
released = Some((open.target, open.start, at, true, false));
|
||||
released = Some((open.target, open.start, at, open.dragging, click));
|
||||
self.open = None;
|
||||
}
|
||||
if let (Some(reason), Some(open)) = (input.cancel, self.open) {
|
||||
@@ -503,13 +518,13 @@ impl<T: Copy> PressMachine<T> {
|
||||
PointerEvent::Moved(at) => {
|
||||
pos = at;
|
||||
if let Some(open) = &mut self.open
|
||||
&& open.released_at.is_none()
|
||||
&& !open.dragging
|
||||
&& !open.abandoned
|
||||
&& at.distance(open.start.origin) > input.slop
|
||||
{
|
||||
if open.sense.contains(Sense::DRAG) {
|
||||
open.dragging = true;
|
||||
drag_started = true;
|
||||
} else {
|
||||
open.abandoned = true;
|
||||
}
|
||||
@@ -524,9 +539,9 @@ impl<T: Copy> PressMachine<T> {
|
||||
pos = at;
|
||||
// A release held for the next pass gives way to a new press; it ends in this pass.
|
||||
if let Some(open) = self.open
|
||||
&& let Some(up) = open.released_at
|
||||
&& let Some((up, click)) = open.released_at
|
||||
{
|
||||
released = Some((open.target, open.start, up, true, false));
|
||||
released = Some((open.target, open.start, up, open.dragging, click));
|
||||
self.open = None;
|
||||
}
|
||||
// A middle or secondary press captures for a drag (a canvas pan, a drag with the secondary button) and
|
||||
@@ -556,9 +571,9 @@ impl<T: Copy> PressMachine<T> {
|
||||
dragging: false,
|
||||
abandoned: false,
|
||||
reported: false,
|
||||
drag_reported: false,
|
||||
released_at: None,
|
||||
});
|
||||
drag_started = false;
|
||||
}
|
||||
PointerEvent::Released { button, pos: at } => {
|
||||
pos = at;
|
||||
@@ -566,12 +581,11 @@ impl<T: Copy> PressMachine<T> {
|
||||
continue;
|
||||
}
|
||||
if let Some(open) = &mut self.open
|
||||
&& drag_started
|
||||
&& open.owes_phase()
|
||||
{
|
||||
open.released_at = Some(at);
|
||||
open.released_at = Some((at, open.clicks()));
|
||||
} else if let Some(open) = self.open.take() {
|
||||
let click = !open.dragging && !open.abandoned && open.sense.contains(Sense::CLICK);
|
||||
released = Some((open.target, open.start, at, open.dragging, click));
|
||||
released = Some((open.target, open.start, at, open.dragging, open.clicks()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -594,17 +608,18 @@ impl<T: Copy> PressMachine<T> {
|
||||
};
|
||||
}
|
||||
let Some(open) = self.open.as_mut() else { return PressStep::default() };
|
||||
let phase = if drag_started {
|
||||
let phase = if !open.reported {
|
||||
PressPhase::Down { start: open.start }
|
||||
} else if open.dragging && !open.drag_reported {
|
||||
open.drag_reported = true;
|
||||
PressPhase::DragStart { start: open.start, pos }
|
||||
} else if open.dragging {
|
||||
PressPhase::Drag { start: open.start, pos, delta: input.delta }
|
||||
} else if open.reported {
|
||||
PressPhase::Held { start: open.start }
|
||||
} else {
|
||||
PressPhase::Down { start: open.start }
|
||||
PressPhase::Held { start: open.start }
|
||||
};
|
||||
open.reported = true;
|
||||
PressStep { target: Some(open.target), phase: Some(phase), dragging: open.dragging, activated: None }
|
||||
PressStep { target: Some(open.target), phase: Some(phase), dragging: open.drag_reported, activated: None }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -639,4 +654,37 @@ mod tests {
|
||||
let step = pass(&mut machine, &[PointerEvent::released(pos2(0.0, 0.0))], Sense::CLICK);
|
||||
assert_eq!(step.activated, Some(1));
|
||||
}
|
||||
|
||||
/// The phases a sequence of passes reports, as `D`own, `H`eld, `S` (drag start), `G` (drag), `R`elease with `!` for an
|
||||
/// activation, `-` for none.
|
||||
fn phases(passes: &[&[PointerEvent]], sense: Sense) -> String {
|
||||
let mut machine = PressMachine::new();
|
||||
passes
|
||||
.iter()
|
||||
.map(|events| {
|
||||
let step = pass(&mut machine, events, sense);
|
||||
match step.phase {
|
||||
Some(PressPhase::Down { .. }) => "D",
|
||||
Some(PressPhase::Held { .. }) => "H",
|
||||
Some(PressPhase::DragStart { .. }) => "S",
|
||||
Some(PressPhase::Drag { .. }) => "G",
|
||||
Some(PressPhase::Release { .. }) if step.activated.is_some() => "R!",
|
||||
Some(PressPhase::Release { .. }) => "R",
|
||||
_ => "-",
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_clicks_whose_passes_share_a_release_and_a_press_report_every_phase_in_order() {
|
||||
let up = PointerEvent::released(pos2(0.0, 0.0));
|
||||
let second = press(0.0, 1.1);
|
||||
assert_eq!(phases(&[&[press(0.0, 1.0)], &[up, second], &[up], &[]], Sense::CLICK), "D R! D R!");
|
||||
// The second press's drag starts in the pass that owes its Down, and the button goes up there too.
|
||||
let far = PointerEvent::Moved(pos2(40.0, 0.0));
|
||||
let passes: [&[PointerEvent]; 5] = [&[press(0.0, 1.0)], &[up, second], &[far, up], &[], &[]];
|
||||
assert_eq!(phases(&passes, Sense::CLICK | Sense::DRAG), "D R! D S R");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,3 +226,42 @@ fn a_double_click_on_a_thumbnail_activates_it_once() {
|
||||
let named = take(&app);
|
||||
assert!(thumb_activations(&named).is_empty(), "a double click on the name is no thumbnail activation: {named:?}");
|
||||
}
|
||||
|
||||
/// A double click whose two clicks arrive in one frame, on a Smart Filter row of a smart object that is not active,
|
||||
/// selects the row and then activates it, as two clicks in two frames do.
|
||||
#[trial]
|
||||
fn a_double_click_in_one_frame_selects_a_smart_filter_row_and_activates_it() {
|
||||
let (mut fixture, app) = rig(tree());
|
||||
take(&app);
|
||||
let filter = node(&fixture, "Gaussian Blur").center();
|
||||
fixture.frame(vec![pointer::moved(filter)]).idle(1);
|
||||
fixture.frame(vec![pointer::press(filter), pointer::release(filter), pointer::press(filter), pointer::release(filter)]).settle();
|
||||
let row = LayerRowKey::Filters(SMART, Some(0));
|
||||
let events = take(&app);
|
||||
let selected = events.iter().position(|event| matches!(event, LayersEvent::Select(select) if select.row.as_ref() == Some(&row)));
|
||||
let activated = events.iter().position(|event| matches!(event, LayersEvent::Activate { row: on, on: ActivateOn::Row, .. } if *on == row));
|
||||
assert!(selected.is_some() && activated.is_some() && selected < activated, "selected, then activated: {events:?}");
|
||||
}
|
||||
|
||||
/// A Smart Filter row dragged onto another smart object's row moves the filter there, with the active layer changing to
|
||||
/// the filter's smart object when the press selects its row.
|
||||
#[trial]
|
||||
fn a_smart_filter_row_dragged_onto_another_smart_object_moves_there() {
|
||||
let mut tree = tree();
|
||||
let other = tree.add(None, LayerInfo::new("Other Smart Object", LayerKind::SmartObject), false);
|
||||
let (mut fixture, app) = rig(tree);
|
||||
take(&app);
|
||||
let from = node(&fixture, "Gaussian Blur").center();
|
||||
let onto = node(&fixture, "Other Smart Object").rect;
|
||||
let to = egui::pos2(onto.center().x, onto.bottom() - onto.height() / 4.0);
|
||||
fixture.drag_hold_at(from, to - from).release_at(to).idle(2);
|
||||
let events = take(&app);
|
||||
let transfers: Vec<_> = events
|
||||
.iter()
|
||||
.filter_map(|event| match event {
|
||||
LayersEvent::Drop(EditEvent::Set { after: LayerDrop::Transfer(t), .. }) => Some((t.rows.clone(), t.to, t.copy)),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(transfers, [(vec![LayerRowKey::Filters(SMART, Some(0))], other, false)], "{events:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user