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