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

[pure refactor] Move ViewerContext & ComponentUiRegistry to viewer_context #2047

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

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

12 changes: 5 additions & 7 deletions crates/re_viewer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ use re_log_types::{ApplicationId, LogMsg, RecordingId};
use re_renderer::WgpuResourcePoolStatistics;
use re_smart_channel::Receiver;
use re_ui::{toasts, Command};
use re_viewer_context::{AppOptions, Caches, PlayState};

use crate::{
misc::{RecordingConfig, ViewerContext},
ui::{data_ui::ComponentUiRegistry, Blueprint},
viewer_analytics::ViewerAnalytics,
use re_viewer_context::{
AppOptions, Caches, ComponentUiRegistry, PlayState, RecordingConfig, ViewerContext,
};

use crate::{ui::Blueprint, viewer_analytics::ViewerAnalytics};

#[cfg(not(target_arch = "wasm32"))]
use re_log_types::TimeRangeF;

Expand Down Expand Up @@ -134,7 +132,7 @@ impl App {
ram_limit_warner: re_memory::RamLimitWarner::warn_at_fraction_of_max(0.75),
re_ui,
text_log_rx,
component_ui_registry: Default::default(),
component_ui_registry: crate::ui::data_ui::create_component_ui_registry(),
rx,
log_dbs: Default::default(),
state,
Expand Down
4 changes: 2 additions & 2 deletions crates/re_viewer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ mod remote_viewer_app;
mod ui;
mod viewer_analytics;

pub(crate) use misc::{mesh_loader, ViewerContext};
pub(crate) use misc::mesh_loader;
use re_log_types::PythonVersion;
pub(crate) use ui::{memory_panel, selection_panel, time_panel, UiVerbosity};
pub(crate) use ui::{memory_panel, selection_panel, time_panel};

pub use app::{App, StartupOptions};
pub use remote_viewer_app::RemoteViewerApp;
Expand Down
3 changes: 0 additions & 3 deletions crates/re_viewer/src/misc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ pub(crate) mod space_info;
mod space_view_highlights;
mod time_control_ui;
mod transform_cache;
mod viewer_context;

pub mod instance_hash_conversions;

#[cfg(not(target_arch = "wasm32"))]
pub(crate) use clipboard::Clipboard;

pub(crate) use viewer_context::*;

#[cfg(not(target_arch = "wasm32"))]
pub(crate) mod profiler;

Expand Down
3 changes: 1 addition & 2 deletions crates/re_viewer/src/misc/queries.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use re_arrow_store::LatestAtQuery;
use re_data_store::{query_latest_single, EntityPath};
use re_log_types::Transform;

use super::ViewerContext;
use re_viewer_context::ViewerContext;

/// Find closest entity with a pinhole transform.
pub fn closest_pinhole_transform(
Expand Down
3 changes: 2 additions & 1 deletion crates/re_viewer/src/ui/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use re_log_types::{
AnnotationContext, RowId,
};
use re_query::query_entity_with_primary;
use re_viewer_context::ViewerContext;

use crate::{misc::ViewerContext, ui::scene::SceneQuery};
use crate::ui::scene::SceneQuery;

#[derive(Clone, Debug)]
pub struct Annotations {
Expand Down
3 changes: 2 additions & 1 deletion crates/re_viewer/src/ui/blueprint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use re_viewer_context::Item;

use crate::misc::{space_info::SpaceInfoCollection, ViewerContext};
use crate::misc::space_info::SpaceInfoCollection;
use re_viewer_context::ViewerContext;

use super::viewport::Viewport;

Expand Down
8 changes: 4 additions & 4 deletions crates/re_viewer/src/ui/data_ui/annotation_context.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use egui::{color_picker, Vec2};
use itertools::Itertools;
use re_log_types::{context::AnnotationInfo, AnnotationContext};

use crate::ui::{annotations::auto_color, UiVerbosity};
use re_viewer_context::{UiVerbosity, ViewerContext};

use super::DataUi;
use crate::ui::annotations::auto_color;

const TABLE_SCROLL_AREA_HEIGHT: f32 = 500.0; // add scroll-bars when we get to this height

impl DataUi for AnnotationContext {
fn data_ui(
&self,
_ctx: &mut crate::misc::ViewerContext<'_>,
_ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
verbosity: crate::ui::UiVerbosity,
verbosity: UiVerbosity,
_query: &re_arrow_store::LatestAtQuery,
) {
match verbosity {
Expand Down
14 changes: 7 additions & 7 deletions crates/re_viewer/src/ui/data_ui/component.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use re_data_store::{ComponentName, EntityPath, InstancePath};
use re_query::ComponentWithInstances;

use crate::ui::item_ui;
use re_viewer_context::{UiVerbosity, ViewerContext};

use super::DataUi;
use crate::ui::item_ui;

// We do NOT implement `DataUi` for just `ComponentWithInstances`
// because we also want the context of what entity it is part of!
Expand All @@ -27,9 +27,9 @@ impl EntityComponentWithInstances {
impl DataUi for EntityComponentWithInstances {
fn data_ui(
&self,
ctx: &mut crate::misc::ViewerContext<'_>,
ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
verbosity: super::UiVerbosity,
verbosity: UiVerbosity,
query: &re_arrow_store::LatestAtQuery,
) {
crate::profile_function!(self.component_name().full_name());
Expand All @@ -45,8 +45,8 @@ impl DataUi for EntityComponentWithInstances {
let num_instances = self.num_instances();

let one_line = match verbosity {
crate::ui::UiVerbosity::Small => true,
crate::UiVerbosity::Reduced | crate::ui::UiVerbosity::All => false,
UiVerbosity::Small => true,
UiVerbosity::Reduced | UiVerbosity::All => false,
};

if num_instances == 0 {
Expand Down Expand Up @@ -109,7 +109,7 @@ impl DataUi for EntityComponentWithInstances {
ctx.component_ui_registry.ui(
ctx,
ui,
crate::ui::UiVerbosity::Small,
UiVerbosity::Small,
query,
&self.entity_path,
&self.component_data,
Expand Down
5 changes: 3 additions & 2 deletions crates/re_viewer/src/ui/data_ui/component_path.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use re_log_types::ComponentPath;
use re_viewer_context::{UiVerbosity, ViewerContext};

use super::DataUi;

impl DataUi for ComponentPath {
fn data_ui(
&self,
ctx: &mut crate::misc::ViewerContext<'_>,
ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
verbosity: crate::ui::UiVerbosity,
verbosity: UiVerbosity,
query: &re_arrow_store::LatestAtQuery,
) {
let store = &ctx.log_db.entity_db.data_store;
Expand Down
170 changes: 60 additions & 110 deletions crates/re_viewer/src/ui/data_ui/component_ui_registry.rs
Original file line number Diff line number Diff line change
@@ -1,79 +1,19 @@
use std::collections::BTreeMap;

use re_arrow_store::LatestAtQuery;
use re_data_store::EntityPath;
use re_log_types::{
component_types::InstanceKey, external::arrow2, Component, ComponentName,
DeserializableComponent, SizeBytes as _,
component_types::InstanceKey, external::arrow2, DeserializableComponent, EntityPath, SizeBytes,
};
use re_query::ComponentWithInstances;

use crate::{misc::ViewerContext, ui::UiVerbosity};
use re_viewer_context::{ComponentUiRegistry, UiVerbosity, ViewerContext};

use super::{DataUi, EntityDataUi};

type ComponentUiCallback = Box<
dyn Fn(
&mut ViewerContext<'_>,
&mut egui::Ui,
UiVerbosity,
&LatestAtQuery,
&EntityPath,
&ComponentWithInstances,
&InstanceKey,
),
>;

/// How to display components in a Ui
pub struct ComponentUiRegistry {
components: BTreeMap<ComponentName, ComponentUiCallback>,
}

impl Default for ComponentUiRegistry {
fn default() -> Self {
let mut registry = Self {
components: Default::default(),
};

// The things that are out-commented are components we have, but
// where the default arrow-format for them looks good enough (at least for now).
// Basically: adding custom UI:s for these out-commented components would be nice, but is not a must.
registry.add::<re_log_types::component_types::AnnotationContext>();
// registry.add::<re_log_types::component_types::Arrow3D>();
// registry.add::<re_log_types::component_types::Box3D>();
// registry.add::<re_log_types::component_types::ClassId>();
registry.add::<re_log_types::component_types::ColorRGBA>();
// registry.add::<re_log_types::component_types::InstanceKey>();
// registry.add::<re_log_types::component_types::KeypointId>();
// registry.add::<re_log_types::component_types::Label>();
registry.add::<re_log_types::component_types::LineStrip2D>();
registry.add::<re_log_types::component_types::LineStrip3D>();
registry.add::<re_log_types::component_types::Mesh3D>();
// registry.add::<re_log_types::component_types::Point2D>();
// registry.add::<re_log_types::component_types::Point3D>();
// registry.add::<re_log_types::component_types::Quaternion>();
// registry.add::<re_log_types::component_types::Radius>();
registry.add::<re_log_types::component_types::Rect2D>();
// registry.add::<re_log_types::component_types::Scalar>();
// registry.add::<re_log_types::component_types::ScalarPlotProps>();
// registry.add::<re_log_types::component_types::Size3D>();
registry.add::<re_log_types::component_types::Tensor>();
registry.add::<re_log_types::component_types::TextEntry>();
registry.add::<re_log_types::component_types::Transform>();
registry.add::<re_log_types::component_types::Vec2D>();
registry.add::<re_log_types::component_types::Vec3D>();
registry.add::<re_log_types::ViewCoordinates>();

registry
}
}

impl ComponentUiRegistry {
fn add<C: DeserializableComponent + EntityDataUi>(&mut self)
pub fn create_component_ui_registry() -> ComponentUiRegistry {
/// Registers how to show a given component in the ui.
pub fn add<C: DeserializableComponent + EntityDataUi>(registry: &mut ComponentUiRegistry)
where
for<'a> &'a C::ArrayType: IntoIterator,
{
self.components.insert(
registry.add(
C::name(),
Box::new(
|ctx, ui, verbosity, query, entity_path, component, instance| match component
Expand All @@ -93,53 +33,63 @@ impl ComponentUiRegistry {
);
}

/// Show a ui for this instance of this component.
#[allow(clippy::too_many_arguments)]
pub fn ui(
&self,
ctx: &mut crate::misc::ViewerContext<'_>,
ui: &mut egui::Ui,
verbosity: crate::ui::UiVerbosity,
query: &LatestAtQuery,
entity_path: &EntityPath,
component: &ComponentWithInstances,
instance_key: &InstanceKey,
) {
crate::profile_function!(component.name().full_name());

if component.name() == InstanceKey::name() {
// The user wants to show a ui for the `InstanceKey` component - well, that's easy:
ui.label(instance_key.to_string());
return;
}
let mut registry = ComponentUiRegistry::new(Box::new(&fallback_component_ui));

// The things that are out-commented are components we have, but
// where the default arrow-format for them looks good enough (at least for now).
// Basically: adding custom UI:s for these out-commented components would be nice, but is not a must.
add::<re_log_types::component_types::AnnotationContext>(&mut registry);
// add::<re_log_types::component_types::Arrow3D>(&mut registry);
// add::<re_log_types::component_types::Box3D>(&mut registry);
// add::<re_log_types::component_types::ClassId>(&mut registry);
add::<re_log_types::component_types::ColorRGBA>(&mut registry);
// add::<re_log_types::component_types::InstanceKey>(&mut registry);
// add::<re_log_types::component_types::KeypointId>(&mut registry);
// add::<re_log_types::component_types::Label>(&mut registry);
add::<re_log_types::component_types::LineStrip2D>(&mut registry);
add::<re_log_types::component_types::LineStrip3D>(&mut registry);
add::<re_log_types::component_types::Mesh3D>(&mut registry);
// add::<re_log_types::component_types::Point2D>(&mut registry);
// add::<re_log_types::component_types::Point3D>(&mut registry);
// add::<re_log_types::component_types::Quaternion>(&mut registry);
// add::<re_log_types::component_types::Radius>(&mut registry);
add::<re_log_types::component_types::Rect2D>(&mut registry);
// add::<re_log_types::component_types::Scalar>(&mut registry);
// add::<re_log_types::component_types::ScalarPlotProps>(&mut registry);
// add::<re_log_types::component_types::Size3D>(&mut registry);
add::<re_log_types::component_types::Tensor>(&mut registry);
add::<re_log_types::component_types::TextEntry>(&mut registry);
add::<re_log_types::component_types::Transform>(&mut registry);
add::<re_log_types::component_types::Vec2D>(&mut registry);
add::<re_log_types::component_types::Vec3D>(&mut registry);
add::<re_log_types::ViewCoordinates>(&mut registry);

registry
}

if let Some(ui_callback) = self.components.get(&component.name()) {
(*ui_callback)(
ctx,
ui,
verbosity,
query,
entity_path,
component,
instance_key,
);
fn fallback_component_ui(
_ctx: &mut ViewerContext<'_>,
ui: &mut egui::Ui,
_verbosity: UiVerbosity,
_query: &LatestAtQuery,
_entity_path: &EntityPath,
component: &ComponentWithInstances,
instance_key: &InstanceKey,
) {
// No special ui implementation - use a generic one:
if let Some(value) = component.lookup_arrow(instance_key) {
let bytes = value.total_size_bytes();
if bytes < 256 {
// For small items, print them
let mut repr = String::new();
let display = arrow2::array::get_display(value.as_ref(), "null");
display(&mut repr, 0).unwrap();
ui.label(repr);
} else {
// No special ui implementation - use a generic one:
if let Some(value) = component.lookup_arrow(instance_key) {
let bytes = value.total_size_bytes();
if bytes < 256 {
// For small items, print them
let mut repr = String::new();
let display = arrow2::array::get_display(value.as_ref(), "null");
display(&mut repr, 0).unwrap();
ui.label(repr);
} else {
ui.label(format!("{bytes} bytes"));
}
} else {
ui.weak("(null)");
}
ui.label(format!("{bytes} bytes"));
}
} else {
ui.weak("(null)");
}
}

Expand Down
Loading