use super::common::{get_mouse_position, set_mouse_position};
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 MouseControlMoveRelativeDriver;
#[async_trait::async_trait]
impl Driver for MouseControlMoveRelativeDriver {
fn name(&self) -> &str {
return "mouse_control_move_relative";
}
fn description(&self) -> &str {
return "Move mouse cursor relative to current position";
}
fn usage_hint(&self) -> &str {
return "Use this skill to move the mouse by a delta from its current position.";
}
fn parameters(&self) -> Vec<DriverParameter> {
return vec![
DriverParameter {
name: "dx".to_string(),
param_type: "integer".to_string(),
description: "Delta X to move (positive=right, negative=left)".to_string(),
required: true,
default: None,
example: Some(json!(100)),
enum_values: None,
},
DriverParameter {
name: "dy".to_string(),
param_type: "integer".to_string(),
description: "Delta Y to move (positive=down, negative=up)".to_string(),
required: true,
default: None,
example: Some(json!(-50)),
enum_values: None,
},
];
}
fn example_call(&self) -> DriverResult<Value> {
return Ok(json!({
"action": "mouse_control_move_relative",
"parameters": {
"dx": 100,
"dy": -50
}
}));
}
fn example_output(&self) -> String {
return "Mouse moved relative by (100, -50)".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_move_relative driver");
let dx = parameters.get("dx").and_then(|v| v.as_i64()).ok_or_else(|| DriverError::missing_parameter("dx"))? as i32;
let dy = parameters.get("dy").and_then(|v| v.as_i64()).ok_or_else(|| DriverError::missing_parameter("dy"))? as i32;
let current = get_mouse_position()?;
let new_x = current.x + dx;
let new_y = current.y + dy;
debug!("Moving mouse from ({}, {}) by ({}, {}) to ({}, {})", current.x, current.y, dx, dy, new_x, new_y);
set_mouse_position(new_x, new_y)?;
let result = format!("Mouse moved relative by ({}, {})", dx, dy);
info!("{}", result);
return Ok(result);
}
fn validate(&self, parameters: &HashMap<String, Value>) -> DriverResult<()> {
parameters.get("dx").and_then(|v| v.as_i64()).ok_or_else(|| DriverError::missing_parameter("dx"))?;
parameters.get("dy").and_then(|v| v.as_i64()).ok_or_else(|| DriverError::missing_parameter("dy"))?;
return Ok(());
}
}