re_data_ui 0.35.0

Provides ui elements for Rerun component data for the Rerun Viewer.
Documentation
use re_arrow_ui::arrow_ui;
use re_ui::UiExt as _;
use re_viewer_context::{ComponentUiRegistry, StoreViewContext};

use super::EntityDataUi;

pub fn register_component_uis(registry: &mut re_viewer_context::ComponentUiRegistry) {
    re_tracing::profile_function!();

    // TODO(#6661): Move this to component_ui_registry. Separate components could simplify this to the extent that multi/single line is enough?
    add_to_registry::<re_sdk_types::components::AnnotationContext>(registry);

    // TODO(#6661): Move this to component_ui_registry. Image preview is a bit hard because row_id and size stuff needs to be known. `ImageBuffer` needs to be handled as well.
    add_to_registry::<re_sdk_types::components::Blob>(registry);
    add_to_registry::<re_sdk_types::components::TensorData>(registry);

    // TODO(#6661): Move this to component_ui_registry. Funky AnnotationContext querying thing. Maybe we can get away with a store querying hack?
    add_to_registry::<re_sdk_types::components::ClassId>(registry);
    add_to_registry::<re_sdk_types::components::KeypointId>(registry);
}

/// Registers how to show a given component in the ui.
pub fn add_to_registry<C: EntityDataUi + re_sdk_types::Component>(
    registry: &mut ComponentUiRegistry,
) {
    registry.add_legacy_display_ui(
        C::name(),
        Box::new(
            |ctx, ui, ui_layout, entity_path, component_descriptor, row_id, component_raw| {
                match C::from_arrow(component_raw) {
                    Ok(components) => match components.len() {
                        1 => {
                            // Legacy display UIs need an active store (entity db + per-store caches) to resolve annotations,
                            // decode images, etc.
                            // TODO(andreas): Some of these only depend on it for caches.
                            if let Some(store_view_ctx) =
                                StoreViewContext::for_active_recording(ctx)
                            {
                                components[0].entity_data_ui(
                                    &store_view_ctx,
                                    ui,
                                    ui_layout,
                                    entity_path,
                                    component_descriptor,
                                    row_id,
                                );
                            } else {
                                arrow_ui(
                                    ui,
                                    ui_layout,
                                    ctx.app_options.timestamp_format,
                                    component_raw,
                                );
                            }
                        }
                        _ => arrow_ui(
                            ui,
                            ui_layout,
                            ctx.app_options.timestamp_format,
                            component_raw,
                        ),
                    },
                    Err(err) => {
                        ui.error_with_details_on_hover("(failed to deserialize)")
                            .on_hover_text(err.to_string());
                    }
                }
            },
        ),
    );
}