retroglyph_ui/interact/consumed.rs
1//! [`Consumed`]: whether [`Interaction::handle_event`](crate::Interaction::handle_event) claimed
2//! an event.
3
4/// Whether an [`Interaction`](crate::Interaction) claimed an event.
5///
6/// Reported by [`handle_event`](crate::Interaction::handle_event), so a caller with more than one
7/// interaction context (a menu bar above a screen stack, a dropdown above a form) knows whether to
8/// keep routing the same event further down. A coarse substitute, like "is the menu currently
9/// open", answers a different and less useful question: it stays `true` for events the menu
10/// doesn't care about (`Resize`, `Paste`, a key it doesn't bind), and it depends on the caller
11/// keeping that flag in sync with state that can change out from under it (closing the menu
12/// without also clearing the flag). `Consumed` instead reports, per event, whether *this* call
13/// actually used it.
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15#[non_exhaustive]
16pub enum Consumed {
17 /// Nothing here claimed the event: keep routing it to whatever's behind this interaction.
18 No,
19 /// This interaction claimed the event: stop routing it further.
20 Yes,
21}
22
23impl Consumed {
24 /// `true` for [`Consumed::Yes`].
25 #[must_use]
26 pub const fn is_yes(self) -> bool {
27 matches!(self, Self::Yes)
28 }
29
30 /// `true` for [`Consumed::No`].
31 #[must_use]
32 pub const fn is_no(self) -> bool {
33 matches!(self, Self::No)
34 }
35}
36
37impl From<bool> for Consumed {
38 /// `true` becomes [`Consumed::Yes`], `false` becomes [`Consumed::No`].
39 fn from(consumed: bool) -> Self {
40 if consumed { Self::Yes } else { Self::No }
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47
48 #[test]
49 fn is_yes_and_is_no_agree_with_the_variant() {
50 assert!(Consumed::Yes.is_yes());
51 assert!(!Consumed::Yes.is_no());
52 assert!(Consumed::No.is_no());
53 assert!(!Consumed::No.is_yes());
54 }
55
56 #[test]
57 fn from_bool_maps_true_and_false() {
58 assert_eq!(Consumed::from(true), Consumed::Yes);
59 assert_eq!(Consumed::from(false), Consumed::No);
60 }
61}