diff options
author | Matt Brubeck <mbrubeck@limpet.net> | 2015-08-10 20:55:19 -0700 |
---|---|---|
committer | Matt Brubeck <mbrubeck@limpet.net> | 2015-10-22 10:35:11 -0700 |
commit | 4ed15a8853fc49d0940ed24d939fd0c407ee80a9 (patch) | |
tree | 87bb4440ffbbe3caf77c72e2338440129b04e225 /components/script/dom | |
parent | 79f300f0387ad8218d06511c429d071cdef0193c (diff) | |
download | servo-4ed15a8853fc49d0940ed24d939fd0c407ee80a9.tar.gz servo-4ed15a8853fc49d0940ed24d939fd0c407ee80a9.zip |
Add bindings for TouchEvent DOM interfaces
Diffstat (limited to 'components/script/dom')
-rw-r--r-- | components/script/dom/bindings/num.rs | 2 | ||||
-rw-r--r-- | components/script/dom/mod.rs | 3 | ||||
-rw-r--r-- | components/script/dom/touch.rs | 80 | ||||
-rw-r--r-- | components/script/dom/touchevent.rs | 117 | ||||
-rw-r--r-- | components/script/dom/touchlist.rs | 50 | ||||
-rw-r--r-- | components/script/dom/webidls/Touch.webidl | 21 | ||||
-rw-r--r-- | components/script/dom/webidls/TouchEvent.webidl | 16 | ||||
-rw-r--r-- | components/script/dom/webidls/TouchList.webidl | 11 |
8 files changed, 299 insertions, 1 deletions
diff --git a/components/script/dom/bindings/num.rs b/components/script/dom/bindings/num.rs index 1169ccdec7f..ed5d78cdb6c 100644 --- a/components/script/dom/bindings/num.rs +++ b/components/script/dom/bindings/num.rs @@ -9,7 +9,7 @@ use num::Float; use std::ops::Deref; /// Encapsulates the IDL restricted float type. -#[derive(JSTraceable, Clone, Eq, PartialEq)] +#[derive(JSTraceable, Clone, Copy, Eq, PartialEq)] pub struct Finite<T: Float>(T); unsafe impl<T: Float> Zeroable for Finite<T> {} diff --git a/components/script/dom/mod.rs b/components/script/dom/mod.rs index ae4524cd05e..b248e2dac1c 100644 --- a/components/script/dom/mod.rs +++ b/components/script/dom/mod.rs @@ -345,6 +345,9 @@ pub mod testbindingproxy; pub mod text; pub mod textdecoder; pub mod textencoder; +pub mod touch; +pub mod touchevent; +pub mod touchlist; pub mod treewalker; pub mod uievent; pub mod url; diff --git a/components/script/dom/touch.rs b/components/script/dom/touch.rs new file mode 100644 index 00000000000..7fc9f42e23d --- /dev/null +++ b/components/script/dom/touch.rs @@ -0,0 +1,80 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +use dom::bindings::codegen::Bindings::TouchBinding; +use dom::bindings::codegen::Bindings::TouchBinding::TouchMethods; +use dom::bindings::global::GlobalRef; +use dom::bindings::js::{JS, MutHeap, Root}; +use dom::bindings::num::Finite; +use dom::bindings::utils::{Reflector, reflect_dom_object}; +use dom::eventtarget::EventTarget; +use dom::window::Window; + +#[dom_struct] +pub struct Touch { + reflector_: Reflector, + identifier: i32, + target: MutHeap<JS<EventTarget>>, + screen_x: f64, + screen_y: f64, + client_x: f64, + client_y: f64, +} + +impl Touch { + fn new_inherited(identifier: i32, target: &EventTarget, + screen_x: Finite<f64>, screen_y: Finite<f64>, + client_x: Finite<f64>, client_y: Finite<f64>) -> Touch { + Touch { + reflector_: Reflector::new(), + identifier: identifier, + target: MutHeap::new(target), + screen_x: *screen_x, + screen_y: *screen_y, + client_x: *client_x, + client_y: *client_y, + } + } + + pub fn new(window: &Window, identifier: i32, target: &EventTarget, + screen_x: Finite<f64>, screen_y: Finite<f64>, + client_x: Finite<f64>, client_y: Finite<f64>) -> Root<Touch> { + reflect_dom_object(box Touch::new_inherited(identifier, target, + screen_x, screen_y, + client_x, client_y), + GlobalRef::Window(window), TouchBinding::Wrap) + } +} + +impl TouchMethods for Touch { + /// https://w3c.github.io/touch-events/#widl-Touch-identifier + fn Identifier(&self) -> i32 { + self.identifier + } + + /// https://w3c.github.io/touch-events/#widl-Touch-target + fn Target(&self) -> Root<EventTarget> { + self.target.get() + } + + /// https://w3c.github.io/touch-events/#widl-Touch-screenX + fn ScreenX(&self) -> Finite<f64> { + Finite::wrap(self.screen_x) + } + + /// https://w3c.github.io/touch-events/#widl-Touch-screenY + fn ScreenY(&self) -> Finite<f64> { + Finite::wrap(self.screen_y) + } + + /// https://w3c.github.io/touch-events/#widl-Touch-clientX + fn ClientX(&self) -> Finite<f64> { + Finite::wrap(self.client_x) + } + + /// https://w3c.github.io/touch-events/#widl-Touch-clientY + fn ClientY(&self) -> Finite<f64> { + Finite::wrap(self.client_y) + } +} diff --git a/components/script/dom/touchevent.rs b/components/script/dom/touchevent.rs new file mode 100644 index 00000000000..99bac602bd6 --- /dev/null +++ b/components/script/dom/touchevent.rs @@ -0,0 +1,117 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +use dom::bindings::codegen::Bindings::TouchEventBinding; +use dom::bindings::codegen::Bindings::TouchEventBinding::TouchEventMethods; +use dom::bindings::codegen::Bindings::UIEventBinding::UIEventMethods; +use dom::bindings::conversions::Castable; +use dom::bindings::global::GlobalRef; +use dom::bindings::js::{JS, MutHeap, Root}; +use dom::bindings::utils::reflect_dom_object; +use dom::event::{Event, EventBubbles, EventCancelable}; +use dom::touchlist::TouchList; +use dom::uievent::UIEvent; +use dom::window::Window; +use std::cell::Cell; +use util::str::DOMString; + +#[dom_struct] +pub struct TouchEvent { + uievent: UIEvent, + touches: MutHeap<JS<TouchList>>, + target_touches: MutHeap<JS<TouchList>>, + changed_touches: MutHeap<JS<TouchList>>, + alt_key: Cell<bool>, + meta_key: Cell<bool>, + ctrl_key: Cell<bool>, + shift_key: Cell<bool>, +} + +impl TouchEvent { + fn new_inherited(touches: &TouchList, + changed_touches: &TouchList, + target_touches: &TouchList) -> TouchEvent { + TouchEvent { + uievent: UIEvent::new_inherited(), + touches: MutHeap::new(touches), + target_touches: MutHeap::new(target_touches), + changed_touches: MutHeap::new(changed_touches), + ctrl_key: Cell::new(false), + shift_key: Cell::new(false), + alt_key: Cell::new(false), + meta_key: Cell::new(false), + } + } + + pub fn new_uninitialized(window: &Window, + touches: &TouchList, + changed_touches: &TouchList, + target_touches: &TouchList) -> Root<TouchEvent> { + reflect_dom_object(box TouchEvent::new_inherited(touches, changed_touches, target_touches), + GlobalRef::Window(window), + TouchEventBinding::Wrap) + } + + pub fn new(window: &Window, + type_: DOMString, + canBubble: EventBubbles, + cancelable: EventCancelable, + view: Option<&Window>, + detail: i32, + touches: &TouchList, + changed_touches: &TouchList, + target_touches: &TouchList, + ctrlKey: bool, + altKey: bool, + shiftKey: bool, + metaKey: bool) -> Root<TouchEvent> { + let ev = TouchEvent::new_uninitialized(window, touches, changed_touches, target_touches); + ev.upcast::<UIEvent>().InitUIEvent(type_, + canBubble == EventBubbles::Bubbles, + cancelable == EventCancelable::Cancelable, + view, detail); + ev.ctrl_key.set(ctrlKey); + ev.alt_key.set(altKey); + ev.shift_key.set(shiftKey); + ev.meta_key.set(metaKey); + ev + } +} + +impl<'a> TouchEventMethods for &'a TouchEvent { + /// https://w3c.github.io/touch-events/#widl-TouchEvent-ctrlKey + fn CtrlKey(&self) -> bool { + self.ctrl_key.get() + } + + /// https://w3c.github.io/touch-events/#widl-TouchEvent-shiftKey + fn ShiftKey(&self) -> bool { + self.shift_key.get() + } + + /// https://w3c.github.io/touch-events/#widl-TouchEvent-altKey + fn AltKey(&self) -> bool { + self.alt_key.get() + } + + /// https://w3c.github.io/touch-events/#widl-TouchEvent-metaKey + fn MetaKey(&self) -> bool { + self.meta_key.get() + } + + /// https://w3c.github.io/touch-events/#widl-TouchEventInit-touches + fn Touches(&self) -> Root<TouchList> { + self.touches.get() + } + + /// https://w3c.github.io/touch-events/#widl-TouchEvent-targetTouches + fn TargetTouches(&self) -> Root<TouchList> { + self.target_touches.get() + } + + /// https://w3c.github.io/touch-events/#widl-TouchEvent-changedTouches + fn ChangedTouches(&self) -> Root<TouchList> { + self.changed_touches.get() + } +} diff --git a/components/script/dom/touchlist.rs b/components/script/dom/touchlist.rs new file mode 100644 index 00000000000..5806f945008 --- /dev/null +++ b/components/script/dom/touchlist.rs @@ -0,0 +1,50 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +use dom::bindings::codegen::Bindings::TouchListBinding; +use dom::bindings::codegen::Bindings::TouchListBinding::TouchListMethods; +use dom::bindings::global::GlobalRef; +use dom::bindings::js::{JS, Root}; +use dom::bindings::utils::{Reflector, reflect_dom_object}; +use dom::touch::Touch; +use dom::window::Window; + +#[dom_struct] +pub struct TouchList { + reflector_: Reflector, + touches: Vec<JS<Touch>>, +} + +impl TouchList { + fn new_inherited(touches: &[&Touch]) -> TouchList { + TouchList { + reflector_: Reflector::new(), + touches: touches.iter().map(|touch| JS::from_ref(*touch)).collect(), + } + } + + pub fn new(window: &Window, touches: &[&Touch]) -> Root<TouchList> { + reflect_dom_object(box TouchList::new_inherited(touches), + GlobalRef::Window(window), TouchListBinding::Wrap) + } +} + +impl TouchListMethods for TouchList { + /// https://w3c.github.io/touch-events/#widl-TouchList-length + fn Length(&self) -> u32 { + self.touches.len() as u32 + } + + /// https://w3c.github.io/touch-events/#widl-TouchList-item-getter-Touch-unsigned-long-index + fn Item(&self, index: u32) -> Option<Root<Touch>> { + self.touches.get(index as usize).map(JS::root) + } + + /// https://w3c.github.io/touch-events/#widl-TouchList-item-getter-Touch-unsigned-long-index + fn IndexedGetter(&self, index: u32, found: &mut bool) -> Option<Root<Touch>> { + let item = self.Item(index); + *found = item.is_some(); + item + } +} diff --git a/components/script/dom/webidls/Touch.webidl b/components/script/dom/webidls/Touch.webidl new file mode 100644 index 00000000000..2504c11a2bf --- /dev/null +++ b/components/script/dom/webidls/Touch.webidl @@ -0,0 +1,21 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +// http://w3c.github.io/touch-events/#idl-def-Touch + +interface Touch { + readonly attribute long identifier; + readonly attribute EventTarget target; + readonly attribute double screenX; + readonly attribute double screenY; + readonly attribute double clientX; + readonly attribute double clientY; + // readonly attribute double pageX; + // readonly attribute double pageY; + // readonly attribute float radiusX; + // readonly attribute float radiusY; + // readonly attribute float rotationAngle; + // readonly attribute float force; +}; diff --git a/components/script/dom/webidls/TouchEvent.webidl b/components/script/dom/webidls/TouchEvent.webidl new file mode 100644 index 00000000000..b291c23be16 --- /dev/null +++ b/components/script/dom/webidls/TouchEvent.webidl @@ -0,0 +1,16 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +// http://w3c.github.io/touch-events/#idl-def-TouchEvent + +interface TouchEvent : UIEvent { + readonly attribute TouchList touches; + readonly attribute TouchList targetTouches; + readonly attribute TouchList changedTouches; + readonly attribute boolean altKey; + readonly attribute boolean metaKey; + readonly attribute boolean ctrlKey; + readonly attribute boolean shiftKey; +}; diff --git a/components/script/dom/webidls/TouchList.webidl b/components/script/dom/webidls/TouchList.webidl new file mode 100644 index 00000000000..f75cc4c9530 --- /dev/null +++ b/components/script/dom/webidls/TouchList.webidl @@ -0,0 +1,11 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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/. */ + +// http://w3c.github.io/touch-events/#idl-def-TouchList + +interface TouchList { + readonly attribute unsigned long length; + getter Touch? item (unsigned long index); +}; |