Send touch events to root pipeline, and allow forwarding to iframes.

Instead of letting the compositor try to find the correct scroll
layer for a touch event, switch touch events to work the same way
that mouse events do.

Touch events are now dispatched to the root pipeline, and then
forwarded to child iframes as required.
This commit is contained in:
Glenn Watson 2016-10-07 16:13:01 +10:00
parent b1c5b91820
commit 291af9d115
5 changed files with 118 additions and 71 deletions

View file

@ -37,7 +37,7 @@ use script_traits::{AnimationState, AnimationTickType, ConstellationControlMsg};
use script_traits::{ConstellationMsg, LayoutControlMsg, MouseButton, MouseEventType}; use script_traits::{ConstellationMsg, LayoutControlMsg, MouseButton, MouseEventType};
use script_traits::{StackingContextScrollState, TouchpadPressurePhase, TouchEventType}; use script_traits::{StackingContextScrollState, TouchpadPressurePhase, TouchEventType};
use script_traits::{TouchId, WindowSizeData}; use script_traits::{TouchId, WindowSizeData};
use script_traits::CompositorEvent::{MouseMoveEvent, MouseButtonEvent, TouchEvent}; use script_traits::CompositorEvent::{self, MouseMoveEvent, MouseButtonEvent, TouchEvent, TouchpadPressureEvent};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::collections::hash_map::Entry::{Occupied, Vacant}; use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::fs::File; use std::fs::File;
@ -1511,12 +1511,27 @@ impl<Window: WindowMethods> IOCompositor<Window> {
} }
} }
fn send_event_to_root_pipeline(&self, event: CompositorEvent) {
let root_pipeline_id = match self.get_root_pipeline_id() {
Some(root_pipeline_id) => root_pipeline_id,
None => return,
};
if let Some(pipeline) = self.pipeline(root_pipeline_id) {
let msg = ConstellationControlMsg::SendEvent(root_pipeline_id, event);
if let Err(e) = pipeline.script_chan.send(msg) {
warn!("Sending control event to script failed ({}).", e);
}
}
}
fn on_touch_down(&mut self, identifier: TouchId, point: TypedPoint2D<f32, DevicePixel>) { fn on_touch_down(&mut self, identifier: TouchId, point: TypedPoint2D<f32, DevicePixel>) {
self.touch_handler.on_touch_down(identifier, point); self.touch_handler.on_touch_down(identifier, point);
if let Some(result) = self.find_topmost_layer_at_point(point / self.scene.scale) { let dppx = self.page_zoom * self.device_pixels_per_screen_px();
result.layer.send_event(self, TouchEvent(TouchEventType::Down, identifier, let translated_point = (point / dppx).to_untyped();
result.point.to_untyped())); self.send_event_to_root_pipeline(TouchEvent(TouchEventType::Down,
} identifier,
translated_point));
} }
fn on_touch_move(&mut self, identifier: TouchId, point: TypedPoint2D<f32, DevicePixel>) { fn on_touch_move(&mut self, identifier: TouchId, point: TypedPoint2D<f32, DevicePixel>) {
@ -1539,20 +1554,22 @@ impl<Window: WindowMethods> IOCompositor<Window> {
self.composite_if_necessary_if_not_using_webrender(CompositingReason::Zoom); self.composite_if_necessary_if_not_using_webrender(CompositingReason::Zoom);
} }
TouchAction::DispatchEvent => { TouchAction::DispatchEvent => {
if let Some(result) = self.find_topmost_layer_at_point(point / self.scene.scale) { let dppx = self.page_zoom * self.device_pixels_per_screen_px();
result.layer.send_event(self, TouchEvent(TouchEventType::Move, identifier, let translated_point = (point / dppx).to_untyped();
result.point.to_untyped())); self.send_event_to_root_pipeline(TouchEvent(TouchEventType::Move,
} identifier,
translated_point));
} }
_ => {} _ => {}
} }
} }
fn on_touch_up(&mut self, identifier: TouchId, point: TypedPoint2D<f32, DevicePixel>) { fn on_touch_up(&mut self, identifier: TouchId, point: TypedPoint2D<f32, DevicePixel>) {
if let Some(result) = self.find_topmost_layer_at_point(point / self.scene.scale) { let dppx = self.page_zoom * self.device_pixels_per_screen_px();
result.layer.send_event(self, TouchEvent(TouchEventType::Up, identifier, let translated_point = (point / dppx).to_untyped();
result.point.to_untyped())); self.send_event_to_root_pipeline(TouchEvent(TouchEventType::Up,
} identifier,
translated_point));
if let TouchAction::Click = self.touch_handler.on_touch_up(identifier, point) { if let TouchAction::Click = self.touch_handler.on_touch_up(identifier, point) {
self.simulate_mouse_click(point); self.simulate_mouse_click(point);
} }
@ -1561,9 +1578,23 @@ impl<Window: WindowMethods> IOCompositor<Window> {
fn on_touch_cancel(&mut self, identifier: TouchId, point: TypedPoint2D<f32, DevicePixel>) { fn on_touch_cancel(&mut self, identifier: TouchId, point: TypedPoint2D<f32, DevicePixel>) {
// Send the event to script. // Send the event to script.
self.touch_handler.on_touch_cancel(identifier, point); self.touch_handler.on_touch_cancel(identifier, point);
if let Some(result) = self.find_topmost_layer_at_point(point / self.scene.scale) { let dppx = self.page_zoom * self.device_pixels_per_screen_px();
result.layer.send_event(self, TouchEvent(TouchEventType::Cancel, identifier, let translated_point = (point / dppx).to_untyped();
result.point.to_untyped())); self.send_event_to_root_pipeline(TouchEvent(TouchEventType::Cancel,
identifier,
translated_point));
}
fn on_touchpad_pressure_event(&self,
point: TypedPoint2D<f32, DevicePixel>,
pressure: f32,
phase: TouchpadPressurePhase) {
if let Some(true) = PREFS.get("dom.forcetouch.enabled").as_boolean() {
let dppx = self.page_zoom * self.device_pixels_per_screen_px();
let translated_point = (point / dppx).to_untyped();
self.send_event_to_root_pipeline(TouchpadPressureEvent(translated_point,
pressure,
phase));
} }
} }
@ -1877,16 +1908,6 @@ impl<Window: WindowMethods> IOCompositor<Window> {
} }
} }
fn on_touchpad_pressure_event(&self, cursor: TypedPoint2D<f32, DevicePixel>, pressure: f32,
phase: TouchpadPressurePhase) {
if let Some(true) = PREFS.get("dom.forcetouch.enabled").as_boolean() {
match self.find_topmost_layer_at_point(cursor / self.scene.scale) {
Some(result) => result.layer.send_touchpad_pressure_event(self, result.point, pressure, phase),
None => {},
}
}
}
fn on_key_event(&self, ch: Option<char>, key: Key, state: KeyState, modifiers: KeyModifiers) { fn on_key_event(&self, ch: Option<char>, key: Key, state: KeyState, modifiers: KeyModifiers) {
let msg = ConstellationMsg::KeyEvent(ch, key, state, modifiers); let msg = ConstellationMsg::KeyEvent(ch, key, state, modifiers);
if let Err(e) = self.constellation_chan.send(msg) { if let Err(e) = self.constellation_chan.send(msg) {

View file

@ -874,22 +874,10 @@ impl<Message, LTF, STF> Constellation<Message, LTF, STF>
debug!("constellation got focus message"); debug!("constellation got focus message");
self.handle_focus_msg(pipeline_id); self.handle_focus_msg(pipeline_id);
} }
FromScriptMsg::ForwardMouseButtonEvent(pipeline_id, event_type, button, point) => { FromScriptMsg::ForwardEvent(pipeline_id, event) => {
let event = CompositorEvent::MouseButtonEvent(event_type, button, point);
let msg = ConstellationControlMsg::SendEvent(pipeline_id, event); let msg = ConstellationControlMsg::SendEvent(pipeline_id, event);
let result = match self.pipelines.get(&pipeline_id) { let result = match self.pipelines.get(&pipeline_id) {
None => { debug!("Pipeline {:?} got mouse button event after closure.", pipeline_id); return; } None => { debug!("Pipeline {:?} got event after closure.", pipeline_id); return; }
Some(pipeline) => pipeline.script_chan.send(msg),
};
if let Err(e) = result {
self.handle_send_error(pipeline_id, e);
}
}
FromScriptMsg::ForwardMouseMoveEvent(pipeline_id, point) => {
let event = CompositorEvent::MouseMoveEvent(Some(point));
let msg = ConstellationControlMsg::SendEvent(pipeline_id, event);
let result = match self.pipelines.get(&pipeline_id) {
None => { debug!("Pipeline {:?} got mouse move event after closure.", pipeline_id); return; }
Some(pipeline) => pipeline.script_chan.send(msg), Some(pipeline) => pipeline.script_chan.send(msg),
}; };
if let Err(e) = result { if let Err(e) = result {

View file

@ -106,7 +106,7 @@ use origin::Origin;
use parse::{MutNullableParserField, ParserRef, ParserRoot}; use parse::{MutNullableParserField, ParserRef, ParserRoot};
use script_layout_interface::message::{Msg, ReflowQueryType}; use script_layout_interface::message::{Msg, ReflowQueryType};
use script_thread::{MainThreadScriptMsg, Runnable}; use script_thread::{MainThreadScriptMsg, Runnable};
use script_traits::{AnimationState, MouseButton, MouseEventType, MozBrowserEvent}; use script_traits::{AnimationState, CompositorEvent, MouseButton, MouseEventType, MozBrowserEvent};
use script_traits::{ScriptMsg as ConstellationMsg, TouchpadPressurePhase}; use script_traits::{ScriptMsg as ConstellationMsg, TouchpadPressurePhase};
use script_traits::{TouchEventType, TouchId}; use script_traits::{TouchEventType, TouchId};
use script_traits::UntrustedNodeAddress; use script_traits::UntrustedNodeAddress;
@ -133,6 +133,11 @@ use url::Url;
use url::percent_encoding::percent_decode; use url::percent_encoding::percent_decode;
use util::prefs::PREFS; use util::prefs::PREFS;
pub enum TouchEventResult {
Processed(bool),
Forwarded,
}
#[derive(JSTraceable, PartialEq, HeapSizeOf)] #[derive(JSTraceable, PartialEq, HeapSizeOf)]
pub enum IsHTMLDocument { pub enum IsHTMLDocument {
HTMLDocument, HTMLDocument,
@ -723,9 +728,8 @@ impl Document {
let child_origin = Point2D::new(rect.X() as f32, rect.Y() as f32); let child_origin = Point2D::new(rect.X() as f32, rect.Y() as f32);
let child_point = client_point - child_origin; let child_point = client_point - child_origin;
let event = ConstellationMsg::ForwardMouseButtonEvent(pipeline_id, let event = CompositorEvent::MouseButtonEvent(mouse_event_type, button, child_point);
mouse_event_type, let event = ConstellationMsg::ForwardEvent(pipeline_id, event);
button, child_point);
self.window.upcast::<GlobalScope>().constellation_chan().send(event).unwrap(); self.window.upcast::<GlobalScope>().constellation_chan().send(event).unwrap();
} }
return; return;
@ -853,14 +857,6 @@ impl Document {
client_point: Point2D<f32>, client_point: Point2D<f32>,
pressure: f32, pressure: f32,
phase_now: TouchpadPressurePhase) { phase_now: TouchpadPressurePhase) {
let phase_before = self.touchpad_pressure_phase.get();
self.touchpad_pressure_phase.set(phase_now);
if phase_before == TouchpadPressurePhase::BeforeClick &&
phase_now == TouchpadPressurePhase::BeforeClick {
return;
}
let node = match self.window.hit_test_query(client_point, false) { let node = match self.window.hit_test_query(client_point, false) {
Some(node_address) => node::from_untrusted_node_address(js_runtime, node_address), Some(node_address) => node::from_untrusted_node_address(js_runtime, node_address),
None => return None => return
@ -877,6 +873,30 @@ impl Document {
}, },
}; };
// If the target is an iframe, forward the event to the child document.
if let Some(iframe) = el.downcast::<HTMLIFrameElement>() {
if let Some(pipeline_id) = iframe.pipeline_id() {
let rect = iframe.upcast::<Element>().GetBoundingClientRect();
let child_origin = Point2D::new(rect.X() as f32, rect.Y() as f32);
let child_point = client_point - child_origin;
let event = CompositorEvent::TouchpadPressureEvent(child_point,
pressure,
phase_now);
let event = ConstellationMsg::ForwardEvent(pipeline_id, event);
self.window.upcast::<GlobalScope>().constellation_chan().send(event).unwrap();
}
return;
}
let phase_before = self.touchpad_pressure_phase.get();
self.touchpad_pressure_phase.set(phase_now);
if phase_before == TouchpadPressurePhase::BeforeClick &&
phase_now == TouchpadPressurePhase::BeforeClick {
return;
}
let node = el.upcast::<Node>(); let node = el.upcast::<Node>();
let target = node.upcast(); let target = node.upcast();
@ -963,7 +983,8 @@ impl Document {
let child_origin = Point2D::new(rect.X() as f32, rect.Y() as f32); let child_origin = Point2D::new(rect.X() as f32, rect.Y() as f32);
let child_point = client_point - child_origin; let child_point = client_point - child_origin;
let event = ConstellationMsg::ForwardMouseMoveEvent(pipeline_id, child_point); let event = CompositorEvent::MouseMoveEvent(Some(child_point));
let event = ConstellationMsg::ForwardEvent(pipeline_id, event);
self.window.upcast::<GlobalScope>().constellation_chan().send(event).unwrap(); self.window.upcast::<GlobalScope>().constellation_chan().send(event).unwrap();
} }
return; return;
@ -1032,9 +1053,11 @@ impl Document {
pub fn handle_touch_event(&self, pub fn handle_touch_event(&self,
js_runtime: *mut JSRuntime, js_runtime: *mut JSRuntime,
event_type: TouchEventType, event_type: TouchEventType,
TouchId(identifier): TouchId, touch_id: TouchId,
point: Point2D<f32>) point: Point2D<f32>)
-> bool { -> TouchEventResult {
let TouchId(identifier) = touch_id;
let event_name = match event_type { let event_name = match event_type {
TouchEventType::Down => "touchstart", TouchEventType::Down => "touchstart",
TouchEventType::Move => "touchmove", TouchEventType::Move => "touchmove",
@ -1044,7 +1067,7 @@ impl Document {
let node = match self.window.hit_test_query(point, false) { let node = match self.window.hit_test_query(point, false) {
Some(node_address) => node::from_untrusted_node_address(js_runtime, node_address), Some(node_address) => node::from_untrusted_node_address(js_runtime, node_address),
None => return false, None => return TouchEventResult::Processed(false),
}; };
let el = match node.downcast::<Element>() { let el = match node.downcast::<Element>() {
Some(el) => Root::from_ref(el), Some(el) => Root::from_ref(el),
@ -1052,10 +1075,25 @@ impl Document {
let parent = node.GetParentNode(); let parent = node.GetParentNode();
match parent.and_then(Root::downcast::<Element>) { match parent.and_then(Root::downcast::<Element>) {
Some(parent) => parent, Some(parent) => parent,
None => return false, None => return TouchEventResult::Processed(false),
} }
}, },
}; };
// If the target is an iframe, forward the event to the child document.
if let Some(iframe) = el.downcast::<HTMLIFrameElement>() {
if let Some(pipeline_id) = iframe.pipeline_id() {
let rect = iframe.upcast::<Element>().GetBoundingClientRect();
let child_origin = Point2D::new(rect.X() as f32, rect.Y() as f32);
let child_point = point - child_origin;
let event = CompositorEvent::TouchEvent(event_type, touch_id, child_point);
let event = ConstellationMsg::ForwardEvent(pipeline_id, event);
self.window.upcast::<GlobalScope>().constellation_chan().send(event).unwrap();
}
return TouchEventResult::Forwarded;
}
let target = Root::upcast::<EventTarget>(el); let target = Root::upcast::<EventTarget>(el);
let window = &*self.window; let window = &*self.window;
@ -1132,8 +1170,8 @@ impl Document {
ReflowReason::MouseEvent); ReflowReason::MouseEvent);
match result { match result {
EventStatus::Canceled => false, EventStatus::Canceled => TouchEventResult::Processed(false),
EventStatus::NotCanceled => true EventStatus::NotCanceled => TouchEventResult::Processed(true),
} }
} }

View file

@ -36,7 +36,7 @@ use dom::bindings::str::DOMString;
use dom::bindings::trace::JSTraceable; use dom::bindings::trace::JSTraceable;
use dom::bindings::utils::WRAP_CALLBACKS; use dom::bindings::utils::WRAP_CALLBACKS;
use dom::browsingcontext::BrowsingContext; use dom::browsingcontext::BrowsingContext;
use dom::document::{Document, DocumentProgressHandler, DocumentSource, FocusType, IsHTMLDocument}; use dom::document::{Document, DocumentProgressHandler, DocumentSource, FocusType, IsHTMLDocument, TouchEventResult};
use dom::element::Element; use dom::element::Element;
use dom::event::{Event, EventBubbles, EventCancelable}; use dom::event::{Event, EventBubbles, EventCancelable};
use dom::globalscope::GlobalScope; use dom::globalscope::GlobalScope;
@ -1934,9 +1934,9 @@ impl ScriptThread {
} }
} }
TouchEvent(event_type, identifier, point) => { TouchEvent(event_type, identifier, point) => {
let handled = self.handle_touch_event(pipeline_id, event_type, identifier, point); let touch_result = self.handle_touch_event(pipeline_id, event_type, identifier, point);
match event_type { match (event_type, touch_result) {
TouchEventType::Down => { (TouchEventType::Down, TouchEventResult::Processed(handled)) => {
let result = if handled { let result = if handled {
// TODO: Wait to see if preventDefault is called on the first touchmove event. // TODO: Wait to see if preventDefault is called on the first touchmove event.
EventResult::DefaultAllowed EventResult::DefaultAllowed
@ -1987,10 +1987,13 @@ impl ScriptThread {
event_type: TouchEventType, event_type: TouchEventType,
identifier: TouchId, identifier: TouchId,
point: Point2D<f32>) point: Point2D<f32>)
-> bool { -> TouchEventResult {
let document = match self.root_browsing_context().find(pipeline_id) { let document = match self.root_browsing_context().find(pipeline_id) {
Some(browsing_context) => browsing_context.active_document(), Some(browsing_context) => browsing_context.active_document(),
None => { warn!("Message sent to closed pipeline {}.", pipeline_id); return true }, None => {
warn!("Message sent to closed pipeline {}.", pipeline_id);
return TouchEventResult::Processed(true)
},
}; };
document.handle_touch_event(self.js_runtime.rt(), event_type, identifier, point) document.handle_touch_event(self.js_runtime.rt(), event_type, identifier, point)
} }

View file

@ -3,10 +3,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use AnimationState; use AnimationState;
use CompositorEvent;
use DocumentState; use DocumentState;
use IFrameLoadInfo; use IFrameLoadInfo;
use MouseButton;
use MouseEventType;
use MozBrowserEvent; use MozBrowserEvent;
use WorkerGlobalScopeInit; use WorkerGlobalScopeInit;
use WorkerScriptLoadOrigin; use WorkerScriptLoadOrigin;
@ -72,10 +71,8 @@ pub enum ScriptMsg {
IpcSender<Result<(IpcSender<CanvasMsg>, GLLimits), String>>), IpcSender<Result<(IpcSender<CanvasMsg>, GLLimits), String>>),
/// Notifies the constellation that this frame has received focus. /// Notifies the constellation that this frame has received focus.
Focus(PipelineId), Focus(PipelineId),
/// Re-send a mouse button event that was sent to the parent window. /// Forward an event that was sent to the parent window.
ForwardMouseButtonEvent(PipelineId, MouseEventType, MouseButton, Point2D<f32>), ForwardEvent(PipelineId, CompositorEvent),
/// Re-send a mouse move event that was sent to the parent window.
ForwardMouseMoveEvent(PipelineId, Point2D<f32>),
/// Requests that the constellation retrieve the current contents of the clipboard /// Requests that the constellation retrieve the current contents of the clipboard
GetClipboardContents(IpcSender<String>), GetClipboardContents(IpcSender<String>),
/// <head> tag finished parsing /// <head> tag finished parsing