Skip to content

Commit

Permalink
generic reset buttons for view property components
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed May 23, 2024
1 parent 2c20040 commit 7c1f618
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
5 changes: 5 additions & 0 deletions crates/re_query/src/latest_at/results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ impl LatestAtResults {
self.components.contains_key(&component_name.into())
}

pub fn contains_non_empty(&self, component_name: impl Into<ComponentName>) -> bool {
self.get(component_name)
.map_or(false, |result| result.num_instances() != 0)
}

/// Returns the [`LatestAtComponentResults`] for the specified [`Component`].
#[inline]
pub fn get(
Expand Down
39 changes: 25 additions & 14 deletions crates/re_space_view/src/view_property_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,31 @@ pub fn view_property_ui<A: Archetype>(
.interactive(false)
.show_flat(
ui,
list_item::PropertyContent::new(display_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::PropertyContent::new(display_name)
.action_button_with_enabled(
&re_ui::icons::RESET,
component_results.contains_non_empty(*component_name),
|| {
ctx.save_empty_blueprint_component_name(
&blueprint_path,
*component_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(),
);
}),
);

if let Some(tooltip) = field_info.map(|info| info.documentation) {
Expand Down
27 changes: 23 additions & 4 deletions crates/re_ui/src/list_item/property_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type PropertyValueFn<'a> = dyn FnOnce(&ReUi, &mut egui::Ui, egui::style::WidgetV

struct PropertyActionButton<'a> {
icon: &'static crate::icons::Icon,
enabled: bool,
on_click: Box<dyn FnOnce() + 'a>,
}

Expand Down Expand Up @@ -93,8 +94,22 @@ impl<'a> PropertyContent<'a> {
// TODO(#6191): accept multiple calls for this function for multiple actions.
#[inline]
pub fn action_button(
self,
icon: &'static crate::icons::Icon,
on_click: impl FnOnce() + 'a,
) -> Self {
self.action_button_with_enabled(icon, true, on_click)
}

/// Right aligned action button.
///
/// Note: for aesthetics, space is always reserved for the action button.
// TODO(#6191): accept multiple calls for this function for multiple actions.
#[inline]
pub fn action_button_with_enabled(
mut self,
icon: &'static crate::icons::Icon,
enabled: bool,
on_click: impl FnOnce() + 'a,
) -> Self {
// TODO(#6191): support multiple action buttons
Expand All @@ -104,6 +119,7 @@ impl<'a> PropertyContent<'a> {
);
self.action_buttons = Some(PropertyActionButton {
icon,
enabled,
on_click: Box::new(on_click),
});
self
Expand Down Expand Up @@ -349,10 +365,13 @@ impl ListItemContent for PropertyContent<'_> {
action_button_rect,
egui::Layout::right_to_left(egui::Align::Center),
);
let button_response = re_ui.small_icon_button(&mut child_ui, action_button.icon);
if button_response.clicked() {
(action_button.on_click)();
}

child_ui.add_enabled_ui(action_button.enabled, |ui| {
let button_response = re_ui.small_icon_button(ui, action_button.icon);
if button_response.clicked() {
(action_button.on_click)();
}
});
}
}

Expand Down

0 comments on commit 7c1f618

Please sign in to comment.