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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/* 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::MouseEventBinding;
use dom::bindings::codegen::Bindings::MouseEventBinding::MouseEventMethods;
use dom::bindings::codegen::Bindings::UIEventBinding::UIEventMethods;
use dom::bindings::conversions::Castable;
use dom::bindings::error::Fallible;
use dom::bindings::global::GlobalRef;
use dom::bindings::js::{JS, MutNullableHeap, Root, RootedReference};
use dom::bindings::utils::reflect_dom_object;
use dom::event::{Event, EventBubbles, EventCancelable};
use dom::eventtarget::EventTarget;
use dom::uievent::UIEvent;
use dom::window::Window;
use std::cell::Cell;
use std::default::Default;
use util::prefs;
use util::str::DOMString;
#[dom_struct]
pub struct MouseEvent {
uievent: UIEvent,
screen_x: Cell<i32>,
screen_y: Cell<i32>,
client_x: Cell<i32>,
client_y: Cell<i32>,
ctrl_key: Cell<bool>,
shift_key: Cell<bool>,
alt_key: Cell<bool>,
meta_key: Cell<bool>,
button: Cell<i16>,
related_target: MutNullableHeap<JS<EventTarget>>,
}
impl MouseEvent {
fn new_inherited() -> MouseEvent {
MouseEvent {
uievent: UIEvent::new_inherited(),
screen_x: Cell::new(0),
screen_y: Cell::new(0),
client_x: Cell::new(0),
client_y: Cell::new(0),
ctrl_key: Cell::new(false),
shift_key: Cell::new(false),
alt_key: Cell::new(false),
meta_key: Cell::new(false),
button: Cell::new(0),
related_target: Default::default(),
}
}
pub fn new_uninitialized(window: &Window) -> Root<MouseEvent> {
reflect_dom_object(box MouseEvent::new_inherited(),
GlobalRef::Window(window),
MouseEventBinding::Wrap)
}
pub fn new(window: &Window,
type_: DOMString,
canBubble: EventBubbles,
cancelable: EventCancelable,
view: Option<&Window>,
detail: i32,
screenX: i32,
screenY: i32,
clientX: i32,
clientY: i32,
ctrlKey: bool,
altKey: bool,
shiftKey: bool,
metaKey: bool,
button: i16,
relatedTarget: Option<&EventTarget>) -> Root<MouseEvent> {
let ev = MouseEvent::new_uninitialized(window);
ev.r().InitMouseEvent(type_, canBubble == EventBubbles::Bubbles, cancelable == EventCancelable::Cancelable,
view, detail,
screenX, screenY, clientX, clientY,
ctrlKey, altKey, shiftKey, metaKey,
button, relatedTarget);
ev
}
pub fn Constructor(global: GlobalRef,
type_: DOMString,
init: &MouseEventBinding::MouseEventInit) -> Fallible<Root<MouseEvent>> {
let bubbles = if init.parent.parent.parent.bubbles {
EventBubbles::Bubbles
} else {
EventBubbles::DoesNotBubble
};
let cancelable = if init.parent.parent.parent.cancelable {
EventCancelable::Cancelable
} else {
EventCancelable::NotCancelable
};
let event = MouseEvent::new(global.as_window(), type_,
bubbles,
cancelable,
init.parent.parent.view.r(),
init.parent.parent.detail,
init.screenX, init.screenY,
init.clientX, init.clientY, init.parent.ctrlKey,
init.parent.altKey, init.parent.shiftKey, init.parent.metaKey,
init.button, init.relatedTarget.r());
Ok(event)
}
}
impl MouseEventMethods for MouseEvent {
// https://w3c.github.io/uievents/#widl-MouseEvent-screenX
fn ScreenX(&self) -> i32 {
self.screen_x.get()
}
// https://w3c.github.io/uievents/#widl-MouseEvent-screenY
fn ScreenY(&self) -> i32 {
self.screen_y.get()
}
// https://w3c.github.io/uievents/#widl-MouseEvent-clientX
fn ClientX(&self) -> i32 {
self.client_x.get()
}
// https://w3c.github.io/uievents/#widl-MouseEvent-clientY
fn ClientY(&self) -> i32 {
self.client_y.get()
}
// https://w3c.github.io/uievents/#widl-MouseEvent-ctrlKey
fn CtrlKey(&self) -> bool {
self.ctrl_key.get()
}
// https://w3c.github.io/uievents/#widl-MouseEvent-shiftKey
fn ShiftKey(&self) -> bool {
self.shift_key.get()
}
// https://w3c.github.io/uievents/#widl-MouseEvent-altKey
fn AltKey(&self) -> bool {
self.alt_key.get()
}
// https://w3c.github.io/uievents/#widl-MouseEvent-metaKey
fn MetaKey(&self) -> bool {
self.meta_key.get()
}
// https://w3c.github.io/uievents/#widl-MouseEvent-button
fn Button(&self) -> i16 {
self.button.get()
}
// https://w3c.github.io/uievents/#widl-MouseEvent-relatedTarget
fn GetRelatedTarget(&self) -> Option<Root<EventTarget>> {
self.related_target.get_rooted()
}
// See discussion at:
// - https://github.com/servo/servo/issues/6643
// - https://bugzilla.mozilla.org/show_bug.cgi?id=1186125
// This returns the same result as current gecko.
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/which
fn Which(&self) -> i32 {
if prefs::get_pref("dom.mouseevent.which.enabled").as_boolean().unwrap_or(false) {
(self.button.get() + 1) as i32
} else {
0
}
}
// https://w3c.github.io/uievents/#widl-MouseEvent-initMouseEvent
fn InitMouseEvent(&self,
typeArg: DOMString,
canBubbleArg: bool,
cancelableArg: bool,
viewArg: Option<&Window>,
detailArg: i32,
screenXArg: i32,
screenYArg: i32,
clientXArg: i32,
clientYArg: i32,
ctrlKeyArg: bool,
altKeyArg: bool,
shiftKeyArg: bool,
metaKeyArg: bool,
buttonArg: i16,
relatedTargetArg: Option<&EventTarget>) {
let event: &Event = self.upcast::<Event>();
if event.dispatching() {
return;
}
let uievent: &UIEvent = self.upcast::<UIEvent>();
uievent.InitUIEvent(typeArg, canBubbleArg, cancelableArg, viewArg, detailArg);
self.screen_x.set(screenXArg);
self.screen_y.set(screenYArg);
self.client_x.set(clientXArg);
self.client_y.set(clientYArg);
self.ctrl_key.set(ctrlKeyArg);
self.alt_key.set(altKeyArg);
self.shift_key.set(shiftKeyArg);
self.meta_key.set(metaKeyArg);
self.button.set(buttonArg);
self.related_target.set(relatedTargetArg);
}
}
|