Skip to content

Commit

Permalink
Add context menu action to copy entity paths to clipboard (#9137)
Browse files Browse the repository at this point in the history
Co-authored-by: Antoine Beyeler <[email protected]>
  • Loading branch information
Wumpf and abey79 authored Feb 26, 2025
1 parent 811e48b commit 7c297e1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
53 changes: 53 additions & 0 deletions crates/viewer/re_context_menu/src/actions/copy_entity_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use itertools::Itertools;
use re_viewer_context::Item;

use crate::{ContextMenuAction, ContextMenuContext};

pub struct CopyEntityPathToClipboard;

impl ContextMenuAction for CopyEntityPathToClipboard {
fn supports_multi_selection(&self, _ctx: &ContextMenuContext<'_>) -> bool {
true
}

fn supports_item(&self, _ctx: &ContextMenuContext<'_>, item: &Item) -> bool {
match item {
Item::AppId(_)
| Item::DataSource(_)
| Item::StoreId(_)
| Item::Container(_)
| Item::View(_) => false,
Item::DataResult(..) | Item::InstancePath(_) | Item::ComponentPath(_) => true,
}
}

fn label(&self, ctx: &ContextMenuContext<'_>) -> String {
if ctx.selection.len() == 1 {
"Copy entity path".to_owned()
} else {
"Copy entity paths".to_owned()
}
}

fn process_selection(&self, ctx: &ContextMenuContext<'_>) {
let clipboard_text = ctx
.selection
.iter()
.filter_map(|(item, _)| match item {
Item::AppId(_)
| Item::DataSource(_)
| Item::StoreId(_)
| Item::Container(_)
| Item::View(_) => None,
Item::DataResult(_, instance_path) | Item::InstancePath(instance_path) => {
Some(instance_path.entity_path.clone())
}
Item::ComponentPath(component_path) => Some(component_path.entity_path.clone()),
})
.map(|entity_path| entity_path.to_string())
.join("\n");

re_log::info!("Copied entity paths to clipboard:\n{}", &clipboard_text);
ctx.viewer_context.egui_ctx().copy_text(clipboard_text);
}
}
18 changes: 10 additions & 8 deletions crates/viewer/re_context_menu/src/actions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
pub(super) mod add_container;
pub(super) mod add_entities_to_new_view;
pub(super) mod add_view;
pub(super) mod clone_view;
pub(super) mod collapse_expand_all;
pub(super) mod move_contents_to_new_container;
pub(super) mod remove;
pub(super) mod show_hide;
pub mod add_container;
pub mod add_entities_to_new_view;
pub mod add_view;
pub mod clone_view;
pub mod collapse_expand_all;
pub mod move_contents_to_new_container;
pub mod remove;
pub mod show_hide;

mod copy_entity_path;
mod screenshot_action;

pub use copy_entity_path::CopyEntityPathToClipboard;
pub use screenshot_action::ScreenshotAction;
2 changes: 2 additions & 0 deletions crates/viewer/re_context_menu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use actions::{
move_contents_to_new_container::MoveContentsToNewContainerAction,
remove::RemoveAction,
show_hide::{HideAction, ShowAction},
CopyEntityPathToClipboard,
};
use sub_menu::SubMenu;

Expand Down Expand Up @@ -152,6 +153,7 @@ fn action_list(
Box::new(ShowAction),
Box::new(HideAction),
Box::new(RemoveAction),
Box::new(CopyEntityPathToClipboard),
],
vec![
Box::new(actions::ScreenshotAction::CopyScreenshot),
Expand Down

0 comments on commit 7c297e1

Please sign in to comment.