libservo: Expose a single InputEvent type and pass it to script (#35430)

This change exposes a single `InputEvent` type and now there is only a
single delegate method for this `WebViewDelegate::notify_input_event`.

- Clipboard events are now handled as `EditingAction` inpute events. In
  the future this can include things like "Select All", etc.

In addition, many parts of the dance to pass these events can now be
simplified due to this abstraction.

- All forwarded events are handled the same way in the `Constellation`,
  though they may carry an optional hit test (for events that have a
  `point`) which affects which `Pipeline` they are sent to.
- In the `ScriptThread` we now accept these `InputEvents` and use them
  everywhere. Now all "compositor events" are "input events".
- This allows removing several data structures which are no longer
  necessary.
- We no longer inform the embedder when an event was handled by a
  WebView as that was only important for a MDI feature that will
  no longer be so important the full-featured `WebView` API.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Mukilan Thiyagarajan <mukilan@igalia.com>
This commit is contained in:
Martin Robinson 2025-02-12 18:07:15 +01:00 committed by GitHub
parent b7b8619e87
commit 0908a47780
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 921 additions and 1200 deletions

View file

@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
pub mod input_events;
pub mod resources;
use std::fmt::{Debug, Error, Formatter};
@ -11,7 +12,7 @@ use base::id::{PipelineId, WebViewId};
use crossbeam_channel::Sender;
use http::{HeaderMap, Method, StatusCode};
use ipc_channel::ipc::IpcSender;
use keyboard_types::KeyboardEvent;
pub use keyboard_types::{KeyboardEvent, Modifiers};
use log::warn;
use malloc_size_of_derive::MallocSizeOf;
use num_derive::FromPrimitive;
@ -19,6 +20,8 @@ use serde::{Deserialize, Serialize};
use servo_url::ServoUrl;
use webrender_api::units::{DeviceIntPoint, DeviceIntRect, DeviceIntSize};
pub use crate::input_events::*;
/// A cursor for the window. This is different from a CSS cursor (see
/// `CursorKind`) in that it has no `Auto` value.
#[repr(u8)]
@ -250,29 +253,12 @@ pub enum EmbedderMsg {
OnDevtoolsStarted(Result<u16, ()>, String),
/// Ask the user to allow a devtools client to connect.
RequestDevtoolsConnection(IpcSender<AllowOrDeny>),
/// The given event was delivered to a pipeline in the given browser.
EventDelivered(WebViewId, CompositorEventVariant),
/// Request to play a haptic effect on a connected gamepad.
PlayGamepadHapticEffect(WebViewId, usize, GamepadHapticEffectType, IpcSender<bool>),
/// Request to stop a haptic effect on a connected gamepad.
StopGamepadHapticEffect(WebViewId, usize, IpcSender<bool>),
}
/// The variant of CompositorEvent that was delivered to a pipeline.
#[derive(Debug, Deserialize, Serialize)]
pub enum CompositorEventVariant {
ResizeEvent,
MouseButtonEvent,
MouseMoveEvent,
TouchEvent,
WheelEvent,
KeyboardEvent,
CompositionEvent,
IMEDismissedEvent,
GamepadEvent,
ClipboardEvent,
}
impl Debug for EmbedderMsg {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
match *self {
@ -312,7 +298,6 @@ impl Debug for EmbedderMsg {
EmbedderMsg::OnDevtoolsStarted(..) => write!(f, "OnDevtoolsStarted"),
EmbedderMsg::RequestDevtoolsConnection(..) => write!(f, "RequestDevtoolsConnection"),
EmbedderMsg::ShowContextMenu(..) => write!(f, "ShowContextMenu"),
EmbedderMsg::EventDelivered(..) => write!(f, "HitTestedEvent"),
EmbedderMsg::PlayGamepadHapticEffect(..) => write!(f, "PlayGamepadHapticEffect"),
EmbedderMsg::StopGamepadHapticEffect(..) => write!(f, "StopGamepadHapticEffect"),
}
@ -535,150 +520,6 @@ impl WebResourceResponse {
}
}
/// The type of input represented by a multi-touch event.
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum TouchEventType {
/// A new touch point came in contact with the screen.
Down,
/// An existing touch point changed location.
Move,
/// A touch point was removed from the screen.
Up,
/// The system stopped tracking a touch point.
Cancel,
}
/// An opaque identifier for a touch point.
///
/// <http://w3c.github.io/touch-events/#widl-Touch-identifier>
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct TouchId(pub i32);
#[derive(
Clone, Copy, Debug, Deserialize, Eq, Hash, MallocSizeOf, Ord, PartialEq, PartialOrd, Serialize,
)]
/// Index of gamepad in list of system's connected gamepads
pub struct GamepadIndex(pub usize);
#[derive(Clone, Debug, Deserialize, Serialize)]
/// The minimum and maximum values that can be reported for axis or button input from this gamepad
pub struct GamepadInputBounds {
/// Minimum and maximum axis values
pub axis_bounds: (f64, f64),
/// Minimum and maximum button values
pub button_bounds: (f64, f64),
}
#[derive(Clone, Debug, Deserialize, Serialize)]
/// The haptic effects supported by this gamepad
pub struct GamepadSupportedHapticEffects {
/// Gamepad support for dual rumble effects
pub supports_dual_rumble: bool,
/// Gamepad support for trigger rumble effects
pub supports_trigger_rumble: bool,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
/// The type of Gamepad event
pub enum GamepadEvent {
/// A new gamepad has been connected
/// <https://www.w3.org/TR/gamepad/#event-gamepadconnected>
Connected(
GamepadIndex,
String,
GamepadInputBounds,
GamepadSupportedHapticEffects,
),
/// An existing gamepad has been disconnected
/// <https://www.w3.org/TR/gamepad/#event-gamepaddisconnected>
Disconnected(GamepadIndex),
/// An existing gamepad has been updated
/// <https://www.w3.org/TR/gamepad/#receiving-inputs>
Updated(GamepadIndex, GamepadUpdateType),
}
#[derive(Clone, Debug, Deserialize, Serialize)]
/// The type of Gamepad input being updated
pub enum GamepadUpdateType {
/// Axis index and input value
/// <https://www.w3.org/TR/gamepad/#dfn-represents-a-standard-gamepad-axis>
Axis(usize, f64),
/// Button index and input value
/// <https://www.w3.org/TR/gamepad/#dfn-represents-a-standard-gamepad-button>
Button(usize, f64),
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum MouseButton {
/// The left mouse button.
Left = 1,
/// The right mouse button.
Right = 2,
/// The middle mouse button.
Middle = 4,
}
/// The types of mouse events
#[derive(Debug, Deserialize, MallocSizeOf, Serialize)]
pub enum MouseEventType {
/// Mouse button clicked
Click,
/// Mouse button down
MouseDown,
/// Mouse button up
MouseUp,
}
/// Mode to measure WheelDelta floats in
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
pub enum WheelMode {
/// Delta values are specified in pixels
DeltaPixel = 0x00,
/// Delta values are specified in lines
DeltaLine = 0x01,
/// Delta values are specified in pages
DeltaPage = 0x02,
}
/// The Wheel event deltas in every direction
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
pub struct WheelDelta {
/// Delta in the left/right direction
pub x: f64,
/// Delta in the up/down direction
pub y: f64,
/// Delta in the direction going into/out of the screen
pub z: f64,
/// Mode to measure the floats in
pub mode: WheelMode,
}
/// The mouse button involved in the event.
/// The types of clipboard events
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum ClipboardEventType {
/// Contents of the system clipboard are changed
Change,
/// Copy
Copy,
/// Cut
Cut,
/// Paste
Paste,
}
impl ClipboardEventType {
/// Convert to event name
pub fn as_str(&self) -> &str {
match *self {
ClipboardEventType::Change => "clipboardchange",
ClipboardEventType::Copy => "copy",
ClipboardEventType::Cut => "cut",
ClipboardEventType::Paste => "paste",
}
}
}
/// The direction of a history traversal
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum TraversalDirection {