script/dom/
inputevent.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use embedder_traits::Cursor;
7use euclid::Point2D;
8use js::rust::HandleObject;
9use style_traits::CSSPixel;
10
11use crate::dom::bindings::codegen::Bindings::InputEventBinding::{self, InputEventMethods};
12use crate::dom::bindings::codegen::Bindings::UIEventBinding::UIEvent_Binding::UIEventMethods;
13use crate::dom::bindings::error::Fallible;
14use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::bindings::str::DOMString;
17use crate::dom::node::Node;
18use crate::dom::uievent::UIEvent;
19use crate::dom::window::Window;
20use crate::script_runtime::CanGc;
21
22#[dom_struct]
23pub(crate) struct InputEvent {
24    uievent: UIEvent,
25    data: Option<DOMString>,
26    is_composing: bool,
27}
28
29impl InputEvent {
30    #[allow(clippy::too_many_arguments)]
31    fn new(
32        window: &Window,
33        proto: Option<HandleObject>,
34        type_: DOMString,
35        can_bubble: bool,
36        cancelable: bool,
37        view: Option<&Window>,
38        detail: i32,
39        data: Option<DOMString>,
40        is_composing: bool,
41        can_gc: CanGc,
42    ) -> DomRoot<InputEvent> {
43        let ev = reflect_dom_object_with_proto(
44            Box::new(InputEvent {
45                uievent: UIEvent::new_inherited(),
46                data,
47                is_composing,
48            }),
49            window,
50            proto,
51            can_gc,
52        );
53        ev.uievent
54            .InitUIEvent(type_, can_bubble, cancelable, view, detail);
55        ev
56    }
57}
58
59impl InputEventMethods<crate::DomTypeHolder> for InputEvent {
60    // https://w3c.github.io/uievents/#dom-inputevent-inputevent
61    fn Constructor(
62        window: &Window,
63        proto: Option<HandleObject>,
64        can_gc: CanGc,
65        type_: DOMString,
66        init: &InputEventBinding::InputEventInit,
67    ) -> Fallible<DomRoot<InputEvent>> {
68        let event = InputEvent::new(
69            window,
70            proto,
71            type_,
72            init.parent.parent.bubbles,
73            init.parent.parent.cancelable,
74            init.parent.view.as_deref(),
75            init.parent.detail,
76            init.data.clone(),
77            init.isComposing,
78            can_gc,
79        );
80        Ok(event)
81    }
82
83    // https://w3c.github.io/uievents/#dom-inputevent-data
84    fn GetData(&self) -> Option<DOMString> {
85        self.data.clone()
86    }
87
88    // https://w3c.github.io/uievents/#dom-inputevent-iscomposing
89    fn IsComposing(&self) -> bool {
90        self.is_composing
91    }
92
93    // https://dom.spec.whatwg.org/#dom-event-istrusted
94    fn IsTrusted(&self) -> bool {
95        self.uievent.IsTrusted()
96    }
97}
98
99/// A [`HitTestResult`] that is the result of doing a hit test based on a less-fine-grained
100/// `CompositorHitTestResult` against our current layout.
101pub(crate) struct HitTestResult {
102    pub node: DomRoot<Node>,
103    pub cursor: Cursor,
104    pub point_in_node: Point2D<f32, CSSPixel>,
105    pub point_in_frame: Point2D<f32, CSSPixel>,
106    pub point_relative_to_initial_containing_block: Point2D<f32, CSSPixel>,
107}