From 59ecdf1e818985aa744a7020227c2452412d4f2d Mon Sep 17 00:00:00 2001 From: MrTanoshii <47116127+MrTanoshii@users.noreply.github.com> Date: Wed, 23 Nov 2022 04:21:15 +0400 Subject: [PATCH] refactor: merge if statements to reduce nesting --- src/app.rs | 79 +++++++++++++++++++++++++++--------------------------- 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/src/app.rs b/src/app.rs index 6387108..dd84a33 100644 --- a/src/app.rs +++ b/src/app.rs @@ -325,47 +325,48 @@ fn move_to( start_coords: (f64, f64), movement_delay_in_ms: u64, ) { - if app_mode == AppMode::Humanlike { + if app_mode == AppMode::Humanlike + && click_position == ClickPosition::Coord + && is_moving_humanlike + { // Move mouse slowly to saved coordinates if requested - if click_position == ClickPosition::Coord && is_moving_humanlike { - let mut current_x = start_coords.0; - let mut current_y = start_coords.1; - loop { - // horizontal movement: determine whether we need to move left, right or not at all - let delta_x: f64 = if current_x < click_coord.0 { - MOUSE_STEP_POS_X.min(click_coord.0 - current_x) - } else if current_x > click_coord.0 { - MOUSE_STEP_NEG_X.max(click_coord.0 - current_x) - } else { - 0.0 - }; - - // vertical movement: determine whether we need to move up, down or not at all - let delta_y: f64 = if current_y < click_coord.1 { - MOUSE_STEP_POS_Y.min(click_coord.1 - current_y) - } else if current_y > click_coord.1 { - MOUSE_STEP_NEG_Y.max(click_coord.1 - current_y) - } else { - 0.0 - }; - - current_x += delta_x; - current_y += delta_y; - - #[cfg(debug_assertions)] - println!( - "Moving by {:?} / {:?}, new pos: {:?} / {:?}", - delta_x, delta_y, current_x, current_y - ); - send(&EventType::MouseMove { - x: current_x, - y: current_y, - }); + let mut current_x = start_coords.0; + let mut current_y = start_coords.1; + loop { + // horizontal movement: determine whether we need to move left, right or not at all + let delta_x: f64 = if current_x < click_coord.0 { + MOUSE_STEP_POS_X.min(click_coord.0 - current_x) + } else if current_x > click_coord.0 { + MOUSE_STEP_NEG_X.max(click_coord.0 - current_x) + } else { + 0.0 + }; + + // vertical movement: determine whether we need to move up, down or not at all + let delta_y: f64 = if current_y < click_coord.1 { + MOUSE_STEP_POS_Y.min(click_coord.1 - current_y) + } else if current_y > click_coord.1 { + MOUSE_STEP_NEG_Y.max(click_coord.1 - current_y) + } else { + 0.0 + }; + + current_x += delta_x; + current_y += delta_y; - thread::sleep(Duration::from_millis(movement_delay_in_ms)); - if current_x == click_coord.0 && current_y == click_coord.1 { - return; - } + #[cfg(debug_assertions)] + println!( + "Moving by {:?} / {:?}, new pos: {:?} / {:?}", + delta_x, delta_y, current_x, current_y + ); + send(&EventType::MouseMove { + x: current_x, + y: current_y, + }); + + thread::sleep(Duration::from_millis(movement_delay_in_ms)); + if current_x == click_coord.0 && current_y == click_coord.1 { + return; } } }