Skip to content

Commit

Permalink
feat: in-memory canvas snapshots (#979)
Browse files Browse the repository at this point in the history
* ignore bacon.toml

* feat: create_snapshot

* formatting

* change docs

Co-authored-by: Marc Espin <[email protected]>

* requested changes

* formatting...

* Apply suggestions from code review

Co-authored-by: Marc Espin <[email protected]>

* formatting...

---------

Co-authored-by: Marc Espin <[email protected]>
  • Loading branch information
RobertasJ and marc2332 authored Oct 13, 2024
1 parent 3305e7c commit 9388a40
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ Cargo.lock
.idea
snapshot_before.png
snapshot_after.png
documents_example
documents_example
bacon.toml
18 changes: 11 additions & 7 deletions crates/testing/src/test_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use freya_core::prelude::{
use freya_engine::prelude::{
raster_n32_premul,
Color,
Data,
EncodedImageFormat,
FontCollection,
FontMgr,
Expand Down Expand Up @@ -299,8 +300,8 @@ impl TestingHandler {
self.utils.sdom()
}

/// Render the app into a canvas and save it into a file.
pub fn save_snapshot(&mut self, snapshot_path: impl Into<PathBuf>) {
/// Render the app into a canvas and make a snapshot of it.
pub fn create_snapshot(&mut self) -> Data {
let fdom = self.utils.sdom.get();
let (width, height) = self.config.size.to_i32().to_tuple();

Expand Down Expand Up @@ -341,16 +342,19 @@ impl TestingHandler {
// Capture snapshot
let image = surface.image_snapshot();
let mut context = surface.direct_context();
let snapshot_data = image
image
.encode(context.as_mut(), EncodedImageFormat::PNG, None)
.expect("Failed to encode the snapshot.");
.expect("Failed to encode the snapshot.")
}

// Save snapshot
/// Render the app into a canvas and save it into a file.
pub fn save_snapshot(&mut self, snapshot_path: impl Into<PathBuf>) {
let mut snapshot_file =
File::create(snapshot_path.into()).expect("Failed to create the snapshot file.");
let snapshot_bytes = snapshot_data.as_bytes();
let snapshot_data = self.create_snapshot();

snapshot_file
.write_all(snapshot_bytes)
.write_all(&snapshot_data)
.expect("Failed to save the snapshot file.");
}

Expand Down

0 comments on commit 9388a40

Please sign in to comment.