-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to screenshot the application (#2293)
Closes #2117 Closes #482 ### What Cmd-P for command palette, then select "Screenshot" command. Rerun will re-style itself for Web for a frame, screenshot that, and then copy it to the clipboard. I only implemented this for the native viewer. You can set the resolution at startup with `--window-size`. All screenshots are captured at 2x pixels-per-point, i.e. pretending that you are on a high-dpi screen, wether you are or not. You can also trigger this from the command line: ``` ❯ cargo rerun ../fiat.rrd --screenshot-to fiat.png Finished dev [optimized + debuginfo] target(s) in 0.46s Running `target/debug/rerun ../fiat.rrd --screenshot-to fiat.png --window-size 1024x768` [2023-05-31T16:11:21Z INFO rerun::run] Loading "../fiat.rrd"… [2023-05-31T16:11:22Z INFO re_viewer::screenshotter] Screenshot saved to "fiat.png" ``` We can use this to generate screenshots for our examples. ### Result: ![fiat](https://github.com/rerun-io/rerun/assets/1148717/98cc125e-6cb5-4d84-81ff-062f54c7fb97) ### Checklist * [x] I have read and agree to [Contributor Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md) * [x] I've included a screenshot or gif (if applicable) <!-- This line will get updated when the PR build summary job finishes. --> PR Build Summary: https://build.rerun.io/pr/2293 <!-- pr-link-docs:start --> Docs preview: https://rerun.io/preview/11b16c5/docs <!-- pr-link-docs:end -->
- Loading branch information
Showing
12 changed files
with
229 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
//! Screenshotting not implemented on web yet because we | ||
//! haven't implemented "copy image to clipboard" there. | ||
/// Helper for screenshotting the entire app | ||
#[cfg(not(target_arch = "wasm32"))] | ||
#[derive(Default)] | ||
pub struct Screenshotter { | ||
countdown: Option<usize>, | ||
target_path: Option<std::path::PathBuf>, | ||
quit: bool, | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
#[must_use] | ||
pub struct ScreenshotterOutput { | ||
/// If true, the screenshotter was told at startup to quit after its donw. | ||
pub quit: bool, | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
impl Screenshotter { | ||
/// Used for generating screenshots in dev builds. | ||
/// | ||
/// Should only be called at startup. | ||
pub fn screenshot_to_path_then_quit(&mut self, path: std::path::PathBuf) { | ||
assert!(self.countdown.is_none(), "screenshotter misused"); | ||
self.request_screenshot(); | ||
self.target_path = Some(path); | ||
} | ||
|
||
pub fn request_screenshot(&mut self) { | ||
// Give app time to change the style, and then wait for animations to finish: | ||
self.countdown = Some(10); | ||
} | ||
|
||
/// Call once per frame | ||
pub fn update( | ||
&mut self, | ||
egui_ctx: &egui::Context, | ||
frame: &mut eframe::Frame, | ||
) -> ScreenshotterOutput { | ||
if let Some(countdown) = &mut self.countdown { | ||
if *countdown == 0 { | ||
frame.request_screenshot(); | ||
} else { | ||
*countdown -= 1; | ||
} | ||
|
||
egui_ctx.request_repaint(); // Make sure we keep counting down | ||
} | ||
|
||
ScreenshotterOutput { quit: self.quit } | ||
} | ||
|
||
/// If true, temporarily re-style the UI to make it suitable for capture! | ||
/// | ||
/// We do the re-styling to create consistent screenshots across platforms. | ||
/// In particular, we style the UI to look like the web viewer. | ||
pub fn is_screenshotting(&self) -> bool { | ||
self.countdown.is_some() | ||
} | ||
|
||
pub fn save(&mut self, image: &egui::ColorImage) { | ||
self.countdown = None; | ||
if let Some(path) = self.target_path.take() { | ||
let w = image.width() as _; | ||
let h = image.height() as _; | ||
let image = | ||
image::RgbaImage::from_raw(w, h, bytemuck::pod_collect_to_vec(&image.pixels)) | ||
.expect("Failed to create image"); | ||
match image.save(&path) { | ||
Ok(()) => { | ||
re_log::info!("Screenshot saved to {path:?}"); | ||
self.quit = true; | ||
} | ||
Err(err) => { | ||
panic!("Failed saving screenshot to {path:?}: {err}"); | ||
} | ||
} | ||
} else { | ||
re_viewer_context::Clipboard::with(|cb| { | ||
cb.set_image(image.size, bytemuck::cast_slice(&image.pixels)); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
// ---------------------------------------------------------------------------- | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
#[derive(Default)] | ||
pub struct Screenshotter {} | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
impl Screenshotter { | ||
#[allow(clippy::unused_self)] | ||
pub fn is_screenshotting(&self) -> bool { | ||
false | ||
} | ||
} |
Oops, something went wrong.