#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FigureInputAction {
Hover { x: f64, y: f64 },
Leave,
DragStart { x: f64, y: f64 },
DragMove { x: f64, y: f64 },
DragEnd { x: f64, y: f64 },
Click { x: f64, y: f64 },
Wheel { delta: f64, x: f64, y: f64 },
}
impl FigureInputAction {
#[inline]
pub fn is_drag(&self) -> bool {
matches!(self, Self::DragStart { .. } | Self::DragMove { .. } | Self::DragEnd { .. })
}
pub fn position(&self) -> Option<(f64, f64)> {
match *self {
Self::Hover { x, y }
| Self::DragStart { x, y }
| Self::DragMove { x, y }
| Self::DragEnd { x, y }
| Self::Click { x, y }
| Self::Wheel { x, y, .. } => Some((x, y)),
Self::Leave => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum FigureOutputAction {
Redraw,
BrushChanged { interval: Option<(f64, f64)> },
HoverChanged,
#[default]
None,
}
impl FigureOutputAction {
#[inline]
pub fn needs_redraw(&self) -> bool {
!matches!(self, Self::None)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_drag_matches_only_drag_lifecycle_variants() {
assert!(FigureInputAction::DragStart { x: 0.0, y: 0.0 }.is_drag());
assert!(FigureInputAction::DragMove { x: 0.0, y: 0.0 }.is_drag());
assert!(FigureInputAction::DragEnd { x: 0.0, y: 0.0 }.is_drag());
assert!(!FigureInputAction::Hover { x: 0.0, y: 0.0 }.is_drag());
assert!(!FigureInputAction::Click { x: 0.0, y: 0.0 }.is_drag());
assert!(!FigureInputAction::Leave.is_drag());
}
#[test]
fn position_extracts_xy_except_for_leave() {
assert_eq!(FigureInputAction::Hover { x: 1.0, y: 2.0 }.position(), Some((1.0, 2.0)));
assert_eq!(FigureInputAction::Wheel { delta: 1.0, x: 3.0, y: 4.0 }.position(), Some((3.0, 4.0)));
assert_eq!(FigureInputAction::Leave.position(), None);
}
#[test]
fn needs_redraw_is_false_only_for_none() {
assert!(FigureOutputAction::Redraw.needs_redraw());
assert!(FigureOutputAction::HoverChanged.needs_redraw());
assert!(FigureOutputAction::BrushChanged { interval: None }.needs_redraw());
assert!(!FigureOutputAction::None.needs_redraw());
assert_eq!(FigureOutputAction::default(), FigureOutputAction::None);
}
}