From 63f9ca9d74bd55409556040059bb3eaeaa8bcc1b Mon Sep 17 00:00:00 2001 From: kayrockscreenprinting Date: Fri, 9 Oct 2026 10:27:46 -0700 Subject: [PATCH] Control: shell dialogs are listed and driven as "shell" (#1811) ui.inspect lists the open shell dialog (Workspace, Pixel Aspect Ratio, Show Extras, 32-bit Preview) with the id "shell", and ui.dialog.set/confirm/cancel accept it; confirm goes through the automation authorizer and the dialog's shared OK. Fixes #1004. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/control.rs | 73 +++++++++++++++++++++++++++++- crates/ui-egui/src/workspace_ui.rs | 44 +++++++++++------- docs/control-protocol.md | 1 + 3 files changed, 101 insertions(+), 17 deletions(-) diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index 3489a52457..52190b73ba 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -210,6 +210,9 @@ fn screen_point(app: &PhotocraftApp, x: f64, y: f64) -> [f32; 2] { [p.x, p.y] } +/// The `dialog` id of the shell's own dialog (`workspace_ui`) in `ui.inspect` and `ui.dialog.*`. +const SHELL_DIALOG: &str = "shell"; + pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest) -> Outcome { let saved = app.session.authorize; if let Some(gate) = app.services.automation_authorize { @@ -528,6 +531,34 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest) } ok(json!({"dialog": app.ui.open_dialog(kind, fields)})) } + // The shell's own dialog (Window › Workspace, View › Pixel Aspect Ratio › Custom, Show + // Extras Options, 32-bit Preview Options), listed by ui.inspect with the id "shell". + "ui.dialog.set" if s("dialog") == Some(SHELL_DIALOG) => { + let Some(field) = s("field") else { return err("need `dialog` and `field`") }; + let value = p.get("value").cloned().unwrap_or(Value::Null); + match app.ui.shell.dialog.as_mut() { + Some((_, fields)) => { + fields.insert(field.to_string(), value); + ok(Value::Null) + } + None => err("no shell dialog is open"), + } + } + "ui.dialog.confirm" if s("dialog") == Some(SHELL_DIALOG) => { + let Some((kind, fields)) = app.ui.shell.dialog.clone() else { return err("no shell dialog is open") }; + if let Some((command, params)) = crate::workspace_ui::dialog_command(&kind, &fields) + && let Some(authorize) = app.services.automation_command.as_ref() + && let Err(error) = authorize(command, ¶ms) + { + return err(error); + } + wrap(crate::workspace_ui::confirm(app, ctx)) + } + "ui.dialog.apply" if s("dialog") == Some(SHELL_DIALOG) => err("`ui.dialog.apply` is for Preferences; use `ui.dialog.confirm`"), + "ui.dialog.cancel" if s("dialog") == Some(SHELL_DIALOG) => match app.ui.shell.dialog.take() { + Some(_) => ok(Value::Null), + None => err("no such dialog"), + }, "ui.dialog.set" => { let (Some(id), Some(field)) = (u("dialog"), s("field")) else { return err("need `dialog` and `field`") }; let value = p.get("value").cloned().unwrap_or(Value::Null); @@ -787,8 +818,11 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest) /// Snapshot of everything on screen, addressable by id. pub fn inspect(app: &PhotocraftApp, ctx: &egui::Context) -> Value { let screen = ctx.content_rect(); - let dialogs: Vec = + let mut dialogs: Vec = app.ui.dialogs.iter().map(|d| json!({"id": d.id, "kind": d.kind, "title": crate::dialogs::title(d), "fields": d.fields})).collect(); + if let Some((kind, fields)) = &app.ui.shell.dialog { + dialogs.push(json!({"id": SHELL_DIALOG, "kind": kind, "title": crate::workspace_ui::title(kind), "fields": fields})); + } json!({ "window": {"width": screen.width(), "height": screen.height(), "pixelsPerPoint": ctx.pixels_per_point()}, "tool": app.ui.tool, @@ -875,6 +909,43 @@ mod tests { } } + #[test] + fn shell_dialogs_are_listed_and_driven_through_ui_dialog() { + // #1004: Window › Workspace and View dialogs live in `ui.shell.dialog`, and ui.inspect and + // ui.dialog.* only knew `ui.dialogs`. + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + let ctx = egui::Context::default(); + let shell = |app: &mut PhotocraftApp| { + let v = call(app, &ctx, "ui.inspect", json!({})); + v["result"]["dialogs"].as_array().unwrap().iter().find(|d| d["id"] == "shell").cloned() + }; + // Listed, then cancelled. + crate::menus::invoke(&mut app, &ctx, "window.workspace.newWorkspace", json!({})).unwrap(); + let d = shell(&mut app).expect("the open shell dialog is listed"); + assert_eq!((d["kind"].as_str(), d["title"].as_str()), (Some("newWorkspace"), Some("New Workspace"))); + assert_eq!(call(&mut app, &ctx, "ui.dialog.cancel", json!({"dialog": "shell"}))["ok"], true); + assert!(app.ui.shell.dialog.is_none() && shell(&mut app).is_none()); + assert_eq!(call(&mut app, &ctx, "ui.dialog.cancel", json!({"dialog": "shell"}))["ok"], false, "nothing left to cancel"); + // A field set over control reaches OK. + crate::menus::invoke(&mut app, &ctx, "view.show.showExtrasOptions", json!({})).unwrap(); + assert!(app.ui.view.show.notes); + assert_eq!(call(&mut app, &ctx, "ui.dialog.set", json!({"dialog": "shell", "field": "notes", "value": false}))["ok"], true); + assert_eq!(shell(&mut app).unwrap()["fields"]["notes"], false); + assert_eq!(call(&mut app, &ctx, "ui.dialog.confirm", json!({"dialog": "shell"}))["ok"], true); + assert!(!app.ui.view.show.notes && app.ui.shell.dialog.is_none()); + // An OK that fails keeps the dialog open and says why. + crate::menus::invoke(&mut app, &ctx, "view.show.showExtrasOptions", json!({})).unwrap(); + call(&mut app, &ctx, "ui.dialog.set", json!({"dialog": "shell", "field": "bogus", "value": true})); + let r = call(&mut app, &ctx, "ui.dialog.confirm", json!({"dialog": "shell"})); + assert_eq!(r["ok"], false); + assert!(r["error"].as_str().unwrap_or("").contains("bogus"), "{r}"); + assert!(app.ui.shell.dialog.is_some()); + // Apply is Preferences-only; numeric ids still address ordinary dialogs only. + assert_eq!(call(&mut app, &ctx, "ui.dialog.apply", json!({"dialog": "shell"}))["ok"], false); + assert_eq!(call(&mut app, &ctx, "ui.dialog.cancel", json!({"dialog": 0}))["ok"], false); + assert!(app.ui.shell.dialog.is_some()); + } + #[test] fn expired_queued_edit_does_not_run_and_a_live_retry_runs_once() { use std::time::{Duration, Instant}; diff --git a/crates/ui-egui/src/workspace_ui.rs b/crates/ui-egui/src/workspace_ui.rs index 3efc3464d7..8933d8d358 100644 --- a/crates/ui-egui/src/workspace_ui.rs +++ b/crates/ui-egui/src/workspace_ui.rs @@ -307,6 +307,29 @@ pub fn dialog_command(kind: &str, f: &Map) -> Option<(&'static st }) } +/// The title of a dialog kind. +pub fn title(kind: &str) -> &'static str { + match kind { + "newWorkspace" => "New Workspace", + "deleteWorkspace" => "Delete Workspace", + "customPar" => "Save Pixel Aspect Ratio", + "preview32" => "32-bit Preview Options", + _ => "Show Extras Options", + } +} + +/// OK on the open dialog (its button, or `ui.dialog.confirm {"dialog": "shell"}`): runs its +/// command, then closes it; on an error the dialog stays open. +pub fn confirm(app: &mut PhotocraftApp, ctx: &egui::Context) -> Result { + let (kind, f) = app.ui.shell.dialog.clone().ok_or("no dialog is open")?; + let (id, p) = dialog_command(&kind, &f).ok_or_else(|| format!("unknown dialog `{kind}`"))?; + let r = crate::menus::invoke(app, ctx, id, p); + if r.is_ok() { + app.ui.shell.dialog = None; + } + r +} + /// Draws the Modifier Keys panel and the open dialog. pub fn windows(app: &mut PhotocraftApp, ctx: &egui::Context) { if app.ui.shell.modifier_keys { @@ -336,13 +359,7 @@ pub fn windows(app: &mut PhotocraftApp, ctx: &egui::Context) { fn dialog(app: &mut PhotocraftApp, ctx: &egui::Context) { let Some((kind, mut f)) = app.ui.shell.dialog.clone() else { return }; let t = Tokens::get(ctx); - let title = match kind.as_str() { - "newWorkspace" => "New Workspace", - "deleteWorkspace" => "Delete Workspace", - "customPar" => "Save Pixel Aspect Ratio", - "preview32" => "32-bit Preview Options", - _ => "Show Extras Options", - }; + let title = title(&kind); let names: Vec = app.session.prefs().workspaces.keys().cloned().collect(); let mut result: Option = None; egui::Window::new(title).id(egui::Id::new("shell-dialog")).collapsible(false).resizable(false).anchor(Align2::CENTER_CENTER, vec2(0.0, 0.0)).show( @@ -440,15 +457,10 @@ fn dialog(app: &mut PhotocraftApp, ctx: &egui::Context) { ); match result { Some(true) => { - if let Some((id, p)) = dialog_command(&kind, &f) { - match crate::menus::invoke(app, ctx, id, p) { - Ok(_) => app.ui.shell.dialog = None, - Err(e) => { - app.ui.status = e; - app.ui.status_error = true; - app.ui.shell.dialog = Some((kind, f)); - } - } + app.ui.shell.dialog = Some((kind, f)); + if let Err(e) = confirm(app, ctx) { + app.ui.status = e; + app.ui.status_error = true; } } Some(false) => app.ui.shell.dialog = None, diff --git a/docs/control-protocol.md b/docs/control-protocol.md index aa85544ae5..6bef0c47fd 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -45,6 +45,7 @@ The transport is `apps/photocraft/src/control_server.rs`, and the handlers are i - `ui.set` also accepts the Eyedropper options bar (#1649): `eyedropperSampleSize` (`"point"` or 1, 3, 5, 11, 31, 51, 101: the side of the averaged square, shared by the Eyedropper, a painting tool's Alt-click eyedropper, the Curves/Color Picker eyedroppers and the Info panel), `eyedropperSample` (`current`, `currentAndBelow`, `all`, `allNoAdjustments`, `currentAndBelowNoAdjustments`) and `eyedropperRing` (boolean, Show Sampling Ring). `ui.inspect` reports them under `toolOptions` (`eyedropper_size`, `eyedropper_sample`, `eyedropper_ring`). The engine command `document.sampleColor {x, y, size, sampleLayer}` returns the colour such a sample picks. Both gradient modes use the current preset, including its opacity stops. Select `Foreground to Transparent` through `gradient.presets.select`, or pass custom `stops` and `transparency: [[0,100],[1,0]]` (location 0..1, opacity 0..100). Use `applyToLayer: false` to edit only the tool preset. Classic gradient drags dispatch `paint.gradient` without overriding them. - `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}`. Like `ui.menu.invoke`, `ui.dialog.confirm` waits for a background job its command starts (a filter dialog's OK) and replies with the result; `"wait": false` replies `{job, pending: true}` at once + - The shell's own dialogs (Window › Workspace › New Workspace / Delete Workspace, View › Pixel Aspect Ratio › Custom Pixel Aspect Ratio…, View › Show › Show Extras Options…, View › 32-bit Preview Options…) are listed in `ui.inspect`'s `dialogs` with the id `"shell"` (at most one is open). `ui.dialog.set`, `ui.dialog.confirm` and `ui.dialog.cancel` take `{"dialog": "shell"}`; OK runs the dialog's command and keeps the dialog open if it fails - Layer Style (`layerStyle {effect?}` opens on that effect kind): the fields hold the dialog's whole state, so agents read and drive it like a user. `effects` lists every effect instance in the layer's order as `{id, kind, on, params, fx}` (`fx` is the effect snapshot the dialog loaded; OK edits that snapshot with `params`, so anything the dialog doesn't model survives). `selected` picks the page (`blendingOptions` or an instance id), `preview` (default on) gates the live canvas preview, `p:blendingOptions` edits blend mode/opacity/fill opacity, and `patternList` names the usable patterns. OK replaces the layer's effects in one step (`layer.layerStyle.replace`); Cancel discards. "New Style…" saves the pending state via `style.presets.new {effects}`, a swatch click applies a style preset, and Make/Reset Default use `layer.layerStyle.makeDefault` / `layer.layerStyle.defaultFor` - Preferences (`ui.menu.invoke {id: "edit.preferences.interface"}`) exposes the sections in its `values` field. Apply (`ui.dialog.apply`) and OK (`ui.dialog.confirm`) on Preferences are refused over control because automation denies whole-preferences updates and filesystem-bearing preference sections. A refused `ui.dialog.apply` returns an error and keeps the dialog open; a refused `ui.dialog.confirm` returns an error but closes and removes the dialog. `ui.dialog.cancel` closes the dialog without saving. Individual safe preferences can instead be changed with `engine.execute` using `prefs.set` (e.g. `{"command": "prefs.set", "params": {"path": "interface.showTooltips", "value": false}}`). Settings marked for the next launch still require a restart. - 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"]`).