fix(input): preserve primary touch identity across contact updates

This commit is contained in:
ldm0
2026-11-24 14:36:23 +08:00
parent e363e2635a
commit f9d053ec7c
3 changed files with 93 additions and 4 deletions
@@ -454,6 +454,7 @@ fn native_pointer_click_preserves_identity_without_copying_contact_properties()
crate::native_bridge::node_runtime_and_handle_from_object(scope, target).unwrap();
let pointer = crate::runtime::RendererPointerEventProperties {
pointer_id: 17,
is_primary: true,
pointer_type: "pen".to_owned(),
pressure: 0.75,
tangential_pressure: 0.25,
@@ -548,6 +549,7 @@ fn native_non_primary_releases_dispatch_pointer_activation_events_in_each_world(
for event_name in ["mousedown", "mouseup"] {
let pointer = crate::runtime::RendererPointerEventProperties {
pointer_id,
is_primary: true,
pointer_type: pointer_type.to_owned(),
pressure: 0.75,
tangential_pressure: 0.25,
@@ -382,6 +382,91 @@ fn ending_contact_updates_preserves_other_touches_and_their_capture() {
}
}
#[cfg(feature = "devtools")]
#[test]
fn touch_primary_identity_survives_moves_partial_ends_and_identifier_reuse() {
let mut vm = new_touch_contact_test_vm();
vm.eval(
r#"
globalThis.__primaryEvents = [];
for (const type of ['pointerdown', 'pointermove', 'pointerup', 'pointercancel']) {
document.addEventListener(type, e => __primaryEvents.push([type, e.target.id, e.isPrimary]));
}
'ready'
"#,
).unwrap();
let first = crate::runtime::RendererTouchPoint {
id: 11,
x: 80.0,
y: 80.0,
};
let second = crate::runtime::RendererTouchPoint {
id: 0,
x: 260.0,
y: 80.0,
};
vm.dispatch_touch_contact_updates(&[first], "touchstart")
.unwrap();
vm.dispatch_touch_contact_updates(&[second], "touchstart")
.unwrap();
vm.dispatch_touch_contact_updates(&[first], "touchend")
.unwrap();
let moved_second = crate::runtime::RendererTouchPoint { x: 264.0, ..second };
vm.dispatch_touch_contact_updates(&[moved_second], "touchmove")
.unwrap();
vm.dispatch_touch_contact_updates(&[first], "touchstart")
.unwrap();
vm.dispatch_touch_contact_updates(&[], "touchend").unwrap();
vm.dispatch_touch_contact_updates(&[first], "touchstart")
.unwrap();
vm.dispatch_touch_contact_updates(&[second], "touchstart")
.unwrap();
vm.dispatch_touch_contact_updates(&[], "touchcancel")
.unwrap();
vm.dispatch_touch_contact_updates(&[second], "touchstart")
.unwrap();
vm.dispatch_touch_contact_updates(&[], "touchend").unwrap();
assert_eq!(
vm.eval("JSON.stringify(__primaryEvents)").unwrap(),
r#"[["pointerdown","first",true],["pointerdown","second",false],["pointerup","first",true],["pointermove","second",false],["pointerdown","first",false],["pointerup","second",false],["pointerup","first",false],["pointerdown","first",true],["pointerdown","second",false],["pointercancel","second",false],["pointercancel","first",true],["pointerdown","second",true],["pointerup","second",true]]"#
);
}
#[cfg(feature = "devtools")]
#[test]
fn changed_touch_batches_preserve_the_primary_flag_on_end() {
let mut vm = new_touch_contact_test_vm();
vm.eval(
r#"
globalThis.__primaryBatch = [];
for (const type of ['pointerdown', 'pointerup']) {
document.addEventListener(type, e => __primaryBatch.push([type, e.target.id, e.isPrimary]));
}
'ready'
"#,
).unwrap();
let points = [
crate::runtime::RendererTouchPoint {
id: 11,
x: 80.0,
y: 80.0,
},
crate::runtime::RendererTouchPoint {
id: 0,
x: 260.0,
y: 80.0,
},
];
vm.dispatch_touch_event_at_points(&points, "touchstart", false)
.unwrap();
vm.dispatch_touch_event_at_points(&[], "touchend", false)
.unwrap();
assert_eq!(
vm.eval("JSON.stringify(__primaryBatch)").unwrap(),
r#"[["pointerdown","first",true],["pointerdown","second",false],["pointerup","second",false],["pointerup","first",true]]"#
);
}
#[cfg(feature = "devtools")]
#[test]
fn contact_updates_on_move_can_start_an_additional_contact() {
@@ -170,8 +170,8 @@ fn mouse_dispatch_emits_pointer_event_properties() {
0.0,
crate::runtime::RendererPointerEventProperties {
pointer_id: 1,
pointer_type: "pen".to_owned(),
is_primary: true,
pointer_type: "pen".to_owned(),
pressure: 0.75,
tangential_pressure: -0.25,
tilt_x: 12.0,
@@ -191,8 +191,8 @@ fn mouse_dispatch_emits_pointer_event_properties() {
0.0,
crate::runtime::RendererPointerEventProperties {
pointer_id: 1,
pointer_type: "pen".to_owned(),
is_primary: true,
pointer_type: "pen".to_owned(),
pressure: 0.0,
tangential_pressure: 0.0,
tilt_x: 0.0,
@@ -955,8 +955,8 @@ fn mouse_hover_dispatches_pointer_boundary_before_mouse_boundary() {
let pointer = crate::runtime::RendererPointerEventProperties {
pointer_id: 1,
pointer_type: "pen".to_owned(),
is_primary: true,
pointer_type: "pen".to_owned(),
pressure: 0.0,
tangential_pressure: 0.0,
tilt_x: 0.0,
@@ -2269,7 +2269,9 @@ fn touch_contacts_keep_primary_identity_until_the_entire_stream_ends() {
r#"[["pointerdown",9,true],["pointerdown",2,false],["pointerup",9,true],["pointermove",2,false],["pointerup",2,false],["pointerdown",2,true],["pointerup",2,true]]"#
);
assert!(vm.active_touch_points.is_empty());
assert!(vm.active_touch_pointer_handles.is_empty());
let host = vm._context_host.borrow();
assert!(!host.pointer_capture_is_active(9));
assert!(!host.pointer_capture_is_active(2));
}
#[test]