Skip to content

Commit

Permalink
Merge pull request #244 from pentamassiv/serde
Browse files Browse the repository at this point in the history
DSL: Remove the DSL and replace it with serde
  • Loading branch information
pentamassiv authored Nov 17, 2023
2 parents 390f69d + 1299eee commit 1ca349c
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 372 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- All: Renamed `mouse_scroll_event` function to `scroll`
- All: Renamed `mouse_location` function to `location`
- All: Renamed `MouseButton` enum to `Button`
- DSL: The DSL was removed and replaced with the `Agent` trait. Activate the `serde` feature to use it. Have a look at the `serde` example to get an idea how to use it

## Added
- Linux: Support X11 without `xdotools`. Use the experimental feature `x11rb` to test it
Expand Down
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ categories = ["development-tools::testing", "api-bindings", "hardware-support"]
license = "MIT"
exclude = [".github", "examples", ".gitignore", "rustfmt.toml"]

[package.metadata.docs.rs]
all-features = true

[features]
default = ["xdo"]
xdo = []
serde = ["dep:serde"]
wayland = [
"dep:wayland-client",
"dep:wayland-protocols-misc",
"dep:wayland-protocols-wlr",
"dep:wayland-protocols-plasma",
"dep:tempfile",
]
xdo = []

[dependencies]
log = "0.4"
Expand Down Expand Up @@ -70,3 +74,8 @@ env_logger = "0.10"
tungstenite = "0.20"
url = "2"
webbrowser = "0.8"
ron = "0.8"

[[example]]
name = "serde"
required-features = ["serde"]
12 changes: 0 additions & 12 deletions examples/dsl.rs

This file was deleted.

2 changes: 1 addition & 1 deletion examples/key.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use enigo::{
Direction::{Press, Release},
Enigo, Key, Keyboard, Settings,
{Direction::Press, Direction::Release},
};
use std::thread;
use std::time::Duration;
Expand Down
2 changes: 1 addition & 1 deletion examples/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use enigo::{
Direction::{Click, Press, Release},
Enigo, Key, Keyboard, Settings,
{Direction::Click, Direction::Press, Direction::Release},
};
use std::thread;
use std::time::Duration;
Expand Down
5 changes: 3 additions & 2 deletions examples/mouse.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use enigo::{
Button, Enigo, Mouse, Settings,
Button,
Direction::{Click, Press, Release},
Enigo, Mouse, Settings,
{Axis::Horizontal, Axis::Vertical},
{Coordinate::Abs, Coordinate::Rel},
{Direction::Click, Direction::Press, Direction::Release},
};
use std::thread;
use std::time::Duration;
Expand Down
2 changes: 0 additions & 2 deletions examples/platform_specific.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ fn main() {

#[cfg(target_os = "windows")]
{
use enigo::Keyboard;

// windows: Enter divide symbol (slash)
enigo.key(Key::Divide, Click).unwrap();

Expand Down
32 changes: 32 additions & 0 deletions examples/serde.rs
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();
}
}
2 changes: 1 addition & 1 deletion examples/timer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use enigo::{
Direction::{Click, Press, Release},
Enigo, Key, Keyboard, Settings,
{Direction::Click, Direction::Press, Direction::Release},
};
use std::{
thread,
Expand Down
51 changes: 51 additions & 0 deletions src/agent.rs
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 {}
Loading

0 comments on commit 1ca349c

Please sign in to comment.