lurq 0.65.1

A Rust UI toolkit with typed components, retained state, reactive signals, GPU rendering, and DevTools.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! `lurq_interact`: synthetic pointer and keyboard input, and `scroll_to`.
//! Input held across calls (`press`, `release`, `key_down`, `key_up`) is in
//! `held`.

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)
}

/// Resolve the action's target window and point (physical px). Ref wins over
/// coordinates and carries its own window.
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;
  }
  // Held modifier keys apply to every action, as with real keys down.
  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),
      ];
      // Intermediate motion: drag consumers (scrollbars, sliders, DnD) track
      // the motion stream, not just endpoints.
      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)?;
      // A ref focuses the input first; otherwise text goes to the current
      // focus.
      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();
      // From a point as the mouse's side buttons go, otherwise from the focus as a key goes.
      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" {
        // Applied directly: `apply_all` has no answer, and whether an element
        // under the point took the drop is the answer.
        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:?}")),
  }
}

/// The string array `name` as paths; empty when absent.
/// The picture a paste holds as pixels: the image file at the path `value` says, decoded, as a program that
/// copied a picture (a screenshot) puts its pixels on the clipboard.
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()
}

/// Adjust every scroll container on the path to `node_id` so the node's
/// bounds (or `target_bounds` inside it, for a canvas item) land inside its
/// viewport (innermost adjustments win because outer containers position the
/// viewport, not the node).
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);
      // The inner scroll moved the target; outer containers must see where it is now.
      target_x -= new_x - old_x;
      target_y -= new_y - old_y;
    }
  }
  Ok(())
}

/// The scroll along one axis that shows `[start, start + size]` in
/// `[view, view + view_size]`, `margin` inside its edge. A target larger
/// than the view shows its start, as `scroll_into_view` shows it (a code
/// block's line wider than its box shows its beginning, not its end), and
/// is left where it is while its start is at least `margin` inside the view.
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
  }
}