stdweb/webapi/events/
focus.rs1use webcore::value::Reference;
2use webcore::try_from::TryInto;
3use webapi::event_target::EventTarget;
4use webapi::event::{IEvent, Event};
5
6pub trait IFocusEvent: IEvent {
12 #[inline]
17 fn related_target( &self ) -> Option< EventTarget > {
18 js!(
19 return @{self.as_ref()}.relatedTarget;
20 ).try_into().ok()
21 }
22}
23
24#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
30#[reference(instance_of = "FocusEvent")]
31#[reference(subclass_of(Event))]
32pub struct FocusRelatedEvent( Reference );
33
34impl IEvent for FocusRelatedEvent {}
35impl IFocusEvent for FocusRelatedEvent {}
36
37#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
43#[reference(instance_of = "FocusEvent")]
44#[reference(event = "focus")]
45#[reference(subclass_of(Event, FocusRelatedEvent))]
46pub struct FocusEvent( Reference );
47
48impl IEvent for FocusEvent {}
49impl IFocusEvent for FocusEvent {}
50
51#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
57#[reference(instance_of = "FocusEvent")]
58#[reference(event = "blur")]
59#[reference(subclass_of(Event, FocusRelatedEvent))]
60pub struct BlurEvent( Reference );
61
62impl IEvent for BlurEvent {}
63impl IFocusEvent for BlurEvent {}
64
65#[cfg(all(test, feature = "web_test"))]
66mod tests {
67 use super::*;
68 use webapi::event::ConcreteEvent;
69
70 #[test]
71 fn test_focus_event() {
72 let event: FocusEvent = js!(
73 return new FocusEvent( "focus" );
74 ).try_into().unwrap();
75 assert_eq!( event.event_type(), "focus" );
76 assert!( event.related_target().is_none() );
77 }
78
79 #[test]
80 fn test_blur_event() {
81 let event: BlurEvent = js!(
82 return new FocusEvent( @{BlurEvent::EVENT_TYPE} );
83 ).try_into().unwrap();
84 assert_eq!( event.event_type(), BlurEvent::EVENT_TYPE );
85 }
86}