Skip to content

Commit

Permalink
Generic view property building, applied to TimeSeriesView's `PlotLe…
Browse files Browse the repository at this point in the history
…gend` (#6400)

### What

* Part of #6237 

What's happening:
* edit uis for:
   * corner2d
   * visible
* introduce generic method for drawing ui for a view property

Next steps in this area:
* improve edit ui overall
    * make it easier to implement (with less boilerplate)
* make default providing easier & more powerful (as described in passing
there)
* do this for more/all properties

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using examples from latest `main` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/6400?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/6400?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG
* [x] If applicable, add a new check to the [release
checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)!

- [PR Build Summary](https://build.rerun.io/pr/6400)
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)

To run all checks from `main`, comment on the PR with `@rerun-bot
full-check`.
  • Loading branch information
Wumpf authored May 22, 2024
1 parent 5befb57 commit c49f480
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 104 deletions.
72 changes: 72 additions & 0 deletions crates/re_data_ui/src/editors/corner2d.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use re_data_store::LatestAtQuery;
use re_entity_db::{external::re_query::LatestAtComponentResults, EntityDb};
use re_log_types::{EntityPath, Instance};
use re_types::blueprint::components::Corner2D;
use re_viewer_context::{UiLayout, ViewerContext};

#[allow(clippy::too_many_arguments)]
pub fn edit_corner2d(
ctx: &ViewerContext<'_>,
ui: &mut egui::Ui,
_ui_layout: UiLayout,
query: &LatestAtQuery,
db: &EntityDb,
entity_path: &EntityPath,
override_path: &EntityPath,
component: &LatestAtComponentResults,
instance: &Instance,
) {
let corner = component
// TODO(#5607): what should happen if the promise is still pending?
.instance::<Corner2D>(db.resolver(), instance.get() as _)
.unwrap_or_else(|| default_corner2d(ctx, query, db, entity_path));
let mut edit_corner = corner;

egui::ComboBox::from_id_source("corner2d")
.selected_text(format!("{corner}"))
.show_ui(ui, |ui| {
ui.style_mut().wrap = Some(false);
ui.set_min_width(64.0);

ui.selectable_value(
&mut edit_corner,
egui_plot::Corner::LeftTop.into(),
format!("{}", Corner2D::from(egui_plot::Corner::LeftTop)),
);
ui.selectable_value(
&mut edit_corner,
egui_plot::Corner::RightTop.into(),
format!("{}", Corner2D::from(egui_plot::Corner::RightTop)),
);
ui.selectable_value(
&mut edit_corner,
egui_plot::Corner::LeftBottom.into(),
format!("{}", Corner2D::from(egui_plot::Corner::LeftBottom)),
);
ui.selectable_value(
&mut edit_corner,
egui_plot::Corner::RightBottom.into(),
format!("{}", Corner2D::from(egui_plot::Corner::RightBottom)),
);
});

if corner != edit_corner {
ctx.save_blueprint_component(override_path, &edit_corner);
}
}

#[inline]
pub fn default_corner2d(
_ctx: &ViewerContext<'_>,
_query: &LatestAtQuery,
_db: &EntityDb,
_entity_path: &EntityPath,
) -> Corner2D {
// TODO(#4194): Want to distinguish the space view this happens in.
// TimeSeriesView: RightBottom
// BarChart: RightTop
// Need to make handling of editors a bit more powerful for this.
// Rough idea right now is to have "default providers" which can be either a View or a Visualizer.
// They then get queried with a ComponentName and return LatestAtComponentResults (or similar).
Corner2D::RightBottom
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// TODO(jleibs): Turn this into a trait
// TODO(jleibs): Turn these methods into a trait.

mod corner2d;
mod visible;

use egui::NumExt as _;
use re_data_store::LatestAtQuery;
use re_entity_db::{external::re_query::LatestAtComponentResults, EntityDb};
use re_log_types::{EntityPath, Instance};
use re_types::{
blueprint::components::{Corner2D, Visible},
components::{
Color, MarkerShape, MarkerSize, Name, Radius, ScalarScattering, StrokeWidth, Text,
},
Expand Down Expand Up @@ -462,11 +466,17 @@ fn register_editor<'a, C>(

pub fn register_editors(registry: &mut re_viewer_context::ComponentUiRegistry) {
register_editor::<Color>(registry, default_color, edit_color_ui);
register_editor::<Corner2D>(
registry,
corner2d::default_corner2d,
corner2d::edit_corner2d,
);
register_editor::<MarkerShape>(registry, default_marker_shape, edit_marker_shape_ui);
register_editor::<MarkerSize>(registry, default_marker_size, edit_marker_size_ui);
register_editor::<Name>(registry, default_name, edit_name_ui);
register_editor::<Radius>(registry, default_radius, edit_radius_ui);
register_editor::<ScalarScattering>(registry, default_scatter, edit_scatter_ui);
register_editor::<StrokeWidth>(registry, default_stroke_width, edit_stroke_width_ui);
register_editor::<Text>(registry, default_text, edit_text_ui);
register_editor::<Visible>(registry, visible::default_visible, visible::edit_visible);
}
44 changes: 44 additions & 0 deletions crates/re_data_ui/src/editors/visible.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use re_data_store::LatestAtQuery;
use re_entity_db::{external::re_query::LatestAtComponentResults, EntityDb};
use re_log_types::{EntityPath, Instance};
use re_types::blueprint::components::Visible;
use re_viewer_context::{UiLayout, ViewerContext};

#[allow(clippy::too_many_arguments)]
pub fn edit_visible(
ctx: &ViewerContext<'_>,
ui: &mut egui::Ui,
_ui_layout: UiLayout,
query: &LatestAtQuery,
db: &EntityDb,
entity_path: &EntityPath,
override_path: &EntityPath,
component: &LatestAtComponentResults,
instance: &Instance,
) {
let visible = component
// TODO(#5607): what should happen if the promise is still pending?
.instance::<Visible>(db.resolver(), instance.get() as _)
.unwrap_or_else(|| default_visible(ctx, query, db, entity_path));
let mut edit_visible = visible;

ui.scope(|ui| {
ui.visuals_mut().widgets.hovered.expansion = 0.0;
ui.visuals_mut().widgets.active.expansion = 0.0;
ui.add(re_ui::toggle_switch(15.0, &mut edit_visible.0));
});

if edit_visible != visible {
ctx.save_blueprint_component(override_path, &edit_visible);
}
}

#[inline]
pub fn default_visible(
_ctx: &ViewerContext<'_>,
_query: &LatestAtQuery,
_db: &EntityDb,
_entity_path: &EntityPath,
) -> Visible {
Visible(true)
}
16 changes: 16 additions & 0 deletions crates/re_entity_db/src/entity_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,22 @@ impl EntityDb {
}
}

/// Queries for the given `component_names` using latest-at semantics.
///
/// See [`re_query::LatestAtResults`] for more information about how to handle the results.
///
/// This is a cached API -- data will be lazily cached upon access.
#[inline]
pub fn latest_at(
&self,
query: &re_data_store::LatestAtQuery,
entity_path: &EntityPath,
component_names: impl IntoIterator<Item = re_types_core::ComponentName>,
) -> re_query::LatestAtResults {
self.query_caches()
.latest_at(self.store(), query, entity_path, component_names)
}

/// Get the latest index and value for a given dense [`re_types_core::Component`].
///
/// This assumes that the row we get from the store contains at most one instance for this
Expand Down
6 changes: 3 additions & 3 deletions crates/re_space_view/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ mod heuristics;
mod screenshot;
mod space_view;
mod space_view_contents;
mod sub_archetypes; // TODO(andreas): better name before `sub_archetype` sticks around?
mod view_properties; // TODO(andreas): better name before `sub_archetype` sticks around?
mod visualizable;

pub use data_query::{DataQuery, EntityOverrideContext, PropertyResolver};
pub use heuristics::suggest_space_view_for_each_entity;
pub use screenshot::ScreenshotMode;
pub use space_view::SpaceViewBlueprint;
pub use space_view_contents::SpaceViewContents;
pub use sub_archetypes::{
pub use view_properties::{
edit_blueprint_component, entity_path_for_view_property, get_blueprint_component,
query_view_property, query_view_property_or_default, view_property,
query_view_property, query_view_property_or_default, view_property, view_property_ui,
};
pub use visualizable::determine_visualizable_entities;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ use re_entity_db::{
};
use re_log_types::EntityPath;
use re_types::Archetype;
use re_viewer_context::{external::re_entity_db::EntityTree, SpaceViewId, ViewerContext};
use re_viewer_context::{
external::{
re_entity_db::EntityTree,
re_ui::{self, list_item},
},
SpaceViewId, ViewerContext,
};

pub fn entity_path_for_view_property<T: Archetype>(
space_view_id: SpaceViewId,
Expand Down Expand Up @@ -122,3 +128,64 @@ pub fn edit_blueprint_component<A: re_types::Archetype, C: re_types::Component +

ret
}

/// Display the UI for editing all components of a blueprint archetype.
///
/// Note that this will show default values for components that are null.
pub fn view_property_ui<A: Archetype>(
ctx: &ViewerContext<'_>,
space_view_id: SpaceViewId,
ui: &mut egui::Ui,
) {
let blueprint_db = ctx.store_context.blueprint;
let blueprint_query = ctx.blueprint_query;
let blueprint_path = entity_path_for_view_property::<A>(space_view_id, blueprint_db.tree());

let component_names = A::all_components();
let component_results = blueprint_db.latest_at(
blueprint_query,
&blueprint_path,
component_names.iter().copied(),
);

let sub_prop_ui = |re_ui: &re_ui::ReUi, ui: &mut egui::Ui| {
for component_name in component_names.as_ref() {
if component_name.is_indicator_component() {
continue;
}

list_item::ListItem::new(re_ui)
.interactive(false)
.show_flat(
ui,
// TODO(andreas): Note that we loose the archetype's field name here, instead we label the item with the component name.
list_item::PropertyContent::new(component_name.short_name()).value_fn(
|_, ui, _| {
ctx.component_ui_registry.edit_ui(
ctx,
ui,
re_viewer_context::UiLayout::List,
blueprint_query,
blueprint_db,
&blueprint_path,
&blueprint_path,
component_results.get_or_empty(*component_name),
component_name,
&0.into(),
);
},
),
);
}
};

list_item::ListItem::new(ctx.re_ui)
.interactive(false)
.show_hierarchical_with_children(
ui,
A::name().full_name(),
true,
list_item::LabelContent::new(A::name().short_name()),
sub_prop_ui,
);
}
95 changes: 4 additions & 91 deletions crates/re_space_view_time_series/src/space_view_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ use egui_plot::{Legend, Line, Plot, PlotPoint, Points};
use re_data_store::TimeType;
use re_format::next_grid_tick_magnitude_ns;
use re_log_types::{EntityPath, TimeInt, TimeZone};
use re_space_view::{controls, query_view_property_or_default};
use re_types::{
blueprint::components::Corner2D, components::Range1D, datatypes::TimeRange,
SpaceViewClassIdentifier, View,
};
use re_space_view::{controls, query_view_property_or_default, view_property_ui};
use re_types::blueprint::archetypes::PlotLegend;
use re_types::{components::Range1D, datatypes::TimeRange, SpaceViewClassIdentifier, View};
use re_ui::list_item;
use re_viewer_context::external::re_entity_db::{
EditableAutoValue, EntityProperties, TimeSeriesAggregator,
Expand Down Expand Up @@ -77,8 +75,6 @@ pub struct TimeSeriesSpaceView;

type ViewType = re_types::blueprint::views::TimeSeriesView;

const DEFAULT_LEGEND_CORNER: egui_plot::Corner = egui_plot::Corner::RightBottom;

impl SpaceViewClass for TimeSeriesSpaceView {
fn identifier() -> SpaceViewClassIdentifier {
ViewType::identifier()
Expand Down Expand Up @@ -192,7 +188,7 @@ impl SpaceViewClass for TimeSeriesSpaceView {
(and readability) in such situations as it prevents overdraw.",
);

legend_ui(ctx, space_view_id, ui);
view_property_ui::<PlotLegend>(ctx, space_view_id, ui);
axis_ui(ctx, space_view_id, ui, state);
});

Expand Down Expand Up @@ -618,89 +614,6 @@ impl SpaceViewClass for TimeSeriesSpaceView {
}
}

fn legend_ui(ctx: &ViewerContext<'_>, space_view_id: SpaceViewId, ui: &mut egui::Ui) {
let blueprint_db = ctx.store_context.blueprint;
let blueprint_query = ctx.blueprint_query;
let (re_types::blueprint::archetypes::PlotLegend { visible, corner }, blueprint_path) =
query_view_property_or_default(space_view_id, blueprint_db, blueprint_query);

let visible = visible.unwrap_or(true.into());
let corner = corner.unwrap_or(DEFAULT_LEGEND_CORNER.into());

let sub_prop_ui = |re_ui: &re_ui::ReUi, ui: &mut egui::Ui| {
// TODO(ab): components should provide the `value_fn` closure or the entire
// `PropertyContent` object

//
// Visible
//

let mut edit_visibility = visible;
list_item::ListItem::new(re_ui)
.interactive(false)
.show_flat(
ui,
list_item::PropertyContent::new("Visible").value_bool_mut(&mut edit_visibility.0),
);
if visible != edit_visibility {
ctx.save_blueprint_component(&blueprint_path, &edit_visibility);
}

//
// Corner
//

let mut edit_corner = corner;
list_item::ListItem::new(re_ui)
.interactive(false)
.show_flat(
ui,
list_item::PropertyContent::new("Corner").value_fn(|_, ui, _| {
egui::ComboBox::from_id_source("legend_corner")
.selected_text(format!("{corner}"))
.show_ui(ui, |ui| {
ui.style_mut().wrap = Some(false);
ui.set_min_width(64.0);

ui.selectable_value(
&mut edit_corner,
egui_plot::Corner::LeftTop.into(),
format!("{}", Corner2D::from(egui_plot::Corner::LeftTop)),
);
ui.selectable_value(
&mut edit_corner,
egui_plot::Corner::RightTop.into(),
format!("{}", Corner2D::from(egui_plot::Corner::RightTop)),
);
ui.selectable_value(
&mut edit_corner,
egui_plot::Corner::LeftBottom.into(),
format!("{}", Corner2D::from(egui_plot::Corner::LeftBottom)),
);
ui.selectable_value(
&mut edit_corner,
egui_plot::Corner::RightBottom.into(),
format!("{}", Corner2D::from(egui_plot::Corner::RightBottom)),
);
});
}),
);
if corner != edit_corner {
ctx.save_blueprint_component(&blueprint_path, &edit_corner);
}
};

list_item::ListItem::new(ctx.re_ui)
.interactive(false)
.show_hierarchical_with_children(
ui,
"time_series_selection_ui_legend",
true,
list_item::LabelContent::new("Legend"),
sub_prop_ui,
);
}

fn axis_ui(
ctx: &ViewerContext<'_>,
space_view_id: SpaceViewId,
Expand Down
Loading

0 comments on commit c49f480

Please sign in to comment.