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

Context menu 4: add "Clone space view" action #5265

Merged
merged 8 commits into from
Feb 26, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn modal_ui(
let subtitle = format!("Create a new Space View to display {title} content.");

if row_ui(ui, icon, title, &subtitle).clicked() {
viewport.add_space_views(std::iter::once(space_view), ctx, target_container);
viewport.add_space_views(std::iter::once(space_view), ctx, target_container, None);
viewport.mark_user_interaction(ctx);
*keep_open = false;
}
Expand Down
52 changes: 51 additions & 1 deletion crates/re_viewport/src/context_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use itertools::Itertools;

use re_log_types::{EntityPath, EntityPathFilter};
use re_space_view::{DataQueryBlueprint, SpaceViewBlueprint};
use re_viewer_context::{ContainerId, Item, Selection, SpaceViewClassIdentifier, ViewerContext};
use re_viewer_context::{
ContainerId, Item, Selection, SpaceViewClassIdentifier, SpaceViewId, ViewerContext,
};

use crate::{Contents, ViewportBlueprint};

Expand Down Expand Up @@ -72,6 +74,19 @@ fn context_menu_items_for_selection_summary(

items
}
SelectionSummary::SingleSpaceView(space_view_id) => {
// We want all the actions available for collections of contents…
let mut items = context_menu_items_for_selection_summary(
ctx,
viewport_blueprint,
item,
SelectionSummary::ContentsItems(vec![Contents::SpaceView(space_view_id)]),
);

items.push(CloneSpaceViewItem::item(space_view_id));

items
}
SelectionSummary::ContentsItems(contents) => {
// exclude the root container from the list of contents, as it cannot be shown/hidden
// nor removed
Expand Down Expand Up @@ -211,6 +226,7 @@ fn possible_child_container_kind(
#[derive(Debug, Clone)]
pub enum SelectionSummary {
SingleContainerItem(ContainerId),
SingleSpaceView(SpaceViewId),
ContentsItems(Vec<Contents>),
Heterogeneous,
Empty,
Expand All @@ -224,6 +240,8 @@ fn summarize_selection(selection: &Selection) -> SelectionSummary {
if selection.len() == 1 {
if let Some(Item::Container(container_id)) = selection.first_item() {
return SelectionSummary::SingleContainerItem(*container_id);
} else if let Some(Item::SpaceView(space_view_id)) = selection.first_item() {
return SelectionSummary::SingleSpaceView(*space_view_id);
}
}

Expand Down Expand Up @@ -373,6 +391,37 @@ impl ContextMenuItem for ContentRemove {
}
}

// ================================================================================================
// Space view items
// ================================================================================================

/// Clone a space view
struct CloneSpaceViewItem {
space_view_id: SpaceViewId,
}

impl CloneSpaceViewItem {
fn item(space_view_id: SpaceViewId) -> Box<dyn ContextMenuItem> {
Box::new(Self { space_view_id })
}
}

impl ContextMenuItem for CloneSpaceViewItem {
fn label(&self, _ctx: &ViewerContext<'_>, _viewport_blueprint: &ViewportBlueprint) -> String {
"Clone".to_owned()
}

fn run(&self, ctx: &ViewerContext<'_>, viewport_blueprint: &ViewportBlueprint) {
if let Some(new_space_view_id) =
viewport_blueprint.duplicate_space_view(&self.space_view_id, ctx)
{
ctx.selection_state()
.set_selection(Item::SpaceView(new_space_view_id));
viewport_blueprint.mark_user_interaction(ctx);
}
}
}

// ================================================================================================
// Container items
// ================================================================================================
Expand Down Expand Up @@ -445,6 +494,7 @@ impl ContextMenuItem for AddSpaceView {
std::iter::once(space_view),
ctx,
Some(self.target_container),
None,
);
viewport_blueprint.mark_user_interaction(ctx);
}
Expand Down
25 changes: 18 additions & 7 deletions crates/re_viewport/src/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ impl ViewportState {

/// Mutation actions to perform on the tree at the end of the frame. These messages are sent by the mutation APIs from
/// [`crate::ViewportBlueprint`].
#[derive(Clone)]
#[derive(Clone, Debug)]
pub enum TreeAction {
/// Add a new space view to the provided container (or the root if `None`).
AddSpaceView(SpaceViewId, Option<ContainerId>),
AddSpaceView(SpaceViewId, Option<ContainerId>, Option<usize>),

/// Add a new container of the provided kind to the provided container (or the root if `None`).
AddContainer(egui_tiles::ContainerKind, Option<ContainerId>),
Expand Down Expand Up @@ -288,7 +288,7 @@ impl<'a, 'b> Viewport<'a, 'b> {
// If so we can no longer automatically change the layout without discarding user edits.
let is_dragging_a_tile = tree.dragged_id(ui.ctx()).is_some();
if tab_viewer.edited || is_dragging_a_tile {
if blueprint.auto_layout {
if blueprint.auto_layout() {
re_log::trace!(
"The user is manipulating the egui_tiles tree - will no longer auto-layout"
);
Expand Down Expand Up @@ -331,7 +331,7 @@ impl<'a, 'b> Viewport<'a, 'b> {
space_view.on_frame_start(ctx, space_view_state.as_mut(), auto_properties);
}

if self.blueprint.auto_space_views {
if self.blueprint.auto_space_views() {
self.spawn_heuristic_space_views(ctx);
}
}
Expand Down Expand Up @@ -392,6 +392,7 @@ impl<'a, 'b> Viewport<'a, 'b> {
}),
ctx,
None,
None,
);
}
}
Expand All @@ -407,9 +408,10 @@ impl<'a, 'b> Viewport<'a, 'b> {

// TODO(#4687): Be extra careful here. If we mark edited inappropriately we can create an infinite edit loop.
for tree_action in self.tree_action_receiver.try_iter() {
re_log::trace!("Processing tree action: {tree_action:?}");
match tree_action {
TreeAction::AddSpaceView(space_view_id, parent_container) => {
if self.blueprint.auto_layout {
TreeAction::AddSpaceView(space_view_id, parent_container, position_in_parent) => {
if self.blueprint.auto_layout() {
// Re-run the auto-layout next frame:
re_log::trace!(
"Added a space view with no user edits yet - will re-run auto-layout"
Expand All @@ -420,11 +422,20 @@ impl<'a, 'b> Viewport<'a, 'b> {
parent_container.or(self.blueprint.root_container)
{
let tile_id = self.tree.tiles.insert_pane(space_view_id);
let container_tile_id = blueprint_id_to_tile_id(&parent_id);
if let Some(egui_tiles::Tile::Container(container)) =
self.tree.tiles.get_mut(blueprint_id_to_tile_id(&parent_id))
self.tree.tiles.get_mut(container_tile_id)
{
re_log::trace!("Inserting new space view into root container");
container.add_child(tile_id);
if let Some(position_in_parent) = position_in_parent {
self.tree.move_tile_to_container(
tile_id,
container_tile_id,
position_in_parent,
true,
);
}
} else {
re_log::trace!("Root was not a container - will re-run auto-layout");
reset = true;
Expand Down
58 changes: 46 additions & 12 deletions crates/re_viewport/src/viewport_blueprint.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::collections::BTreeMap;
use std::sync::atomic::{AtomicBool, Ordering};

use ahash::HashMap;
use egui_tiles::{SimplificationOptions, TileId};

use re_data_store::LatestAtQuery;
use re_entity_db::EntityPath;
use re_query::query_archetype;
Expand Down Expand Up @@ -42,10 +44,13 @@ pub struct ViewportBlueprint {
/// Whether the viewport layout is determined automatically.
///
/// Set to `false` the first time the user messes around with the viewport blueprint.
pub auto_layout: bool,
/// Note: we use a mutex here because writes needs to be effective immediately during the frame.
auto_layout: AtomicBool,

/// Whether or not space views should be created automatically.
pub auto_space_views: bool,
/// Whether space views should be created automatically.
///
/// Note: we use a mutex here because writes needs to be effective immediately during the frame.
auto_space_views: AtomicBool,

/// Channel to pass Blueprint mutation messages back to the [`crate::Viewport`]
tree_action_sender: std::sync::mpsc::Sender<TreeAction>,
Expand Down Expand Up @@ -139,8 +144,8 @@ impl ViewportBlueprint {
root_container,
tree,
maximized,
auto_layout,
auto_space_views,
auto_layout: auto_layout.into(),
auto_space_views: auto_space_views.into(),
tree_action_sender,
}

Expand Down Expand Up @@ -239,7 +244,17 @@ impl ViewportBlueprint {
let new_space_view = space_view.duplicate(ctx.store_context.blueprint, ctx.blueprint_query);
let new_space_view_id = new_space_view.id;

self.add_space_views(std::iter::once(new_space_view), ctx, None);
let parent_and_pos =
self.find_parent_and_position_index(&Contents::SpaceView(*space_view_id));

self.add_space_views(
std::iter::once(new_space_view),
ctx,
parent_and_pos.map(|(parent, _)| parent),
parent_and_pos.map(|(_, pos)| pos),
);

self.mark_user_interaction(ctx);

Some(new_space_view_id)
}
Expand Down Expand Up @@ -267,7 +282,7 @@ impl ViewportBlueprint {
}

pub fn mark_user_interaction(&self, ctx: &ViewerContext<'_>) {
if self.auto_layout {
if self.auto_layout() {
re_log::trace!("User edits - will no longer auto-layout");
}

Expand All @@ -294,6 +309,7 @@ impl ViewportBlueprint {
space_views: impl Iterator<Item = SpaceViewBlueprint>,
ctx: &ViewerContext<'_>,
parent_container: Option<ContainerId>,
position_in_parent: Option<usize>,
) -> Vec<SpaceViewId> {
let mut new_ids: Vec<_> = vec![];

Expand All @@ -309,7 +325,11 @@ impl ViewportBlueprint {

if !new_ids.is_empty() {
for id in &new_ids {
self.send_tree_action(TreeAction::AddSpaceView(*id, parent_container));
self.send_tree_action(TreeAction::AddSpaceView(
*id,
parent_container,
position_in_parent,
));
}

let updated_ids: Vec<_> = self.space_views.keys().chain(new_ids.iter()).collect();
Expand Down Expand Up @@ -556,7 +576,7 @@ impl ViewportBlueprint {
Contents::Container(container_id) => {
if let Some(container) = self.container(container_id) {
if visible != container.visible {
if self.auto_layout {
if self.auto_layout() {
re_log::trace!(
"Container visibility changed - will no longer auto-layout"
);
Expand All @@ -574,7 +594,7 @@ impl ViewportBlueprint {
Contents::SpaceView(space_view_id) => {
if let Some(space_view) = self.space_view(space_view_id) {
if visible != space_view.visible {
if self.auto_layout {
if self.auto_layout() {
re_log::trace!(
"Space-view visibility changed - will no longer auto-layout"
);
Expand Down Expand Up @@ -617,20 +637,34 @@ impl ViewportBlueprint {

#[inline]
pub fn set_auto_layout(&self, value: bool, ctx: &ViewerContext<'_>) {
if self.auto_layout != value {
let old_value = self.auto_layout.swap(value, Ordering::SeqCst);

if old_value != value {
let component = AutoLayout(value);
ctx.save_blueprint_component(&VIEWPORT_PATH.into(), component);
}
}

#[inline]
pub fn auto_layout(&self) -> bool {
self.auto_layout.load(Ordering::SeqCst)
}

#[inline]
pub fn set_auto_space_views(&self, value: bool, ctx: &ViewerContext<'_>) {
if self.auto_space_views != value {
let old_value = self.auto_space_views.swap(value, Ordering::SeqCst);

if old_value != value {
let component = AutoSpaceViews(value);
ctx.save_blueprint_component(&VIEWPORT_PATH.into(), component);
}
}

#[inline]
pub fn auto_space_views(&self) -> bool {
self.auto_space_views.load(Ordering::SeqCst)
}

#[inline]
pub fn set_maximized(&self, space_view_id: Option<SpaceViewId>, ctx: &ViewerContext<'_>) {
if self.maximized != space_view_id {
Expand Down
1 change: 1 addition & 0 deletions tests/python/release_checklist/check_context_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Hide
- Remove
- Move to new container
- Clone
* Check both work as expected.
* Right-click on the viewport and check for context menu content:
- Add Container
Expand Down
Loading