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

Codegen DerefMut & Deref for all trivial components #6470

Merged
merged 6 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
18 changes: 18 additions & 0 deletions crates/re_edit_ui/src/datatype_editors/bool_toggle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// Generic editor for a boolean value.
pub fn edit_bool(
_ctx: &re_viewer_context::ViewerContext<'_>,
ui: &mut egui::Ui,
value: &mut impl std::ops::DerefMut<Target = bool>,
) -> egui::Response {
edit_bool_impl(ui, value)
}

/// Non monomorphized implementation of [`edit_bool`].
fn edit_bool_impl(ui: &mut egui::Ui, value: &mut bool) -> egui::Response {
ui.scope(move |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, value))
})
.inner
}
20 changes: 20 additions & 0 deletions crates/re_edit_ui/src/datatype_editors/float_drag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use egui::NumExt as _;

/// Generic editor for a f32 value from zero to infinity.
pub fn edit_f32_zero_to_inf(
_ctx: &re_viewer_context::ViewerContext<'_>,
ui: &mut egui::Ui,
value: &mut impl std::ops::DerefMut<Target = f32>,
) -> egui::Response {
edit_f32_zero_to_inf_impl(ui, value)
}

/// Non monomorphized implementation of [`edit_f32_zero_to_inf`].
fn edit_f32_zero_to_inf_impl(ui: &mut egui::Ui, value: &mut f32) -> egui::Response {
let speed = (*value * 0.01).at_least(0.001);
ui.add(
egui::DragValue::new(value)
.clamp_range(0.0..=f32::INFINITY)
.speed(speed),
)
}
7 changes: 7 additions & 0 deletions crates/re_edit_ui/src/datatype_editors/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod bool_toggle;
mod float_drag;
mod singleline_string;

pub use bool_toggle::edit_bool;
pub use float_drag::edit_f32_zero_to_inf;
pub use singleline_string::edit_singleline_string;
19 changes: 19 additions & 0 deletions crates/re_edit_ui/src/datatype_editors/singleline_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// Generic singleline string editor.
pub fn edit_singleline_string(
_ctx: &re_viewer_context::ViewerContext<'_>,
ui: &mut egui::Ui,
value: &mut impl std::ops::DerefMut<Target = re_types::datatypes::Utf8>,
) -> egui::Response {
edit_singleline_string_impl(ui, value)
}

/// Non monomorphized implementation of [`edit_singleline_string`].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really need to do that everywhere... or rather, the compiler should 😒

fn edit_singleline_string_impl(
ui: &mut egui::Ui,
value: &mut re_types::datatypes::Utf8,
) -> egui::Response {
let mut edit_name = value.to_string();
let response = egui::TextEdit::singleline(&mut edit_name).show(ui).response;
*value = edit_name.into();
response
}
79 changes: 14 additions & 65 deletions crates/re_edit_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
//! This should be called by `re_viewer` on startup.

mod corner2d;
mod datatype_editors;
mod marker_shape;
mod response_utils;
mod visible;

// ----

use egui::NumExt as _;
use re_types::components::{Color, MarkerSize, Name, Radius, StrokeWidth, Text};
use re_types::{
blueprint::components::Visible,
components::{Color, MarkerSize, Name, Radius, StrokeWidth, Text},
};
use re_viewer_context::ViewerContext;

// ----
Expand All @@ -29,60 +29,6 @@ fn edit_color_ui(_ctx: &ViewerContext<'_>, ui: &mut egui::Ui, value: &mut Color)
response
}

fn edit_text_ui(_ctx: &ViewerContext<'_>, ui: &mut egui::Ui, value: &mut Text) -> egui::Response {
let mut edit_text = value.to_string();
let response = egui::TextEdit::singleline(&mut edit_text).show(ui).response;
*value = edit_text.into();
response
}

fn edit_name_ui(_ctx: &ViewerContext<'_>, ui: &mut egui::Ui, value: &mut Name) -> egui::Response {
let mut edit_name = value.to_string();
let response = egui::TextEdit::singleline(&mut edit_name).show(ui).response;
*value = edit_name.into();
response
}

fn edit_radius_ui(
_ctx: &ViewerContext<'_>,
ui: &mut egui::Ui,
value: &mut Radius,
) -> egui::Response {
let speed = (value.0 * 0.01).at_least(0.001);

ui.add(
egui::DragValue::new(&mut value.0)
.clamp_range(0.0..=f64::INFINITY)
.speed(speed),
)
}

fn edit_stroke_width_ui(
_ctx: &ViewerContext<'_>,
ui: &mut egui::Ui,
value: &mut StrokeWidth,
) -> egui::Response {
let speed = (value.0 * 0.01).at_least(0.001);
ui.add(
egui::DragValue::new(&mut value.0)
.clamp_range(0.0..=f64::INFINITY)
.speed(speed),
)
}

fn edit_marker_size_ui(
_ctx: &ViewerContext<'_>,
ui: &mut egui::Ui,
value: &mut MarkerSize,
) -> egui::Response {
let speed = (value.0 * 0.01).at_least(0.001);
ui.add(
egui::DragValue::new(&mut value.0)
.clamp_range(0.0..=f64::INFINITY)
.speed(speed),
)
}

// ----

/// Registers all editors of this crate in the component UI registry.
Expand All @@ -93,10 +39,13 @@ pub fn register_editors(registry: &mut re_viewer_context::ComponentUiRegistry) {
registry.add_editor(edit_color_ui);
registry.add_editor(corner2d::edit_corner2d);
registry.add_editor(marker_shape::edit_marker_shape_ui);
registry.add_editor(edit_marker_size_ui);
registry.add_editor(edit_name_ui);
registry.add_editor(edit_radius_ui);
registry.add_editor(edit_stroke_width_ui);
registry.add_editor(edit_text_ui);
registry.add_editor(visible::edit_visible);

registry.add_editor::<Visible>(datatype_editors::edit_bool);

registry.add_editor::<Text>(datatype_editors::edit_singleline_string);
registry.add_editor::<Name>(datatype_editors::edit_singleline_string);

registry.add_editor::<Radius>(datatype_editors::edit_f32_zero_to_inf);
registry.add_editor::<MarkerSize>(datatype_editors::edit_f32_zero_to_inf);
registry.add_editor::<StrokeWidth>(datatype_editors::edit_f32_zero_to_inf);
}
15 changes: 0 additions & 15 deletions crates/re_edit_ui/src/visible.rs

This file was deleted.

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

7 changes: 7 additions & 0 deletions crates/re_types/src/blueprint/components/active_tab.rs

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

16 changes: 16 additions & 0 deletions crates/re_types/src/blueprint/components/column_share.rs

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

7 changes: 7 additions & 0 deletions crates/re_types/src/blueprint/components/included_content.rs

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

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

7 changes: 7 additions & 0 deletions crates/re_types/src/blueprint/components/query_expression.rs

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

16 changes: 16 additions & 0 deletions crates/re_types/src/blueprint/components/row_share.rs

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

7 changes: 7 additions & 0 deletions crates/re_types/src/blueprint/components/space_view_class.rs

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

7 changes: 7 additions & 0 deletions crates/re_types/src/blueprint/components/space_view_origin.rs

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

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

16 changes: 16 additions & 0 deletions crates/re_types/src/blueprint/components/visible.rs

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

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

Loading
Loading