use crate::app::events::MouseButton;
use crate::app::runtime::Tree;
use crate::app::events::ScrollPhase;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct SyntheticModifiers {
pub shift: bool,
pub ctrl: bool,
pub alt: bool,
pub meta: bool,
}
impl SyntheticModifiers {
pub fn shift(mut self) -> Self {
self.shift = true;
self
}
pub fn ctrl(mut self) -> Self {
self.ctrl = true;
self
}
pub fn alt(mut self) -> Self {
self.alt = true;
self
}
pub fn meta(mut self) -> Self {
self.meta = true;
self
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum SyntheticInputKind {
MouseMove {
x: f32,
y: f32,
},
MouseDown {
x: f32,
y: f32,
button: MouseButton,
},
MouseUp {
x: f32,
y: f32,
button: MouseButton,
},
Click {
x: f32,
y: f32,
button: MouseButton,
},
Wheel {
x: f32,
y: f32,
delta_x: f32,
delta_y: f32,
},
KeyDown {
key: String,
code: String,
},
KeyUp {
key: String,
code: String,
},
Char(char),
}
#[derive(Clone, Debug, PartialEq)]
pub struct SyntheticInput {
kind: SyntheticInputKind,
modifiers: SyntheticModifiers,
}
impl SyntheticInput {
pub fn new(kind: SyntheticInputKind) -> Self {
Self {
kind,
modifiers: SyntheticModifiers::default(),
}
}
pub fn with_modifiers(mut self, modifiers: SyntheticModifiers) -> Self {
self.modifiers = modifiers;
self
}
pub fn kind(&self) -> &SyntheticInputKind {
&self.kind
}
pub fn modifiers(&self) -> SyntheticModifiers {
self.modifiers
}
pub fn mouse_move(x: f32, y: f32) -> Self {
Self::new(SyntheticInputKind::MouseMove { x, y })
}
pub fn mouse_down(x: f32, y: f32, button: MouseButton) -> Self {
Self::new(SyntheticInputKind::MouseDown { x, y, button })
}
pub fn mouse_up(x: f32, y: f32, button: MouseButton) -> Self {
Self::new(SyntheticInputKind::MouseUp { x, y, button })
}
pub fn click(x: f32, y: f32) -> Self {
Self::new(SyntheticInputKind::Click {
x,
y,
button: MouseButton::Left,
})
}
pub fn click_button(x: f32, y: f32, button: MouseButton) -> Self {
Self::new(SyntheticInputKind::Click { x, y, button })
}
pub fn wheel(x: f32, y: f32, delta_x: f32, delta_y: f32) -> Self {
Self::new(SyntheticInputKind::Wheel {
x,
y,
delta_x,
delta_y,
})
}
pub fn key_down(key: impl Into<String>) -> Self {
let key = key.into();
Self::new(SyntheticInputKind::KeyDown {
code: physical_code_for(&key),
key,
})
}
pub fn key_up(key: impl Into<String>) -> Self {
let key = key.into();
Self::new(SyntheticInputKind::KeyUp {
code: physical_code_for(&key),
key,
})
}
pub fn char(character: char) -> Self {
Self::new(SyntheticInputKind::Char(character))
}
pub fn text(value: &str) -> Vec<Self> {
value.chars().map(Self::char).collect()
}
}
pub fn apply(tree: &mut Tree, input: &SyntheticInput) {
let m = input.modifiers();
match input.kind() {
SyntheticInputKind::MouseMove { x, y } => {
tree.mouse_move_with_modifiers(*x, *y, m.shift, m.ctrl, m.alt);
}
SyntheticInputKind::MouseDown { x, y, button } => {
tree.mouse_down_with_modifiers(*x, *y, *button, m.shift, m.ctrl, m.alt);
}
SyntheticInputKind::MouseUp { x, y, button } => {
tree.mouse_up_with_modifiers(*x, *y, *button, m.shift, m.ctrl, m.alt);
}
SyntheticInputKind::Click { x, y, button } => {
tree.mouse_move_with_modifiers(*x, *y, m.shift, m.ctrl, m.alt);
tree.mouse_down_with_modifiers(*x, *y, *button, m.shift, m.ctrl, m.alt);
tree.mouse_up_with_modifiers(*x, *y, *button, m.shift, m.ctrl, m.alt);
}
SyntheticInputKind::Wheel {
x,
y,
delta_x,
delta_y,
} => {
tree.mouse_move_with_modifiers(*x, *y, m.shift, m.ctrl, m.alt);
tree.scroll(*x, *y, 0.0, 0.0, ScrollPhase::Start);
tree.scroll(*x, *y, *delta_x, *delta_y, ScrollPhase::Scroll);
tree.scroll(*x, *y, 0.0, 0.0, ScrollPhase::End);
}
SyntheticInputKind::KeyDown { key, code } => {
tree.key_down_with_meta(key.clone(), code.clone(), m.shift, m.ctrl, m.alt, m.meta);
}
SyntheticInputKind::KeyUp { key, code } => {
tree.key_up_with_meta(key.clone(), code.clone(), m.shift, m.ctrl, m.alt, m.meta);
}
SyntheticInputKind::Char(character) => {
let key = character.to_string();
let code = physical_code_for(&key);
tree.key_down_with_meta(key.clone(), code.clone(), m.shift, m.ctrl, m.alt, m.meta);
tree.key_up_with_meta(key, code, m.shift, m.ctrl, m.alt, m.meta);
}
}
}
fn physical_code_for(key: &str) -> String {
let mut chars = key.chars();
match (chars.next(), chars.next()) {
(Some(single), None) if single.is_ascii_alphabetic() => {
format!("Key{}", single.to_ascii_uppercase())
}
(Some(single), None) if single.is_ascii_digit() => format!("Digit{single}"),
_ => key.to_owned(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn letters_and_digits_get_physical_codes() {
assert_eq!(physical_code_for("a"), "KeyA");
assert_eq!(physical_code_for("Z"), "KeyZ");
assert_eq!(physical_code_for("7"), "Digit7");
assert_eq!(physical_code_for("Enter"), "Enter");
assert_eq!(physical_code_for("ArrowDown"), "ArrowDown");
}
#[test]
fn text_expands_to_one_event_per_character() {
let events = SyntheticInput::text("hi!");
assert_eq!(events.len(), 3);
assert_eq!(events[0].kind(), &SyntheticInputKind::Char('h'));
assert_eq!(events[2].kind(), &SyntheticInputKind::Char('!'));
}
#[test]
fn modifiers_are_carried_with_the_event() {
let event = SyntheticInput::click(1.0, 2.0)
.with_modifiers(SyntheticModifiers::default().shift().ctrl());
assert!(event.modifiers().shift);
assert!(event.modifiers().ctrl);
assert!(!event.modifiers().alt);
}
}