#766: deliver ScriptUI mouse events to script listeners

Problem: ScriptUI controls never sent mouse events to scripts.
addEventListener("mousedown" | "mouseup" | "mousemove" | "mouseover" |
"mouseout" | "click") listeners never ran, so custom-drawn controls
(onDraw + mouse, as DuScriptUI builds them) could not react to the pointer.
docs/plugins.md listed this as missing.

Fix: scriptui.js builds a ScriptUI MouseEvent (clientX/clientY relative to
the target, button, detail = click count, screenX/Y, modifier keys,
getModifierState, stopPropagation, preventDefault) and delivers it in the
capture, target and bubble phases. addEventListener takes the capture flag.
The parent walk is bounded, so a script-made parent cycle cannot hang it.
Each published control lists the mouse events a listener wants
(Widget::mouse, inherited from containers). The new scriptui.mouse
command dispatches one event with clamped, finite values and rejects
unknown types and disabled controls. The egui ScriptUI view hit-tests the
pointer over script windows and docked panels, skipping covered layers and
windows blocked by a modal dialog. It sends mouseover/mouseout,
mousemove, mousedown/mouseup with click counts, and a click carrying the
pointer details in place of the plain click, so a checkbox still toggles
once. Handlers run under the existing script limits; a throwing listener
reports an error and the window keeps working.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vangel76
2026-10-11 12:39:23 +02:00
co-authored by Claude Opus 5.5
parent 9a7d3e7699
commit 1c06a793a5
10 changed files with 536 additions and 15 deletions
+47
View File
@@ -224,3 +224,50 @@ fn on_draw_concave_fills_and_images_render() {
h.state_mut().session.execute("scriptui.close", json!({})).unwrap();
let _ = std::fs::remove_dir_all(&dir);
}
/// ScriptUI mouse events (#766): the pointer over a script window reaches the script's
/// `addEventListener` listeners (element-relative position, button, click count), and a
/// pointer click on a checkbox with a click listener toggles it once.
#[test]
fn script_windows_send_mouse_events_to_listeners() {
let mut h = harness();
let code = r#"
var w = new Window("palette", "Pad");
var pad = w.add("group", undefined, undefined, { name: "pad" });
pad.preferredSize = [120, 80];
var log = w.add("statictext", undefined, "", { name: "log" });
log.preferredSize = [400, 20];
var snap = w.add("checkbox", undefined, "Snap", { name: "snap" });
var seen = [];
var rec = function (e) { seen.push(e.type + " " + Math.round(e.clientX) + "," + Math.round(e.clientY) + " b" + e.button + " d" + e.detail); log.text = seen.join("|"); };
pad.addEventListener("mouseover", rec);
pad.addEventListener("mousedown", rec);
pad.addEventListener("mouseup", rec);
pad.addEventListener("click", rec);
snap.addEventListener("click", function (e) { log.text = "snap " + snap.value + " d" + e.detail; });
w.show();
"#;
let r = h.state_mut().session.execute("script.run", json!({"code": code, "name": "pad.jsx"})).unwrap();
assert_eq!(r["ok"], true, "{r}");
h.run_steps(3);
let win = h.state().session.script_ui.windows[0].id;
let text = |h: &Harness<'_, EffectcraftApp>, name: &str| h.state().session.script_ui.windows[0].root.lookup(&json!(name)).cloned().unwrap();
let pad = rect(&h, &format!("scriptui.{win}.pad"));
let at = pad.min + vec2(10.0, 20.0);
click_at(&mut h, at);
assert_eq!(text(&h, "log").text, "mouseover 10,20 b0 d0|mousedown 10,20 b0 d1|mouseup 10,20 b0 d1|click 10,20 b0 d1");
click(&mut h, &format!("scriptui.{win}.snap"));
let (snap, log) = (text(&h, "snap"), text(&h, "log"));
assert!(snap.checked, "toggled once");
assert_eq!(log.text, "snap true d1");
h.state_mut().session.execute("scriptui.close", json!({})).unwrap();
}
fn click_at(h: &mut Harness<'_, EffectcraftApp>, c: egui::Pos2) {
h.input_mut().events.push(Event::PointerMoved(c));
h.run_steps(1);
for pressed in [true, false] {
h.input_mut().events.push(Event::PointerButton { pos: c, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() });
}
h.run_steps(3);
}