Skip to main content

snapshot/
snapshot.rs

1//! Launch the CAD application headless from Rust, wait for its first picture,
2//! click its File menu, and save what it shows as a PNG. No window, no browser.
3//!
4//!     cargo run --example snapshot -- [path/to/brep-app]
5use brep_stream_client::{Input, Launcher, Mods, PointerButton};
6use std::time::{Duration, Instant};
7
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let program = std::env::args().nth(1).unwrap_or_else(|| "brep-app".into());
10    let started = Instant::now();
11    // 1280 x 800 logical pixels at density 1. The app ends with the session.
12    let session = Launcher::new(&program).size(1280.0, 800.0, 1.0).launch()?;
13    session.wait_still(Duration::from_millis(1500), Duration::from_secs(120))?;
14    let (width, height) = {
15        let frame = session.frame();
16        (frame.width, frame.height)
17    };
18    println!("ready in {:.1} s: {width} x {height} pixels", started.elapsed().as_secs_f32());
19
20    // The File menu sits at (148, 19) in the default layout.
21    let (x, y) = (148.0, 19.0);
22    let clicked = Instant::now();
23    session.send(Input::PointerMove { x, y });
24    session.send(Input::Button { x, y, button: PointerButton::Primary, down: true, mods: Mods::NONE });
25    session.send(Input::Button { x, y, button: PointerButton::Primary, down: false, mods: Mods::NONE });
26    session.wait_frame(Duration::from_secs(10))?;
27    println!("the click came back as a new frame in {} ms", clicked.elapsed().as_millis());
28    session.wait_still(Duration::from_millis(500), Duration::from_secs(30))?;
29
30    {
31        let frame = session.frame();
32        image::save_buffer("brep-cad.png", &frame.rgba, frame.width, frame.height, image::ColorType::Rgba8)?;
33        println!("saved brep-cad.png after {} frames", frame.frames);
34    }
35    let status = session.shutdown(Duration::from_secs(15))?;
36    println!("the app exited: {status}");
37    Ok(())
38}