Skip to content

Commit

Permalink
Switch pressed_buttons function to oneliner
Browse files Browse the repository at this point in the history
Also reformated code according to nightly rustfmt, except to a buggy comment wrap.
  • Loading branch information
paulora2405 committed Nov 23, 2023
1 parent 47a437a commit b6379cf
Showing 1 changed file with 21 additions and 37 deletions.
58 changes: 21 additions & 37 deletions src/macos/macos_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ impl Mouse for Enigo {
let Ok(event) =
CGEvent::new_mouse_event(self.event_source.clone(), event_type, dest, button)
else {
return Err(InputError::Simulate(
"failed creating event to enter mouse button",
));
return Err(InputError::Simulate("failed creating event to enter mouse button"));
};
event.set_integer_value_field(EventField::MOUSE_EVENT_CLICK_STATE, click_count);

Expand All @@ -200,9 +198,7 @@ impl Mouse for Enigo {
let Ok(event) =
CGEvent::new_mouse_event(self.event_source.clone(), event_type, dest, button)
else {
return Err(InputError::Simulate(
"failed creating event to enter mouse button",
));
return Err(InputError::Simulate("failed creating event to enter mouse button"));
};

event.set_integer_value_field(EventField::MOUSE_EVENT_CLICK_STATE, click_count);
Expand All @@ -214,7 +210,7 @@ impl Mouse for Enigo {

fn move_mouse(&mut self, x: i32, y: i32, coordinate: Coordinate) -> InputResult<()> {
debug!("\x1b[93mmove_mouse(x: {x:?}, y: {y:?}, coordinate:{coordinate:?})\x1b[0m");
let pressed = Self::pressed_buttons()?;
let pressed = unsafe { AppKit::NSEvent::pressedMouseButtons() };
let (current_x, current_y) = self.location()?;

let (absolute, relative) = match coordinate {
Expand All @@ -228,16 +224,16 @@ impl Mouse for Enigo {
} else if pressed & 2 > 0 {
(CGEventType::RightMouseDragged, CGMouseButton::Right)
} else {
(CGEventType::MouseMoved, CGMouseButton::Left) // The mouse button here is ignored so it can be anything
(CGEventType::MouseMoved, CGMouseButton::Left) // The mouse button
// here is ignored so
// it can be anything
};

let dest = CGPoint::new(absolute.0 as f64, absolute.1 as f64);
let Ok(event) =
CGEvent::new_mouse_event(self.event_source.clone(), event_type, dest, button)
else {
return Err(InputError::Simulate(
"failed creating event to move the mouse",
));
return Err(InputError::Simulate("failed creating event to move the mouse"));
};

// Add information by how much the mouse was moved
Expand All @@ -262,14 +258,16 @@ impl Mouse for Enigo {
Axis::Vertical => (1, -length, 0),
};

let Ok(event) = CGEvent::new_scroll_event(
self.event_source.clone(),
ScrollEventUnit::LINE,
ax,
len_x,
len_y,
0,
) else {
let Ok(event) =
CGEvent::new_scroll_event(
self.event_source.clone(),
ScrollEventUnit::LINE,
ax,
len_x,
len_y,
0,
)
else {
return Err(InputError::Simulate("failed creating event to scroll"));
};

Expand Down Expand Up @@ -323,9 +321,7 @@ impl Keyboard for Enigo {
// event.set_string(chunk)) truncates strings down to 20 characters
for chunk in chunks(text, 20) {
let Ok(event) = CGEvent::new_keyboard_event(self.event_source.clone(), 0, true) else {
return Err(InputError::Simulate(
"failed creating event to enter the text",
));
return Err(InputError::Simulate("failed creating event to enter the text"));
};
event.set_string(chunk);

Expand All @@ -343,9 +339,7 @@ impl Keyboard for Enigo {
}

let Ok(keycode) = CGKeyCode::try_from(key) else {
return Err(InputError::InvalidInput(
"virtual keycodes on macOS have to fit into u16",
));
return Err(InputError::InvalidInput("virtual keycodes on macOS have to fit into u16"));
};
self.raw(keycode, direction)?;

Expand Down Expand Up @@ -373,9 +367,7 @@ impl Keyboard for Enigo {
thread::sleep(Duration::from_millis(self.delay));
let Ok(event) = CGEvent::new_keyboard_event(self.event_source.clone(), keycode, true)
else {
return Err(InputError::Simulate(
"failed creating event to press the key",
));
return Err(InputError::Simulate("failed creating event to press the key"));
};

event.post(CGEventTapLocation::HID);
Expand All @@ -385,9 +377,7 @@ impl Keyboard for Enigo {
thread::sleep(Duration::from_millis(self.delay));
let Ok(event) = CGEvent::new_keyboard_event(self.event_source.clone(), keycode, false)
else {
return Err(InputError::Simulate(
"failed creating event to release the key",
));
return Err(InputError::Simulate("failed creating event to release the key"));
};

event.post(CGEventTapLocation::HID);
Expand Down Expand Up @@ -465,12 +455,6 @@ impl Enigo {
self.held.clone()
}

#[allow(clippy::unnecessary_wraps)]
fn pressed_buttons() -> InputResult<usize> {
let pressed_buttons = unsafe { AppKit::NSEvent::pressedMouseButtons() };
Ok(pressed_buttons)
}

// On macOS, we have to determine ourselves if it was a double click of a mouse
// button. The Enigo struct stores the information needed to do so. This
// function checks if the button was pressed down again fast enough to issue a
Expand Down

0 comments on commit b6379cf

Please sign in to comment.