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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
// https://www.apache.org/licenses/LICENSE-2.0
//! Event handling: IsUsed and Scroll types
use crate::geom::{Offset, Rect};
pub use IsUsed::{Unused, Used};
/// Return type of event-handling methods
///
/// This type is convertible to/from `bool` and supports the expected bit-wise
/// OR operator (`a | b`, `*a |= b`).
///
/// The type also implements negation with output type `bool`, thus allowing
/// `if is_used.into() { ... }` and `if !is_used { ... }`. An implementation of
/// `Deref` would be preferred, but the trait can only output a reference.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum IsUsed {
/// Event was unused
///
/// Unused events may be used by a parent/ancestor widget or passed to
/// another handler until used.
Unused,
/// Event is used, no other result
Used,
}
impl From<bool> for IsUsed {
fn from(is_used: bool) -> Self {
match is_used {
false => Self::Unused,
true => Self::Used,
}
}
}
impl From<IsUsed> for bool {
fn from(is_used: IsUsed) -> bool {
is_used == Used
}
}
impl std::ops::BitOr for IsUsed {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self {
match (self, rhs) {
(Unused, Unused) => Unused,
_ => Used,
}
}
}
impl std::ops::BitOrAssign for IsUsed {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
*self = *self | rhs;
}
}
impl std::ops::Not for IsUsed {
type Output = bool;
#[inline]
fn not(self) -> bool {
self != Used
}
}
/// Request to / notification of scrolling from a child
///
/// See: [`EventCx::set_scroll`](super::EventCx::set_scroll).
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
#[must_use]
pub enum Scroll {
/// No scrolling
#[default]
None,
/// Child has scrolled; no further scrolling needed
///
/// External scroll bars use this as a notification to update self.
Scrolled,
/// Pan region by the given offset
///
/// This may be returned to scroll the closest scrollable ancestor region.
/// This region should attempt to scroll self by this offset, then, if all
/// the offset was used, return `Scroll::Scrolled`, otherwise return
/// `Scroll::Offset(delta)` with the unused offset `delta`.
///
/// With the usual scroll offset conventions, this delta must be subtracted
/// from the scroll offset.
Offset(Offset),
/// Focus the given rect
Rect(Rect),
}