Add bindings for TouchEvent DOM interfaces

This commit is contained in:
Matt Brubeck 2015-08-10 20:55:19 -07:00
parent 79f300f038
commit 4ed15a8853
9 changed files with 302 additions and 1 deletions

View file

@ -9,7 +9,7 @@ use num::Float;
use std::ops::Deref; use std::ops::Deref;
/// Encapsulates the IDL restricted float type. /// Encapsulates the IDL restricted float type.
#[derive(JSTraceable, Clone, Eq, PartialEq)] #[derive(JSTraceable, Clone, Copy, Eq, PartialEq)]
pub struct Finite<T: Float>(T); pub struct Finite<T: Float>(T);
unsafe impl<T: Float> Zeroable for Finite<T> {} unsafe impl<T: Float> Zeroable for Finite<T> {}

View file

@ -345,6 +345,9 @@ pub mod testbindingproxy;
pub mod text; pub mod text;
pub mod textdecoder; pub mod textdecoder;
pub mod textencoder; pub mod textencoder;
pub mod touch;
pub mod touchevent;
pub mod touchlist;
pub mod treewalker; pub mod treewalker;
pub mod uievent; pub mod uievent;
pub mod url; pub mod url;

View file

@ -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)
}
}

View file

@ -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()
}
}

View file

@ -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
}
}

View file

@ -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;
};

View file

@ -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;
};

View file

@ -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);
};

View file

@ -198,6 +198,9 @@ var interfaceNamesInGlobalScope = [
"Text", "Text",
"TextDecoder", "TextDecoder",
"TextEncoder", "TextEncoder",
"Touch",
"TouchEvent",
"TouchList",
"TreeWalker", "TreeWalker",
"UIEvent", "UIEvent",
"URL", "URL",