use crate::style::Point;
#[derive(Clone, Debug)]
pub enum Event {
PointerMove {
position: Point,
finger_id: Option<u64>,
},
PointerDown {
position: Point,
button: MouseButton,
finger_id: Option<u64>,
},
PointerUp {
position: Point,
button: MouseButton,
finger_id: Option<u64>,
},
Click {
position: Point,
button: MouseButton,
finger_id: Option<u64>,
modifiers: Modifiers,
},
DragStart {
position: Point,
button: MouseButton,
finger_id: Option<u64>,
},
DragMove {
position: Point,
delta_x: f32,
delta_y: f32,
button: MouseButton,
finger_id: Option<u64>,
},
DragEnd {
position: Point,
button: MouseButton,
finger_id: Option<u64>,
},
DragCancel {
finger_id: Option<u64>,
},
Scroll {
delta_x: f32,
delta_y: f32,
},
KeyDown {
key: Key,
modifiers: Modifiers,
},
KeyUp {
key: Key,
modifiers: Modifiers,
},
Pinch {
delta: f64,
position: Point,
phase: GesturePhase,
},
Rotate {
delta: f32,
position: Point,
phase: GesturePhase,
},
Custom {
type_id: std::any::TypeId,
payload: std::rc::Rc<dyn std::any::Any>,
},
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum GesturePhase {
Started,
Moved,
Ended,
Cancelled,
}
impl Event {
pub fn finger_id(&self) -> Option<u64> {
match self {
Event::PointerMove { finger_id, .. }
| Event::PointerDown { finger_id, .. }
| Event::PointerUp { finger_id, .. }
| Event::Click { finger_id, .. }
| Event::DragStart { finger_id, .. }
| Event::DragMove { finger_id, .. }
| Event::DragEnd { finger_id, .. }
| Event::DragCancel { finger_id, .. } => *finger_id,
_ => None,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum MouseButton {
Left,
Right,
Middle,
Back,
Forward,
Other(u16),
}
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub enum Key {
Character(String),
Enter,
Tab,
Space,
Backspace,
Delete,
Escape,
ArrowUp,
ArrowDown,
ArrowLeft,
ArrowRight,
Home,
End,
PageUp,
PageDown,
Shift,
Control,
Alt,
Meta,
F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,
Other(String),
}
#[derive(Clone, Copy, Default, PartialEq, Eq, Debug)]
pub struct Modifiers {
pub shift: bool,
pub ctrl: bool,
pub alt: bool,
pub meta: bool,
}
impl Modifiers {
pub const NONE: Self = Self {
shift: false,
ctrl: false,
alt: false,
meta: false,
};
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum EventPhase {
Capture,
Bubble,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum EventStatus {
Ignored,
Consumed,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct KeyHeldInfo {
pub held_duration: std::time::Duration,
pub repeat_count: u32,
}
impl KeyHeldInfo {
pub fn repeat_multiplier(&self) -> f32 {
let ms = self.held_duration.as_millis() as f32;
if ms < 400.0 {
return 1.0;
}
if ms > 2000.0 {
return 4.0;
}
1.0 + (ms - 400.0) / 1600.0 * 3.0
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum DragAxis {
Free,
Horizontal,
Vertical,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum DropType {
Text,
Files,
Custom(String),
}
impl DropType {
pub fn matches(&self, kind: &DragKind) -> bool {
match (self, kind) {
(DropType::Text, DragKind::Text) => true,
(DropType::Files, DragKind::Files) => true,
(DropType::Custom(a), DragKind::Custom(b)) => a == b,
_ => false,
}
}
}
#[derive(Clone, Debug)]
pub struct DragData {
pub kind: DragKind,
pub text: Option<String>,
pub paths: Vec<std::path::PathBuf>,
pub position: Option<Point>,
pub label: Option<String>,
}
impl DragData {
pub fn text(text: impl Into<String>) -> Self {
let t = text.into();
Self {
kind: DragKind::Text,
label: Some(t.clone()),
text: Some(t),
paths: Vec::new(),
position: None,
}
}
pub fn files(paths: Vec<std::path::PathBuf>) -> Self {
Self {
kind: DragKind::Files,
text: None,
label: None,
paths,
position: None,
}
}
pub fn custom(kind: impl Into<String>, data: impl Into<String>) -> Self {
Self {
kind: DragKind::Custom(kind.into()),
text: Some(data.into()),
label: None,
paths: Vec::new(),
position: None,
}
}
pub fn as_text(&self) -> Option<&str> {
self.text.as_deref()
}
}
impl Default for DragData {
fn default() -> Self {
Self {
kind: DragKind::Files,
text: None,
paths: Vec::new(),
position: None,
label: None,
}
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum DragKind {
Text,
Files,
Custom(String),
}
impl DragKind {
pub fn as_custom_str(&self) -> Option<&str> {
match self {
DragKind::Custom(s) => Some(s),
_ => None,
}
}
pub fn to_drop_type(&self) -> DropType {
match self {
DragKind::Text => DropType::Text,
DragKind::Files => DropType::Files,
DragKind::Custom(s) => DropType::Custom(s.clone()),
}
}
}