A3: ui.screenshot answers with the absolute path and checks the file
A relative path is resolved against the app's working directory before the capture, and the answer names that absolute path, so a file written elsewhere than expected is visible. After writing, the file is read back and a missing or short file is an error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
af24899ac5
commit
3c80ab87c9
4 changed files with 36 additions and 10 deletions
|
|
@ -39,7 +39,8 @@ const CAPTURE_TIMEOUT: Duration = Duration::from_secs(10);
|
|||
///
|
||||
/// # Errors
|
||||
/// A message if the window does not exist or is minimised, the capture fails
|
||||
/// or times out, the result is not a PNG, or the file cannot be written.
|
||||
/// or times out, the result is not a PNG, or the file cannot be written (or
|
||||
/// is missing or short when read back).
|
||||
pub fn capture_window(app: &AppHandle, label: &str, path: &Path) -> Result<(), String> {
|
||||
let window = app
|
||||
.get_webview_window(label)
|
||||
|
|
@ -55,7 +56,17 @@ pub fn capture_window(app: &AppHandle, label: &str, path: &Path) -> Result<(), S
|
|||
if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) {
|
||||
std::fs::create_dir_all(parent).map_err(|e| format!("{}: {e}", parent.display()))?;
|
||||
}
|
||||
std::fs::write(path, png).map_err(|e| format!("{}: {e}", path.display()))
|
||||
std::fs::write(path, &png).map_err(|e| format!("{}: {e}", path.display()))?;
|
||||
match std::fs::metadata(path) {
|
||||
Ok(meta) if meta.len() == png.len() as u64 => Ok(()),
|
||||
Ok(meta) => Err(format!(
|
||||
"{}: wrote {} of {} bytes",
|
||||
path.display(),
|
||||
meta.len(),
|
||||
png.len()
|
||||
)),
|
||||
Err(e) => Err(format!("{}: not found after writing: {e}", path.display())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks that `bytes` start with the PNG signature and stay within
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@ pub const ENGINE_EXECUTE: &str = "engine.execute";
|
|||
/// `engine.listCommands`: lists the engine's commands.
|
||||
pub const ENGINE_LIST_COMMANDS: &str = "engine.listCommands";
|
||||
/// `ui.screenshot`: writes a PNG of the main window without focusing it.
|
||||
/// A relative `path` is resolved against the app's working directory; the
|
||||
/// result names the absolute path written.
|
||||
pub const UI_SCREENSHOT: &str = "ui.screenshot";
|
||||
/// `events.subscribe`: streams engine events on this connection.
|
||||
pub const EVENTS_SUBSCRIBE: &str = "events.subscribe";
|
||||
|
|
|
|||
|
|
@ -190,10 +190,13 @@ fn dispatch(request: Request, backend: &dyn Backend) -> Response {
|
|||
Err(e) => Response::err(id, "internal", e.to_string()),
|
||||
},
|
||||
UI_SCREENSHOT => match serde_json::from_value::<ScreenshotParams>(params) {
|
||||
Ok(p) => match backend.screenshot(&p.path) {
|
||||
Ok(()) => Response::ok(id, json!({ "path": p.path })),
|
||||
Err(message) => Response::err(id, codes::SCREENSHOT_FAILED, message),
|
||||
},
|
||||
Ok(p) => {
|
||||
let path = std::path::absolute(&p.path).unwrap_or(p.path);
|
||||
match backend.screenshot(&path) {
|
||||
Ok(()) => Response::ok(id, json!({ "path": path })),
|
||||
Err(message) => Response::err(id, codes::SCREENSHOT_FAILED, message),
|
||||
}
|
||||
}
|
||||
Err(e) => Response::err(id, codes::INVALID_PARAMS, e.to_string()),
|
||||
},
|
||||
APP_AUTH => Response::ok(id, json!({ "authenticated": true })),
|
||||
|
|
@ -256,10 +259,12 @@ mod tests {
|
|||
}
|
||||
|
||||
fn screenshot(&self, path: &Path) -> Result<(), String> {
|
||||
if path.as_os_str() == "panic" {
|
||||
panic!("screenshot exploded");
|
||||
assert!(path.is_absolute(), "{}", path.display());
|
||||
match path.file_name().and_then(|n| n.to_str()) {
|
||||
Some("panic") => panic!("screenshot exploded"),
|
||||
Some("ok.png") => Ok(()),
|
||||
_ => Err("no window".into()),
|
||||
}
|
||||
Err("no window".into())
|
||||
}
|
||||
|
||||
fn subscribe(&self, _sink: crate::EventSink) {}
|
||||
|
|
@ -332,6 +337,14 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn screenshot_reports_the_absolute_path() {
|
||||
let r = call(r#"{"id":1,"method":"ui.screenshot","params":{"path":"shots/ok.png"}}"#);
|
||||
let path = std::path::PathBuf::from(r.result.unwrap()["path"].as_str().unwrap());
|
||||
assert!(path.is_absolute());
|
||||
assert!(path.ends_with("shots/ok.png"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn backend_panics_become_internal_errors() {
|
||||
let r = call(r#"{"id":9,"method":"ui.screenshot","params":{"path":"panic"}}"#);
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ As built in M0:
|
|||
|
||||
- Transport: one JSON object per line, UTF-8, at most 16 MiB per line; longer lines get an error response and the connection is closed.
|
||||
- Request: `{"id": <number|string>, "method": "<name>", "params": {...}}`. Response: `{"id": ..., "result": ...}` or `{"id": ..., "error": {"code": "...", "message": "..."}}`. A line that is not valid JSON gets an error response with `"id": null`.
|
||||
- Methods: `app.ping`, `app.auth {token}`, `app.focus`, `engine.execute {command, params}`, `engine.listCommands`, `ui.screenshot {path}`, `events.subscribe`. Names and docs live in `crates/control/src/methods.rs`.
|
||||
- Methods: `app.ping`, `app.auth {token}`, `app.focus`, `engine.execute {command, params}`, `engine.listCommands`, `ui.screenshot {path}` (→ `{path}`, absolute; the file is read back after writing and a missing or short file is an error), `events.subscribe`. Names and docs live in `crates/control/src/methods.rs`.
|
||||
- `events.subscribe` answers `{subscribed: true}` and from then on writes `{"event": {...}}` lines for every engine event on that connection, between responses; a shared writer writes each line whole. Subscribing twice is a no-op. Each subscriber has a queue of 4096 events (`lutra_control::EVENT_QUEUE`); a subscriber that falls behind or disconnects is dropped by `Subscribers::publish`, and the connection gets a final `{"event": {"kind": "subscriptionLost"}}` line if it is still open.
|
||||
- `lutra_control` defines a `Backend` trait; the desktop app implements it (engine access, effects, screenshots, event subscribers), tests implement it with a bare engine (`lutra_testkit::EngineBackend`). Hosts publish each outcome's events through `lutra_control::Subscribers`. One thread accepts connections and each connection gets its own thread; no async runtime is needed in the crate.
|
||||
- Screenshots never focus, raise or show the window; `app.focus` is the only method that does. On Windows the webview captures itself with WebView2 `CapturePreview` (PNG into a memory stream, scheduled on the main thread through `with_webview`, the control thread waits up to 10 s), so windows covering Lutra do not appear and the native title bar is not part of the image (`apps/lutra-desktop/src/screenshot/webview2.rs`). Fallback on macOS and Linux until WKWebView's and WebKitGTK's snapshot APIs are wired: `xcap` captures the screen region under the client area (it does not enumerate windows of its own process, whose title queries can deadlock), without focusing, so covering windows appear (`screenshot/region.rs`). A capture that does not arrive within 10 s is explained: the main thread never ran the request (blocked), or WebView2 accepted it but drew no frame; in that case the page's `document.visibilityState` is read and a hidden page is reported with its likely causes (locked session, display asleep, minimised or on another virtual desktop). Covered, off-screen and DWM-cloaked windows capture fine (checked on Windows 11). A `--control` instance builds its window and webview with `focused(false)`; when it is started from the foreground app's process tree it still becomes the foreground window about 1 s after start (seen 2026-10-29; see `docs/gaps.md`). The ignored test `apps/lutra-desktop/tests/screenshot_focus.rs` starts a built app and fails if its window is ever in the foreground while it starts and takes screenshots.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue