vizia_core/input/
entry.rs

1use crate::context::EventContext;
2
3/// An entry inside of a [`Keymap`](crate::prelude::Keymap).
4///
5/// It consists of an action which is usually just an enum variant
6/// and a callback function that gets called if the action got triggered.
7#[derive(Copy, Clone)]
8pub struct KeymapEntry<T>
9where
10    T: 'static + Clone + PartialEq + Send + Sync,
11{
12    action: T,
13    on_action: fn(&mut EventContext),
14}
15
16impl<T> KeymapEntry<T>
17where
18    T: 'static + Clone + PartialEq + Send + Sync,
19{
20    /// Creates a new keymap entry.
21    ///
22    /// # Examples
23    ///
24    /// ```
25    /// # use vizia_core::prelude::*;
26    /// #
27    /// # #[derive(Copy, Clone, PartialEq)]
28    /// # enum Action {
29    /// #     One,
30    /// # }
31    /// #
32    /// KeymapEntry::new(Action::One, |_| debug!("Action One"));
33    /// ```
34    pub fn new(action: T, on_action: fn(&mut EventContext)) -> Self {
35        Self { action, on_action }
36    }
37
38    /// Returns the action of the keymap entry.
39    pub fn action(&self) -> &T {
40        &self.action
41    }
42
43    /// Returns the `on_action` callback function of the keymap entry.
44    pub fn on_action(&self) -> &fn(&mut EventContext) {
45        &self.on_action
46    }
47}
48
49impl<T> PartialEq for KeymapEntry<T>
50where
51    T: 'static + Clone + PartialEq + Send + Sync,
52{
53    fn eq(&self, other: &Self) -> bool {
54        self.action == other.action
55    }
56}
57
58impl<T> PartialEq<T> for KeymapEntry<T>
59where
60    T: 'static + Clone + PartialEq + Send + Sync,
61{
62    fn eq(&self, other: &T) -> bool {
63        self.action == *other
64    }
65}