use ahash::HashMap;
use egui_tiles::{Behavior as _, EditAction};
use re_context_menu::{context_menu_ui_for_item, SelectionUpdateBehavior};
use re_log_types::{EntityPath, ResolvedEntityPathRule, RuleEffect};
use re_ui::{design_tokens, ContextExt as _, DesignTokens, Icon, UiExt as _};
use re_viewer_context::{
icon_for_container_kind, Contents, DragAndDropFeedback, DragAndDropPayload, Item,
PublishedViewInfo, SystemExecutionOutput, ViewId, ViewQuery, ViewStates, ViewerContext,
};
use re_viewport_blueprint::{
create_entity_add_info, ViewBlueprint, ViewportBlueprint, ViewportCommand,
};
use crate::system_execution::{execute_systems_for_all_views, execute_systems_for_view};
pub struct ViewportUi {
pub blueprint: ViewportBlueprint,
}
impl ViewportUi {
pub fn new(blueprint: ViewportBlueprint) -> Self {
Self { blueprint }
}
pub fn viewport_ui(
&self,
ui: &mut egui::Ui,
ctx: &ViewerContext<'_>,
view_states: &mut ViewStates,
) {
let Self { blueprint } = self;
let is_zero_sized_viewport = ui.available_size().min_elem() <= 0.0;
if is_zero_sized_viewport || !ui.is_visible() {
return;
}
let mut maximized = blueprint.maximized;
if let Some(view_id) = blueprint.maximized {
if !blueprint.views.contains_key(&view_id) {
maximized = None;
} else if let Some(tile_id) = blueprint.tree.tiles.find_pane(&view_id) {
if !blueprint.tree.tiles.is_visible(tile_id) {
maximized = None;
}
}
}
let mut tree = if let Some(view_id) = blueprint.maximized {
let mut tiles = egui_tiles::Tiles::default();
let tile_id = Contents::View(view_id).as_tile_id();
tiles.insert(tile_id, egui_tiles::Tile::Pane(view_id));
egui_tiles::Tree::new("viewport_tree", tile_id, tiles)
} else {
blueprint.tree.clone()
};
let executed_systems_per_view =
execute_systems_for_all_views(ctx, &tree, &blueprint.views, view_states);
let contents_per_tile_id = blueprint
.contents_iter()
.map(|contents| (contents.as_tile_id(), contents))
.collect();
ui.scope(|ui| {
ui.spacing_mut().item_spacing.x = DesignTokens::view_padding() as f32;
re_tracing::profile_scope!("tree.ui");
let mut egui_tiles_delegate = TilesDelegate {
view_states,
ctx,
viewport_blueprint: blueprint,
maximized: &mut maximized,
executed_systems_per_view,
contents_per_tile_id,
edited: false,
tile_dropped: false,
};
tree.ui(&mut egui_tiles_delegate, ui);
let dragged_payload = egui::DragAndDrop::payload::<DragAndDropPayload>(ui.ctx());
let dragged_payload = dragged_payload.as_ref().and_then(|payload| {
if let DragAndDropPayload::Entities { entities } = payload.as_ref() {
Some(entities)
} else {
None
}
});
for contents in blueprint.contents_iter() {
let tile_id = contents.as_tile_id();
if let Some(rect) = tree.tiles.rect(tile_id) {
let item = contents.as_item();
let mut hovered = ctx.hovered().contains_item(&item);
let selected = ctx.selection().contains_item(&item);
if hovered && ui.rect_contains_pointer(rect) {
hovered = false;
}
let should_display_drop_destination_frame = 'scope: {
if !ui.rect_contains_pointer(rect) {
break 'scope false;
}
let Some(view_blueprint) = contents
.as_view_id()
.and_then(|view_id| self.blueprint.view(&view_id))
else {
break 'scope false;
};
let Some(dragged_payload) = dragged_payload else {
break 'scope false;
};
Self::handle_drop_entities_to_view(ctx, view_blueprint, dragged_payload)
};
let stroke = if should_display_drop_destination_frame {
design_tokens().drop_target_container_stroke()
} else if hovered {
ui.ctx().hover_stroke()
} else if selected {
ui.ctx().selection_stroke()
} else {
continue;
};
if matches!(contents, Contents::View(_))
&& !should_display_drop_destination_frame
{
continue;
}
let top_layer_id =
egui::LayerId::new(ui.layer_id().order, ui.id().with("child_id"));
ui.ctx().set_sublayer(ui.layer_id(), top_layer_id); let painter = ui.painter().clone().with_layer_id(top_layer_id);
painter.rect_stroke(rect, 0.0, stroke, egui::StrokeKind::Inside);
if should_display_drop_destination_frame {
painter.rect_filled(
rect.shrink(stroke.width),
0.0,
stroke.color.gamma_multiply(0.1),
);
}
}
}
if blueprint.maximized.is_none() {
let is_dragging_a_tile = tree.dragged_id(ui.ctx()).is_some();
if egui_tiles_delegate.edited || is_dragging_a_tile {
if blueprint.auto_layout() {
re_log::trace!(
"The user is manipulating the egui_tiles tree - will no longer \
auto-layout"
);
}
blueprint.set_auto_layout(false, ctx);
}
if egui_tiles_delegate.edited {
if egui_tiles_delegate.tile_dropped {
tree.simplify(&egui_tiles::SimplificationOptions {
prune_empty_tabs: true,
prune_empty_containers: false,
prune_single_child_tabs: true,
prune_single_child_containers: false,
all_panes_must_have_tabs: true,
join_nested_linear_containers: false,
});
}
self.blueprint
.deferred_commands
.lock()
.push(ViewportCommand::SetTree(tree));
}
}
});
self.blueprint.set_maximized(maximized, ctx);
}
fn handle_drop_entities_to_view(
ctx: &ViewerContext<'_>,
view_blueprint: &ViewBlueprint,
entities: &[EntityPath],
) -> bool {
let add_info = create_entity_add_info(
ctx,
ctx.recording().tree(),
view_blueprint,
ctx.lookup_query_result(view_blueprint.id),
);
let can_entity_be_added = |entity: &EntityPath| {
add_info
.get(entity)
.is_some_and(|info| info.can_add_self_or_descendant.is_compatible_and_missing())
};
let any_is_visualizable = entities.iter().any(can_entity_be_added);
ctx.drag_and_drop_manager
.set_feedback(if any_is_visualizable {
DragAndDropFeedback::Accept
} else {
DragAndDropFeedback::Reject
});
if !any_is_visualizable {
return false;
}
if ctx.egui_ctx.input(|i| i.pointer.any_released()) {
egui::DragAndDrop::clear_payload(ctx.egui_ctx);
view_blueprint
.contents
.mutate_entity_path_filter(ctx, |filter| {
for entity in entities {
if can_entity_be_added(entity) {
filter.add_rule(
RuleEffect::Include,
ResolvedEntityPathRule::including_subtree(entity),
);
}
}
});
ctx.selection_state()
.set_selection(Item::View(view_blueprint.id));
false
} else {
any_is_visualizable
}
}
pub fn on_frame_start(&self, ctx: &ViewerContext<'_>) {
re_tracing::profile_function!();
self.blueprint.spawn_heuristic_views(ctx);
}
pub fn save_to_blueprint_store(self, ctx: &ViewerContext<'_>) {
self.blueprint.save_to_blueprint_store(ctx);
}
}
struct TilesDelegate<'a, 'b> {
view_states: &'a mut ViewStates,
ctx: &'a ViewerContext<'b>,
viewport_blueprint: &'a ViewportBlueprint,
maximized: &'a mut Option<ViewId>,
executed_systems_per_view: HashMap<ViewId, (ViewQuery<'a>, SystemExecutionOutput)>,
contents_per_tile_id: HashMap<egui_tiles::TileId, Contents>,
edited: bool,
tile_dropped: bool,
}
impl<'a> egui_tiles::Behavior<ViewId> for TilesDelegate<'a, '_> {
fn pane_ui(
&mut self,
ui: &mut egui::Ui,
_tile_id: egui_tiles::TileId,
view_id: &mut ViewId,
) -> egui_tiles::UiResponse {
re_tracing::profile_function!();
let Some(view_blueprint) = self.viewport_blueprint.view(view_id) else {
return Default::default();
};
let is_zero_sized_viewport = ui.available_size().min_elem() <= 0.0;
if is_zero_sized_viewport || !ui.is_visible() {
return Default::default();
}
let Some(latest_at) = self.ctx.rec_cfg.time_ctrl.read().time_int() else {
ui.centered_and_justified(|ui| {
ui.weak("No time selected");
});
return Default::default();
};
let (query, system_output) = self.executed_systems_per_view.remove(view_id).unwrap_or_else(|| {
if cfg!(debug_assertions) {
re_log::warn_once!(
"Visualizers for view {:?} haven't been executed prior to display. This should never happen, please report a bug.",
view_blueprint.display_name_or_default()
);
}
let ctx: &'a ViewerContext<'_> = self.ctx;
let view = view_blueprint;
re_tracing::profile_scope!("late-system-execute", view.class_identifier().as_str());
let query_result = ctx.lookup_query_result(view.id);
let mut per_visualizer_data_results = re_viewer_context::PerSystemDataResults::default();
{
re_tracing::profile_scope!("per_system_data_results");
query_result.tree.visit(&mut |node| {
for system in &node.data_result.visualizers {
per_visualizer_data_results
.entry(*system)
.or_default()
.push(&node.data_result);
}
true
});
}
let class = view_blueprint.class(self.ctx.view_class_registry);
execute_systems_for_view(ctx, view, latest_at, self.view_states.get_mut_or_create(*view_id, class))
});
let class = view_blueprint.class(self.ctx.view_class_registry);
let view_state = self.view_states.get_mut_or_create(*view_id, class);
ui.scope(|ui| {
class
.ui(self.ctx, ui, view_state, &query, system_output)
.unwrap_or_else(|err| {
re_log::error!(
"Error in view UI (class: {}, display name: {}): {err}",
view_blueprint.class_identifier(),
class.display_name(),
);
});
ui.ctx().memory_mut(|mem| {
mem.caches
.cache::<re_viewer_context::ViewRectPublisher>()
.set(
*view_id,
PublishedViewInfo {
name: view_blueprint.display_name_or_default().as_ref().to_owned(),
rect: ui.max_rect(),
},
);
});
});
Default::default()
}
fn tab_title_for_pane(&mut self, view_id: &ViewId) -> egui::WidgetText {
if let Some(view) = self.viewport_blueprint.view(view_id) {
view.display_name_or_default().as_ref().into()
} else {
re_log::warn_once!("ViewId missing during egui_tiles");
self.ctx.egui_ctx.error_text("Internal error").into()
}
}
#[allow(clippy::fn_params_excessive_bools)]
fn tab_ui(
&mut self,
tiles: &mut egui_tiles::Tiles<ViewId>,
ui: &mut egui::Ui,
id: egui::Id,
tile_id: egui_tiles::TileId,
tab_state: &egui_tiles::TabState,
) -> egui::Response {
let tab_widget = TabWidget::new(self, ui, tiles, tile_id, tab_state, 1.0);
let response = ui
.interact(tab_widget.rect, id, egui::Sense::click_and_drag())
.on_hover_cursor(egui::CursorIcon::Grab);
if ui.is_rect_visible(tab_widget.rect) && !tab_state.is_being_dragged {
tab_widget.paint(ui);
}
let item = tiles.get(tile_id).and_then(|tile| match tile {
egui_tiles::Tile::Pane(view_id) => Some(Item::View(*view_id)),
egui_tiles::Tile::Container(_) => {
if let Some(Contents::Container(container_id)) =
self.contents_per_tile_id.get(&tile_id)
{
Some(Item::Container(*container_id))
} else {
None
}
}
});
if let Some(item) = item {
context_menu_ui_for_item(
self.ctx,
self.viewport_blueprint,
&item,
&response,
SelectionUpdateBehavior::OverrideSelection,
);
self.ctx
.handle_select_hover_drag_interactions(&response, item, false);
}
response
}
fn drag_ui(
&mut self,
tiles: &egui_tiles::Tiles<ViewId>,
ui: &mut egui::Ui,
tile_id: egui_tiles::TileId,
) {
let tab_widget = TabWidget::new(
self,
ui,
tiles,
tile_id,
&egui_tiles::TabState {
active: true,
is_being_dragged: true,
..Default::default()
},
0.5,
);
let frame = egui::Frame::NONE;
frame.show(ui, |ui| {
tab_widget.paint(ui);
});
}
fn retain_pane(&mut self, view_id: &ViewId) -> bool {
self.viewport_blueprint.views.contains_key(view_id)
}
fn top_bar_right_ui(
&mut self,
tiles: &egui_tiles::Tiles<ViewId>,
ui: &mut egui::Ui,
_tile_id: egui_tiles::TileId,
tabs: &egui_tiles::Tabs,
_scroll_offset: &mut f32,
) {
let Some(active) = tabs.active.and_then(|active| tiles.get(active)) else {
return;
};
let egui_tiles::Tile::Pane(view_id) = active else {
return;
};
let view_id = *view_id;
let Some(view_blueprint) = self.viewport_blueprint.view(&view_id) else {
return;
};
let num_views = tiles.tiles().filter(|tile| tile.is_pane()).count();
ui.add_space(8.0); if *self.maximized == Some(view_id) {
if ui
.small_icon_button(&re_ui::icons::MINIMIZE)
.on_hover_text("Restore - show all spaces")
.clicked()
{
*self.maximized = None;
}
} else if num_views > 1 {
if ui
.small_icon_button(&re_ui::icons::MAXIMIZE)
.on_hover_text("Maximize view")
.clicked()
{
*self.maximized = Some(view_id);
}
}
let view_class = view_blueprint.class(self.ctx.view_class_registry);
let view_state = self.view_states.get_mut_or_create(view_id, view_class);
view_class
.extra_title_bar_ui(
self.ctx,
ui,
view_state,
&view_blueprint.space_origin,
view_id,
)
.unwrap_or_else(|err| {
re_log::error!(
"Error in view title bar UI (class: {}, display name: {}): {err}",
view_blueprint.class_identifier(),
view_class.display_name(),
);
});
ui.help_hover_button().on_hover_ui(|ui| {
view_class.help(ui.ctx()).ui(ui);
});
}
fn tab_bar_color(&self, _visuals: &egui::Visuals) -> egui::Color32 {
re_ui::design_tokens().tab_bar_color
}
fn dragged_overlay_color(&self, visuals: &egui::Visuals) -> egui::Color32 {
visuals.panel_fill.gamma_multiply(0.5)
}
fn drag_preview_stroke(&self, _visuals: &egui::Visuals) -> egui::Stroke {
egui::Stroke::new(1.0, egui::Color32::WHITE.gamma_multiply(0.5))
}
fn drag_preview_color(&self, _visuals: &egui::Visuals) -> egui::Color32 {
egui::Color32::WHITE.gamma_multiply(0.1)
}
fn tab_bar_height(&self, _style: &egui::Style) -> f32 {
re_ui::DesignTokens::title_bar_height()
}
fn simplification_options(&self) -> egui_tiles::SimplificationOptions {
re_viewport_blueprint::tree_simplification_options()
}
fn on_edit(&mut self, edit_action: egui_tiles::EditAction) {
re_log::trace!("Tree edit: {edit_action:?}");
match edit_action {
EditAction::TileDropped => {
self.tile_dropped = true;
self.edited = true;
}
EditAction::TabSelected | EditAction::TileResized => {
self.edited = true;
}
EditAction::TileDragged => {
}
}
}
}
struct TabWidget {
galley: std::sync::Arc<egui::Galley>,
rect: egui::Rect,
galley_rect: egui::Rect,
icon: &'static Icon,
icon_size: egui::Vec2,
icon_rect: egui::Rect,
bg_color: egui::Color32,
text_color: egui::Color32,
unnamed_style: bool,
}
impl TabWidget {
fn new<'a>(
tab_viewer: &'a mut TilesDelegate<'_, '_>,
ui: &'a mut egui::Ui,
tiles: &'a egui_tiles::Tiles<ViewId>,
tile_id: egui_tiles::TileId,
tab_state: &egui_tiles::TabState,
gamma: f32,
) -> Self {
struct TabDesc {
label: egui::WidgetText,
user_named: bool,
icon: &'static re_ui::Icon,
item: Option<Item>,
}
let tab_desc = match tiles.get(tile_id) {
Some(egui_tiles::Tile::Pane(view_id)) => {
if let Some(view) = tab_viewer.viewport_blueprint.view(view_id) {
TabDesc {
label: tab_viewer.tab_title_for_pane(view_id),
user_named: view.display_name.is_some(),
icon: view.class(tab_viewer.ctx.view_class_registry).icon(),
item: Some(Item::View(*view_id)),
}
} else {
re_log::warn_once!("View {view_id} not found");
TabDesc {
label: tab_viewer.ctx.egui_ctx.error_text("Unknown view").into(),
icon: &re_ui::icons::VIEW_GENERIC,
user_named: false,
item: None,
}
}
}
Some(egui_tiles::Tile::Container(container)) => {
if let Some(Contents::Container(container_id)) =
tab_viewer.contents_per_tile_id.get(&tile_id)
{
let (label, user_named) = if let Some(container_blueprint) =
tab_viewer.viewport_blueprint.container(container_id)
{
(
container_blueprint
.display_name_or_default()
.as_ref()
.into(),
container_blueprint.display_name.is_some(),
)
} else {
re_log::warn_once!("Container {container_id} missing during egui_tiles");
(
tab_viewer.ctx.egui_ctx.error_text("Internal error").into(),
false,
)
};
TabDesc {
label,
user_named,
icon: icon_for_container_kind(&container.kind()),
item: Some(Item::Container(*container_id)),
}
} else {
if container.kind() == egui_tiles::ContainerKind::Tabs {
if let Some(tile_id) = container.only_child() {
return Self::new(tab_viewer, ui, tiles, tile_id, tab_state, gamma);
}
}
re_log::warn_once!("Container for tile ID {tile_id:?} not found");
TabDesc {
label: tab_viewer
.ctx
.egui_ctx
.error_text("Unknown container")
.into(),
icon: &re_ui::icons::VIEW_GENERIC,
user_named: false,
item: None,
}
}
}
None => {
re_log::warn_once!("Tile {tile_id:?} not found");
TabDesc {
label: tab_viewer.ctx.egui_ctx.error_text("Internal error").into(),
icon: &re_ui::icons::VIEW_UNKNOWN,
user_named: false,
item: None,
}
}
};
let hovered = tab_desc
.item
.as_ref()
.is_some_and(|item| tab_viewer.ctx.hovered().contains_item(item));
let selected = tab_desc
.item
.as_ref()
.is_some_and(|item| tab_viewer.ctx.selection().contains_item(item));
let icon_size = DesignTokens::small_icon_size();
let icon_width_plus_padding = icon_size.x + DesignTokens::text_to_icon_padding();
let text = if !tab_desc.user_named {
tab_desc.label.italics()
} else {
tab_desc.label
};
let font_id = egui::TextStyle::Button.resolve(ui.style());
let galley = text.into_galley(ui, Some(egui::TextWrapMode::Extend), f32::INFINITY, font_id);
let x_margin = tab_viewer.tab_title_spacing(ui.visuals());
let (_, rect) = ui.allocate_space(egui::vec2(
galley.size().x + 2.0 * x_margin + icon_width_plus_padding,
DesignTokens::title_bar_height(),
));
let galley_rect = egui::Rect::from_two_pos(
rect.min + egui::vec2(icon_width_plus_padding, 0.0),
rect.max,
);
let icon_rect = egui::Rect::from_center_size(
egui::pos2(rect.left() + x_margin + icon_size.x / 2.0, rect.center().y),
icon_size,
);
let bg_color = if selected {
ui.visuals().selection.bg_fill
} else if hovered {
ui.visuals().widgets.hovered.bg_fill
} else {
tab_viewer.tab_bar_color(ui.visuals())
};
let bg_color = bg_color.gamma_multiply(gamma);
let text_color = tab_viewer
.tab_text_color(ui.visuals(), tiles, tile_id, tab_state)
.gamma_multiply(gamma);
Self {
galley,
rect,
galley_rect,
icon: tab_desc.icon,
icon_size,
icon_rect,
bg_color,
text_color,
unnamed_style: !tab_desc.user_named,
}
}
fn paint(self, ui: &egui::Ui) {
ui.painter().rect_filled(self.rect, 0.0, self.bg_color);
let icon_image = self
.icon
.as_image()
.fit_to_exact_size(self.icon_size)
.tint(self.text_color);
icon_image.paint_at(ui, self.icon_rect);
let label_color = if self.unnamed_style {
self.text_color.gamma_multiply(0.5)
} else {
self.text_color
};
ui.painter().galley(
egui::Align2::CENTER_CENTER
.align_size_within_rect(self.galley.size(), self.galley_rect)
.min,
self.galley,
label_color,
);
}
}