Skip to content

Commit

Permalink
all: Add Token::Location and `Token::MainDisplay
Browse files Browse the repository at this point in the history
They allow you to check your expectations and help with writing tests
  • Loading branch information
pentamassiv committed Sep 14, 2024
1 parent 7d92afa commit 784b599
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
- win, macOS: Allow marking events that were created by enigo. Have a look at the additional field of the `Settings` struct and the new method `get_marker_value` of the `Enigo` struct (only available on Windows and macOS)
- macOS: Fallback to ASCII-capable keyboard layout for handling non-standard input sources
- macOS: Check if the application has the neccessary permissions. If they are missing, `enigo` will ask the user to grant them. You can change this default behavior with the `Settings` when constructing an `Enigo` struct.
- all: Added `Token::Location` and `Token::MainDisplay` mostly for debugging purposes. They allow you to check if your expectations are correct

## Fixed
- win: Respect the language of the current window to determine the which scancodes to send
- win: Respect the language of the current window to determine which scancodes to send
- win: Send the virtual key and its scan code in the events to work with programs that only look at one of the two
- macOS: Moving the mouse no longer breaks simulating input

Expand Down
35 changes: 35 additions & 0 deletions src/agent.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{Axis, Button, Coordinate, Direction, Enigo, InputResult, Key, Keyboard, Mouse};

use log::error;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -46,6 +47,16 @@ pub enum Token {
#[cfg_attr(feature = "serde", serde(alias = "S"))]
#[cfg_attr(feature = "serde", serde(alias = "s"))]
Scroll(i32, #[cfg_attr(feature = "serde", serde(default))] Axis),
/// Call the [`Mouse::location`] fn and compare the return values with the values of this enum. Log an error if they are not equal
/// This variant contains the EXPECTED location of the mouse
#[cfg_attr(feature = "serde", serde(alias = "L"))]
#[cfg_attr(feature = "serde", serde(alias = "l"))]
Location(i32, i32),
/// Call the [`Mouse::main_display`] fn and compare the return values with the values of this enum. Log an error if they are not equal
/// This variant contains the EXPECTED size of the main display
#[cfg_attr(feature = "serde", serde(alias = "D"))]
#[cfg_attr(feature = "serde", serde(alias = "d"))]
MainDisplay(i32, i32),
}

pub trait Agent
Expand All @@ -69,6 +80,30 @@ where
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),
Token::Location(expected_x, expected_y) => match self.location() {
Ok((actual_x, actual_y)) => {
if actual_x != *expected_x || actual_y != *expected_y {
error!("The mouse is not at the expected location");
}
Ok(())
}
Err(e) => {
error!("There was an error getting the location of the mouse");
Err(e)
}
},
Token::MainDisplay(expected_width, expected_height) => match self.main_display() {
Ok((actual_x, actual_y)) => {
if actual_x != *expected_width || actual_y != *expected_height {
error!("The size of the main display is not what was expected");
}
Ok(())
}
Err(e) => {
error!("There was an error getting the size of the main display");
Err(e)
}
},
}
}
}
Expand Down

0 comments on commit 784b599

Please sign in to comment.