Skip to content

Commit

Permalink
Better way to address min content width and fix for the markershape menu
Browse files Browse the repository at this point in the history
  • Loading branch information
abey79 committed May 2, 2024
1 parent 4b591e8 commit 9b16b01
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 26 deletions.
50 changes: 35 additions & 15 deletions crates/re_data_ui/src/editors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,22 +263,42 @@ fn edit_marker_shape_ui(
// no spacing between list items
ui.spacing_mut().item_spacing.y = 0.0;

// Hack needed for ListItem to click its highlight bg rect correctly:
ui.set_clip_rect(
ui.clip_rect()
.with_max_x(ui.max_rect().max.x + ui.spacing().menu_margin.right),
let item_width = 100.0;

// workaround to force `ui.max_rect()` to reflect the content size
ui.allocate_space(egui::vec2(item_width, 0.0));

let background_x_range = ui
.spacing()
.menu_margin
.expand_rect(ui.max_rect())
.x_range();

re_ui::list_item2::list_item_scope(
ui,
"marker_shape",
Some(background_x_range),
|ui| {
for marker in MarkerShape::ALL {
let response = ctx
.re_ui
.list_item2()
.selected(edit_marker == marker)
.show_flat(
ui,
re_ui::list_item2::LabelContent::new(marker.to_string())
.min_desired_width(item_width)
.with_icon_fn(|_re_ui, ui, rect, visuals| {
paint_marker(ui, marker.into(), rect, visuals.text_color());
}),
);

if response.clicked() {
edit_marker = marker;
}
}
},
);

for marker in MarkerShape::ALL {
let list_item = re_ui::ListItem::new(ctx.re_ui, marker.to_string())
.with_icon_fn(|_re_ui, ui, rect, visuals| {
paint_marker(ui, marker.into(), rect, visuals.text_color());
})
.selected(edit_marker == marker);
if list_item.show_flat(ui).clicked() {
edit_marker = marker;
}
}
});

if edit_marker != current_marker {
Expand Down
20 changes: 17 additions & 3 deletions crates/re_ui/src/list_item2/label_content.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use egui::{text::TextWrapping, Align, Align2, Ui};
use egui::{text::TextWrapping, Align, Align2, NumExt, Ui};

use super::{ContentContext, DesiredWidth, ListItemContent};
use crate::{Icon, LabelStyle, ReUi};
Expand All @@ -18,6 +18,7 @@ pub struct LabelContent<'a> {
buttons_fn: Option<Box<dyn FnOnce(&ReUi, &mut egui::Ui) -> egui::Response + 'a>>,
always_show_buttons: bool,

min_desired_width: f32,
exact_width: bool,
}

Expand All @@ -32,6 +33,7 @@ impl<'a> LabelContent<'a> {
icon_fn: None,
buttons_fn: None,
always_show_buttons: false,
min_desired_width: 0.0,
exact_width: false,
}
}
Expand Down Expand Up @@ -82,12 +84,23 @@ impl<'a> LabelContent<'a> {
/// By default, [`LabelContent`] uses the available width. By setting `exact_width` to true,
/// the exact width required by the label (and the icon if any) is allocated instead. See
/// [`super::DesiredWidth::Exact`].
///
/// Note that if [`Self::min_desired_width`] is set, it is used as a minimum value.
#[inline]
pub fn exact_width(mut self, exact_width: bool) -> Self {
self.exact_width = exact_width;
self
}

/// Set the minimum desired for the content.
///
/// This defaults to zero.
#[inline]
pub fn min_desired_width(mut self, min_desired_width: f32) -> Self {
self.min_desired_width = min_desired_width;
self
}

/// Provide an [`Icon`] to be displayed on the left of the item.
#[inline]
pub fn with_icon(self, icon: &'a Icon) -> Self {
Expand Down Expand Up @@ -148,6 +161,7 @@ impl ListItemContent for LabelContent<'_> {
icon_fn,
buttons_fn,
always_show_buttons,
min_desired_width: _,
exact_width: _,
} = *self;

Expand Down Expand Up @@ -259,9 +273,9 @@ impl ListItemContent for LabelContent<'_> {

// The `ceil()` is needed to avoid some rounding errors which leads to text being
// truncated even though we allocated enough space.
DesiredWidth::Exact(desired_width.ceil())
DesiredWidth::Exact(desired_width.ceil().at_least(self.min_desired_width))
} else {
DesiredWidth::STANDARD
DesiredWidth::AtLeast(self.min_desired_width)
}
}
}
7 changes: 0 additions & 7 deletions crates/re_ui/src/list_item2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ impl Default for DesiredWidth {
}
}

impl DesiredWidth {
/// Reasonable suggested desired width to be used by [`ListItemContent`] implementations.
///
/// Both [`PropertyContent`] and [`LabelContent`] use this value as a default.
pub const STANDARD: Self = Self::AtLeast(150.0);
}

pub trait ListItemContent {
/// UI for everything that is after the indent and the collapsing triangle (if any).
///
Expand Down
15 changes: 14 additions & 1 deletion crates/re_ui/src/list_item2/property_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct PropertyActionButton<'a> {
/// value (which may be editable).
pub struct PropertyContent<'a> {
label: egui::WidgetText,
min_desired_width: f32,
icon_fn: Option<Box<IconFn<'a>>>,
show_only_when_collapsed: bool,
value_fn: Option<Box<PropertyValueFn<'a>>>,
Expand All @@ -35,13 +36,24 @@ impl<'a> PropertyContent<'a> {
pub fn new(label: impl Into<egui::WidgetText>) -> Self {
Self {
label: label.into(),
min_desired_width: 200.0,
icon_fn: None,
show_only_when_collapsed: true,
value_fn: None,
action_buttons: None,
}
}

/// Set the minimum desired width for the entire content.
///
/// Since there is no possibly way to meaningfully collapse two to three columns worth of
/// content, this is set to 200.0 by default.
#[inline]
pub fn min_desired_width(mut self, min_desired_width: f32) -> Self {
self.min_desired_width = min_desired_width;
self
}

/// Provide an [`Icon`] to be displayed on the left of the label.
#[inline]
pub fn with_icon(self, icon: &'a Icon) -> Self {
Expand Down Expand Up @@ -171,6 +183,7 @@ impl ListItemContent for PropertyContent<'_> {
fn ui(self: Box<Self>, re_ui: &ReUi, ui: &mut Ui, context: &ContentContext<'_>) {
let Self {
label,
min_desired_width: _,
icon_fn,
show_only_when_collapsed,
value_fn,
Expand Down Expand Up @@ -324,6 +337,6 @@ impl ListItemContent for PropertyContent<'_> {
}

fn desired_width(&self, _re_ui: &ReUi, _ui: &Ui) -> DesiredWidth {
DesiredWidth::STANDARD
DesiredWidth::AtLeast(self.min_desired_width)
}
}
2 changes: 2 additions & 0 deletions crates/re_viewer/src/ui/override_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ pub fn override_ui(
.show_flat(
ui,
re_ui::list_item2::PropertyContent::new(component_name.short_name())
.min_desired_width(150.0)
.action_button(&re_ui::icons::CLOSE, || {
ctx.save_empty_blueprint_component_name(
&overrides.individual_override_path,
Expand Down Expand Up @@ -336,6 +337,7 @@ pub fn override_visualizer_ui(
ctx.re_ui.list_item2().interactive(false).show_flat(
ui,
re_ui::list_item2::LabelContent::new(viz_name.as_str())
.min_desired_width(150.0)
.with_buttons(|re_ui, ui| {
let response = re_ui.small_icon_button(ui, &re_ui::icons::CLOSE);
if response.clicked() {
Expand Down

0 comments on commit 9b16b01

Please sign in to comment.