Skip to main content

ferroui_controls/platform/
i_platform_feedback.rs

1/// Performs audible or haptic feedback for user interactions.
2pub trait IPlatformFeedback {
3    /// Performs the feedback; returns whether it was performed.
4    fn perform(&self, feedback: FeedbackAction, type_: FeedbackType) -> bool;
5}
6
7/// The kind of feedback an element gives.
8#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
9#[repr(i32)]
10pub enum FeedbackType {
11    #[default]
12    None = 0,
13    Auto = 1,
14    Sound = 2,
15    Haptic = 3,
16}
17
18/// An interaction that feedback can be given for.
19#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
20pub struct FeedbackAction {
21    key: &'static str,
22}
23
24impl FeedbackAction {
25    /// The key that identifies the action. For platform backends.
26    pub const fn key(&self) -> &'static str {
27        self.key
28    }
29
30    /// The feedback of a click.
31    pub const fn click() -> Self {
32        Self { key: "Click" }
33    }
34
35    /// The feedback of holding.
36    pub const fn hold() -> Self {
37        Self { key: "Hold" }
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn actions_are_identified_by_their_key() {
47        assert_eq!(FeedbackAction::click().key(), "Click");
48        assert_eq!(FeedbackAction::hold().key(), "Hold");
49        assert_eq!(FeedbackAction::click(), FeedbackAction::click());
50        assert_ne!(FeedbackAction::click(), FeedbackAction::hold());
51    }
52}