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