slint_snapshot/lib.rs
1//! Headless RGBA and PNG snapshots for compiled Slint components.
2//!
3//! Application-specific concerns such as scenario registration, fixtures,
4//! locales, command-line parsing, and asset export belong in each project's
5//! preview runner.
6//!
7//! Pure in-memory RGBA comparison is available in [`comparison`]. The optional
8//! `testing` Cargo feature adds PNG baselines, explicit create/accept modes,
9//! failure artifacts, and test assertions.
10//!
11//! The runtime does not connect to a desktop or windowing system. By default,
12//! it also does not enable Slint's system-font stack or link the final preview
13//! executable to Fontconfig.
14//!
15//! Slint permits only one platform per process, so a snapshot runtime should
16//! normally live in a dedicated preview or test executable.
17//!
18//! # Threading
19//!
20//! The default feature set uses Slint's `unsafe-single-threaded` runtime.
21//! [`SnapshotRuntime`] is neither `Send` nor `Sync`; create, use, and drop it on
22//! one thread. The optional `system-fonts` feature enables `slint/std`.
23//!
24//! # Fonts and resources
25//!
26//! Consumers that render text or images without `system-fonts` must compile
27//! their `.slint` files with
28//! `slint_build::EmbedResourcesKind::EmbedForSoftwareRenderer`. Import a fixed
29//! font and select it with `default-font-family` when snapshot reproducibility
30//! matters. Cargo features are additive, so another dependency enabling
31//! `slint/std` also enables Slint's system-font stack for the final binary.
32//! It also enables rendering of `Path` elements; the default configuration
33//! does not render those elements. On Linux, building with `system-fonts`
34//! requires the Fontconfig development files discoverable by `pkg-config`.
35//!
36//! # Example
37//!
38//! ```
39//! use slint::ComponentHandle;
40//! use slint_snapshot::SnapshotRuntime;
41//!
42//! slint::slint! {
43//! #[edition = "1.19"]
44//! export component ColorCard inherits Window {
45//! background: #202538;
46//! Rectangle {
47//! width: 96px;
48//! height: 64px;
49//! background: #7c5cff;
50//! border-radius: 12px;
51//! }
52//! }
53//! }
54//!
55//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
56//! let runtime = SnapshotRuntime::new()?;
57//! let ui = ColorCard::new().expect("create the Slint component");
58//! runtime.set_size(ui.window(), (320, 180), 1.0)?;
59//!
60//! let frame = runtime.render(ui.window())?;
61//! assert_eq!(frame.dimensions(), (320, 180));
62//! let png = frame.encode_png()?;
63//! assert!(png.starts_with(b"\x89PNG\r\n\x1a\n"));
64//! # Ok(())
65//! # }
66//! ```
67
68#![cfg_attr(docsrs, feature(doc_cfg))]
69
70pub mod comparison;
71pub mod frame;
72mod png_codec;
73pub mod runtime;
74
75#[cfg(feature = "testing")]
76#[cfg_attr(docsrs, doc(cfg(feature = "testing")))]
77pub mod testing;
78
79pub use frame::RenderedFrame;
80pub use runtime::SnapshotRuntime;