use crate::mouse::mouse_input;
use crate::script_engine::instruction::{
InstructionData, InstructionHandler, InstructionMetadata, ScriptError,
};
use crate::script_engine::VMContext;
use crate::utils::sleep_ms;
#[derive(Clone)]
pub struct DbClickParams {
pub x: Option<i32>,
pub y: Option<i32>,
pub delay_ms: u32,
}
pub struct DbClickHandler;
impl InstructionHandler for DbClickHandler {
fn name(&self) -> &str {
"dbclick"
}
#[inline]
fn parse(&self, args: &[&str]) -> Result<InstructionData, ScriptError> {
let mut x: Option<i32> = None;
let mut y: Option<i32> = None;
let mut delay_ms: u32 = 0;
match args.len() {
0 => {}
1 => {
if let Ok(val) = args[0].parse::<i32>() {
if val < 0 {
return Err(ScriptError::ParseError(
format!("Coordinates cannot be negative: {}", val).into(),
));
}
x = Some(val);
} else {
return Err(ScriptError::ParseError(
format!("Invalid argument '{}'. Expected dbclick [x] [y] [delay_ms]", args[0]).into(),
));
}
}
2 => {
if let Ok(val) = args[0].parse::<i32>() {
if val < 0 {
return Err(ScriptError::ParseError(
format!("Coordinates cannot be negative: {}", val).into(),
));
}
x = Some(val);
} else {
return Err(ScriptError::ParseError(
format!("Invalid argument '{}'. Expected dbclick [x] [y] [delay_ms]", args[0]).into(),
));
}
if let Ok(val) = args[1].parse::<i32>() {
if val < 0 {
return Err(ScriptError::ParseError(
format!("Coordinates cannot be negative: {}", val).into(),
));
}
y = Some(val);
} else {
return Err(ScriptError::ParseError(
format!("Invalid argument '{}'. Expected dbclick [x] [y] [delay_ms]", args[1]).into(),
));
}
}
_ => {
if let Ok(val) = args[0].parse::<i32>() {
if val < 0 {
return Err(ScriptError::ParseError(
format!("Coordinates cannot be negative: {}", val).into(),
));
}
x = Some(val);
} else {
return Err(ScriptError::ParseError(
format!("Invalid argument '{}'. Expected dbclick [x] [y] [delay_ms]", args[0]).into(),
));
}
if let Ok(val) = args[1].parse::<i32>() {
if val < 0 {
return Err(ScriptError::ParseError(
format!("Coordinates cannot be negative: {}", val).into(),
));
}
y = Some(val);
} else {
return Err(ScriptError::ParseError(
format!("Invalid argument '{}'. Expected dbclick [x] [y] [delay_ms]", args[1]).into(),
));
}
if let Ok(val) = args[2].parse::<u32>() {
delay_ms = val;
} else if let Ok(val) = args[2].parse::<i32>() {
if val >= 0 {
delay_ms = val as u32;
}
}
}
}
if let (Some(x_val), Some(y_val)) = (x, y) {
if x_val < 0 || y_val < 0 {
return Err(ScriptError::ParseError(
format!("Coordinates cannot be negative: ({}, {})", x_val, y_val).into(),
));
}
}
Ok(InstructionData::Custom(Box::new(DbClickParams { x, y, delay_ms })))
}
#[inline]
fn execute(
&self,
vm: &mut VMContext,
data: &InstructionData,
_metadata: Option<&InstructionMetadata>,
) -> Result<(), ScriptError> {
let params = data.extract_custom::<DbClickParams>("Invalid dbclick parameters")?;
if let (Some(x), Some(y)) = (params.x, params.y) {
let (screen_x, screen_y) = match vm.process.window_geometry {
Some(geo) => (x + geo.window_left, y + geo.window_top),
None => (x, y),
};
mouse_input::set_cursor_pos(screen_x, screen_y).map_err(|e| {
ScriptError::ExecutionError(format!("SetCursorPos failed: {:?}", e))
})?;
}
let click_inputs = mouse_input::build_click_left();
mouse_input::execute_inputs(&click_inputs).map_err(|e| {
ScriptError::ExecutionError(format!("Double-click failed: {:?}", e))
})?;
sleep_ms(50);
mouse_input::execute_inputs(&click_inputs).map_err(|e| {
ScriptError::ExecutionError(format!("Double-click failed: {:?}", e))
})?;
if params.delay_ms > 0 {
sleep_ms(params.delay_ms);
}
Ok(())
}
}