F2: ui.screenshot captures the webview without focusing the window

On Windows the webview renders itself into a PNG through WebView2
CapturePreview (scheduled on the main thread via with_webview, 10 s
timeout, PNG signature and 64 MiB cap checked). macOS and Linux keep the
xcap screen-region capture as a documented fallback, now without focusing.
app.focus is the only control method that changes focus. A --control
instance opens its window without taking the foreground. An ignored live
test samples the foreground window while the app starts and takes five
screenshots and fails if Lutra ever owns it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-27 17:13:38 +01:00
parent d567f3957d
commit f49b42836a
12 changed files with 398 additions and 84 deletions

View file

@ -1,30 +1,45 @@
//! Screenshots of Lutra's main window for `ui.screenshot`.
//!
//! `xcap` refuses to enumerate windows of the calling process (querying their
//! titles can deadlock), so the screenshot is taken from the screen instead:
//! the main window is brought to the front, its client area is located in
//! physical pixels through Tauri, and that region of the monitor is captured.
//! Anything covering the window at that moment shows up in the image.
//! A screenshot never focuses, raises or shows the window: `app.focus` is the
//! only control method that does. Taking one while the user types into
//! another app must not pull their keystrokes into Lutra.
//!
//! - Windows: the webview renders itself into a PNG through WebView2's
//! `CapturePreview` ([`webview2`]). The image is the page as the webview
//! draws it, whether or not other windows cover Lutra; the native title bar
//! is not part of it.
//! - Other platforms (fallback): the window's client area is located in
//! physical pixels through Tauri and that region of its monitor is captured
//! with `xcap` ([`region`]). Windows covering Lutra at that moment appear in
//! the image. WKWebView's and WebKitGTK's own snapshot APIs can replace it.
use std::path::Path;
use std::time::Duration;
use tauri::{AppHandle, Manager};
use xcap::Monitor;
use xcap::image::ImageFormat;
/// Time for the compositor to show the window after it was focused.
const SETTLE: Duration = Duration::from_millis(150);
#[cfg(not(windows))]
mod region;
#[cfg(windows)]
mod webview2;
/// Captures the client area of the window labelled `label` and writes it to
/// `path` as PNG. Must not be called on the main thread, which has to stay
/// free to answer the window queries. Focusing the window is best effort: a
/// failure is ignored because the capture still works when the window is
/// already in front.
/// The eight bytes every PNG file starts with.
const PNG_SIGNATURE: [u8; 8] = [0x89, b'P', b'N', b'G', b'\r', b'\n', 0x1a, b'\n'];
/// Largest PNG accepted from the platform capture.
const MAX_PNG_BYTES: usize = 64 * 1024 * 1024;
/// How long to wait for the platform capture before giving up.
const CAPTURE_TIMEOUT: Duration = Duration::from_secs(10);
/// Captures the window labelled `label` and writes it to `path` as PNG,
/// without changing which window is in front or has the keyboard focus.
/// Must not be called on the main thread: the capture is scheduled there and
/// this function waits for it.
///
/// # Errors
/// A message if the window does not exist or is minimised, the monitor
/// cannot be captured, or the PNG cannot be written.
/// 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.
pub fn capture_window(app: &AppHandle, label: &str, path: &Path) -> Result<(), String> {
let window = app
.get_webview_window(label)
@ -32,76 +47,43 @@ pub fn capture_window(app: &AppHandle, label: &str, path: &Path) -> Result<(), S
if window.is_minimized().unwrap_or(false) {
return Err("the main window is minimised".into());
}
let _ = window.set_focus();
std::thread::sleep(SETTLE);
let pos = window.inner_position().map_err(|e| e.to_string())?;
let size = window.inner_size().map_err(|e| e.to_string())?;
let centre_x = pos
.x
.saturating_add(i32::try_from(size.width / 2).unwrap_or(0));
let centre_y = pos
.y
.saturating_add(i32::try_from(size.height / 2).unwrap_or(0));
let monitor = Monitor::from_point(centre_x, centre_y)
.map_err(|e| format!("cannot find the window's monitor: {e}"))?;
let (mx, my) = (
monitor.x().map_err(|e| e.to_string())?,
monitor.y().map_err(|e| e.to_string())?,
);
let (mw, mh) = (
monitor.width().map_err(|e| e.to_string())?,
monitor.height().map_err(|e| e.to_string())?,
);
let region = clip(
pos.x.saturating_sub(mx),
pos.y.saturating_sub(my),
size.width,
size.height,
mw,
mh,
)
.ok_or("the main window is outside its monitor")?;
let image = monitor
.capture_region(region.0, region.1, region.2, region.3)
.map_err(|e| format!("cannot capture the screen: {e}"))?;
#[cfg(windows)]
let png = webview2::capture_png(&window)?;
#[cfg(not(windows))]
let png = region::capture_png(&window)?;
check_png(&png)?;
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()))?;
}
image
.save_with_format(path, ImageFormat::Png)
.map_err(|e| format!("{}: {e}", path.display()))
std::fs::write(path, png).map_err(|e| format!("{}: {e}", path.display()))
}
/// Clips a rectangle given relative to a monitor's origin to the monitor's
/// bounds, returning `(x, y, width, height)` or `None` if nothing is left.
fn clip(x: i32, y: i32, w: u32, h: u32, mw: u32, mh: u32) -> Option<(u32, u32, u32, u32)> {
let left = i64::from(x).max(0);
let top = i64::from(y).max(0);
let right = (i64::from(x) + i64::from(w)).min(i64::from(mw));
let bottom = (i64::from(y) + i64::from(h)).min(i64::from(mh));
if right <= left || bottom <= top {
return None;
/// Checks that `bytes` start with the PNG signature and stay within
/// [`MAX_PNG_BYTES`].
///
/// # Errors
/// A message naming what is wrong.
fn check_png(bytes: &[u8]) -> Result<(), String> {
if bytes.len() > MAX_PNG_BYTES {
return Err(format!("the capture is larger than {MAX_PNG_BYTES} bytes"));
}
Some((
u32::try_from(left).ok()?,
u32::try_from(top).ok()?,
u32::try_from(right - left).ok()?,
u32::try_from(bottom - top).ok()?,
))
if !bytes.starts_with(&PNG_SIGNATURE) {
return Err("the capture is not a PNG image".into());
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::clip;
use super::*;
#[test]
fn clips_to_the_monitor() {
assert_eq!(clip(10, 20, 100, 50, 1920, 1080), Some((10, 20, 100, 50)));
assert_eq!(clip(-10, -5, 100, 50, 1920, 1080), Some((0, 0, 90, 45)));
assert_eq!(
clip(1900, 1000, 100, 100, 1920, 1080),
Some((1900, 1000, 20, 80))
);
assert_eq!(clip(2000, 0, 100, 100, 1920, 1080), None);
fn accepts_png_and_rejects_the_rest() {
let mut png = PNG_SIGNATURE.to_vec();
png.extend_from_slice(b"rest");
assert!(check_png(&png).is_ok());
assert!(check_png(b"GIF89a").is_err());
assert!(check_png(&[]).is_err());
assert!(check_png(&vec![0; MAX_PNG_BYTES + 1]).is_err());
}
}