use super::common::{MouseButton, get_mouse_position, mouse_click};
use crate::{
DriverCallback, DriverCategory, DriverContext, DriverError, DriverResult,
types::{Driver, DriverParameter},
};
use serde_json::{Value, json};
use std::collections::HashMap;
use tracing::{debug, info};
#[derive(Debug)]
pub struct MouseControlClickDriver;
#[async_trait::async_trait]
impl Driver for MouseControlClickDriver {
fn name(&self) -> &str {
return "mouse_control_click";
}
fn description(&self) -> &str {
return "Click at the current mouse position or specified coordinates";
}
fn usage_hint(&self) -> &str {
return "Use this skill to perform a left mouse click. Optionally specify x and y coordinates to move before clicking.";
}
fn parameters(&self) -> Vec<DriverParameter> {
return vec![
DriverParameter {
name: "x".to_string(),
param_type: "integer".to_string(),
description: "X coordinate to click at".to_string(),
required: false,
default: None,
example: Some(Value::Number(500.into())),
enum_values: None,
},
DriverParameter {
name: "y".to_string(),
param_type: "integer".to_string(),
description: "Y coordinate to click at".to_string(),
required: false,
default: None,
example: Some(Value::Number(300.into())),
enum_values: None,
},
];
}
fn example_call(&self) -> DriverResult<Value> {
return Ok(json!({
"action": "mouse_control_click",
"parameters": {
"x": 500,
"y": 300
}
}));
}
fn example_output(&self) -> String {
return "Mouse clicked at (500, 300)".to_string();
}
fn category(&self) -> DriverCategory {
return DriverCategory::Mouse;
}
async fn execute(
&self,
parameters: &HashMap<String, Value>,
_callback: Option<&dyn DriverCallback>,
_context: Option<&DriverContext>,
) -> DriverResult<String> {
debug!("Executing mouse_control_click driver");
let x = parameters.get("x").and_then(|v| v.as_i64()).map(|v| v as i32);
let y = parameters.get("y").and_then(|v| v.as_i64()).map(|v| v as i32);
let (click_x, click_y) = if let (Some(px), Some(py)) = (x, y) {
(px, py)
} else {
let pos = get_mouse_position()?;
(pos.x, pos.y)
};
debug!("Clicking at ({}, {})", click_x, click_y);
mouse_click(MouseButton::Left, click_x, click_y)?;
let result = format!("Mouse clicked at ({}, {})", click_x, click_y);
info!("{}", result);
return Ok(result);
}
fn validate(&self, _parameters: &HashMap<String, Value>) -> DriverResult<()> {
return Ok(());
}
}