script/dom/
focusevent.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 js::rust::HandleObject;
7
8use crate::dom::bindings::codegen::Bindings::FocusEventBinding;
9use crate::dom::bindings::codegen::Bindings::FocusEventBinding::FocusEventMethods;
10use crate::dom::bindings::codegen::Bindings::UIEventBinding::UIEventMethods;
11use crate::dom::bindings::error::Fallible;
12use crate::dom::bindings::inheritance::Castable;
13use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
14use crate::dom::bindings::root::DomRoot;
15use crate::dom::bindings::str::DOMString;
16use crate::dom::event::{Event, EventBubbles, EventCancelable};
17use crate::dom::eventtarget::EventTarget;
18use crate::dom::uievent::UIEvent;
19use crate::dom::window::Window;
20use crate::script_runtime::CanGc;
21
22#[dom_struct]
23pub(crate) struct FocusEvent {
24    uievent: UIEvent,
25}
26
27impl FocusEvent {
28    fn new_inherited() -> FocusEvent {
29        FocusEvent {
30            uievent: UIEvent::new_inherited(),
31        }
32    }
33
34    pub(crate) fn new_uninitialized(window: &Window, can_gc: CanGc) -> DomRoot<FocusEvent> {
35        Self::new_uninitialized_with_proto(window, None, can_gc)
36    }
37
38    pub(crate) fn new_uninitialized_with_proto(
39        window: &Window,
40        proto: Option<HandleObject>,
41        can_gc: CanGc,
42    ) -> DomRoot<FocusEvent> {
43        reflect_dom_object_with_proto(Box::new(FocusEvent::new_inherited()), window, proto, can_gc)
44    }
45
46    #[allow(clippy::too_many_arguments)]
47    pub(crate) fn new(
48        window: &Window,
49        type_: DOMString,
50        can_bubble: EventBubbles,
51        cancelable: EventCancelable,
52        view: Option<&Window>,
53        detail: i32,
54        related_target: Option<&EventTarget>,
55        can_gc: CanGc,
56    ) -> DomRoot<FocusEvent> {
57        Self::new_with_proto(
58            window,
59            None,
60            type_,
61            can_bubble,
62            cancelable,
63            view,
64            detail,
65            related_target,
66            can_gc,
67        )
68    }
69
70    #[allow(clippy::too_many_arguments)]
71    fn new_with_proto(
72        window: &Window,
73        proto: Option<HandleObject>,
74        type_: DOMString,
75        can_bubble: EventBubbles,
76        cancelable: EventCancelable,
77        view: Option<&Window>,
78        detail: i32,
79        related_target: Option<&EventTarget>,
80        can_gc: CanGc,
81    ) -> DomRoot<FocusEvent> {
82        let ev = FocusEvent::new_uninitialized_with_proto(window, proto, can_gc);
83        ev.upcast::<UIEvent>().InitUIEvent(
84            type_,
85            bool::from(can_bubble),
86            bool::from(cancelable),
87            view,
88            detail,
89        );
90        ev.upcast::<Event>().set_related_target(related_target);
91        ev
92    }
93}
94
95impl FocusEventMethods<crate::DomTypeHolder> for FocusEvent {
96    /// <https://w3c.github.io/uievents/#dom-focusevent-focusevent>
97    fn Constructor(
98        window: &Window,
99        proto: Option<HandleObject>,
100        can_gc: CanGc,
101        type_: DOMString,
102        init: &FocusEventBinding::FocusEventInit,
103    ) -> Fallible<DomRoot<FocusEvent>> {
104        let bubbles = EventBubbles::from(init.parent.parent.bubbles);
105        let cancelable = EventCancelable::from(init.parent.parent.cancelable);
106        let event = FocusEvent::new_with_proto(
107            window,
108            proto,
109            type_,
110            bubbles,
111            cancelable,
112            init.parent.view.as_deref(),
113            init.parent.detail,
114            init.relatedTarget.as_deref(),
115            can_gc,
116        );
117        event
118            .upcast::<Event>()
119            .set_composed(init.parent.parent.composed);
120        Ok(event)
121    }
122
123    /// <https://w3c.github.io/uievents/#widl-FocusEvent-relatedTarget>
124    fn GetRelatedTarget(&self) -> Option<DomRoot<EventTarget>> {
125        self.upcast::<Event>().related_target()
126    }
127
128    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
129    fn IsTrusted(&self) -> bool {
130        self.uievent.IsTrusted()
131    }
132}