Skip to main content

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