Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Reject bad params in the control channel's synthetic input methods (#2449) (#2656)

Fabio Lussi233237e9dc6a
diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs
index 7710c228c..75f6f4c60 100644
--- a/crates/ui-egui/src/control.rs
+++ b/crates/ui-egui/src/control.rs
@@ -13,10 +13,12 @@
//! - `ui.dialog.open {kind, fields?}` (kinds: newDocument, about, layerStyle {effect?}, colorPicker {target: foreground|background}, command {command}) / `ui.dialog.set {dialog, field, value}` / `ui.dialog.confirm {dialog, wait?}` / `ui.dialog.cancel {dialog}`
//! - `ui.dialog.apply {dialog}`: commit Preferences changes without closing the dialog
//! - `ui.window.open {document?}` / `ui.window.close {window}`: extra document windows
-//! - `ui.pointer {events: [{kind: down|move|up, x, y, pressure?, tiltX?, tiltY?, rotation?}], modifiers?, button?}`: drive the active tool in document coordinates (`button: "secondary"` opens the tool's canvas context menu (the pasteboard colour menu outside the image) or Brush Preset picker, or erases with Preferences › Tools › Right-click with painting tools = erase)
+//! - `ui.pointer {events: [{kind: down|move|up, x, y, pressure?, tiltX?, tiltY?, rotation?}], tool?, modifiers?, button?}`: drive the active tool in document coordinates (`button: "secondary"` opens the tool's canvas context menu (the pasteboard colour menu outside the image) or Brush Preset picker, or erases with Preferences › Tools › Right-click with painting tools = erase)
//! - `ui.click {x, y, button?, count?}` / `ui.move {x, y}`: synthetic pointer input in screen points
//! (`count` at most [`MAX_CLICKS`])
//! - `ui.key {key, command?, shift?, alt?, ctrl?}` / `ui.type {text}`: synthetic keyboard input
+//! - The synthetic input methods reject unknown fields, wrong types, unknown enum values and
+//! out-of-range numbers before acting, naming the parameter (#2449)
//! - `ui.resize {width, height}`: resize the main window
//! - `ui.gpu.simulateLoss {error?}`: act as if the wgpu device was lost (or, with `error: true`,
//! reported an error): the app switches to the CPU renderer for the rest of the session, as on a
@@ -195,6 +197,62 @@ fn whole_object<T: serde::Serialize + serde::de::DeserializeOwned>(current: &T,
}
serde_json::from_value(v.clone()).map(Some).map_err(|e| format!("{field}: {e}"))
}
+
+/// The buttons `ui.pointer` and `ui.click` accept (`right` and `secondary` are the same one).
+const BUTTONS: [&str; 4] = ["left", "right", "secondary", "middle"];
+/// The modifier flags `ui.key` reads, top-level or under `modifiers`; `ui.pointer` adds `space`.
+const KEY_MODIFIERS: [&str; 4] = ["shift", "alt", "command", "ctrl"];
+const POINTER_MODIFIERS: [&str; 5] = ["shift", "alt", "command", "ctrl", "space"];
+
+/// A synthetic input method's params (or one nested object, named `what`): an object whose keys
+/// are all in `fields`, returned with its null fields dropped so they mean "omitted". A typo must
+/// not reply ok after acting on a default (#2449), as `ui.set` already promises (#412).
+fn input_object(v: &Value, what: Option<&str>, fields: &[&str]) -> std::result::Result<Value, String> {
+ let object = match (v, what) {
+ (Value::Null, None) => return Ok(json!({})),
+ (Value::Object(o), _) => o,
+ (_, what) => return Err(format!("{} must be an object", what.unwrap_or("params"))),
+ };
+ if let Some(key) = object.keys().find(|k| !fields.contains(&k.as_str())) {
+ let field = what.map(|w| format!("{w} field")).unwrap_or_else(|| "field".into());
+ return Err(format!("unknown {field} `{key}` (fields: {})", fields.join(", ")));
+ }
+ Ok(Value::Object(object.iter().filter(|(_, v)| !v.is_null()).map(|(k, v)| (k.clone(), v.clone())).collect()))
+}
+
+/// An optional finite number named `name` in errors, within `range` when given.
+fn input_number(v: &Value, key: &str, name: &str, range: Option<(f64, f64)>) -> std::result::Result<Option<f64>, String> {
+ let n = num_field(v, key).map_err(|_| format!("{name} must be a finite number"))?;
+ match (n, range) {
+ (Some(n), Some((lo, hi))) if !(lo..=hi).contains(&n) => Err(format!("{name} must be between {lo} and {hi} (got {n})")),
+ _ => Ok(n),
+ }
+}
+
+/// An optional string named `name` in errors, one of `accepted` when given.
+fn input_choice<'a>(v: &'a Value, key: &str, name: &str, accepted: &[&str]) -> std::result::Result<Option<&'a str>, String> {
+ match v.get(key) {
+ None => Ok(None),
+ Some(Value::String(s)) if accepted.is_empty() || accepted.contains(&s.as_str()) => Ok(Some(s)),
+ Some(Value::String(s)) => Err(format!("unknown {name} `{s}` ({})", accepted.join(", "))),
+ Some(_) => Err(format!("{name} must be a string")),
+ }
+}
+
+/// The modifier flags of `ui.pointer` / `ui.key`: grouped under `modifiers` when present (the
+/// top-level flags are then ignored, but still type-checked), else top-level.
+fn input_modifiers(p: &Value, flags: &[&str]) -> std::result::Result<Value, String> {
+ for flag in flags {
+ bool_field(p, flag)?;
+ }
+ let Some(m) = p.get("modifiers") else { return Ok(p.clone()) };
+ let m = input_object(m, Some("modifiers"), flags)?;
+ for flag in flags {
+ bool_field(&m, flag).map_err(|e| format!("modifiers.{e}"))?;
+ }
+ Ok(m)
+}
+
fn wrap(r: Result<Value, String>) -> Outcome {
match r {
Ok(v) => ok(v),
@@ -781,9 +839,45 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest)
if app.ui.windows.len() < before { ok(Value::Null) } else { err(format!("no window {id}")) }
}
"ui.pointer" => {
- let Some(events) = p.get("events").and_then(Value::as_array) else { return err("missing `events`") };
- let mods = modifiers_from(p);
- if let Some(t) = s("tool").and_then(Tool::from_name) {
+ // Every field and event is validated before the first event runs, so a rejected call
+ // changes nothing (#2449).
+ let parsed = (|| -> std::result::Result<_, String> {
+ let fields = ["events", "tool", "modifiers", "button", "shift", "alt", "command", "ctrl", "space"];
+ let p = input_object(p, None, &fields)?;
+ let tool = match input_choice(&p, "tool", "tool", &[])? {
+ Some(t) => Some(Tool::from_name(t).ok_or_else(|| format!("unknown tool `{t}`"))?),
+ None => None,
+ };
+ let button = input_choice(&p, "button", "button", &BUTTONS)?.unwrap_or("left").to_string();
+ // Modifier flags may be top-level or grouped under "modifiers".
+ let m = input_modifiers(&p, &POINTER_MODIFIERS)?;
+ let Some(events) = p.get("events") else { return Err("missing `events`".into()) };
+ let Some(events) = events.as_array() else { return Err("events must be an array".into()) };
+ let mut parsed = Vec::with_capacity(events.len());
+ for (i, e) in events.iter().enumerate() {
+ let name = format!("events[{i}]");
+ let e = input_object(e, Some(&name), &["kind", "x", "y", "pressure", "tiltX", "tiltY", "rotation"])?;
+ let num = |k: &str, range| input_number(&e, k, &format!("{name}.{k}"), range);
+ let (x, y) = (num("x", None)?.unwrap_or(0.0), num("y", None)?.unwrap_or(0.0));
+ let pr = num("pressure", Some((0.0, 1.0)))?.unwrap_or(1.0) as f32;
+ let ev = match input_choice(&e, "kind", &format!("{name}.kind"), &["down", "move", "up"])?.unwrap_or("move") {
+ "down" => ToolEvent::Down { x, y, pressure: pr },
+ "up" => ToolEvent::Up { x, y },
+ _ => ToolEvent::Move { x, y, pressure: pr },
+ };
+ let tilt = |k: &str, range| num(k, Some(range)).map(|v| v.map(|v| v as f32));
+ let pen = (tilt("tiltX", (-90.0, 90.0))?, tilt("tiltY", (-90.0, 90.0))?, tilt("rotation", (0.0, 360.0))?);
+ parsed.push((ev, x, y, pr, pen));
+ }
+ Ok((tool, button, m, parsed))
+ })();
+ let (tool, button, m, events) = match parsed {
+ Ok(parsed) => parsed,
+ Err(e) => return err(e),
+ };
+ let secondary = matches!(button.as_str(), "secondary" | "right");
+ let mods = modifiers_from(&m);
+ if let Some(t) = tool {
app.ui.tool = t;
}
// Space held: the Crop tool moves the frame being drawn, a marquee, lasso or shape
@@ -791,15 +885,7 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest)
// top-level or grouped under "modifiers".
let space = p.get("modifiers").unwrap_or(p).get("space").and_then(Value::as_bool).unwrap_or(false);
crate::crop_ui::set_space(app, space);
- for e in events {
- let x = e.get("x").and_then(Value::as_f64).unwrap_or(0.0);
- let y = e.get("y").and_then(Value::as_f64).unwrap_or(0.0);
- let pr = e.get("pressure").and_then(Value::as_f64).unwrap_or(1.0) as f32;
- let ev = match e.get("kind").and_then(Value::as_str).unwrap_or("move") {
- "down" => ToolEvent::Down { x, y, pressure: pr },
- "up" => ToolEvent::Up { x, y },
- _ => ToolEvent::Move { x, y, pressure: pr },
- };
+ for (ev, x, y, pr, pen) in events {
// With the Color Picker on top the image is its eyedropper, as for the mouse.
if crate::color_picker_ui::top(app).is_some() {
if !matches!(ev, ToolEvent::Up { .. }) {
@@ -809,7 +895,7 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest)
}
// Curves' modal eyedroppers use document coordinates here, matching the native
// canvas path after its ViewXform conversion. Sample on press only.
- if !matches!(s("button"), Some("secondary" | "right")) && crate::adjust_dialog::picker_armed(app) {
+ if !secondary && crate::adjust_dialog::picker_armed(app) {
if matches!(ev, ToolEvent::Down { .. }) {
crate::adjust_dialog::sample_at(app, x, y);
}
@@ -824,12 +910,12 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest)
if let Some(d) = app.ui.dialogs.last_mut() {
d.fields.insert("__pointerDown".into(), json!(!up && (down || held)));
}
- if !up && (down || held) && !space && !matches!(s("button"), Some("secondary" | "right" | "middle")) {
+ if !up && (down || held) && !space && !secondary && button != "middle" {
crate::color_range_ui::pick_top(app, [x, y], mods);
}
continue;
}
- if matches!(s("button"), Some("secondary" | "right")) {
+ if secondary {
let down = matches!(ev, ToolEvent::Down { .. });
// Right-click with the Move tool, or ⌘/Ctrl+right-click: list the layers there.
if crate::layer_pick_ui::is_gesture(app.ui.tool, mods) {
@@ -856,8 +942,6 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest)
}
}
// A simulated pen: tilt/rotation reach the stroke like a real stylus's (see `stylus`).
- let tilt = |k: &str| e.get(k).and_then(Value::as_f64).map(|v| v as f32);
- let pen = (tilt("tiltX"), tilt("tiltY"), tilt("rotation"));
if pen != (None, None, None) {
let (tilt_x, tilt_y, rotation) = (pen.0.unwrap_or(0.0), pen.1.unwrap_or(0.0), pen.2.unwrap_or(0.0));
app.stylus.feed.set(Some(crate::stylus::PenSample { pressure: pr, tilt_x, tilt_y, rotation, eraser: false }));
@@ -884,19 +968,28 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest)
ok(json!({"status": app.ui.status}))
}
"ui.click" | "ui.move" => {
- // Screen coordinates in points (as reported by ui.inspect window size).
- let x = p.get("x").and_then(Value::as_f64).unwrap_or(0.0) as f32;
- let y = p.get("y").and_then(Value::as_f64).unwrap_or(0.0) as f32;
- let pos = egui::pos2(x, y);
- let button = match s("button").unwrap_or("left") {
- "right" | "secondary" => egui::PointerButton::Secondary,
- "middle" => egui::PointerButton::Middle,
- _ => egui::PointerButton::Primary,
+ let click = req.method == "ui.click";
+ let fields: &[&str] = if click { &["x", "y", "button", "count"] } else { &["x", "y"] };
+ let parsed = (|| -> std::result::Result<_, String> {
+ let p = input_object(p, None, fields)?;
+ // Screen coordinates in points (as reported by ui.inspect window size).
+ let x = input_number(&p, "x", "x", None)?.unwrap_or(0.0) as f32;
+ let y = input_number(&p, "y", "y", None)?.unwrap_or(0.0) as f32;
+ let button = match input_choice(&p, "button", "button", &BUTTONS)?.unwrap_or("left") {
+ "right" | "secondary" => egui::PointerButton::Secondary,
+ "middle" => egui::PointerButton::Middle,
+ _ => egui::PointerButton::Primary,
+ };
+ let clicks = if click { uint_field(&p, "count")?.unwrap_or(1) } else { 0 };
+ if clicks > MAX_CLICKS {
+ return Err(format!("`count` must be at most {MAX_CLICKS} (got {clicks})"));
+ }
+ Ok((egui::pos2(x, y), button, clicks))
+ })();
+ let (pos, button, clicks) = match parsed {
+ Ok(parsed) => parsed,
+ Err(e) => return err(e),
};
- let clicks = if req.method == "ui.click" { u("count").unwrap_or(1) } else { 0 };
- if clicks > MAX_CLICKS {
- return err(format!("`count` must be at most {MAX_CLICKS} (got {clicks})"));
- }
app.synthetic.push(egui::Event::PointerMoved(pos));
for _ in 0..clicks {
app.synthetic.push(egui::Event::PointerButton { pos, button, pressed: true, modifiers: Default::default() });
@@ -906,9 +999,18 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest)
Outcome::AfterInput
}
"ui.key" => {
- let Some(name) = s("key") else { return err("missing `key`") };
- let Some(key) = egui::Key::from_name(name) else { return err(format!("unknown key `{name}`")) };
- let modifiers = modifiers_from(p);
+ let parsed = (|| -> std::result::Result<_, String> {
+ let p = input_object(p, None, &["key", "modifiers", "shift", "alt", "command", "ctrl"])?;
+ let Some(name) = input_choice(&p, "key", "key", &[])? else { return Err("missing `key`".into()) };
+ let key = egui::Key::from_name(name).ok_or_else(|| format!("unknown key `{name}`"))?;
+ // Modifier flags may be top-level or grouped under "modifiers".
+ Ok((key, input_modifiers(&p, &KEY_MODIFIERS)?))
+ })();
+ let (key, m) = match parsed {
+ Ok(parsed) => parsed,
+ Err(e) => return err(e),
+ };
+ let modifiers = modifiers_from(&m);
app.synthetic.push(egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers });
app.synthetic.push(egui::Event::Key { key, physical_key: None, pressed: false, repeat: false, modifiers });
ctx.request_repaint();
@@ -951,15 +1053,21 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest)
ok(Value::Null)
}
"ui.screenshot" => {
+ let parsed = input_object(p, None, &["path", "focus"])
+ .and_then(|p| Ok((input_choice(&p, "path", "path", &[])?.map(str::to_string), bool_field(&p, "focus")?)));
+ let (path, focus) = match parsed {
+ Ok(parsed) => parsed,
+ Err(e) => return err(e),
+ };
// Occluded windows don't render on macOS, so the screenshot would never arrive: raise first.
- if p.get("focus").and_then(Value::as_bool).unwrap_or(true) {
+ if focus.unwrap_or(true) {
ctx.send_viewport_cmd(egui::ViewportCommand::Focus);
}
// The capture itself is issued by `PhotocraftApp::issue_screenshots` once open/close
// animations (modals, popups) have settled.
let token = app.ui.alloc_id();
ctx.request_repaint();
- Outcome::Screenshot { token, path: s("path").map(str::to_string) }
+ Outcome::Screenshot { token, path }
}
"app.open" => match s("path") {
Some(path) => {
@@ -2176,6 +2284,172 @@ mod tests {
}
}
+ /// #2449: the error of a rejected synthetic input call, after checking it changed nothing.
+ fn rejected(app: &mut PhotocraftApp, ctx: &egui::Context, method: &str, params: Value, names: &str) -> String {
+ let (tool, revision) = (app.ui.tool, app.session.active().map(|d| d.revision));
+ let r = call(app, ctx, method, params.clone());
+ assert_eq!(r["ok"], false, "{method} {params}: {r}");
+ let error = r["error"].as_str().unwrap().to_string();
+ assert!(error.contains(names), "{method} {params}: `{error}` should name `{names}`");
+ assert!(app.synthetic.is_empty(), "{method} {params}: a rejected call queues nothing");
+ assert_eq!(app.ui.tool, tool, "{method} {params}: a rejected call keeps the tool");
+ assert_eq!(app.session.active().map(|d| d.revision), revision, "{method} {params}: a rejected call edits nothing");
+ error
+ }
+
+ #[test]
+ fn ui_pointer_rejects_bad_params_before_acting() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ let ctx = egui::Context::default();
+ app.run("file.new", json!({"width": 64, "height": 64})).unwrap();
+ let stroke = json!([{"kind": "down", "x": 5, "y": 5}, {"kind": "move", "x": 30, "y": 30}, {"kind": "up", "x": 30, "y": 30}]);
+ // The same unknown tool is the same error as `ui.set`'s.
+ let set = call(&mut app, &ctx, "ui.set", json!({"tool": "noSuchTool"}));
+ let error = rejected(&mut app, &ctx, "ui.pointer", json!({"tool": "noSuchTool", "events": stroke}), "noSuchTool");
+ assert_eq!(json!(error), set["error"]);
+ let event = |extra: Value| {
+ let mut e = json!({"kind": "down", "x": 5, "y": 5});
+ e.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
+ // A valid stroke first: a later bad event must not let the earlier ones run.
+ json!({"tool": "move", "events": [stroke[0], stroke[1], stroke[2], e]})
+ };
+ let cases = [
+ // Unknown fields, top-level, under `modifiers` and in an event.
+ (json!({"tol": "move", "events": stroke}), "tol"),
+ (json!({"modifiers": {"shfit": true}, "events": stroke}), "shfit"),
+ (event(json!({"presure": 0.5})), "presure"),
+ // Wrong JSON types.
+ (json!({"tool": 3, "events": stroke}), "tool"),
+ (json!({"events": "down"}), "events"),
+ (json!({}), "events"),
+ (json!({"events": [5]}), "events[0]"),
+ (event(json!({"x": "abc"})), "x"),
+ (event(json!({"y": true})), "y"),
+ (event(json!({"kind": 1})), "kind"),
+ (json!({"shift": "yes", "events": stroke}), "shift"),
+ (json!({"modifiers": {"alt": 1}, "events": stroke}), "alt"),
+ (json!({"modifiers": true, "events": stroke}), "modifiers"),
+ (json!({"button": 2, "events": stroke}), "button"),
+ (json!([1, 2]), "params"),
+ // Values outside the accepted set.
+ (json!({"button": "ritgh", "events": stroke}), "button"),
+ (event(json!({"kind": "dwon"})), "kind"),
+ // Numbers outside their documented range.
+ (event(json!({"pressure": 99})), "pressure"),
+ (event(json!({"pressure": -0.1})), "pressure"),
+ (event(json!({"tiltX": 91})), "tiltX"),
+ (event(json!({"tiltY": -90.5})), "tiltY"),
+ (event(json!({"rotation": 361})), "rotation"),
+ (event(json!({"rotation": -1})), "rotation"),
+ ];
+ for (params, names) in cases {
+ rejected(&mut app, &ctx, "ui.pointer", params, names);
+ }
+ let error = rejected(&mut app, &ctx, "ui.pointer", json!({"button": "ritgh", "events": stroke}), "button");
+ assert!(error.contains("right") && error.contains("secondary"), "the error lists the buttons: {error}");
+ let error = rejected(&mut app, &ctx, "ui.pointer", event(json!({"kind": "dwon"})), "kind");
+ assert!(error.contains("down") && error.contains("move") && error.contains("up"), "the error lists the kinds: {error}");
+
+ // Valid calls still run: an omitted kind is a move, null is an omitted field, and the
+ // documented range ends are accepted.
+ let before = app.session.active().unwrap().revision;
+ let pen = json!({"kind": "down", "x": 5, "y": 5, "pressure": 0, "tiltX": -90, "tiltY": 90, "rotation": 360});
+ let valid = json!({"tool": "brush", "button": null, "modifiers": {"shift": false}, "events": [pen, {"x": 20, "y": 20, "pressure": 1}, {"kind": "up", "x": 20, "y": 20}]});
+ let r = call(&mut app, &ctx, "ui.pointer", valid);
+ assert_eq!(r["ok"], true, "{r}");
+ assert_eq!(app.ui.tool, Tool::Brush);
+ assert!(app.session.active().unwrap().revision > before, "the stroke painted");
+ for button in ["left", "middle", "right", "secondary"] {
+ let r = call(
+ &mut app,
+ &ctx,
+ "ui.pointer",
+ json!({"tool": "move", "button": button, "events": [{"kind": "down", "x": 5, "y": 5}, {"kind": "up", "x": 5, "y": 5}]}),
+ );
+ assert_eq!(r["ok"], true, "{button}: {r}");
+ }
+ assert!(app.ui.layer_menu.is_some(), "a right click with Move still opens the layer menu");
+ }
+
+ #[test]
+ fn ui_click_and_move_reject_bad_params_before_queueing_input() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ let ctx = egui::Context::default();
+ let cases = [
+ ("ui.click", json!({"x": 10, "y": 10, "buton": "right"}), "buton"),
+ ("ui.move", json!({"x": 10, "y": 10, "count": 2}), "count"),
+ ("ui.click", json!({"x": "abc", "y": 10}), "x"),
+ ("ui.move", json!({"x": 10, "y": [10]}), "y"),
+ ("ui.click", json!({"x": 10, "y": 10, "count": "dos"}), "count"),
+ ("ui.click", json!({"x": 10, "y": 10, "count": 1.5}), "count"),
+ ("ui.click", json!({"x": 10, "y": 10, "count": -1}), "count"),
+ ("ui.click", json!({"x": 10, "y": 10, "button": true}), "button"),
+ ("ui.click", json!({"x": 10, "y": 10, "button": "ritgh"}), "button"),
+ ("ui.click", json!({"x": 10, "y": 10, "count": MAX_CLICKS + 1}), "count"),
+ ("ui.click", json!("click"), "params"),
+ ];
+ for (method, params, names) in cases {
+ rejected(&mut app, &ctx, method, params, names);
+ }
+ let error = rejected(&mut app, &ctx, "ui.click", json!({"x": 10, "y": 10, "button": "ritgh"}), "button");
+ assert!(error.contains("left") && error.contains("right") && error.contains("middle"), "the error lists the buttons: {error}");
+ // Valid calls queue what they did before; an omitted button is a left click.
+ for (method, params, button, events) in [
+ ("ui.click", json!({"x": 10, "y": 10}), egui::PointerButton::Primary, 3),
+ ("ui.click", json!({"x": 10, "y": 10, "button": "right", "count": 2}), egui::PointerButton::Secondary, 5),
+ ("ui.click", json!({"x": 10, "y": 10, "button": "secondary"}), egui::PointerButton::Secondary, 3),
+ ("ui.click", json!({"x": 10.5, "y": 10, "button": "middle", "count": null}), egui::PointerButton::Middle, 3),
+ ("ui.move", json!({"x": 10, "y": 10}), egui::PointerButton::Primary, 1),
+ ] {
+ let (req, _rx) = ControlRequest::new(method, params.clone());
+ assert!(matches!(handle(&mut app, &ctx, &req), Outcome::AfterInput), "{method} {params}");
+ assert_eq!(app.synthetic.len(), events, "{method} {params}");
+ if let Some(egui::Event::PointerButton { button: queued, .. }) = app.synthetic.get(1) {
+ assert_eq!(*queued, button, "{method} {params}");
+ }
+ app.synthetic.clear();
+ }
+ }
+
+ #[test]
+ fn ui_key_rejects_bad_params_before_queueing_input() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ let ctx = egui::Context::default();
+ let cases = [
+ (json!({"key": "N", "ctl": true}), "ctl"),
+ (json!({"key": "N", "modifiers": {"space": true}}), "space"),
+ (json!({"key": "N", "ctrl": "yes"}), "ctrl"),
+ (json!({"key": "N", "modifiers": {"shift": 1}}), "shift"),
+ (json!({"key": "N", "modifiers": "ctrl"}), "modifiers"),
+ (json!({"key": 5}), "key"),
+ (json!({}), "key"),
+ (json!({"key": "NoSuchKey"}), "NoSuchKey"),
+ ];
+ for (params, names) in cases {
+ rejected(&mut app, &ctx, "ui.key", params, names);
+ }
+ for params in [json!({"key": "N", "ctrl": true}), json!({"key": "N", "shift": false, "modifiers": {"ctrl": true}})] {
+ let (req, _rx) = ControlRequest::new("ui.key", params.clone());
+ assert!(matches!(handle(&mut app, &ctx, &req), Outcome::AfterInput), "{params}");
+ assert!(matches!(app.synthetic.first(), Some(egui::Event::Key { key: egui::Key::N, modifiers, .. }) if modifiers.ctrl), "{params}");
+ app.synthetic.clear();
+ }
+ }
+
+ #[test]
+ fn ui_screenshot_rejects_bad_params_before_capturing() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ let ctx = egui::Context::default();
+ for (params, names) in [(json!({"focus": "yes"}), "focus"), (json!({"path": 5}), "path"), (json!({"pth": "shot.png"}), "pth")] {
+ rejected(&mut app, &ctx, "ui.screenshot", params, names);
+ }
+ for (params, path) in [(json!({}), None), (Value::Null, None), (json!({"path": "shot.png", "focus": false}), Some("shot.png"))] {
+ let (req, _rx) = ControlRequest::new("ui.screenshot", params.clone());
+ let Outcome::Screenshot { path: queued, .. } = handle(&mut app, &ctx, &req) else { panic!("{params}: expected a capture") };
+ assert_eq!(queued.as_deref(), path, "{params}");
+ }
+ }
+
/// A key event's modifiers, as `ui.key` queued them.
fn key_modifiers(app: &mut PhotocraftApp, ctx: &egui::Context, params: Value) -> egui::Modifiers {
app.synthetic.clear();
diff --git a/docs/control-protocol.md b/docs/control-protocol.md
index 1c69df465..78fcc71d7 100644
--- a/docs/control-protocol.md
+++ b/docs/control-protocol.md
@@ -61,10 +61,11 @@ The transport is `apps/photocraft/src/control_server.rs`, and the handlers are i
- Edit › Fill… (`ui.menu.invoke {id: "edit.fill"}`, Shift+F5, Shift+Backspace) opens the Fill dialog; its fields are `edit.fill`'s params (`contents`, `color`, `pattern`, `colorAdaptation`, `mode`, `opacity`, `preserveTransparency`), and OK remembers them in the preferences (`dialogs["edit.fill"]`).
- A pixel tool pressed on a type, shape, Smart Object or fill layer (e.g. through `ui.pointer`) opens the "Rasterize?" prompt instead of painting: a dialog with `__rasterize` (`type|shape|smartObject|fill`), `message`, `layer`, `tool` and `at`. `ui.dialog.confirm` runs the `layer.rasterize.*` command and then paints at `at` (two history states, `{"rasterized", "painted"}`); `ui.dialog.cancel` does nothing.
- `ui.window.open {document?}` / `ui.window.close {window}`: extra document windows
-- `ui.pointer {events: [{kind: down|move|up, x, y, pressure?, tiltX?, tiltY?, rotation?}], tool?, modifiers?, button?}`: drive the active tool in document coordinates (pressure 0..1, tilt in degrees -90..90, barrel rotation 0..360: a simulated pen). Modifier flags (`shift`, `alt`, `command`, `ctrl`, `space`) go either at the top level or grouped under `modifiers`; when `modifiers` is present, top-level flags are ignored. `space: true` holds Space (the Crop frame, marquee, lasso or shape being drawn then moves instead of growing). With the Crop tool in its default mode (Use Classic Mode off) a drag inside the box moves the image and one outside turns it, the view panning and turning with it; a drag's `move`/`up` points are read as the view showed the document at its `down`, so an `up` where the last `move` was changes nothing more. `command: true` with a painting, retouching, eraser, gradient, eyedropper or other non-selection tool makes the gesture the Move tool's, as holding ⌘ (Ctrl off the Mac) does in the app: a drag moves the active layer (the selected pixels when there is a selection), with `alt: true` as well it duplicates the layer first, and a click picks the layer under the point; the tool is unchanged afterwards. The Hand, Rotate View, Zoom, Crop, Slice, Path Selection, Pen, shape, Type and selection tools keep `command` for themselves. `ctrl: true` with `alt: true` and a brush tool is the on-canvas brush resize instead (in the app, Ctrl+Alt-drag off the Mac). `button: "right"` (or `"secondary"`) is the right button: with the Move tool (or `command: true` with any tool) it opens the canvas layer menu (`layerMenu` in `ui.inspect` lists the layers there, topmost first; select one with `layer.select`); with Marquee, Lasso, Magic Wand, Object Selection, or Pen it opens a tool context menu (`canvasToolMenu` in `ui.inspect`, with ordered command ids, separators, and enabled states); with a painting tool it opens the Brush Preset picker, or erases with Preferences › Tools › Right-click with painting tools = erase. The menus open at the screen position of the pressed document point. Use `ui.context.choose {id}` for enabled tool-context actions. The Pen menu is documented in `docs/context-menu-parity.md`. With a Color Picker as the top dialog, `down` and `move` sample the image into its new colour instead (its eyedropper, like a click on the canvas) and the tool is not driven. A `down`/`up` at the same point with the Rectangle, Ellipse, Triangle or Polygon tool opens Create Rectangle / Ellipse / Triangle / Polygon (Photoshop's click-to-size dialog); set its `width` and `height` (document pixels), `fromCenter`, `radii` [tl, tr, br, bl] (Rectangle) or `sides` and `starRatio` (percent, Polygon) with `ui.dialog.set`, then `ui.dialog.confirm` runs `shape.create` at the clicked point.
+- `ui.pointer {events: [{kind: down|move|up, x, y, pressure?, tiltX?, tiltY?, rotation?}], tool?, modifiers?, button?}`: drive the active tool in document coordinates (pressure 0..1, tilt in degrees -90..90, barrel rotation 0..360: a simulated pen). Modifier flags (`shift`, `alt`, `command`, `ctrl`, `space`) go either at the top level or grouped under `modifiers`; when `modifiers` is present, top-level flags are ignored. `space: true` holds Space (the Crop frame, marquee, lasso or shape being drawn then moves instead of growing). With the Crop tool in its default mode (Use Classic Mode off) a drag inside the box moves the image and one outside turns it, the view panning and turning with it; a drag's `move`/`up` points are read as the view showed the document at its `down`, so an `up` where the last `move` was changes nothing more. `command: true` with a painting, retouching, eraser, gradient, eyedropper or other non-selection tool makes the gesture the Move tool's, as holding ⌘ (Ctrl off the Mac) does in the app: a drag moves the active layer (the selected pixels when there is a selection), with `alt: true` as well it duplicates the layer first, and a click picks the layer under the point; the tool is unchanged afterwards. The Hand, Rotate View, Zoom, Crop, Slice, Path Selection, Pen, shape, Type and selection tools keep `command` for themselves. `ctrl: true` with `alt: true` and a brush tool is the on-canvas brush resize instead (in the app, Ctrl+Alt-drag off the Mac). `button: "right"` (or `"secondary"`) is the right button: with the Move tool (or `command: true` with any tool) it opens the canvas layer menu (`layerMenu` in `ui.inspect` lists the layers there, topmost first; select one with `layer.select`); with Marquee, Lasso, Magic Wand, Object Selection, or Pen it opens a tool context menu (`canvasToolMenu` in `ui.inspect`, with ordered command ids, separators, and enabled states); with a painting tool it opens the Brush Preset picker, or erases with Preferences › Tools › Right-click with painting tools = erase. The menus open at the screen position of the pressed document point. Use `ui.context.choose {id}` for enabled tool-context actions. The Pen menu is documented in `docs/context-menu-parity.md`. `button` is `left` (default), `right`, `secondary` or `middle`, and an event's `kind` is `down`, `move` (default) or `up`. With a Color Picker as the top dialog, `down` and `move` sample the image into its new colour instead (its eyedropper, like a click on the canvas) and the tool is not driven. A `down`/`up` at the same point with the Rectangle, Ellipse, Triangle or Polygon tool opens Create Rectangle / Ellipse / Triangle / Polygon (Photoshop's click-to-size dialog); set its `width` and `height` (document pixels), `fromCenter`, `radii` [tl, tr, br, bl] (Rectangle) or `sides` and `starRatio` (percent, Polygon) with `ui.dialog.set`, then `ui.dialog.confirm` runs `shape.create` at the clicked point.
- Magnetic Lasso (`tool: "magneticLasso"`): a `down`/`up` pair is a click. The first sets the first fastening point (its modifiers set the selection mode); later clicks fasten a point, or close the border on the first point. `move` events trace the border with or without a button held, fastening points by themselves; with `alt: true` a click draws a straight segment and a drag a freehand one. `ui.inspect` shows the border in progress under `magnetic` (`path`, `anchors`, `live`, `mode`). `ui.key` Enter closes it along the edges, Backspace removes the last fastening point, Escape cancels. To select along edges in one call instead, use the `select.magneticLasso` command with rough points around the shape.
-- `ui.click {x, y, button?, count?}` / `ui.move {x, y}`: synthetic pointer input in screen points (`button`: `left` (default), `right` or `middle`). `count` (default 1, `2` for a double-click) is at most 256; a larger one is an error and queues nothing
+- `ui.click {x, y, button?, count?}` / `ui.move {x, y}`: synthetic pointer input in screen points (`button`: `left` (default), `right` (or `secondary`) or `middle`). `count` (default 1, `2` for a double-click) is at most 256; a larger one is an error and queues nothing
- `ui.key {key, command?, shift?, alt?, ctrl?}` (flags may also be grouped under `modifiers`): press and release a key, e.g. `{"key": "ArrowLeft", "shift": true}`
+- `ui.pointer`, `ui.click`, `ui.move`, `ui.key` and `ui.screenshot` validate every parameter before acting, as `ui.set` does: an unknown field (top-level, under `modifiers` or in an event), a value of the wrong JSON type (`"x": "abc"`, `"ctrl": "yes"`), a `button`, `kind`, `tool` or `key` outside the accepted set, or a number outside its range (`pressure`, tilt, `rotation`, `count`) is an error naming the parameter, and nothing is queued or applied. A `null` field counts as omitted and takes its default
- `ui.type {text}`: type text (goes to the focused widget, or to the canvas while the Type tool is editing)
- `ui.resize {width, height}`: resize the main window
- `ui.gpu.simulateLoss {error?}`: act as if the wgpu device was lost (or reported an error with `error: true`). The app switches to the CPU image compositor for the rest of the session and shows the same recovery warning as on a real loss. `gpuFallbackNotice` in `ui.inspect` contains the reason while the warning is open. Keep Using CPU saves CPU compatibility for the next launch; Retry GPU saves GPU mode and asks the user to save and restart. The window renderer still requires a working graphics or software adapter. Returns `wasActive` and `gpuInfo`. For testing the fallback