script/dom/event/
focusevent.rs1use dom_struct::dom_struct;
6use js::context::JSContext;
7use js::rust::HandleObject;
8use script_bindings::reflector::reflect_dom_object_with_proto_and_cx;
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
23pub(crate) enum FocusEventType {
25 Focus,
27 Blur,
29}
30
31#[dom_struct]
32pub(crate) struct FocusEvent {
33 uievent: UIEvent,
34}
35
36impl FocusEvent {
37 fn new_inherited() -> FocusEvent {
38 FocusEvent {
39 uievent: UIEvent::new_inherited(),
40 }
41 }
42
43 pub(crate) fn new_uninitialized(cx: &mut JSContext, window: &Window) -> DomRoot<FocusEvent> {
44 Self::new_uninitialized_with_proto(cx, window, None)
45 }
46
47 pub(crate) fn new_uninitialized_with_proto(
48 cx: &mut JSContext,
49 window: &Window,
50 proto: Option<HandleObject>,
51 ) -> DomRoot<FocusEvent> {
52 reflect_dom_object_with_proto_and_cx(
53 Box::new(FocusEvent::new_inherited()),
54 window,
55 proto,
56 cx,
57 )
58 }
59
60 #[expect(clippy::too_many_arguments)]
61 pub(crate) fn new(
62 cx: &mut JSContext,
63 window: &Window,
64 event_type: Atom,
65 can_bubble: EventBubbles,
66 cancelable: EventCancelable,
67 view: Option<&Window>,
68 detail: i32,
69 related_target: Option<&EventTarget>,
70 ) -> DomRoot<FocusEvent> {
71 Self::new_with_proto(
72 cx,
73 window,
74 None,
75 event_type,
76 can_bubble,
77 cancelable,
78 view,
79 detail,
80 related_target,
81 )
82 }
83
84 #[expect(clippy::too_many_arguments)]
85 fn new_with_proto(
86 cx: &mut JSContext,
87 window: &Window,
88 proto: Option<HandleObject>,
89 event_type: Atom,
90 can_bubble: EventBubbles,
91 cancelable: EventCancelable,
92 view: Option<&Window>,
93 detail: i32,
94 related_target: Option<&EventTarget>,
95 ) -> DomRoot<FocusEvent> {
96 let ev = FocusEvent::new_uninitialized_with_proto(cx, window, proto);
97 ev.upcast::<UIEvent>().init_event(
98 event_type,
99 bool::from(can_bubble),
100 bool::from(cancelable),
101 view,
102 detail,
103 );
104 ev.upcast::<Event>().set_related_target(related_target);
105 ev
106 }
107}
108
109impl FocusEventMethods<crate::DomTypeHolder> for FocusEvent {
110 fn Constructor(
112 cx: &mut JSContext,
113 window: &Window,
114 proto: Option<HandleObject>,
115 event_type: DOMString,
116 init: &FocusEventBinding::FocusEventInit,
117 ) -> Fallible<DomRoot<FocusEvent>> {
118 let bubbles = EventBubbles::from(init.parent.parent.bubbles);
119 let cancelable = EventCancelable::from(init.parent.parent.cancelable);
120 let event = FocusEvent::new_with_proto(
121 cx,
122 window,
123 proto,
124 event_type.into(),
125 bubbles,
126 cancelable,
127 init.parent.view.as_deref(),
128 init.parent.detail,
129 init.relatedTarget.as_deref(),
130 );
131 event
132 .upcast::<Event>()
133 .set_composed(init.parent.parent.composed);
134 Ok(event)
135 }
136
137 fn GetRelatedTarget(&self) -> Option<DomRoot<EventTarget>> {
139 self.upcast::<Event>().related_target()
140 }
141
142 fn IsTrusted(&self) -> bool {
144 self.uievent.IsTrusted()
145 }
146}