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
use crate::context::EventContext;

/// An entry inside of a [`Keymap`](crate::prelude::Keymap).
///
/// It consists of an action which is usually just an enum variant
/// and a callback function that gets called if the action got triggered.
#[derive(Copy, Clone)]
pub struct KeymapEntry<T>
where
    T: 'static + Clone + PartialEq + Send + Sync,
{
    action: T,
    on_action: fn(&mut EventContext),
}

impl<T> KeymapEntry<T>
where
    T: 'static + Clone + PartialEq + Send + Sync,
{
    /// Creates a new keymap entry.
    ///
    /// # Examples
    ///
    /// ```
    /// # use vizia_core::prelude::*;
    /// #
    /// # #[derive(Copy, Clone, PartialEq)]
    /// # enum Action {
    /// #     One,
    /// # }
    /// #
    /// KeymapEntry::new(Action::One, |_| debug!("Action One"));
    /// ```
    pub fn new(action: T, on_action: fn(&mut EventContext)) -> Self {
        Self { action, on_action }
    }

    /// Returns the action of the keymap entry.
    pub fn action(&self) -> &T {
        &self.action
    }

    /// Returns the `on_action` callback function of the keymap entry.
    pub fn on_action(&self) -> &fn(&mut EventContext) {
        &self.on_action
    }
}

impl<T> PartialEq for KeymapEntry<T>
where
    T: 'static + Clone + PartialEq + Send + Sync,
{
    fn eq(&self, other: &Self) -> bool {
        self.action == other.action
    }
}

impl<T> PartialEq<T> for KeymapEntry<T>
where
    T: 'static + Clone + PartialEq + Send + Sync,
{
    fn eq(&self, other: &T) -> bool {
        self.action == *other
    }
}