From b15bd765967a0ceeee41e09ade451435a601cea0 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 13 Dec 2020 13:57:17 +0100 Subject: [PATCH] Pick default multiline on Labels based on layout and if text contains \n --- CHANGELOG.md | 1 + egui/src/widgets/mod.rs | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5b04bc8..106b9689 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * Remove minimum button width * Refactored `egui::Layout` substantially, changing its interface. * Calling ``on_hover_text`/`on_hover_ui` multiple times will stack tooltips underneath the previous ones. +* Text wrapping on labels, buttons, checkboxes and radio buttons is now based on the layout. ### Removed 🔥 diff --git a/egui/src/widgets/mod.rs b/egui/src/widgets/mod.rs index 85faeb31..6c9552fb 100644 --- a/egui/src/widgets/mod.rs +++ b/egui/src/widgets/mod.rs @@ -31,7 +31,7 @@ pub trait Widget { pub struct Label { // TODO: not pub pub(crate) text: String, - pub(crate) multiline: bool, + pub(crate) multiline: Option, pub(crate) text_style: Option, pub(crate) text_color: Option, } @@ -40,7 +40,7 @@ impl Label { pub fn new(text: impl Into) -> Self { Self { text: text.into(), - multiline: true, + multiline: None, text_style: None, text_color: None, } @@ -50,8 +50,14 @@ impl Label { &self.text } + /// If `true`, the text will wrap at the `max_width`. + /// By default `multiline` will be true in vertical layouts + /// and horizontal layouts with wrapping, + /// and false on non-wrapping horizontal layouts. + /// + /// If the text has any newlines (`\n`) in it, multiline will automatically turn on. pub fn multiline(mut self, multiline: bool) -> Self { - self.multiline = multiline; + self.multiline = Some(multiline); self } @@ -86,7 +92,7 @@ impl Label { pub fn layout_width(&self, ui: &Ui, max_width: f32) -> Galley { let text_style = self.text_style_or_default(ui.style()); let font = &ui.fonts()[text_style]; - if self.multiline { + if self.is_multiline(ui) { font.layout_multiline(self.text.clone(), max_width) // TODO: avoid clone } else { font.layout_single_line(self.text.clone()) // TODO: avoid clone @@ -118,11 +124,20 @@ impl Label { pub fn text_style_or_default(&self, style: &Style) -> TextStyle { self.text_style.unwrap_or(style.body_text_style) } + + fn is_multiline(&self, ui: &Ui) -> bool { + self.multiline.unwrap_or_else(|| { + let layout = ui.layout(); + layout.is_vertical() + || layout.is_horizontal() && layout.main_wrap() + || self.text.contains('\n') + }) + } } impl Widget for Label { fn ui(self, ui: &mut Ui) -> Response { - if self.multiline + if self.is_multiline(ui) && ui.layout().main_dir() == Direction::LeftToRight && ui.layout().main_wrap() {