Implement WheelEvent Interface

Note: The WheelEvent interface supports rotation in all 3 spatial
dimensions. This implementation only supports two due to limitations
in the Glutin compositor.

The wheelevent interface is a dom interface that triggers for any
attached device that can rotate in one or more spatial dimensions.
Traditionally this is the mouse wheel though other devices could be
used as well. E.g. the trackball on a trackball mouse.
This commit is contained in:
Robert Snakard 2019-02-24 21:24:36 +00:00 committed by Josh Matthews
parent 8f11b52d9a
commit 35bca991ad
16 changed files with 347 additions and 146 deletions

View file

@ -23,7 +23,7 @@ use keyboard_types::{Key, KeyState, KeyboardEvent};
use servo::compositing::windowing::{AnimationState, MouseWindowEvent, WindowEvent};
use servo::compositing::windowing::{EmbedderCoordinates, WindowMethods};
use servo::embedder_traits::Cursor;
use servo::script_traits::TouchEventType;
use servo::script_traits::{TouchEventType, WheelMode, WheelDelta};
use servo::servo_config::opts;
use servo::servo_geometry::DeviceIndependentPixel;
use servo::style_traits::DevicePixel;
@ -400,14 +400,22 @@ impl WindowPortsMethods for Window {
)));
},
glutin::WindowEvent::MouseWheel { delta, phase, .. } => {
let (mut dx, mut dy) = match delta {
MouseScrollDelta::LineDelta(dx, dy) => (dx, dy * LINE_HEIGHT),
let (mut dx, mut dy, mode) = match delta {
MouseScrollDelta::LineDelta(dx, dy) => (dx as f64, (dy * LINE_HEIGHT) as f64,
WheelMode::DeltaLine),
MouseScrollDelta::PixelDelta(position) => {
let position =
position.to_physical(self.device_hidpi_factor().get() as f64);
(position.x as f32, position.y as f32)
(position.x as f64, position.y as f64, WheelMode::DeltaPixel)
},
};
// Create wheel event before snapping to the major axis of movement
let wheel_delta = WheelDelta { x: dx, y: dy, z: 0.0, mode };
let pos = self.mouse_pos.get();
let position = TypedPoint2D::new(pos.x as f32, pos.y as f32);
let wheel_event = WindowEvent::Wheel(wheel_delta, position);
// Scroll events snap to the major axis of movement, with vertical
// preferred over horizontal.
if dy.abs() >= dx.abs() {
@ -416,10 +424,13 @@ impl WindowPortsMethods for Window {
dy = 0.0;
}
let scroll_location = ScrollLocation::Delta(TypedVector2D::new(dx, dy));
let scroll_location = ScrollLocation::Delta(TypedVector2D::new(dx as f32, dy as f32));
let phase = winit_phase_to_touch_event_type(phase);
let event = WindowEvent::Scroll(scroll_location, self.mouse_pos.get(), phase);
self.event_queue.borrow_mut().push(event);
let scroll_event = WindowEvent::Scroll(scroll_location, self.mouse_pos.get(), phase);
// Send events
self.event_queue.borrow_mut().push(wheel_event);
self.event_queue.borrow_mut().push(scroll_event);
},
glutin::WindowEvent::Touch(touch) => {
use servo::script_traits::TouchId;