use std::cell::Cell;
use std::default::Default;
use dom_struct::dom_struct;
use js::rust::HandleObject;
use servo_atoms::Atom;
use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use crate::dom::bindings::codegen::Bindings::UIEventBinding;
use crate::dom::bindings::codegen::Bindings::UIEventBinding::UIEventMethods;
use crate::dom::bindings::error::Fallible;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::event::{Event, EventBubbles, EventCancelable};
use crate::dom::window::Window;
use crate::script_runtime::CanGc;
#[dom_struct]
pub struct UIEvent {
event: Event,
view: MutNullableDom<Window>,
detail: Cell<i32>,
}
impl UIEvent {
pub fn new_inherited() -> UIEvent {
UIEvent {
event: Event::new_inherited(),
view: Default::default(),
detail: Cell::new(0),
}
}
pub fn new_uninitialized(window: &Window, can_gc: CanGc) -> DomRoot<UIEvent> {
Self::new_uninitialized_with_proto(window, None, can_gc)
}
fn new_uninitialized_with_proto(
window: &Window,
proto: Option<HandleObject>,
can_gc: CanGc,
) -> DomRoot<UIEvent> {
reflect_dom_object_with_proto(Box::new(UIEvent::new_inherited()), window, proto, can_gc)
}
pub fn new(
window: &Window,
type_: DOMString,
can_bubble: EventBubbles,
cancelable: EventCancelable,
view: Option<&Window>,
detail: i32,
can_gc: CanGc,
) -> DomRoot<UIEvent> {
Self::new_with_proto(
window, None, type_, can_bubble, cancelable, view, detail, can_gc,
)
}
#[allow(clippy::too_many_arguments)]
fn new_with_proto(
window: &Window,
proto: Option<HandleObject>,
type_: DOMString,
can_bubble: EventBubbles,
cancelable: EventCancelable,
view: Option<&Window>,
detail: i32,
can_gc: CanGc,
) -> DomRoot<UIEvent> {
let ev = UIEvent::new_uninitialized_with_proto(window, proto, can_gc);
ev.InitUIEvent(
type_,
bool::from(can_bubble),
bool::from(cancelable),
view,
detail,
);
ev
}
}
impl UIEventMethods for UIEvent {
fn Constructor(
window: &Window,
proto: Option<HandleObject>,
can_gc: CanGc,
type_: DOMString,
init: &UIEventBinding::UIEventInit,
) -> Fallible<DomRoot<UIEvent>> {
let bubbles = EventBubbles::from(init.parent.bubbles);
let cancelable = EventCancelable::from(init.parent.cancelable);
let event = UIEvent::new_with_proto(
window,
proto,
type_,
bubbles,
cancelable,
init.view.as_deref(),
init.detail,
can_gc,
);
Ok(event)
}
fn GetView(&self) -> Option<DomRoot<Window>> {
self.view.get()
}
fn Detail(&self) -> i32 {
self.detail.get()
}
fn InitUIEvent(
&self,
type_: DOMString,
can_bubble: bool,
cancelable: bool,
view: Option<&Window>,
detail: i32,
) {
let event = self.upcast::<Event>();
if event.dispatching() {
return;
}
event.init_event(Atom::from(type_), can_bubble, cancelable);
self.view.set(view);
self.detail.set(detail);
}
fn IsTrusted(&self) -> bool {
self.event.IsTrusted()
}
}