use super::{
resolve::{ref_center_physical, ref_scroll_bounds, resolve_ref},
windows::{requested_window, window_tree_mut},
};
use crate::{
app::{
App, Tree,
events::{ClipboardContent, ClipboardImage, DraggedPaths, MouseButton},
synthetic_input::{self, SyntheticInput, SyntheticModifiers},
},
core::NodeId,
layout::{layout_kind::LayoutKind, layout_result::LayoutResult},
mcp::{
McpState,
sessions::SessionId,
shared::{McpToolOutput, McpToolResult},
},
node::node::Node,
};
pub(super) fn parse_modifiers(args: &serde_json::Value) -> SyntheticModifiers {
let mut modifiers = SyntheticModifiers::default();
if let Some(list) = args.get("modifiers").and_then(|value| value.as_array()) {
for entry in list.iter().filter_map(|value| value.as_str()) {
match entry {
"shift" => modifiers.shift = true,
"ctrl" => modifiers.ctrl = true,
"alt" => modifiers.alt = true,
"meta" => modifiers.meta = true,
_ => {}
}
}
}
modifiers
}
pub(super) fn parse_button(args: &serde_json::Value) -> MouseButton {
match args.get("button").and_then(|value| value.as_str()) {
Some("right") => MouseButton::Right,
Some("middle") => MouseButton::Middle,
Some("back") => MouseButton::Back,
Some("forward") => MouseButton::Forward,
_ => MouseButton::Left,
}
}
pub(super) fn arg_f32(args: &serde_json::Value, name: &str) -> Option<f32> {
args
.get(name)
.and_then(|value| value.as_f64())
.map(|value| value as f32)
}
pub(super) fn resolve_point(
tree: &mut Tree,
state: &McpState,
args: &serde_json::Value,
ref_key: &str,
x_key: &str,
y_key: &str,
) -> Result<(String, Option<(f32, f32)>), String> {
if let Some(ref_id) = args.get(ref_key).and_then(|value| value.as_str()) {
let resolved = resolve_ref(state, ref_id)?;
let target = window_tree_mut(tree, &resolved.window, state.include_devtools)?;
let point = ref_center_physical(target, &resolved, ref_id)?;
return Ok((resolved.window, Some(point)));
}
let window = requested_window(args);
match (arg_f32(args, x_key), arg_f32(args, y_key)) {
(Some(x), Some(y)) => Ok((window, Some((x, y)))),
_ => Ok((window, None)),
}
}
pub(super) fn interact_tool(
tree: &mut Tree,
app: &App,
state: &McpState,
args: &serde_json::Value,
session: Option<SessionId>,
) -> McpToolResult {
let action = args
.get("action")
.and_then(|value| value.as_str())
.ok_or("`action` is required")?;
if let Some(result) = super::held::held_action(tree, state, args, action, session) {
return result;
}
let modifiers = super::held::action_modifiers(tree, state, args);
let button = parse_button(args);
let apply_all = |target: &mut Tree, inputs: Vec<SyntheticInput>| {
for input in inputs {
synthetic_input::apply(target, &input.with_modifiers(modifiers));
}
target.request_redraw();
};
match action {
"request_close" => {
let window = requested_window(args);
let target = window_tree_mut(tree, &window, state.include_devtools)?;
target
.window()
.dispatch_close_request(crate::app::CloseRequestSource::App);
target.request_redraw();
let close_queued = target.window().close_queued();
Ok(McpToolOutput::Json(serde_json::json!({
"ok": true, "window": window, "close_queued": close_queued,
"stayed_open": !close_queued,
"note": "Snapshot after handler dispatch; a retained request may proceed later. OS teardown is asynchronous."
})))
}
"menu_activate" => {
let id = args
.get("id")
.and_then(|v| v.as_str())
.ok_or("menu_activate needs `id`")?;
let activated = app.shared.menu.dispatch(id, tree.window(), tree.window());
tree.request_redraw();
Ok(McpToolOutput::Json(
serde_json::json!({ "id": id, "activated": activated }),
))
}
"click" | "double_click" | "move" => {
let (window, point) = resolve_point(tree, state, args, "ref", "x", "y")?;
let (x, y) = point.ok_or("this action needs a `ref` or `x`/`y` coordinates")?;
let target = window_tree_mut(tree, &window, state.include_devtools)?;
let inputs = match action {
"click" => vec![SyntheticInput::click_button(x, y, button)],
"double_click" => vec![
SyntheticInput::click_button(x, y, button),
SyntheticInput::click_button(x, y, button),
],
_ => vec![SyntheticInput::mouse_move(x, y)],
};
apply_all(target, inputs);
Ok(McpToolOutput::Json(serde_json::json!({
"ok": true, "action": action, "window": window, "x": x, "y": y
})))
}
"drag" => {
let (window, point) = resolve_point(tree, state, args, "ref", "x", "y")?;
let (from_x, from_y) = point.ok_or("drag needs a start `ref` or `x`/`y`")?;
let (to_window, to_point) = resolve_point(tree, state, args, "to_ref", "to_x", "to_y")?;
let (to_x, to_y) = to_point.ok_or("drag needs an end `to_ref` or `to_x`/`to_y`")?;
if args.get("to_ref").is_some() && to_window != window {
return Err("drag start and end must be in the same window".into());
}
let target = window_tree_mut(tree, &window, state.include_devtools)?;
let mut inputs = vec![
SyntheticInput::mouse_move(from_x, from_y),
SyntheticInput::mouse_down(from_x, from_y, button),
];
const STEPS: u32 = 6;
for step in 1..=STEPS {
let t = step as f32 / STEPS as f32;
inputs.push(SyntheticInput::mouse_move(
from_x + (to_x - from_x) * t,
from_y + (to_y - from_y) * t,
));
}
inputs.push(SyntheticInput::mouse_up(to_x, to_y, button));
apply_all(target, inputs);
Ok(McpToolOutput::Json(serde_json::json!({
"ok": true, "action": "drag", "window": window,
"from": [from_x, from_y], "to": [to_x, to_y]
})))
}
"wheel" => {
let (window, point) = resolve_point(tree, state, args, "ref", "x", "y")?;
let (x, y) = point.ok_or("wheel needs a `ref` or `x`/`y`")?;
let delta_x = arg_f32(args, "delta_x").unwrap_or(0.0);
let delta_y = arg_f32(args, "delta_y").unwrap_or(0.0);
if delta_x == 0.0 && delta_y == 0.0 {
return Err("wheel needs a non-zero delta_x or delta_y".into());
}
let target = window_tree_mut(tree, &window, state.include_devtools)?;
apply_all(target, vec![SyntheticInput::wheel(x, y, delta_x, delta_y)]);
Ok(McpToolOutput::Json(
serde_json::json!({ "ok": true, "action": "wheel", "window": window }),
))
}
"key" => {
let key = args
.get("key")
.and_then(|value| value.as_str())
.ok_or("key action needs `key`")?;
let window = requested_window(args);
let target = window_tree_mut(tree, &window, state.include_devtools)?;
apply_all(target, vec![SyntheticInput::key_down(key), SyntheticInput::key_up(key)]);
Ok(McpToolOutput::Json(
serde_json::json!({ "ok": true, "action": "key", "key": key, "window": window }),
))
}
"type" => {
let text = args
.get("text")
.and_then(|value| value.as_str())
.ok_or("type action needs `text`")?;
let (window, point) = resolve_point(tree, state, args, "ref", "x", "y")?;
let target = window_tree_mut(tree, &window, state.include_devtools)?;
let mut inputs = Vec::new();
if let Some((x, y)) = point
&& args.get("ref").is_some()
{
inputs.push(SyntheticInput::click(x, y));
}
inputs.extend(SyntheticInput::text(text));
apply_all(target, inputs);
Ok(McpToolOutput::Json(
serde_json::json!({ "ok": true, "action": "type", "window": window }),
))
}
"history_back" | "history_forward" => {
use crate::app::{
events::{NavigationCommand, NavigationSource},
window::NavigationOrigin,
};
let command = if action == "history_back" {
NavigationCommand::Back
} else {
NavigationCommand::Forward
};
let (window, point) = resolve_point(tree, state, args, "ref", "x", "y")?;
let target = window_tree_mut(tree, &window, state.include_devtools)?;
let scale = target.scale_factor();
let origin = match point {
Some((x, y)) => NavigationOrigin::Point(x / scale, y / scale),
None => NavigationOrigin::Focus,
};
let taken = target.navigate_from_input(command, NavigationSource::App, origin);
target.request_redraw();
Ok(McpToolOutput::Json(serde_json::json!({
"ok": true, "action": action, "window": window, "taken": taken,
})))
}
"paste" => {
let text = args.get("text").and_then(|value| value.as_str());
let files = path_list(args, "files")?;
let image = args.get("image").map(pasted_image).transpose()?;
if text.is_none() && files.is_empty() && image.is_none() {
return Err(
"paste needs `text`, `files` and/or `image`; the OS clipboard is never read for a synthetic paste".into(),
);
}
let mut content = ClipboardContent::files(files);
if let Some(text) = text {
content = content.with_text(text);
}
if let Some(image) = image {
content = content.with_image(image);
}
let window = requested_window(args);
let target = window_tree_mut(tree, &window, state.include_devtools)?;
let kinds = content.kinds();
apply_all(target, vec![SyntheticInput::paste(content)]);
Ok(McpToolOutput::Json(serde_json::json!({
"ok": true, "action": "paste", "window": window,
"text": kinds.text, "files": kinds.files, "image": kinds.image,
"note": "delivered to the focused text input and its ancestors' on_paste handlers; nothing without a focused input"
})))
}
"drag_files" | "drop_files" => {
let paths = path_list(args, "paths")?;
if paths.is_empty() {
return Err(format!("{action} needs `paths`"));
}
let (window, point) = resolve_point(tree, state, args, "ref", "x", "y")?;
let (x, y) = point.ok_or("this action needs a `ref` or `x`/`y` coordinates")?;
let target = window_tree_mut(tree, &window, state.include_devtools)?;
let paths = DraggedPaths::from_paths(paths);
let count = paths.len();
let folders = paths.folders().count();
let mut reply = serde_json::json!({
"ok": true, "action": action, "window": window, "x": x, "y": y,
"paths": count, "folders": folders
});
if action == "drop_files" {
let handled = crate::app::events::with_synthetic_input(|| target.file_drop(paths, x, y));
target.request_redraw();
reply["handled"] = serde_json::Value::Bool(handled);
} else {
apply_all(target, vec![SyntheticInput::file_drag_enter(paths, x, y)]);
reply["accepted"] = serde_json::json!(target.file_drag_accepted());
}
Ok(McpToolOutput::Json(reply))
}
"file_drag_leave" => {
let window = requested_window(args);
let target = window_tree_mut(tree, &window, state.include_devtools)?;
let had_drag = target.has_file_drag();
apply_all(target, vec![SyntheticInput::file_drag_leave()]);
Ok(McpToolOutput::Json(serde_json::json!({
"ok": true, "action": "file_drag_leave", "window": window, "had_drag": had_drag
})))
}
"scroll_to" => {
let ref_id = args
.get("ref")
.and_then(|value| value.as_str())
.ok_or("scroll_to needs a `ref`")?;
let resolved = resolve_ref(state, ref_id)?;
let target = window_tree_mut(tree, &resolved.window, state.include_devtools)?;
let item_bounds = match resolved.canvas_item {
Some(_) => Some(ref_scroll_bounds(target, &resolved, ref_id)?),
None => None,
};
scroll_into_view(target, resolved.node_id, item_bounds, ref_id)?;
target.request_redraw();
Ok(McpToolOutput::Json(serde_json::json!({
"ok": true, "action": "scroll_to", "window": resolved.window,
"note": "scroll containers adjusted; element positions changed — re-read the tree before coordinate-based actions"
})))
}
other => Err(format!("unknown action {other:?}")),
}
}
fn pasted_image(value: &serde_json::Value) -> Result<ClipboardImage, String> {
let path = value.as_str().ok_or("`image` must be the path of an image file")?;
let decoded = image::open(path)
.map_err(|error| format!("`image` {path}: {error}"))?
.to_rgba8();
let (width, height) = decoded.dimensions();
ClipboardImage::from_rgba(width, height, decoded.into_raw()).map_err(|error| format!("`image` {path}: {error}"))
}
fn path_list(args: &serde_json::Value, name: &str) -> Result<Vec<std::path::PathBuf>, String> {
let Some(list) = args.get(name) else {
return Ok(Vec::new());
};
let list = list
.as_array()
.ok_or_else(|| format!("`{name}` must be an array of path strings"))?;
list
.iter()
.map(|value| {
value
.as_str()
.map(std::path::PathBuf::from)
.ok_or_else(|| format!("`{name}` must be an array of path strings"))
})
.collect()
}
pub(super) fn scroll_into_view(
tree: &Tree,
node_id: NodeId,
target_bounds: Option<[f32; 4]>,
ref_id: &str,
) -> Result<(), String> {
struct ScrollAncestor<'n> {
state: &'n crate::layout::layout_kind::ScrollState,
viewport: [f32; 4],
}
fn walk<'n>(
node: &'n Node,
layout: &LayoutResult,
abs: (f32, f32),
node_id: NodeId,
ancestors: &mut Vec<ScrollAncestor<'n>>,
) -> Option<[f32; 4]> {
if node.node_id() == node_id {
return Some([abs.0, abs.1, layout.size.width, layout.size.height]);
}
let is_scroll = matches!(node.layout_kind(), LayoutKind::ScrollModifier { .. });
if let LayoutKind::ScrollModifier { state, .. } = node.layout_kind() {
ancestors.push(ScrollAncestor {
state,
viewport: [abs.0, abs.1, layout.size.width, layout.size.height],
});
}
for (index, child) in node.children().iter().enumerate() {
if let Some(child_layout) = layout.children.get(index)
&& let Some(found) = walk(
child,
&child_layout.result,
(abs.0 + child_layout.offset.x, abs.1 + child_layout.offset.y),
node_id,
ancestors,
)
{
return Some(found);
}
}
if is_scroll {
ancestors.pop();
}
None
}
let root = tree
.root()
.ok_or_else(|| format!("window for ref {ref_id:?} has no mounted tree"))?;
let layout = tree
.last_layout()
.ok_or("no layout available yet; wait for a frame first")?;
let mut ancestors = Vec::new();
let node_bounds = walk(root.node, layout, (0.0, 0.0), node_id, &mut ancestors)
.ok_or_else(|| format!("ref {ref_id:?} no longer resolves to a live element; call lurq_read_tree again"))?;
let target = target_bounds.unwrap_or(node_bounds);
if ancestors.is_empty() {
return Ok(());
}
const MARGIN: f32 = 8.0;
let [mut target_x, mut target_y, target_w, target_h] = target;
for ancestor in ancestors.iter().rev() {
let [view_x, view_y, view_w, view_h] = ancestor.viewport;
let delta_y = axis_delta(target_y, target_h, view_y, view_h, MARGIN);
let delta_x = axis_delta(target_x, target_w, view_x, view_w, MARGIN);
if delta_x != 0.0 || delta_y != 0.0 {
let state = ancestor.state;
let max_x = (state.content_width() - state.viewport_width()).max(0.0);
let max_y = (state.content_height() - state.viewport_height()).max(0.0);
let (old_x, old_y) = (state.scroll_x(), state.scroll_y());
let (new_x, new_y) = ((old_x + delta_x).clamp(0.0, max_x), (old_y + delta_y).clamp(0.0, max_y));
state.set_scroll(new_x, new_y);
target_x -= new_x - old_x;
target_y -= new_y - old_y;
}
}
Ok(())
}
fn axis_delta(start: f32, size: f32, view: f32, view_size: f32, margin: f32) -> f32 {
let end = start + size;
let view_end = view + view_size;
if start < view || (size > view_size && (start < view + margin || start >= view_end)) {
start - view - margin
} else if end > view_end && size <= view_size {
end - view_end + margin
} else {
0.0
}
}