Skip to content

Commit

Permalink
Automatic min-width of ListItem
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jun 3, 2024
1 parent cf14472 commit d9d0781
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions crates/re_ui/src/list_item/label_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +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,
min_desired_width: Option<f32>,
exact_width: bool,
}

Expand All @@ -33,7 +33,7 @@ impl<'a> LabelContent<'a> {
icon_fn: None,
buttons_fn: None,
always_show_buttons: false,
min_desired_width: 0.0,
min_desired_width: None,
exact_width: false,
}
}
Expand Down Expand Up @@ -97,7 +97,7 @@ impl<'a> LabelContent<'a> {
/// 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.min_desired_width = Some(min_desired_width);
self
}

Expand Down Expand Up @@ -253,7 +253,7 @@ impl ListItemContent for LabelContent<'_> {
}

fn desired_width(&self, _re_ui: &ReUi, ui: &Ui) -> DesiredWidth {
if self.exact_width {
let measured_width = {
//TODO(ab): ideally there wouldn't be as much code duplication with `Self::ui`
let mut text = self.text.clone();
if self.italics || self.label_style == LabelStyle::Unnamed {
Expand All @@ -273,9 +273,20 @@ 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().at_least(self.min_desired_width))
desired_width.ceil()
};

// TODO(emilk): use the egui `UiStack` to check if we're somewhere resizable,
// and only then turn on text truncation.
// For instance: in a tooltip we must not truncate the text, because
// the user can't resize the tooltip to read the full text.
// But if we're in a panel, the user can resize the panel to read the full text.
let min_desired_width = self.min_desired_width.unwrap_or(measured_width);

if self.exact_width {
DesiredWidth::Exact(measured_width.at_least(min_desired_width))
} else {
DesiredWidth::AtLeast(self.min_desired_width)
DesiredWidth::AtLeast(min_desired_width)
}
}
}

0 comments on commit d9d0781

Please sign in to comment.