Skip to content

Commit 9ef9022

Browse files
committed
Add screenshot target and source
1 parent 781f0a2 commit 9ef9022

File tree

7 files changed

+49
-36
lines changed

7 files changed

+49
-36
lines changed

crates/viewer/re_space_view/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ mod instance_hash_conversions;
1111
mod outlines;
1212
mod query;
1313
mod results_ext;
14-
mod screenshot;
1514
mod view_property_ui;
1615

1716
pub use annotation_context_utils::{
@@ -31,7 +30,6 @@ pub use query::{
3130
pub use results_ext::{
3231
HybridLatestAtResults, HybridResults, HybridResultsChunkIter, RangeResultsExt,
3332
};
34-
pub use screenshot::ScreenshotMode;
3533
pub use view_property_ui::view_property_ui;
3634

3735
pub mod external {

crates/viewer/re_space_view/src/screenshot.rs

-9
This file was deleted.

crates/viewer/re_space_view_spatial/src/ui.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use egui::{epaint::util::OrderedFloat, text::TextWrapping, NumExt as _, WidgetTe
22

33
use re_format::format_f32;
44
use re_math::BoundingBox;
5-
use re_space_view::ScreenshotMode;
65
use re_types::{
76
archetypes::Pinhole, blueprint::components::VisualBounds2D, components::ViewCoordinates,
87
image::ImageKind,
98
};
109
use re_ui::UiExt as _;
1110
use re_viewer_context::{
12-
HoverHighlight, SelectionHighlight, SpaceViewHighlights, SpaceViewState, ViewerContext,
11+
HoverHighlight, ScreenshotTarget, SelectionHighlight, SpaceViewHighlights, SpaceViewState,
12+
ViewerContext,
1313
};
1414

1515
use crate::{
@@ -344,18 +344,18 @@ pub fn paint_loading_spinners(
344344
pub fn screenshot_context_menu(
345345
_ctx: &ViewerContext<'_>,
346346
_response: &egui::Response,
347-
) -> Option<ScreenshotMode> {
347+
) -> Option<ScreenshotTarget> {
348348
#[cfg(not(target_arch = "wasm32"))]
349349
{
350350
if _ctx.app_options.experimental_space_view_screenshots {
351351
let mut take_screenshot = None;
352352
_response.context_menu(|ui| {
353353
ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Extend);
354354
if ui.button("Save screenshot to disk").clicked() {
355-
take_screenshot = Some(ScreenshotMode::SaveAndCopyToClipboard);
355+
take_screenshot = Some(ScreenshotTarget::SaveAndCopyToClipboard);
356356
ui.close_menu();
357357
} else if ui.button("Copy screenshot to clipboard").clicked() {
358-
take_screenshot = Some(ScreenshotMode::CopyToClipboard);
358+
take_screenshot = Some(ScreenshotTarget::CopyToClipboard);
359359
ui.close_menu();
360360
}
361361
});

crates/viewer/re_space_view_spatial/src/ui_2d.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,14 @@ impl SpatialSpaceView2D {
249249

250250
// ------------------------------------------------------------------------
251251

252-
if let Some(mode) = screenshot_context_menu(ctx, &response) {
252+
if let Some(target) = screenshot_context_menu(ctx, &response) {
253253
ctx.egui_ctx
254254
.send_viewport_cmd(egui::ViewportCommand::Screenshot(egui::UserData::new(
255255
re_viewer_context::ScreenshotInfo {
256-
space_view: Some(query.space_view_id),
257256
ui_rect: Some(response.rect),
258257
pixels_per_point: ui.ctx().pixels_per_point(),
258+
source: re_viewer_context::ScreenshotSource::SpaceView(query.space_view_id),
259+
target,
259260
},
260261
)));
261262
}

crates/viewer/re_viewer/src/app.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::sync::Arc;
22

33
use itertools::Itertools as _;
4+
45
use re_build_info::CrateVersion;
56
use re_data_source::{DataSource, FileContents};
67
use re_entity_db::entity_db::EntityDb;
@@ -12,8 +13,8 @@ use re_viewer_context::{
1213
command_channel,
1314
store_hub::{BlueprintPersistence, StoreHub, StoreHubStats},
1415
AppOptions, BlueprintUndoState, CommandReceiver, CommandSender, ComponentUiRegistry, PlayState,
15-
SpaceViewClass, SpaceViewClassRegistry, SpaceViewClassRegistryError, StoreContext,
16-
SystemCommand, SystemCommandSender,
16+
ScreenshotInfo, SpaceViewClass, SpaceViewClassRegistry, SpaceViewClassRegistryError,
17+
StoreContext, SystemCommand, SystemCommandSender,
1718
};
1819

1920
use crate::app_blueprint::PanelStateOverrides;
@@ -1904,13 +1905,14 @@ impl eframe::App for App {
19041905
if let Some(info) = &user_data
19051906
.data
19061907
.as_ref()
1907-
.and_then(|data| data.downcast_ref::<re_viewer_context::ScreenshotInfo>())
1908+
.and_then(|data| data.downcast_ref::<ScreenshotInfo>())
19081909
{
19091910
re_log::info!("Screenshot info: {info:?}");
19101911
let ScreenshotInfo {
1911-
space_view,
19121912
ui_rect,
19131913
pixels_per_point,
1914+
source,
1915+
target,
19141916
} = (*info).clone();
19151917

19161918
let rgba = if let Some(ui_rect) = ui_rect {

crates/viewer/re_viewer_context/src/lib.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,31 @@ pub fn contents_name_style(name: &ContentsName) -> re_ui::LabelStyle {
133133
/// Specified what we are screenshotting.
134134
#[derive(Clone, Debug, PartialEq)]
135135
pub struct ScreenshotInfo {
136-
pub space_view: Option<SpaceViewId>,
136+
/// What portion of the UI to take a screenshot of (in ui points).
137137
pub ui_rect: Option<egui::Rect>,
138138
pub pixels_per_point: f32,
139+
140+
/// What we are screenshotting.
141+
pub source: ScreenshotSource,
142+
143+
/// Where to put the screenshot.
144+
pub target: ScreenshotTarget,
145+
}
146+
147+
/// What we are screenshotting.
148+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
149+
pub enum ScreenshotSource {
150+
WholeApp,
151+
152+
SpaceView(SpaceViewId),
153+
}
154+
155+
/// Where to put the screenshot.
156+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
157+
pub enum ScreenshotTarget {
158+
/// The screenshot will be saved to disc and copied to the clipboard.
159+
SaveAndCopyToClipboard,
160+
161+
/// The screenshot will be copied to the clipboard.
162+
CopyToClipboard,
139163
}
+10-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
use re_space_view::ScreenshotMode;
1+
use re_viewer_context::ScreenshotTarget;
22
use re_viewport_blueprint::SpaceViewBlueprint;
33

4+
// TODO: use this
45
pub fn handle_pending_space_view_screenshots(
56
space_view: &SpaceViewBlueprint,
67
data: &[u8],
78
extent: glam::UVec2,
8-
mode: ScreenshotMode,
9+
target: ScreenshotTarget,
910
) {
1011
// Set to clipboard.
1112
#[cfg(not(target_arch = "wasm32"))]
1213
re_viewer_context::Clipboard::with(|clipboard| {
1314
clipboard.set_image([extent.x as _, extent.y as _], data);
1415
});
15-
if mode == ScreenshotMode::CopyToClipboard {
16+
if target == ScreenshotTarget::CopyToClipboard {
1617
return;
1718
}
1819

@@ -33,20 +34,16 @@ pub fn handle_pending_space_view_screenshots(
3334
i += 1;
3435
};
3536
let filename = std::path::Path::new(&filename);
37+
let readable_path = filename.canonicalize().unwrap_or(filename.to_path_buf());
38+
39+
// TODO: use rfd to pick a path?
3640

3741
match image::save_buffer(filename, data, extent.x, extent.y, image::ColorType::Rgba8) {
38-
Ok(_) => {
39-
re_log::info!(
40-
"Saved screenshot to {:?}.",
41-
filename.canonicalize().unwrap_or(filename.to_path_buf())
42-
);
42+
Ok(()) => {
43+
re_log::info!("Saved screenshot to {readable_path:?}.",);
4344
}
4445
Err(err) => {
45-
re_log::error!(
46-
"Failed to safe screenshot to {:?}: {}",
47-
filename.canonicalize().unwrap_or(filename.to_path_buf()),
48-
err
49-
);
46+
re_log::error!("Failed to save screenshot to {readable_path:?}: {err}");
5047
}
5148
}
5249
}

0 commit comments

Comments
 (0)