-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #244 from pentamassiv/serde
DSL: Remove the DSL and replace it with serde
- Loading branch information
Showing
14 changed files
with
137 additions
and
372 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use enigo::{ | ||
agent::{Agent, Token}, | ||
Button, Enigo, Key, Settings, | ||
}; | ||
use std::{thread, time::Duration}; | ||
|
||
fn main() { | ||
env_logger::init(); | ||
thread::sleep(Duration::from_secs(2)); | ||
let mut enigo = Enigo::new(&Settings::default()).unwrap(); | ||
|
||
// write text, move the mouse (10/10) relative from the cursors position, scroll | ||
// down, enter the unicode U+1F525 (🔥) and then select all | ||
let tokens = vec![ | ||
Token::Text("Hello World! ❤️".to_string()), | ||
Token::MoveMouse(10, 10, enigo::Coordinate::Rel), | ||
Token::Scroll(5, enigo::Axis::Vertical), | ||
Token::Button(Button::Left, enigo::Direction::Click), | ||
Token::Key(Key::Unicode('🔥'), enigo::Direction::Click), | ||
Token::Key(Key::Control, enigo::Direction::Press), | ||
Token::Key(Key::Unicode('a'), enigo::Direction::Click), | ||
Token::Key(Key::Control, enigo::Direction::Release), | ||
]; | ||
|
||
let serialized = ron::to_string(&tokens).unwrap(); | ||
println!("serialized = {serialized}"); | ||
|
||
let deserialized_tokens: Vec<_> = ron::from_str(&serialized).unwrap(); | ||
for token in &deserialized_tokens { | ||
enigo.execute(token).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::{Axis, Button, Coordinate, Direction, Enigo, InputResult, Key, Keyboard, Mouse}; | ||
|
||
#[cfg(feature = "serde")] | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
pub enum Token { | ||
/// Call the [`Keyboard::text`] fn with the string as text | ||
Text(String), | ||
/// Call the [`Keyboard::key`] fn with the given key and direction | ||
Key(Key, Direction), | ||
/// Call the [`Keyboard::raw`] fn with the given keycode and direction | ||
Raw(u16, Direction), | ||
/// Call the [`Mouse::button`] fn with the given mouse button and direction | ||
Button(Button, Direction), | ||
/// Call the [`Mouse::move_mouse`] fn. The first i32 is the value to move on | ||
/// the x-axis and the second i32 is the value to move on the y-axis. The | ||
/// coordinate defines if the given coordinates are absolute of relative to | ||
/// the current position of the mouse. | ||
MoveMouse(i32, i32, Coordinate), | ||
/// Call the [`Mouse::scroll`] fn. | ||
Scroll(i32, Axis), | ||
} | ||
|
||
pub trait Agent | ||
where | ||
Self: Keyboard, | ||
Self: Mouse, | ||
{ | ||
/// Execute the action associated with the token. A [`Token::Text`] will | ||
/// enter text, a [`Token::Scroll`] will scroll and so forth. Have a look at | ||
/// the documentation of the [`Token`] enum for more information. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Same as the individual functions. Have a look at [`InputResult`] for a | ||
/// list of possible errors | ||
fn execute(&mut self, token: &Token) -> InputResult<()> { | ||
match token { | ||
Token::Text(text) => self.text(text), | ||
Token::Key(key, direction) => self.key(*key, *direction), | ||
Token::Raw(keycode, direction) => self.raw(*keycode, *direction), | ||
Token::Button(button, direction) => self.button(*button, *direction), | ||
Token::MoveMouse(x, y, coordinate) => self.move_mouse(*x, *y, *coordinate), | ||
Token::Scroll(length, axis) => self.scroll(*length, *axis), | ||
} | ||
} | ||
} | ||
|
||
impl Agent for Enigo {} |
Oops, something went wrong.