Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

egui-based view screenshotting #8258

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions crates/viewer/re_space_view_spatial/src/ui_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,14 @@ impl SpatialSpaceView2D {
// ------------------------------------------------------------------------

if let Some(mode) = screenshot_context_menu(ctx, &response) {
view_builder
.schedule_screenshot(render_ctx, query.space_view_id.gpu_readback_id(), mode)
.ok();
ctx.egui_ctx
.send_viewport_cmd(egui::ViewportCommand::Screenshot(egui::UserData::new(
re_viewer_context::ScreenshotInfo {
space_view: Some(query.space_view_id),
ui_rect: Some(response.rect),
pixels_per_point: ui.ctx().pixels_per_point(),
},
)));
}

// Draw a re_renderer driven view.
Expand Down
33 changes: 31 additions & 2 deletions crates/viewer/re_viewer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1864,8 +1864,37 @@ impl eframe::App for App {
#[cfg(not(target_arch = "wasm32"))]
egui_ctx.input(|i| {
for event in &i.raw.events {
if let egui::Event::Screenshot { image, .. } = event {
self.screenshotter.save(image);
if let egui::Event::Screenshot {
image, user_data, ..
} = event
{
if let Some(info) = &user_data
.data
.as_ref()
.and_then(|data| data.downcast_ref::<re_viewer_context::ScreenshotInfo>())
{
re_log::info!("Screenshot info: {info:?}");
let ScreenshotInfo {
space_view,
ui_rect,
pixels_per_point,
} = (*info).clone();

let rgba = if let Some(ui_rect) = ui_rect {
Arc::new(image.region(&ui_rect, Some(pixels_per_point)))
} else {
image.clone()
};

re_viewer_context::Clipboard::with(|clipboard| {
clipboard.set_image(
[rgba.width(), rgba.height()],
bytemuck::cast_slice(rgba.as_raw()),
);
});
} else {
self.screenshotter.save(image);
}
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion crates/viewer/re_viewer/src/screenshotter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Screenshotter {
// is done and transferred to ram.
// Obviously we want to send the command this command only once, so we keep counting down
// to negatives until we get a call to `save` which then disables the counter.
egui_ctx.send_viewport_cmd(egui::ViewportCommand::Screenshot);
egui_ctx.send_viewport_cmd(egui::ViewportCommand::Screenshot(Default::default()));
}
*countdown -= 1;

Expand Down
10 changes: 10 additions & 0 deletions crates/viewer/re_viewer_context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,13 @@ pub fn contents_name_style(name: &ContentsName) -> re_ui::LabelStyle {
ContentsName::Placeholder(_) => re_ui::LabelStyle::Unnamed,
}
}

/// Info given to egui when taking a screenshot.
///
/// Specified what we are screenshotting.
#[derive(Clone, Debug, PartialEq)]
pub struct ScreenshotInfo {
pub space_view: Option<SpaceViewId>,
pub ui_rect: Option<egui::Rect>,
pub pixels_per_point: f32,
}
Loading