1use crate::{Event, Input};
2
3pub trait FocusEvent: Sized {
5 fn from_focused(focused: bool, old_event: &Self) -> Option<Self>;
9 fn focus<U, F>(&self, f: F) -> Option<U>
11 where
12 F: FnMut(bool) -> U;
13 fn focus_args(&self) -> Option<bool> {
15 self.focus(|val| val)
16 }
17}
18
19impl FocusEvent for Event {
20 fn from_focused(focused: bool, old_event: &Self) -> Option<Self> {
21 let timestamp = if let Event::Input(_, x) = old_event {
22 *x
23 } else {
24 None
25 };
26 Some(Event::Input(Input::Focus(focused), timestamp))
27 }
28
29 fn focus<U, F>(&self, mut f: F) -> Option<U>
30 where
31 F: FnMut(bool) -> U,
32 {
33 match *self {
34 Event::Input(Input::Focus(focused), _) => Some(f(focused)),
35 _ => None,
36 }
37 }
38}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43
44 #[test]
45 fn test_input_focus() {
46 use super::super::Input;
47
48 let e: Event = Input::Focus(false).into();
49 let x: Option<Event> = FocusEvent::from_focused(true, &e);
50 let y: Option<Event> = x
51 .clone()
52 .unwrap()
53 .focus(|focused| FocusEvent::from_focused(focused, x.as_ref().unwrap()))
54 .unwrap();
55 assert_eq!(x, y);
56 }
57}