From 814121903a881fbd302ddc0117957401377d02e7 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 13 Sep 2020 09:30:52 +0200 Subject: [PATCH] remove Label::auto_shrink and replace with ui.shrink_width_to_current() --- egui/src/context.rs | 4 ++-- egui/src/ui.rs | 13 +++++++++++++ egui/src/widgets.rs | 19 ++++--------------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/egui/src/context.rs b/egui/src/context.rs index dcca8de5..c7fb0622 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -600,9 +600,9 @@ impl Context { } }); + ui.shrink_width_to_current(); // don't let the text below grow this window wider ui.add( - label!("NOTE: the position of this window cannot be reset from within itself.") - .auto_shrink(), + label!("NOTE: the position of this window cannot be reset from within itself."), // .auto_shrink(), ); } } diff --git a/egui/src/ui.rs b/egui/src/ui.rs index 7d73e350..49d2e23d 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -209,6 +209,19 @@ impl Ui { self.desired_rect.max.y = self.top_left().y + height; } + /// Helper: shrinks the max/desired width to the current width, + /// so further widgets will try not to be wider than previous widgets. + /// Useful for normal vertical layouts. + pub fn shrink_width_to_current(&mut self) { + self.set_desired_width(self.child_bounds().width()) + } + + /// Helper: shrinks the max/desired height to the current height, + /// so further widgets will try not to be wider than previous widgets. + pub fn shrink_height_to_current(&mut self) { + self.set_desired_height(self.child_bounds().height()) + } + /// Size of content pub fn bounding_size(&self) -> Vec2 { self.child_bounds.size() diff --git a/egui/src/widgets.rs b/egui/src/widgets.rs index 7b1cae42..074e3c86 100644 --- a/egui/src/widgets.rs +++ b/egui/src/widgets.rs @@ -33,7 +33,6 @@ pub struct Label { // TODO: not pub pub(crate) text: String, pub(crate) multiline: bool, - auto_shrink: bool, pub(crate) text_style: Option, pub(crate) text_color: Option, } @@ -43,7 +42,6 @@ impl Label { Self { text: text.into(), multiline: true, - auto_shrink: false, text_style: None, text_color: None, } @@ -58,14 +56,6 @@ impl Label { self } - /// If true, will word wrap to `ui.available_finite().width()`. - /// If false (default), will word wrap to `ui.available().width()`. - /// This only makes a difference for auto-sized parents. - pub fn auto_shrink(mut self) -> Self { - self.auto_shrink = true; - self - } - /// If you do not set a `TextStyle`, the default `style.text_style`. pub fn text_style(mut self, text_style: TextStyle) -> Self { self.text_style = Some(text_style); @@ -82,11 +72,10 @@ impl Label { } pub fn layout(&self, ui: &Ui) -> font::Galley { - let max_width = if self.auto_shrink { - ui.available_finite().width() - } else { - ui.available().width() - }; + let max_width = ui.available().width(); + // Prevent word-wrapping after a single letter, and other silly shit: + // TODO: general "don't force labels and similar to wrap so early" + // TODO: max_width = max_width.at_least(ui.spacing.first_wrap_width); self.layout_width(ui, max_width) }