implement Touchevent prevent default behavior (#35031)

* implement Touchevent prevent default behavior

* The status change logic of the `TouchHandler` is changed.
> The `WaitingForScript` state is canceled. TouchAction can be identified
  based on the current touch type and numbers if touch points.
* Sends current event to script thread along with recognized `TouchAction`.
> After dispatch event, script thread sends a `TouchEventProcess(EventResult)`
  message to main thread. If the event is set to `DefaultAllowed`, the
  corresponding `TouchAction` information is added.
* After receiving `DefaultAllowed(TouchAction)` message, main thread executes corresponding action.
> `DefaultPrevented(TouchEventType)` is received. Use `prevent_click` to mark
  that the default `Click` is blocked, and `prevent_move` to mark that the
  default `Scroll` and `Zoom` are blocked. In this way, all TouchActions
  implement preventDefault.
Signed-off-by: Bi Fuguo <1782765876@qq.com>

* fix some suggestions

* support preventDefault fling
* move `TouchAction` to share touch directory
* check preventDefault everytime when touch
* fix zoom ineffective

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

* fix some suggestions

rename on_event_processed to  on_touch_event_processed
clear unused features

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

* Optimizes pan performance by continuously sliding without waiting for the eventhandler.

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

* resolve conflict

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

---------

Signed-off-by: Bi Fuguo <1782765876@qq.com>
Signed-off-by: kongbai1996 <1782765876@qq.com>
This commit is contained in:
Bi Fuguo 2025-02-17 18:50:04 +08:00 committed by GitHub
parent 6dce329acc
commit 3fe42a0b5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 273 additions and 255 deletions

View file

@ -19,7 +19,7 @@ use servo::webrender_api::ScrollLocation;
use servo::{
AllowOrDenyRequest, AuthenticationRequest, FilterPattern, GamepadHapticEffectType, LoadStatus,
PermissionRequest, PromptDefinition, PromptOrigin, PromptResult, Servo, ServoDelegate,
ServoError, TouchEventAction, WebView, WebViewDelegate,
ServoError, TouchEventType, WebView, WebViewDelegate,
};
#[cfg(target_os = "linux")]
use tinyfiledialogs::MessageBoxIcon;
@ -274,36 +274,36 @@ impl RunningAppState {
0.0,
-self.inner().window.page_height() + 2.0 * LINE_HEIGHT,
));
webview.notify_scroll_event(scroll_location, origin, TouchEventAction::Move);
webview.notify_scroll_event(scroll_location, origin, TouchEventType::Move);
})
.shortcut(Modifiers::empty(), Key::PageUp, || {
let scroll_location = ScrollLocation::Delta(Vector2D::new(
0.0,
self.inner().window.page_height() - 2.0 * LINE_HEIGHT,
));
webview.notify_scroll_event(scroll_location, origin, TouchEventAction::Move);
webview.notify_scroll_event(scroll_location, origin, TouchEventType::Move);
})
.shortcut(Modifiers::empty(), Key::Home, || {
webview.notify_scroll_event(ScrollLocation::Start, origin, TouchEventAction::Move);
webview.notify_scroll_event(ScrollLocation::Start, origin, TouchEventType::Move);
})
.shortcut(Modifiers::empty(), Key::End, || {
webview.notify_scroll_event(ScrollLocation::End, origin, TouchEventAction::Move);
webview.notify_scroll_event(ScrollLocation::End, origin, TouchEventType::Move);
})
.shortcut(Modifiers::empty(), Key::ArrowUp, || {
let location = ScrollLocation::Delta(Vector2D::new(0.0, 3.0 * LINE_HEIGHT));
webview.notify_scroll_event(location, origin, TouchEventAction::Move);
webview.notify_scroll_event(location, origin, TouchEventType::Move);
})
.shortcut(Modifiers::empty(), Key::ArrowDown, || {
let location = ScrollLocation::Delta(Vector2D::new(0.0, -3.0 * LINE_HEIGHT));
webview.notify_scroll_event(location, origin, TouchEventAction::Move);
webview.notify_scroll_event(location, origin, TouchEventType::Move);
})
.shortcut(Modifiers::empty(), Key::ArrowLeft, || {
let location = ScrollLocation::Delta(Vector2D::new(LINE_HEIGHT, 0.0));
webview.notify_scroll_event(location, origin, TouchEventAction::Move);
webview.notify_scroll_event(location, origin, TouchEventType::Move);
})
.shortcut(Modifiers::empty(), Key::ArrowRight, || {
let location = ScrollLocation::Delta(Vector2D::new(-LINE_HEIGHT, 0.0));
webview.notify_scroll_event(location, origin, TouchEventAction::Move);
webview.notify_scroll_event(location, origin, TouchEventType::Move);
});
}
}

View file

@ -26,8 +26,8 @@ use servo::webrender_traits::rendering_context::{OffscreenRenderingContext, Rend
use servo::webrender_traits::SurfmanRenderingContext;
use servo::{
Cursor, InputEvent, Key, KeyState, KeyboardEvent, MouseButton as ServoMouseButton,
MouseButtonAction, MouseButtonEvent, MouseMoveEvent, Theme, TouchEvent, TouchEventAction,
TouchId, WebView, WheelDelta, WheelEvent, WheelMode,
MouseButtonAction, MouseButtonEvent, MouseMoveEvent, Theme, TouchAction, TouchEvent,
TouchEventType, TouchId, WebView, WheelDelta, WheelEvent, WheelMode,
};
use surfman::{Connection, Context, Device, SurfaceType};
use url::Url;
@ -606,7 +606,7 @@ impl WindowPortsMethods for Window {
}
let scroll_location = ScrollLocation::Delta(Vector2D::new(dx as f32, dy as f32));
let phase = winit_phase_to_touch_event_action(phase);
let phase = winit_phase_to_touch_event_type(phase);
// Send events
webview.notify_input_event(InputEvent::Wheel(WheelEvent { delta, point }));
@ -618,9 +618,10 @@ impl WindowPortsMethods for Window {
},
WindowEvent::Touch(touch) => {
webview.notify_input_event(InputEvent::Touch(TouchEvent {
action: winit_phase_to_touch_event_action(touch.phase),
event_type: winit_phase_to_touch_event_type(touch.phase),
id: TouchId(touch.id as i32),
point: Point2D::new(touch.location.x as f32, touch.location.y as f32),
action: TouchAction::NoAction,
}));
},
WindowEvent::PinchGesture { delta, .. } => {
@ -729,12 +730,12 @@ impl WindowMethods for Window {
}
}
fn winit_phase_to_touch_event_action(phase: TouchPhase) -> TouchEventAction {
fn winit_phase_to_touch_event_type(phase: TouchPhase) -> TouchEventType {
match phase {
TouchPhase::Started => TouchEventAction::Down,
TouchPhase::Moved => TouchEventAction::Move,
TouchPhase::Ended => TouchEventAction::Up,
TouchPhase::Cancelled => TouchEventAction::Cancel,
TouchPhase::Started => TouchEventType::Down,
TouchPhase::Moved => TouchEventType::Move,
TouchPhase::Ended => TouchEventType::Up,
TouchPhase::Cancelled => TouchEventType::Cancel,
}
}

View file

@ -23,7 +23,8 @@ use servo::{
InputMethodType, Key, KeyState, KeyboardEvent, LoadStatus, MediaSessionActionType,
MediaSessionEvent, MouseButton, MouseButtonAction, MouseButtonEvent, MouseMoveEvent,
NavigationRequest, PermissionRequest, PromptDefinition, PromptOrigin, PromptResult, Servo,
ServoDelegate, ServoError, TouchEvent, TouchEventAction, TouchId, WebView, WebViewDelegate,
ServoDelegate, ServoError, TouchAction, TouchEvent, TouchEventType, TouchId, WebView,
WebViewDelegate,
};
use url::Url;
@ -449,7 +450,7 @@ impl RunningAppState {
self.active_webview().notify_scroll_event(
scroll_location,
Point2D::new(x, y),
TouchEventAction::Down,
TouchEventType::Down,
);
self.perform_updates();
}
@ -463,7 +464,7 @@ impl RunningAppState {
self.active_webview().notify_scroll_event(
scroll_location,
Point2D::new(x, y),
TouchEventAction::Move,
TouchEventType::Move,
);
self.perform_updates();
}
@ -478,7 +479,7 @@ impl RunningAppState {
self.active_webview().notify_scroll_event(
scroll_location,
Point2D::new(x, y),
TouchEventAction::Up,
TouchEventType::Up,
);
self.perform_updates();
}
@ -487,9 +488,10 @@ impl RunningAppState {
pub fn touch_down(&self, x: f32, y: f32, pointer_id: i32) {
self.active_webview()
.notify_input_event(InputEvent::Touch(TouchEvent {
action: TouchEventAction::Down,
event_type: TouchEventType::Down,
id: TouchId(pointer_id),
point: Point2D::new(x, y),
action: TouchAction::NoAction,
}));
self.perform_updates();
}
@ -498,9 +500,10 @@ impl RunningAppState {
pub fn touch_move(&self, x: f32, y: f32, pointer_id: i32) {
self.active_webview()
.notify_input_event(InputEvent::Touch(TouchEvent {
action: TouchEventAction::Move,
event_type: TouchEventType::Move,
id: TouchId(pointer_id),
point: Point2D::new(x, y),
action: TouchAction::NoAction,
}));
self.perform_updates();
}
@ -509,9 +512,10 @@ impl RunningAppState {
pub fn touch_up(&self, x: f32, y: f32, pointer_id: i32) {
self.active_webview()
.notify_input_event(InputEvent::Touch(TouchEvent {
action: TouchEventAction::Up,
event_type: TouchEventType::Up,
id: TouchId(pointer_id),
point: Point2D::new(x, y),
action: TouchAction::NoAction,
}));
self.perform_updates();
}
@ -520,9 +524,10 @@ impl RunningAppState {
pub fn touch_cancel(&self, x: f32, y: f32, pointer_id: i32) {
self.active_webview()
.notify_input_event(InputEvent::Touch(TouchEvent {
action: TouchEventAction::Cancel,
event_type: TouchEventType::Cancel,
id: TouchId(pointer_id),
point: Point2D::new(x, y),
action: TouchAction::NoAction,
}));
self.perform_updates();
}