1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use webcore::value::Reference;
use webcore::try_from::TryInto;
use webapi::event_target::EventTarget;
use webapi::event::{IEvent, Event, ConcreteEvent};
pub trait IFocusEvent: IEvent {
#[inline]
fn related_target( &self ) -> Option< EventTarget > {
js!(
return @{self.as_ref()}.relatedTarget;
).try_into().ok()
}
}
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "FocusEvent")]
#[reference(subclass_of(Event))]
pub struct FocusRelatedEvent( Reference );
impl IEvent for FocusRelatedEvent {}
impl IFocusEvent for FocusRelatedEvent {}
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "FocusEvent")]
#[reference(subclass_of(Event, FocusRelatedEvent))]
pub struct FocusEvent( Reference );
impl IEvent for FocusEvent {}
impl IFocusEvent for FocusEvent {}
impl ConcreteEvent for FocusEvent {
const EVENT_TYPE: &'static str = "focus";
}
#[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
#[reference(instance_of = "FocusEvent")]
#[reference(subclass_of(Event, FocusRelatedEvent))]
pub struct BlurEvent( Reference );
impl IEvent for BlurEvent {}
impl IFocusEvent for BlurEvent {}
impl ConcreteEvent for BlurEvent {
const EVENT_TYPE: &'static str = "blur";
}
#[cfg(all(test, feature = "web_test"))]
mod tests {
use super::*;
#[test]
fn test_focus_event() {
let event: FocusEvent = js!(
return new FocusEvent( "focus" );
).try_into().unwrap();
assert_eq!( event.event_type(), "focus" );
assert!( event.related_target().is_none() );
}
#[test]
fn test_blur_event() {
let event: BlurEvent = js!(
return new FocusEvent( @{BlurEvent::EVENT_TYPE} );
).try_into().unwrap();
assert_eq!( event.event_type(), BlurEvent::EVENT_TYPE );
}
}