1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* 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 https://mozilla.org/MPL/2.0/. */

use std::cell::Cell;
use std::default::Default;

use dom_struct::dom_struct;
use js::rust::HandleObject;
use servo_atoms::Atom;

use super::node::document_from_node;
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::codegen::Bindings::WindowBinding::Window_Binding::WindowMethods;
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::element::Element;
use crate::dom::event::{Event, EventBubbles, EventCancelable};
use crate::dom::eventtarget::EventTarget;
use crate::dom::window::Window;
use crate::script_runtime::CanGc;

// https://w3c.github.io/uievents/#interface-uievent
#[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.initialize_ui_event(
            type_,
            view.map(|window| window.upcast::<EventTarget>()),
            can_bubble,
            cancelable,
        );
        ev.detail.set(detail);
        ev
    }

    /// <https://w3c.github.io/uievents/#initialize-a-uievent>
    pub fn initialize_ui_event(
        &self,
        type_: DOMString,
        target_: Option<&EventTarget>,
        bubbles: EventBubbles,
        cancelable: EventCancelable,
    ) {
        // 1. Initialize the base Event attributes:
        self.event
            .init_event(type_.into(), bool::from(bubbles), bool::from(cancelable));
        self.event.set_target(target_);
        // 2. Initialize view/detail:
        if let Some(target_) = target_ {
            let element = target_.downcast::<Element>();
            let document = match element {
                Some(element) => document_from_node(element),
                None => target_.downcast::<Window>().unwrap().Document(),
            };
            self.view.set(Some(document.window()));
        }
        self.detail.set(0_i32);
    }

    pub fn set_detail(&self, detail_: i32) {
        self.detail.set(detail_);
    }
}

impl UIEventMethods<crate::DomTypeHolder> for UIEvent {
    /// <https://w3c.github.io/uievents/#dom-uievent-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)
    }

    // https://w3c.github.io/uievents/#widl-UIEvent-view
    fn GetView(&self) -> Option<DomRoot<Window>> {
        self.view.get()
    }

    // https://w3c.github.io/uievents/#widl-UIEvent-detail
    fn Detail(&self) -> i32 {
        self.detail.get()
    }

    // https://w3c.github.io/uievents/#widl-UIEvent-initUIEvent
    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);
    }

    // https://dom.spec.whatwg.org/#dom-event-istrusted
    fn IsTrusted(&self) -> bool {
        self.event.IsTrusted()
    }
}