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::datatransfer::DataTransfer;
18use crate::dom::node::Node;
19use crate::dom::staticrange::StaticRange;
20use crate::dom::uievent::UIEvent;
21use crate::dom::window::Window;
22use crate::script_runtime::CanGc;
23
24#[dom_struct]
25pub(crate) struct InputEvent {
26    uievent: UIEvent,
27    data: Option<DOMString>,
28    is_composing: bool,
29    input_type: DOMString,
30}
31
32impl InputEvent {
33    #[allow(clippy::too_many_arguments)]
34    pub(crate) fn new(
35        window: &Window,
36        proto: Option<HandleObject>,
37        type_: DOMString,
38        can_bubble: bool,
39        cancelable: bool,
40        view: Option<&Window>,
41        detail: i32,
42        data: Option<DOMString>,
43        is_composing: bool,
44        input_type: DOMString,
45        can_gc: CanGc,
46    ) -> DomRoot<InputEvent> {
47        let ev = reflect_dom_object_with_proto(
48            Box::new(InputEvent {
49                uievent: UIEvent::new_inherited(),
50                data,
51                is_composing,
52                input_type,
53            }),
54            window,
55            proto,
56            can_gc,
57        );
58        ev.uievent
59            .InitUIEvent(type_, can_bubble, cancelable, view, detail);
60        ev
61    }
62}
63
64impl InputEventMethods<crate::DomTypeHolder> for InputEvent {
65    /// <https://w3c.github.io/uievents/#dom-inputevent-inputevent>
66    fn Constructor(
67        window: &Window,
68        proto: Option<HandleObject>,
69        can_gc: CanGc,
70        type_: DOMString,
71        init: &InputEventBinding::InputEventInit,
72    ) -> Fallible<DomRoot<InputEvent>> {
73        let event = InputEvent::new(
74            window,
75            proto,
76            type_,
77            init.parent.parent.bubbles,
78            init.parent.parent.cancelable,
79            init.parent.view.as_deref(),
80            init.parent.detail,
81            init.data.clone(),
82            init.isComposing,
83            init.inputType.clone(),
84            can_gc,
85        );
86        Ok(event)
87    }
88
89    /// <https://w3c.github.io/uievents/#dom-inputevent-data>
90    fn GetData(&self) -> Option<DOMString> {
91        self.data.clone()
92    }
93
94    /// <https://w3c.github.io/uievents/#dom-inputevent-iscomposing>
95    fn IsComposing(&self) -> bool {
96        self.is_composing
97    }
98
99    /// <https://w3c.github.io/uievents/#dom-inputevent-inputtype>
100    fn InputType(&self) -> DOMString {
101        self.input_type.clone()
102    }
103
104    /// <https://w3c.github.io/input-events/#dom-inputevent-datatransfer>
105    fn GetDataTransfer(&self) -> Option<DomRoot<DataTransfer>> {
106        // TODO: Populate dataTransfer for contenteditable
107        None
108    }
109
110    /// <https://w3c.github.io/input-events/#dom-inputevent-gettargetranges>
111    fn GetTargetRanges(&self) -> Vec<DomRoot<StaticRange>> {
112        // TODO: Populate targetRanges for contenteditable
113        Vec::new()
114    }
115
116    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
117    fn IsTrusted(&self) -> bool {
118        self.uievent.IsTrusted()
119    }
120}
121
122/// A [`HitTestResult`] that is the result of doing a hit test based on a less-fine-grained
123/// `CompositorHitTestResult` against our current layout.
124pub(crate) struct HitTestResult {
125    pub node: DomRoot<Node>,
126    pub cursor: Cursor,
127    pub point_in_node: Point2D<f32, CSSPixel>,
128    pub point_in_frame: Point2D<f32, CSSPixel>,
129    pub point_relative_to_initial_containing_block: Point2D<f32, CSSPixel>,
130}