add cancelable property to the TouchEvent (#35713)

* add `cancelable` property to the `TouchEvent`
set cancellable = false when sending move events to script, if the first touch_move event did not cancel it

Signed-off-by: kongbai1996 <1782765876@qq.com>

* modified review commentss

Signed-off-by: kongbai1996 <1782765876@qq.com>

---------

Signed-off-by: kongbai1996 <1782765876@qq.com>
This commit is contained in:
Bi Fuguo 2025-03-02 16:12:26 +08:00 committed by GitHub
parent 67bd557f30
commit 929f87f598
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 17 additions and 2 deletions

View file

@ -1396,7 +1396,7 @@ impl IOCompositor {
self.send_touch_event(event);
}
fn on_touch_move(&mut self, event: TouchEvent) {
fn on_touch_move(&mut self, mut event: TouchEvent) {
let action: TouchMoveAction = self.touch_handler.on_touch_move(event.id, event.point);
if TouchMoveAction::NoAction != action {
// if first move processed and allowed, we directly process the move event,
@ -1405,6 +1405,8 @@ impl IOCompositor {
.touch_handler
.move_allowed(self.touch_handler.current_sequence_id)
{
// https://w3c.github.io/touch-events/#cancelability
event.disable_cancelable();
match action {
TouchMoveAction::Scroll(delta, point) => self.on_scroll_window_event(
ScrollLocation::Delta(LayoutVector2D::from_untyped(delta.to_untyped())),

View file

@ -2090,7 +2090,7 @@ impl Document {
window,
DOMString::from(event_name),
EventBubbles::Bubbles,
EventCancelable::Cancelable,
EventCancelable::from(event.is_cancelable()),
Some(window),
0i32,
&touches,

View file

@ -147,6 +147,8 @@ pub struct TouchEvent {
pub event_type: TouchEventType,
pub id: TouchId,
pub point: DevicePoint,
/// cancelable default value is true, once the first move has been processed by script disable it.
cancelable: bool,
/// The sequence_id will be set by servo's touch handler.
sequence_id: Option<TouchSequenceId>,
}
@ -158,6 +160,7 @@ impl TouchEvent {
id,
point,
sequence_id: None,
cancelable: true,
}
}
/// Embedders should ignore this.
@ -175,6 +178,16 @@ impl TouchEvent {
pub fn expect_sequence_id(&self) -> TouchSequenceId {
self.sequence_id.expect("Sequence ID not initialized")
}
#[doc(hidden)]
pub fn disable_cancelable(&mut self) {
self.cancelable = false;
}
#[doc(hidden)]
pub fn is_cancelable(&self) -> bool {
self.cancelable
}
}
/// Mode to measure WheelDelta floats in