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

Add space view icons to various context menus #6235

Merged
merged 7 commits into from
May 7, 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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions crates/re_space_view_spatial/src/space_view_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,22 +286,24 @@ fn visual_bounds_ui(ctx: &ViewerContext<'_>, space_view_id: SpaceViewId, ui: &mu
re_space_view::edit_blueprint_component::<VisualBounds, re_types::components::Range2D, ()>(
ctx,
space_view_id,
|range2d: &mut Option<re_types::components::Range2D>| {
|range2d_opt: &mut Option<re_types::components::Range2D>| {
ctx.re_ui
.grid_left_hand_label(ui, "Visible bounds")
.on_hover_text(tooltip);
ui.vertical(|ui| {
ui.style_mut().wrap = Some(false);

if let Some(range2d) = range2d {
if let Some(range2d) = range2d_opt {
let rect = egui::Rect::from(*range2d);
let (min, max) = (rect.min, rect.max);
ui.label(format!("x [{} - {}]", format_f32(min.x), format_f32(max.x),));
ui.label(format!("y [{} - {}]", format_f32(min.y), format_f32(max.y),));
}

if ui.button("Reset visible bounds").clicked() {
*range2d = None;
if ui.button("Reset visible bounds").clicked() {
*range2d_opt = None;
}
} else {
ui.weak("Default");
}
});
ui.end_row();
Expand Down
37 changes: 20 additions & 17 deletions crates/re_space_view_spatial/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,13 @@ impl SpatialSpaceViewState {
auto_size_world_heuristic(&self.bounding_boxes.accumulated, self.scene_num_primitives);

ctx.re_ui.grid_left_hand_label(ui, "Default size");
ui.vertical(|ui| {
ui.horizontal(|ui| {

egui::Grid::new("default_sizes")
.num_columns(2)
.show(ui, |ui| {
ctx.re_ui
.grid_left_hand_label(ui, "Point radius")
.on_hover_text("Point radius used whenever not explicitly specified");
ui.push_id("points", |ui| {
size_ui(
ui,
Expand All @@ -128,22 +133,20 @@ impl SpatialSpaceViewState {
&mut self.auto_size_config.point_radius,
);
});
ui.label("Point radius")
.on_hover_text("Point radius used whenever not explicitly specified");
});
ui.horizontal(|ui| {
ui.push_id("lines", |ui| {
size_ui(
ui,
1.5,
auto_size_world,
&mut self.auto_size_config.line_radius,
);
ui.label("Line radius")
.on_hover_text("Line radius used whenever not explicitly specified");
});
ui.end_row();

ctx.re_ui
.grid_left_hand_label(ui, "Line radius")
.on_hover_text("Line radius used whenever not explicitly specified");
size_ui(
ui,
1.5,
auto_size_world,
&mut self.auto_size_config.line_radius,
);
ui.end_row();
});
});

ui.end_row();
}

Expand Down
32 changes: 32 additions & 0 deletions crates/re_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,3 +1227,35 @@ pub fn markdown_ui(ui: &mut egui::Ui, id: egui::Id, markdown: &str) {

egui_commonmark::CommonMarkViewer::new(id).show(ui, &mut commonmark_cache.lock(), markdown);
}

/// A drop-down menu with a list of options.
///
/// Designed for use with [`list_item2`] content.
///
/// Use this instead of using [`egui::ComboBox`] directly.
pub fn drop_down_menu(
ui: &mut egui::Ui,
id_source: impl std::hash::Hash,
min_width: f32,
selected_text: String,
content: impl FnOnce(&mut egui::Ui),
) {
// TODO(emilk): make the button itself a `ListItem2`
egui::ComboBox::from_id_source(id_source)
.selected_text(selected_text)
.show_ui(ui, |ui| {
ui.set_min_width(min_width);

let background_x_range = ui
.spacing()
.menu_margin
.expand_rect(ui.max_rect())
.x_range();

list_item2::list_item_scope(ui, "inner_scope", |ui| {
full_span::full_span_scope(ui, background_x_range, |ui| {
content(ui);
});
});
});
}
1 change: 1 addition & 0 deletions crates/re_viewer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ rfd.workspace = true
ron.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
static_assertions.workspace = true
thiserror.workspace = true
time = { workspace = true, features = ["formatting"] }
web-time.workspace = true
Expand Down
87 changes: 46 additions & 41 deletions crates/re_viewer/src/ui/selection_panel.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use egui::{NumExt as _, Ui};
use egui_tiles::ContainerKind;

use re_data_ui::{
image_meaning_for_entity, item_ui,
Expand Down Expand Up @@ -211,11 +212,7 @@ fn container_children(
ui.strong("Contents");

ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if ctx
.re_ui
.small_icon_button(ui, &re_ui::icons::ADD)
.clicked()
{
if ctx.re_ui.small_icon_button(ui, &icons::ADD).clicked() {
viewport.show_add_space_view_or_container_modal(*container_id);
}
});
Expand Down Expand Up @@ -375,9 +372,9 @@ fn what_is_selected_ui(
ui,
component_name.short_name(),
Some(if is_static {
&re_ui::icons::COMPONENT_STATIC
&icons::COMPONENT_STATIC
} else {
&re_ui::icons::COMPONENT
&icons::COMPONENT
}),
&format!(
"Component {} of entity '{}'",
Expand Down Expand Up @@ -635,41 +632,15 @@ fn container_top_level_properties(
ui.label("Kind");

let mut container_kind = container.container_kind;
egui::ComboBox::from_id_source("container_kind")
.selected_text(format!("{container_kind:?}"))
.show_ui(ui, |ui| {
ui.style_mut().wrap = Some(false);
ui.set_min_width(64.0);

ui.selectable_value(
&mut container_kind,
egui_tiles::ContainerKind::Tabs,
format!("{:?}", egui_tiles::ContainerKind::Tabs),
);
ui.selectable_value(
&mut container_kind,
egui_tiles::ContainerKind::Horizontal,
format!("{:?}", egui_tiles::ContainerKind::Horizontal),
);
ui.selectable_value(
&mut container_kind,
egui_tiles::ContainerKind::Vertical,
format!("{:?}", egui_tiles::ContainerKind::Vertical),
);
ui.selectable_value(
&mut container_kind,
egui_tiles::ContainerKind::Grid,
format!("{:?}", egui_tiles::ContainerKind::Grid),
);
});
container_kind_selection_ui(ctx, ui, &mut container_kind);

viewport
.blueprint
.set_container_kind(*container_id, container_kind);

ui.end_row();

if container.container_kind == egui_tiles::ContainerKind::Grid {
if container.container_kind == ContainerKind::Grid {
ui.label("Columns");

fn columns_to_string(columns: &Option<u32>) -> String {
Expand Down Expand Up @@ -735,10 +706,10 @@ fn container_top_level_properties(

if container.contents.len() > 1
&& match container.container_kind {
egui_tiles::ContainerKind::Tabs => false,
egui_tiles::ContainerKind::Horizontal
| egui_tiles::ContainerKind::Vertical
| egui_tiles::ContainerKind::Grid => true,
ContainerKind::Tabs => false,
ContainerKind::Horizontal | ContainerKind::Vertical | ContainerKind::Grid => {
true
}
}
&& ui
.add_enabled(
Expand All @@ -754,6 +725,40 @@ fn container_top_level_properties(
});
}

fn container_kind_selection_ui(
ctx: &ViewerContext<'_>,
ui: &mut Ui,
in_out_kind: &mut ContainerKind,
) {
let min_width = 90.0;
let selected_text = format!("{in_out_kind:?}");

re_ui::drop_down_menu(ui, "container_kind", min_width, selected_text, |ui| {
ui.style_mut().wrap = Some(false);

static_assertions::const_assert_eq!(ContainerKind::ALL.len(), 4);
for (kind, icon) in [
(ContainerKind::Tabs, &icons::CONTAINER_TABS),
(ContainerKind::Grid, &icons::CONTAINER_GRID),
(ContainerKind::Horizontal, &icons::CONTAINER_HORIZONTAL),
(ContainerKind::Vertical, &icons::CONTAINER_VERTICAL),
] {
let response = ctx
.re_ui
.list_item2()
.selected(*in_out_kind == kind)
.show_flat(
ui,
re_ui::list_item2::LabelContent::new(format!("{kind:?}")).with_icon(icon),
);

if response.clicked() {
*in_out_kind = kind;
}
}
});
}

// TODO(#4560): this code should be generic and part of re_data_ui
/// Show a list item for a single container child.
///
Expand All @@ -780,7 +785,7 @@ fn show_list_item_for_container_child(
.with_icon(space_view.class(ctx.space_view_class_registry).icon())
.with_buttons(|re_ui, ui| {
let response = re_ui
.small_icon_button(ui, &re_ui::icons::REMOVE)
.small_icon_button(ui, &icons::REMOVE)
.on_hover_text("Remove this space view");

if response.clicked() {
Expand All @@ -806,7 +811,7 @@ fn show_list_item_for_container_child(
.with_icon(icon_for_container_kind(&container.container_kind))
.with_buttons(|re_ui, ui| {
let response = re_ui
.small_icon_button(ui, &re_ui::icons::REMOVE)
.small_icon_button(ui, &icons::REMOVE)
.on_hover_text("Remove this container");

if response.clicked() {
Expand Down
1 change: 1 addition & 0 deletions crates/re_viewport/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ nohash-hasher.workspace = true
once_cell.workspace = true
rayon.workspace = true
smallvec.workspace = true
static_assertions.workspace = true
22 changes: 18 additions & 4 deletions crates/re_viewport/src/context_menu/actions/add_container.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
use egui_tiles::ContainerKind;

use re_ui::icons;
use re_viewer_context::{ContainerId, Item};

use crate::context_menu::{ContextMenuAction, ContextMenuContext};

/// Add a container of a specific type
pub(crate) struct AddContainerAction(pub egui_tiles::ContainerKind);
pub(crate) struct AddContainerAction(pub ContainerKind);

impl ContextMenuAction for AddContainerAction {
fn supports_selection(&self, ctx: &ContextMenuContext<'_>) -> bool {
if let Some(Item::Container(container_id)) = ctx.selection.single_item() {
if let Some(container) = ctx.viewport_blueprint.container(container_id) {
let is_linear = matches!(
container.container_kind,
ContainerKind::Horizontal | ContainerKind::Vertical
);
// same-kind linear containers cannot be nested
(container.container_kind != egui_tiles::ContainerKind::Vertical
&& container.container_kind != egui_tiles::ContainerKind::Horizontal)
|| container.container_kind != self.0
!is_linear || container.container_kind != self.0
} else {
// unknown container
false
Expand All @@ -22,6 +27,15 @@ impl ContextMenuAction for AddContainerAction {
}
}

fn icon(&self) -> Option<&'static re_ui::Icon> {
match self.0 {
ContainerKind::Tabs => Some(&icons::CONTAINER_TABS),
ContainerKind::Horizontal => Some(&icons::CONTAINER_HORIZONTAL),
ContainerKind::Vertical => Some(&icons::CONTAINER_VERTICAL),
ContainerKind::Grid => Some(&icons::CONTAINER_GRID),
}
}

fn label(&self, _ctx: &ContextMenuContext<'_>) -> String {
format!("{:?}", self.0)
}
Expand Down
14 changes: 11 additions & 3 deletions crates/re_viewport/src/context_menu/actions/add_space_view.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
use re_space_view::SpaceViewBlueprint;
use re_types::SpaceViewClassIdentifier;
use re_ui::Icon;
use re_viewer_context::{ContainerId, Item, RecommendedSpaceView};

use crate::context_menu::{ContextMenuAction, ContextMenuContext};

/// Add a space view of the specific class
pub(crate) struct AddSpaceViewAction(pub SpaceViewClassIdentifier);
pub(crate) struct AddSpaceViewAction {
pub icon: &'static Icon,
pub id: SpaceViewClassIdentifier,
}

impl ContextMenuAction for AddSpaceViewAction {
fn supports_item(&self, _ctx: &ContextMenuContext<'_>, item: &Item) -> bool {
matches!(item, Item::Container(_))
}

fn icon(&self) -> Option<&'static re_ui::Icon> {
Some(self.icon)
}

fn label(&self, ctx: &ContextMenuContext<'_>) -> String {
ctx.viewer_context
.space_view_class_registry
.get_class_or_log_error(&self.0)
.get_class_or_log_error(&self.id)
.display_name()
.to_owned()
}

fn process_container(&self, ctx: &ContextMenuContext<'_>, container_id: &ContainerId) {
let space_view = SpaceViewBlueprint::new(self.0, RecommendedSpaceView::root());
let space_view = SpaceViewBlueprint::new(self.id, RecommendedSpaceView::root());

ctx.viewport_blueprint.add_space_views(
std::iter::once(space_view),
Expand Down
Loading
Loading