Skip to main content

kas_core/event/
response.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License in the LICENSE-APACHE file or at:
4//     https://www.apache.org/licenses/LICENSE-2.0
5
6//! Event handling: IsUsed and Scroll types
7
8#[allow(unused)] use super::EventCx;
9use super::components::KineticStart;
10#[allow(unused)] use crate::Events;
11use crate::geom::{Offset, Rect, Vec2};
12
13pub use IsUsed::{Unused, Used};
14
15/// Return type of event-handling methods
16///
17/// This type is convertible to/from `bool` and supports the expected bit-wise
18/// OR operator (`a | b`, `*a |= b`).
19///
20/// The type also implements negation with output type `bool`, thus allowing
21/// `if is_used.into() { ... }` and `if !is_used { ... }`. An implementation of
22/// `Deref` would be preferred, but the trait can only output a reference.
23#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
24pub enum IsUsed {
25    /// Event was unused
26    ///
27    /// Unused events may be used by a parent/ancestor widget or passed to
28    /// another handler until used.
29    Unused,
30    /// Event is used, no other result
31    Used,
32}
33
34impl From<bool> for IsUsed {
35    fn from(is_used: bool) -> Self {
36        match is_used {
37            false => Self::Unused,
38            true => Self::Used,
39        }
40    }
41}
42
43impl From<IsUsed> for bool {
44    fn from(is_used: IsUsed) -> bool {
45        is_used == Used
46    }
47}
48
49impl std::ops::BitOr for IsUsed {
50    type Output = Self;
51    #[inline]
52    fn bitor(self, rhs: Self) -> Self {
53        match (self, rhs) {
54            (Unused, Unused) => Unused,
55            _ => Used,
56        }
57    }
58}
59impl std::ops::BitOrAssign for IsUsed {
60    #[inline]
61    fn bitor_assign(&mut self, rhs: Self) {
62        *self = *self | rhs;
63    }
64}
65
66impl std::ops::Not for IsUsed {
67    type Output = bool;
68    #[inline]
69    fn not(self) -> bool {
70        self != Used
71    }
72}
73
74/// Scroll notification and requests
75///
76/// This is used by [`EventCx::set_scroll`] and [`Events::handle_scroll`].
77#[derive(Clone, Debug, Default, PartialEq)]
78#[must_use]
79pub enum Scroll {
80    /// No scrolling
81    #[default]
82    None,
83    /// A child has scrolled
84    ///
85    /// No further scrolling is needed but external scroll bars may need to
86    /// update themselves.
87    Scrolled,
88    /// Pan by the given offset
89    ///
90    /// This is returned when pointer motion should cause scrolling but the
91    /// widget handling the motion event is unable to scroll itself.
92    /// The handler should attempt to scroll itself by this offset then set
93    /// `Scroll::Offset(remainder)` (if non-zero) or `Scroll::Scrolled`.
94    ///
95    /// With the usual scroll offset conventions, this delta must be subtracted
96    /// from the scroll offset.
97    Offset(Vec2),
98    /// Start kinetic scrolling
99    ///
100    /// This is returned when pointer motion should cause kinetic scrolling but
101    /// the widget handling the motion event is unable to scroll itself.
102    Kinetic(KineticStart),
103    /// Focus the given rect
104    ///
105    /// This is specified in the child's coordinate space. It is assumed that
106    /// any parent with non-zero translation will intercept this value and
107    /// either consume or translate it.
108    Rect(Rect),
109}
110
111impl std::ops::Sub<Offset> for Scroll {
112    type Output = Self;
113
114    #[inline]
115    fn sub(self, rhs: Offset) -> Self {
116        match self {
117            Scroll::Rect(rect) => Scroll::Rect(rect - rhs),
118            other => other,
119        }
120    }
121}