servoshell: Move touch event simulation to servoshell (#39352)

This change removes the `DebugOption` (`-Z`) for touch event simulation
and moves the implementation of the feature to servoshell. The resaoning
for this is:

 - This is really a servoshell feature and can be implemented on top of
   the API. This moves more code out of the already too-complicated
   renderer.
 - I would like to consolidate `DebugOptions` into a `ServoLogOptions`
   to collect all options for configuring Servo logging. This requires
   moving away all of the non-logging options.
 - Eventually touch event simulation will be able to reuse the fling
   implementation from servoshell as we are actually simulating touch
   events sent to the `WebView`.

Testing: This changes a conditional feature that's used for manual
debugging.
It is difficult to write tests for this as there are no servoshell tests
that
verify input handling.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-09-23 09:00:11 +02:00 committed by GitHub
parent 70d54e86bd
commit b73538a676
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 142 additions and 116 deletions

View file

@ -79,6 +79,9 @@ pub struct Window {
/// the target of egui rendering and also where Servo rendering results are finally
/// blitted.
window_rendering_context: Rc<WindowRenderingContext>,
/// A helper that simulates touch events when the `--simulate-touch-events` flag
/// is enabled.
touch_event_simulator: Option<TouchEventSimulator>,
// Keep this as the last field of the struct to ensure that the rendering context is
// dropped first.
// (https://github.com/servo/servo/issues/36711)
@ -179,6 +182,9 @@ impl Window {
modifiers_state: Cell::new(ModifiersState::empty()),
toolbar_height: Cell::new(Default::default()),
window_rendering_context,
touch_event_simulator: servoshell_preferences
.simulate_touch_events
.then(Default::default),
rendering_context,
}
}
@ -275,7 +281,29 @@ impl Window {
}
/// Helper function to handle a click
fn handle_mouse(&self, webview: &WebView, button: MouseButton, action: ElementState) {
fn handle_mouse_button_event(
&self,
webview: &WebView,
button: MouseButton,
action: ElementState,
) {
// `point` can be outside viewport, such as at toolbar with negative y-coordinate.
let point = self.webview_relative_mouse_point.get();
if !webview.rect().contains(point) {
return;
}
if self
.touch_event_simulator
.as_ref()
.is_some_and(|touch_event_simulator| {
touch_event_simulator
.maybe_consume_move_button_event(webview, button, action, point)
})
{
return;
}
let mouse_button = match &button {
MouseButton::Left => ServoMouseButton::Left,
MouseButton::Right => ServoMouseButton::Right,
@ -285,11 +313,6 @@ impl Window {
MouseButton::Other(value) => ServoMouseButton::Other(*value),
};
let point = self.webview_relative_mouse_point.get();
// `point` can be outside viewport, such as at toolbar with negative y-coordinate.
if !webview.rect().contains(point) {
return;
}
let action = match action {
ElementState::Pressed => MouseButtonAction::Down,
ElementState::Released => MouseButtonAction::Up,
@ -302,6 +325,36 @@ impl Window {
)));
}
/// Helper function to handle mouse move events.
fn handle_mouse_move_event(&self, webview: &WebView, position: PhysicalPosition<f64>) {
let mut point = winit_position_to_euclid_point(position).to_f32();
point.y -= (self.toolbar_height() * self.hidpi_scale_factor()).0;
let previous_point = self.webview_relative_mouse_point.get();
self.webview_relative_mouse_point.set(point);
if !webview.rect().contains(point) {
if webview.rect().contains(previous_point) {
webview.notify_input_event(InputEvent::MouseLeftViewport(
MouseLeftViewportEvent::default(),
));
}
return;
}
if self
.touch_event_simulator
.as_ref()
.is_some_and(|touch_event_simulator| {
touch_event_simulator.maybe_consume_mouse_move_event(webview, point)
})
{
return;
}
webview.notify_input_event(InputEvent::MouseMove(MouseMoveEvent::new(point)));
}
/// Handle key events before sending them to Servo.
fn handle_intercepted_key_bindings(
&self,
@ -623,22 +676,10 @@ impl WindowPortsMethods for Window {
WindowEvent::KeyboardInput { event, .. } => self.handle_keyboard_input(state, event),
WindowEvent::ModifiersChanged(modifiers) => self.modifiers_state.set(modifiers.state()),
WindowEvent::MouseInput { state, button, .. } => {
self.handle_mouse(&webview, button, state);
self.handle_mouse_button_event(&webview, button, state);
},
WindowEvent::CursorMoved { position, .. } => {
let mut point = winit_position_to_euclid_point(position).to_f32();
point.y -= (self.toolbar_height() * self.hidpi_scale_factor()).0;
let previous_point = self.webview_relative_mouse_point.get();
if webview.rect().contains(point) {
webview.notify_input_event(InputEvent::MouseMove(MouseMoveEvent::new(point)));
} else if webview.rect().contains(previous_point) {
webview.notify_input_event(InputEvent::MouseLeftViewport(
MouseLeftViewportEvent::default(),
));
}
self.webview_relative_mouse_point.set(point);
self.handle_mouse_move_event(&webview, position);
},
WindowEvent::CursorLeft { .. } => {
if webview
@ -967,3 +1008,57 @@ impl XRWindowPose {
self.xr_rotation.set(rotation);
}
}
#[derive(Default)]
pub struct TouchEventSimulator {
pub left_mouse_button_down: Cell<bool>,
}
impl TouchEventSimulator {
fn maybe_consume_move_button_event(
&self,
webview: &WebView,
button: MouseButton,
action: ElementState,
point: Point2D<f32, DevicePixel>,
) -> bool {
if button != MouseButton::Left {
return false;
}
if action == ElementState::Pressed && !self.left_mouse_button_down.get() {
webview.notify_input_event(InputEvent::Touch(TouchEvent::new(
TouchEventType::Down,
TouchId(0),
point,
)));
self.left_mouse_button_down.set(true);
} else if action == ElementState::Released {
webview.notify_input_event(InputEvent::Touch(TouchEvent::new(
TouchEventType::Up,
TouchId(0),
point,
)));
self.left_mouse_button_down.set(false);
}
true
}
fn maybe_consume_mouse_move_event(
&self,
webview: &WebView,
point: Point2D<f32, DevicePixel>,
) -> bool {
if !self.left_mouse_button_down.get() {
return false;
}
webview.notify_input_event(InputEvent::Touch(TouchEvent::new(
TouchEventType::Move,
TouchId(0),
point,
)));
true
}
}

View file

@ -71,6 +71,8 @@ pub(crate) struct ServoShellPreferences {
/// An override for the screen resolution. This is useful for testing behavior on different screen sizes,
/// such as the screen of a mobile device.
pub screen_size_override: Option<Size2D<u32, DeviceIndependentPixel>>,
/// Whether or not to simulate touch events using mouse events.
pub simulate_touch_events: bool,
/// If not-None, the path to a file to output the default WebView's rendered output
/// after waiting for a stable image, this implies `Self::exit_after_load`.
pub output_image_path: Option<String>,
@ -82,15 +84,12 @@ pub(crate) struct ServoShellPreferences {
/// `None` to disable WebDriver or `Some` with a port number to start a server to listen to
/// remote WebDriver commands.
pub webdriver_port: Option<u16>,
/// Whether the CLI option to enable experimental prefs was present at startup.
pub experimental_prefs_enabled: bool,
/// Log filter given in the `log_filter` spec as a String, if any.
/// If a filter is passed, the logger should adjust accordingly.
#[cfg(target_env = "ohos")]
pub log_filter: Option<String>,
/// Log also to a file
#[cfg(target_env = "ohos")]
pub log_to_file: bool,
@ -106,6 +105,7 @@ impl Default for ServoShellPreferences {
initial_window_size: Size2D::new(1024, 740),
no_native_titlebar: true,
screen_size_override: None,
simulate_touch_events: false,
searchpage: "https://duckduckgo.com/html/?q=%s".into(),
tracing_filter: None,
url: None,
@ -505,6 +505,11 @@ struct CmdArgs {
parse(parse_resolution_string), fallback(None))]
screen_size_override: Option<Size2D<u32, DeviceIndependentPixel>>,
/// Use mouse events to simulate touch events. Left button presses will be converted to touch
/// and mouse movements while the left button is pressed will be converted to touch movements.
#[bpaf(long("simulate-touch-events"))]
simulate_touch_events: bool,
/// Define a custom filter for traces. Overrides `SERVO_TRACING` if set.
#[bpaf(long("tracing-filter"), argument("FILTER"))]
tracing_filter: Option<String>,
@ -663,6 +668,7 @@ pub(crate) fn parse_command_line_arguments(args: Vec<String>) -> ArgumentParsing
tracing_filter: cmd_args.tracing_filter,
initial_window_size: cmd_args.window_size.unwrap_or(default_window_size),
screen_size_override: cmd_args.screen_size_override,
simulate_touch_events: cmd_args.simulate_touch_events,
webdriver_port: cmd_args.webdriver_port,
output_image_path: cmd_args.output.map(|p| p.to_string_lossy().into_owned()),
exit_after_stable_image: cmd_args.exit,
@ -728,10 +734,6 @@ fn print_debug_options_usage(app: &str) {
"Usage: {} debug option,[options,...]\n\twhere options include\n\nOptions:",
app
);
print_option(
"convert-mouse-to-touch",
"Send touch events instead of mouse events",
);
print_option(
"disable-share-style-cache",
"Disable the style sharing cache.",