From f9d053ec7c8c68fa680dc7bcd0adb6bb0fbc38e7 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Sun, 4 Oct 2026 23:40:26 +0800 Subject: [PATCH] fix(input): preserve primary touch identity across contact updates --- .../browser_api/native_event_construction.rs | 2 + .../tests/browser_api/pointer_capture.rs | 85 +++++++++++++++++++ .../tests/dom_xhr/dom/pointer_and_mouse.rs | 10 ++- 3 files changed, 93 insertions(+), 4 deletions(-) diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/native_event_construction.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/native_event_construction.rs index 32956441668..3baa2e53cf3 100644 --- a/moli-renderer-v8/src/script_vm/tests/browser_api/native_event_construction.rs +++ b/moli-renderer-v8/src/script_vm/tests/browser_api/native_event_construction.rs @@ -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, diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/pointer_capture.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/pointer_capture.rs index 7f02f9a2f29..8f9fe2da913 100644 --- a/moli-renderer-v8/src/script_vm/tests/browser_api/pointer_capture.rs +++ b/moli-renderer-v8/src/script_vm/tests/browser_api/pointer_capture.rs @@ -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() { diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/pointer_and_mouse.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/pointer_and_mouse.rs index e6c438edd68..e880361b8f2 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/pointer_and_mouse.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/pointer_and_mouse.rs @@ -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]