use std::cell::Cell;
use dom_struct::dom_struct;
use js::context::JSContext;
use script_bindings::reflector::reflect_dom_object_with_cx;
use style::Atom;
use crate::dom::bindings::codegen::Bindings::TouchEventBinding::TouchEventMethods;
use crate::dom::bindings::codegen::Bindings::UIEventBinding::UIEventMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::{DomRoot, MutDom};
use crate::dom::event::{Event, EventBubbles, EventCancelable, EventComposed};
use crate::dom::touchlist::TouchList;
use crate::dom::uievent::UIEvent;
use crate::dom::window::Window;
#[dom_struct]
pub(crate) struct TouchEvent {
uievent: UIEvent,
touches: MutDom<TouchList>,
target_touches: MutDom<TouchList>,
changed_touches: MutDom<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: MutDom::new(touches),
target_touches: MutDom::new(target_touches),
changed_touches: MutDom::new(changed_touches),
ctrl_key: Cell::new(false),
shift_key: Cell::new(false),
alt_key: Cell::new(false),
meta_key: Cell::new(false),
}
}
pub(crate) fn new_uninitialized(
cx: &mut JSContext,
window: &Window,
touches: &TouchList,
changed_touches: &TouchList,
target_touches: &TouchList,
) -> DomRoot<TouchEvent> {
reflect_dom_object_with_cx(
Box::new(TouchEvent::new_inherited(
touches,
changed_touches,
target_touches,
)),
window,
cx,
)
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
cx: &mut JSContext,
window: &Window,
event_type: Atom,
can_bubble: EventBubbles,
cancelable: EventCancelable,
composed: EventComposed,
view: Option<&Window>,
detail: i32,
touches: &TouchList,
changed_touches: &TouchList,
target_touches: &TouchList,
ctrl_key: bool,
alt_key: bool,
shift_key: bool,
meta_key: bool,
) -> DomRoot<TouchEvent> {
let ev =
TouchEvent::new_uninitialized(cx, window, touches, changed_touches, target_touches);
ev.upcast::<UIEvent>().init_event(
event_type,
bool::from(can_bubble),
bool::from(cancelable),
view,
detail,
);
ev.upcast::<Event>().set_composed(bool::from(composed));
ev.ctrl_key.set(ctrl_key);
ev.alt_key.set(alt_key);
ev.shift_key.set(shift_key);
ev.meta_key.set(meta_key);
ev
}
}
impl TouchEventMethods<crate::DomTypeHolder> for TouchEvent {
fn CtrlKey(&self) -> bool {
self.ctrl_key.get()
}
fn ShiftKey(&self) -> bool {
self.shift_key.get()
}
fn AltKey(&self) -> bool {
self.alt_key.get()
}
fn MetaKey(&self) -> bool {
self.meta_key.get()
}
fn Touches(&self) -> DomRoot<TouchList> {
self.touches.get()
}
fn TargetTouches(&self) -> DomRoot<TouchList> {
self.target_touches.get()
}
fn ChangedTouches(&self) -> DomRoot<TouchList> {
self.changed_touches.get()
}
fn IsTrusted(&self) -> bool {
self.uievent.IsTrusted()
}
}