Implement user interaction task source

This commit is contained in:
Keith Yeung 2016-03-12 17:58:52 -05:00
parent 12a96f7194
commit cc049515dc
8 changed files with 71 additions and 59 deletions

View file

@ -173,15 +173,6 @@ impl<'a> GlobalRef<'a> {
} }
} }
/// `ScriptChan` used to send messages to the event loop of this global's
/// thread.
pub fn user_interaction_task_source(&self) -> Box<ScriptChan + Send> {
match *self {
GlobalRef::Window(ref window) => window.user_interaction_task_source(),
GlobalRef::Worker(ref worker) => worker.script_chan(),
}
}
/// `ScriptChan` used to send messages to the event loop of this global's /// `ScriptChan` used to send messages to the event loop of this global's
/// thread. /// thread.
pub fn networking_task_source(&self) -> Box<ScriptChan + Send> { pub fn networking_task_source(&self) -> Box<ScriptChan + Send> {

View file

@ -12,10 +12,8 @@ use dom::bindings::codegen::Bindings::HTMLInputElementBinding;
use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods; use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods;
use dom::bindings::codegen::Bindings::KeyboardEventBinding::KeyboardEventMethods; use dom::bindings::codegen::Bindings::KeyboardEventBinding::KeyboardEventMethods;
use dom::bindings::error::{Error, ErrorResult}; use dom::bindings::error::{Error, ErrorResult};
use dom::bindings::global::GlobalRef;
use dom::bindings::inheritance::Castable; use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, LayoutJS, Root, RootedReference}; use dom::bindings::js::{JS, LayoutJS, Root, RootedReference};
use dom::bindings::refcounted::Trusted;
use dom::document::Document; use dom::document::Document;
use dom::element::{AttributeMutation, Element, RawLayoutElementHelpers, LayoutElementHelpers}; use dom::element::{AttributeMutation, Element, RawLayoutElementHelpers, LayoutElementHelpers};
use dom::event::{Event, EventBubbles, EventCancelable}; use dom::event::{Event, EventBubbles, EventCancelable};
@ -31,9 +29,7 @@ use dom::nodelist::NodeList;
use dom::validation::Validatable; use dom::validation::Validatable;
use dom::virtualmethods::VirtualMethods; use dom::virtualmethods::VirtualMethods;
use msg::constellation_msg::ConstellationChan; use msg::constellation_msg::ConstellationChan;
use script_runtime::CommonScriptMsg; use script_runtime::ScriptChan;
use script_runtime::ScriptThreadEventCategory::InputEvent;
use script_thread::Runnable;
use script_traits::ScriptMsg as ConstellationMsg; use script_traits::ScriptMsg as ConstellationMsg;
use std::borrow::ToOwned; use std::borrow::ToOwned;
use std::cell::Cell; use std::cell::Cell;
@ -919,7 +915,13 @@ impl VirtualMethods for HTMLInputElement {
self.value_changed.set(true); self.value_changed.set(true);
if event.IsTrusted() { if event.IsTrusted() {
ChangeEventRunnable::send(self.upcast::<Node>()); let window = window_from_node(self);
let task_source = window.user_interaction_task_source();
let _ = task_source.queue_event(
&self.upcast(),
atom!("input"),
EventBubbles::Bubbles,
EventCancelable::NotCancelable);
} }
self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage); self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);
@ -1148,31 +1150,3 @@ impl Activatable for HTMLInputElement {
} }
} }
} }
pub struct ChangeEventRunnable {
element: Trusted<Node>,
}
impl ChangeEventRunnable {
pub fn send(node: &Node) {
let handler = Trusted::new(node);
let dispatcher = ChangeEventRunnable {
element: handler,
};
let chan = window_from_node(node).user_interaction_task_source();
let _ = chan.send(CommonScriptMsg::RunnableMsg(InputEvent, box dispatcher));
}
}
impl Runnable for ChangeEventRunnable {
fn handler(self: Box<ChangeEventRunnable>) {
let target = self.element.root();
let window = window_from_node(target.r());
let window = window.r();
let event = Event::new(GlobalRef::Window(window),
atom!("input"),
EventBubbles::Bubbles,
EventCancelable::NotCancelable);
target.upcast::<EventTarget>().dispatch_event(&event);
}
}

View file

@ -14,18 +14,18 @@ use dom::bindings::reflector::{Reflectable};
use dom::document::Document; use dom::document::Document;
use dom::element::RawLayoutElementHelpers; use dom::element::RawLayoutElementHelpers;
use dom::element::{AttributeMutation, Element}; use dom::element::{AttributeMutation, Element};
use dom::event::{Event}; use dom::event::{Event, EventBubbles, EventCancelable};
use dom::htmlelement::HTMLElement; use dom::htmlelement::HTMLElement;
use dom::htmlfieldsetelement::HTMLFieldSetElement; use dom::htmlfieldsetelement::HTMLFieldSetElement;
use dom::htmlformelement::{FormControl, HTMLFormElement}; use dom::htmlformelement::{FormControl, HTMLFormElement};
use dom::htmlinputelement::ChangeEventRunnable;
use dom::keyboardevent::KeyboardEvent; use dom::keyboardevent::KeyboardEvent;
use dom::node::{ChildrenMutation, Node, NodeDamage, UnbindContext}; use dom::node::{ChildrenMutation, Node, NodeDamage, UnbindContext};
use dom::node::{document_from_node}; use dom::node::{document_from_node, window_from_node};
use dom::nodelist::NodeList; use dom::nodelist::NodeList;
use dom::validation::Validatable; use dom::validation::Validatable;
use dom::virtualmethods::VirtualMethods; use dom::virtualmethods::VirtualMethods;
use msg::constellation_msg::ConstellationChan; use msg::constellation_msg::ConstellationChan;
use script_runtime::ScriptChan;
use script_traits::ScriptMsg as ConstellationMsg; use script_traits::ScriptMsg as ConstellationMsg;
use std::cell::Cell; use std::cell::Cell;
use std::ops::Range; use std::ops::Range;
@ -373,7 +373,13 @@ impl VirtualMethods for HTMLTextAreaElement {
self.value_changed.set(true); self.value_changed.set(true);
if event.IsTrusted() { if event.IsTrusted() {
ChangeEventRunnable::send(self.upcast::<Node>()); let window = window_from_node(self);
let task_source = window.user_interaction_task_source();
let _ = task_source.queue_event(
&self.upcast(),
atom!("input"),
EventBubbles::Bubbles,
EventCancelable::NotCancelable);
} }
self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage); self.upcast::<Node>().dirty(NodeDamage::OtherNodeDamage);

View file

@ -277,7 +277,7 @@ impl Window {
self.dom_manipulation_task_source.clone() self.dom_manipulation_task_source.clone()
} }
pub fn user_interaction_task_source(&self) -> Box<ScriptChan + Send> { pub fn user_interaction_task_source(&self) -> UserInteractionTaskSource {
self.user_interaction_task_source.clone() self.user_interaction_task_source.clone()
} }

View file

@ -100,7 +100,7 @@ use task_source::dom_manipulation::{DOMManipulationTaskSource, DOMManipulationTa
use task_source::file_reading::FileReadingTaskSource; use task_source::file_reading::FileReadingTaskSource;
use task_source::history_traversal::HistoryTraversalTaskSource; use task_source::history_traversal::HistoryTraversalTaskSource;
use task_source::networking::NetworkingTaskSource; use task_source::networking::NetworkingTaskSource;
use task_source::user_interaction::UserInteractionTaskSource; use task_source::user_interaction::{UserInteractionTaskSource, UserInteractionTask};
use time::Tm; use time::Tm;
use url::{Url, Position}; use url::{Url, Position};
use util::opts; use util::opts;
@ -222,6 +222,8 @@ pub enum MainThreadScriptMsg {
Navigate(PipelineId, LoadData), Navigate(PipelineId, LoadData),
/// Tasks that originate from the DOM manipulation task source /// Tasks that originate from the DOM manipulation task source
DOMManipulation(DOMManipulationTask), DOMManipulation(DOMManipulationTask),
/// Tasks that originate from the user interaction task source
UserInteraction(UserInteractionTask),
} }
impl OpaqueSender<CommonScriptMsg> for Box<ScriptChan + Send> { impl OpaqueSender<CommonScriptMsg> for Box<ScriptChan + Send> {
@ -934,6 +936,8 @@ impl ScriptThread {
self.collect_reports(reports_chan), self.collect_reports(reports_chan),
MainThreadScriptMsg::DOMManipulation(task) => MainThreadScriptMsg::DOMManipulation(task) =>
task.handle_task(self), task.handle_task(self),
MainThreadScriptMsg::UserInteraction(task) =>
task.handle_task(),
} }
} }

View file

@ -18,8 +18,10 @@ impl TaskSource<DOMManipulationTask> for DOMManipulationTaskSource {
fn queue(&self, msg: DOMManipulationTask) -> Result<(), ()> { fn queue(&self, msg: DOMManipulationTask) -> Result<(), ()> {
self.0.send(MainThreadScriptMsg::DOMManipulation(msg)).map_err(|_| ()) self.0.send(MainThreadScriptMsg::DOMManipulation(msg)).map_err(|_| ())
} }
}
fn clone(&self) -> Box<TaskSource<DOMManipulationTask> + Send> { impl DOMManipulationTaskSource {
pub fn clone(&self) -> Box<TaskSource<DOMManipulationTask> + Send> {
box DOMManipulationTaskSource((&self.0).clone()) box DOMManipulationTaskSource((&self.0).clone())
} }
} }

View file

@ -12,5 +12,4 @@ use std::result::Result;
pub trait TaskSource<T> { pub trait TaskSource<T> {
fn queue(&self, msg: T) -> Result<(), ()>; fn queue(&self, msg: T) -> Result<(), ()>;
fn clone(&self) -> Box<TaskSource<T> + Send>;
} }

View file

@ -2,19 +2,55 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* 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 script_runtime::{CommonScriptMsg, ScriptChan}; use dom::bindings::refcounted::Trusted;
use dom::event::{EventBubbles, EventCancelable};
use dom::eventtarget::EventTarget;
use script_runtime::ScriptChan;
use script_thread::MainThreadScriptMsg; use script_thread::MainThreadScriptMsg;
use std::result::Result;
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
use string_cache::Atom;
use task_source::TaskSource;
#[derive(JSTraceable)] #[derive(JSTraceable)]
pub struct UserInteractionTaskSource(pub Sender<MainThreadScriptMsg>); pub struct UserInteractionTaskSource(pub Sender<MainThreadScriptMsg>);
impl ScriptChan for UserInteractionTaskSource { impl TaskSource<UserInteractionTask> for UserInteractionTaskSource {
fn send(&self, msg: CommonScriptMsg) -> Result<(), ()> { fn queue(&self, msg: UserInteractionTask) -> Result<(), ()> {
self.0.send(MainThreadScriptMsg::Common(msg)).map_err(|_| ()) self.0.send(MainThreadScriptMsg::UserInteraction(msg)).map_err(|_| ())
} }
}
fn clone(&self) -> Box<ScriptChan + Send> {
box UserInteractionTaskSource((&self.0).clone()) impl UserInteractionTaskSource {
pub fn queue_event(&self,
target: &EventTarget,
name: Atom,
bubbles: EventBubbles,
cancelable: EventCancelable) {
let target = Trusted::new(target);
let _ = self.0.send(MainThreadScriptMsg::UserInteraction(UserInteractionTask::FireEvent(
target, name, bubbles, cancelable)));
}
pub fn clone(&self) -> UserInteractionTaskSource {
UserInteractionTaskSource((&self.0).clone())
}
}
pub enum UserInteractionTask {
// https://dom.spec.whatwg.org/#concept-event-fire
FireEvent(Trusted<EventTarget>, Atom, EventBubbles, EventCancelable),
}
impl UserInteractionTask {
pub fn handle_task(self) {
use self::UserInteractionTask::*;
match self {
FireEvent(element, name, bubbles, cancelable) => {
let target = element.root();
target.fire_event(&*name, bubbles, cancelable);
}
}
} }
} }