script/dom/event/
focusevent.rs1use 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
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(window: &Window, can_gc: CanGc) -> DomRoot<FocusEvent> {
44 Self::new_uninitialized_with_proto(window, None, can_gc)
45 }
46
47 pub(crate) fn new_uninitialized_with_proto(
48 window: &Window,
49 proto: Option<HandleObject>,
50 can_gc: CanGc,
51 ) -> DomRoot<FocusEvent> {
52 reflect_dom_object_with_proto(Box::new(FocusEvent::new_inherited()), window, proto, can_gc)
53 }
54
55 #[expect(clippy::too_many_arguments)]
56 pub(crate) fn new(
57 window: &Window,
58 event_type: Atom,
59 can_bubble: EventBubbles,
60 cancelable: EventCancelable,
61 view: Option<&Window>,
62 detail: i32,
63 related_target: Option<&EventTarget>,
64 can_gc: CanGc,
65 ) -> DomRoot<FocusEvent> {
66 Self::new_with_proto(
67 window,
68 None,
69 event_type,
70 can_bubble,
71 cancelable,
72 view,
73 detail,
74 related_target,
75 can_gc,
76 )
77 }
78
79 #[expect(clippy::too_many_arguments)]
80 fn new_with_proto(
81 window: &Window,
82 proto: Option<HandleObject>,
83 event_type: Atom,
84 can_bubble: EventBubbles,
85 cancelable: EventCancelable,
86 view: Option<&Window>,
87 detail: i32,
88 related_target: Option<&EventTarget>,
89 can_gc: CanGc,
90 ) -> DomRoot<FocusEvent> {
91 let ev = FocusEvent::new_uninitialized_with_proto(window, proto, can_gc);
92 ev.upcast::<UIEvent>().init_event(
93 event_type,
94 bool::from(can_bubble),
95 bool::from(cancelable),
96 view,
97 detail,
98 );
99 ev.upcast::<Event>().set_related_target(related_target);
100 ev
101 }
102}
103
104impl FocusEventMethods<crate::DomTypeHolder> for FocusEvent {
105 fn Constructor(
107 window: &Window,
108 proto: Option<HandleObject>,
109 can_gc: CanGc,
110 event_type: DOMString,
111 init: &FocusEventBinding::FocusEventInit,
112 ) -> Fallible<DomRoot<FocusEvent>> {
113 let bubbles = EventBubbles::from(init.parent.parent.bubbles);
114 let cancelable = EventCancelable::from(init.parent.parent.cancelable);
115 let event = FocusEvent::new_with_proto(
116 window,
117 proto,
118 event_type.into(),
119 bubbles,
120 cancelable,
121 init.parent.view.as_deref(),
122 init.parent.detail,
123 init.relatedTarget.as_deref(),
124 can_gc,
125 );
126 event
127 .upcast::<Event>()
128 .set_composed(init.parent.parent.composed);
129 Ok(event)
130 }
131
132 fn GetRelatedTarget(&self) -> Option<DomRoot<EventTarget>> {
134 self.upcast::<Event>().related_target()
135 }
136
137 fn IsTrusted(&self) -> bool {
139 self.uievent.IsTrusted()
140 }
141}