breadhelp/src/screenshot.rs
Breadway 8a1f019b42 breadhelp: add --screenshot CLI mode for automated capture
Three views ("home", "learn", "ask") — one per tab in the Home/Learn/Ask
Stack, switched via the same set_visible_child_name the tab switcher
itself uses, then captured as a full known-size canvas (the window is a
plain top-level, not layer-shell).

Plumbed through breadhelp's own cli::Action/parse() (three more fields,
same shape as force_onboard/autostart/suggest) rather than clap, matching
the app's existing non-clap idiom.

The HANDLES_COMMAND_LINE + thread_local HANDLE singleton architecture
needed one addition beyond the usual NON_UNIQUE-flag fix: every
invocation (including a --screenshot one) normally forwards to the
already-built window over D-Bus and reuses it — worse than the plain
GApplication case in the other apps, since here it's not just "message
the existing instance" but literally switching tabs on and re-capturing
the operator's real, live help-center window. NON_UNIQUE is now set
whenever --screenshot is present in argv (checked before the
Application is even built, since cli::parse() only runs per-invocation
inside connect_command_line).
2026-07-29 11:49:28 +08:00

67 lines
2.4 KiB
Rust

//! `--screenshot` CLI mode: switch to the named tab, capture it via
//! `bread-screenshots`, then exit — driven by `bread-ecosystem`'s
//! `bread-capture` orchestrator, or run standalone for one-off captures.
//!
//! breadhelp has three tabs worth capturing (Home/Learn/Ask), switched via
//! the same `Stack::set_visible_child_name` the tab switcher itself uses —
//! see `ui::tabs`. The window is a plain top-level (not layer-shell), so a
//! full known-size canvas capture is enough, same reasoning as breadpad's
//! popup view.
use gtk4::prelude::*;
use std::path::PathBuf;
use std::time::Duration;
/// Extra settle time after `map` for the first frame to actually paint
/// before grim runs — `map` fires once the surface exists, not once
/// anything has been drawn into it.
const SETTLE_DELAY: Duration = Duration::from_millis(300);
const KNOWN_VIEWS: &[&str] = &["home", "learn", "ask"];
#[derive(Clone)]
pub struct ScreenshotRequest {
pub view: String,
pub output: PathBuf,
pub width: u32,
pub height: u32,
}
/// Wire up the given view's screenshot sequence against an already-built
/// window and its tab `Stack`. Every path here ends by exiting the process
/// — it never returns control to the normal help-center UI.
pub fn dispatch(window: &gtk4::ApplicationWindow, stack: &gtk4::Stack, req: ScreenshotRequest) {
if !KNOWN_VIEWS.contains(&req.view.as_str()) {
eprintln!(
"breadhelp: unknown screenshot view '{}' (known: {})",
req.view,
KNOWN_VIEWS.join(", ")
);
std::process::exit(1);
}
stack.set_visible_child_name(&req.view);
let output = req.output;
let (width, height) = (req.width as i32, req.height as i32);
window.connect_map(move |_| {
let output = output.clone();
gtk4::glib::timeout_add_local_once(SETTLE_DELAY, move || {
finish(bread_screenshots::capture_region(0, 0, width, height, &output));
});
});
// The caller (`ui::window::present`) returns immediately after this for
// the screenshot path, skipping its own normal `window.present()` call
// — trigger it here instead, so `connect_map` above actually has
// something to fire for.
window.present();
}
fn finish(result: anyhow::Result<()>) {
match result {
Ok(()) => std::process::exit(0),
Err(e) => {
eprintln!("breadhelp: screenshot capture failed: {e}");
std::process::exit(1);
}
}
}