EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust
Merge pull request #775 from vangel76/script-766-mouse-events
Brandon Thomas9ebabccbec95
diff --cc crates/engine/src/scriptui.rsindex 8049efde7,620a11242..6d218a7bb--- a/crates/engine/src/scriptui.rs+++ b/crates/engine/src/scriptui.rs@@@ -713,12 -696,42 +722,48 @@@ pub(crate) fn set(s: &mut Session, p: &dispatch(s, ScriptUiEvent { window, widget, kind: kind.into(), value })}+/// `script.runTasks`: run the scripts' `app.scheduleTask` tasks that are due now (frontends do+/// it every frame; headless hosts and agents call this).+pub(crate) fn run_tasks(s: &mut Session, _: &Value) -> Result<Value> {+ ScriptUi::run_tasks(s).ok_or_else(|| EngineError::Other("scripting is not available in this build".into()))+}++ /// `scriptui.mouse`: a mouse event on a control, as the pointer would make it: the script's+ /// `addEventListener` listeners on the control and its containers receive a MouseEvent+ /// (`clientX`/`clientY` relative to the control, `button`, `detail` = click count, modifier+ /// keys). A `click` also does what clicking the control does (toggle a checkbox, press a button).+ pub(crate) fn mouse(s: &mut Session, p: &Value) -> Result<Value> {+ let c = "scriptui.mouse";+ let window = window_p(s, p, c)?;+ let widget = widget_p(s, window, p, c)?;+ let ty = p.get("type").and_then(Value::as_str).unwrap_or("click").to_ascii_lowercase();+ let Some(ty) = MOUSE_EVENTS.iter().find(|t| **t == ty) else {+ return Err(crate::commands::bad(c, format!("unknown mouse event `{ty}` (one of {})", MOUSE_EVENTS.join(", "))));+ };+ let w = s.script_ui.window(window).and_then(|w| w.root.find(widget)).cloned().unwrap_or_default();+ if !w.enabled {+ return Err(crate::commands::bad(c, format!("control {widget} is disabled")));+ }+ // Hostile input stays finite and bounded.+ const FAR: f64 = 1.0e6;+ let num = |k: &str, lo: f64, hi: f64, d: f64| p.get(k).and_then(Value::as_f64).filter(|v| v.is_finite()).map_or(d, |v| v.clamp(lo, hi));+ let flag = |k: &str| p.get(k).and_then(Value::as_bool).unwrap_or(false);+ let clicks = if matches!(*ty, "mousemove" | "mouseover" | "mouseout") { 0.0 } else { 1.0 };+ let value = json!({+ "x": num("x", -FAR, FAR, w.bounds[2] / 2.0),+ "y": num("y", -FAR, FAR, w.bounds[3] / 2.0),+ "screenX": num("screenX", -FAR, FAR, 0.0),+ "screenY": num("screenY", -FAR, FAR, 0.0),+ "button": num("button", 0.0, 2.0, 0.0).round(),+ "detail": num("detail", 0.0, 1000.0, clicks).round(),+ "altKey": flag("altKey"),+ "ctrlKey": flag("ctrlKey"),+ "metaKey": flag("metaKey"),+ "shiftKey": flag("shiftKey"),+ });+ dispatch(s, ScriptUiEvent { window, widget, kind: (*ty).to_string(), value })+ }+/// `scriptui.close`: close a window (a dialog's `show()` returns `result`, default 2 = Cancel).pub(crate) fn close(s: &mut Session, p: &Value) -> Result<Value> {let window = window_p(s, p, "scriptui.close")?;diff --cc crates/script/src/lib.rsindex efa14f7d6,cc38fe7fb..067b85fd2--- a/crates/script/src/lib.rs+++ b/crates/script/src/lib.rs@@@ -102,7 -90,5 +102,9 @@@ mod tests_tasksmod tests_ui;#[cfg(test)]mod tests_ui_more;++// #760: `$.evalFile` and `BridgeTalk`.+#[cfg(test)]+mod tests_evalfile;+ #[cfg(test)]+ mod tests_ui_mouse;diff --cc docs/scripting-parity.mdindex 66b57372d,c4b0a197a..f891da74a--- a/docs/scripting-parity.md+++ b/docs/scripting-parity.md@@@ -18,13 -18,11 +18,13 @@@ our own scripts; no real-world script l| Expression object model | `thisComp`, `thisLayer`, `wiggle`, `loopOut`, `valueAtTime`, `sampleImage`, `footage()`, text style API… | Same members, from the reference | good | `crates/expr/src/tests.rs`; behaviour of edge cases unmeasured (G1) || Expression editing | Inline field, error bar, pick whip, Expression Language menu, external editors | Inline field that grows, error bar, pick whips (property and value), language menu | partial | External editor support (#364) || Expression performance | Cached, multi-frame | Per-thread boa contexts | unknown | No benchmark |-| Scripting object model | `app`, `Project`, items, layers, properties, keys, eases, render queue, `TextDocument`, `Shape`, markers, `File`/`Folder`, `Socket`, `app.settings` | Same classes, edits run through undoable engine commands; match names table | good | `crates/script` tests (≈ 10 test files) |+| Scripting object model | `app`, `Project`, items, layers, properties, keys, eases, render queue, `TextDocument`, `Shape`, markers, `File`/`Folder`, `Socket`, `app.settings` | Same classes, edits run through undoable engine commands; match names table; layers, groups and properties are callable (`layer("Effects")(1)` is `.property(...)`) | good | `crates/script` tests (≈ 10 test files) |+| `$.evalFile`, `BridgeTalk` | Evaluate another script file; identify the host application and message other Adobe applications | `$.evalFile` evaluates a file relative to the calling file (nested at most 32 deep, under the scripts' file gate) and returns its last value; `BridgeTalk` names the host honestly (`appName` `effectcraft`, its version); messaging other applications throws "not supported" | partial | `tests_evalfile.rs` (#760); no inter-application messaging |- | ScriptUI | Dialogs, palettes, dockable panels, resource strings, `onDraw` / ScriptUIGraphics | Same, dockable panels reopen at launch | good | `ui_scriptui`, `tests_ui*` |+ | ScriptUI | Dialogs, palettes, dockable panels, resource strings, `onDraw` / ScriptUIGraphics, mouse events | Same, dockable panels reopen at launch | good | `ui_scriptui`, `tests_ui*` |+| `app.scheduleTask` / `cancelTask` | One-shot and repeating tasks (polling, panel refresh) until `cancelTask` | Same: repeating tasks run every `delay` ms (at least 50 ms, at most 64 pending per script) until `cancelTask`, until they throw (reported once, then cancelled), or until their owner goes: a script's last window closing, or the project closing for scripts without windows; frontends tick them every frame, agents with `script.runTasks` | good | `tests_tasks.rs` (#773); one-shot tasks of a script file still run right after it, not after their delay || Scripts menu, ScriptUI Panels folder | yes | yes | same | [plugins.md](plugins.md) || Startup / Shutdown folders | yes | not run | missing | plugins.md |-| `#include`, `#target` directives | yes | a script starting with one doesn't parse | missing | plugins.md; common in real scripts |+| `#include`, `#target` directives | yes | `#include` / `//@include` / `#includepath` inline files relative to the including script (once each, nested at most 32 deep, under the scripts' file gate; errors name the included file and line); `#target`, `#targetengine`, `#strict` are ignored | good | `tests_include.rs` (#759); installed scripts and panels have no folder of their own, so they include from the project's folder or absolute paths || `.jsxbin` | yes | no | out of scope | No public specification || Keyboard shortcuts for scripts | yes | no | missing | plugins.md || Command-line rendering `aerender` | yes | `effectcraft-cli render`, `script`, `exec` | same purpose | `apps/effectcraft-cli` |