From 5880c951585b398f72279bf4d439ec61bc75d4aa Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 13 Dec 2020 18:14:18 +0100 Subject: [PATCH] ui.horizontal_for_text: Size and spacing made for text --- CHANGELOG.md | 10 +++++----- egui/src/ui.rs | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 106b9689..7534ea06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,11 +15,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * Works in any text. * Great for button icons. * The Demo app comes with a Font Book to explore the available glyphs. -* Wrapping layouts: - * `ui.horizontal_wrapped(|ui| ...)`: Add widgets on a row but wrap at `max_size`. - * `ui.horizontal_wrapped_for_text`: Like `horizontal_wrapped`, but with spacing made for embedding text. +* `ui.horizontal_wrapped(|ui| ...)`: Add widgets on a row but wrap at `max_size`. +* `ui.horizontal_wrapped_for_text`: Like `ui.horizontal_wrapped`, but with spacing made for embedding text. +* `ui.horizontal_for_text`: Like `ui.horizontal`, but with spacing made for embedding text. * `egui::Layout` now supports justified layouts where contents is _also_ centered, right-aligned, etc. -* `ui.allocate_ui(size, |ui| ...)`: Easily created a sized child-`Ui`. +* `ui.allocate_ui(size, |ui| ...)`: Easily created child-`Ui` of a given size. * `SelectableLabel` (`ui.selectable_label` and `ui.selectable_value`): A text-button that can be selected. * `ui.small_button`: A smaller button that looks good embedded in text. * Add `Resize::id_source` and `ScrollArea::id_source` to let the user avoid Id clashes. @@ -29,7 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * Changed default font to [Ubuntu-Light](https://fonts.google.com/specimen/Ubuntu). * 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. +* 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/ui.rs b/egui/src/ui.rs index ac33006c..9b2f573a 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -850,6 +850,28 @@ impl Ui { self.horizontal_with_main_wrap(false, add_contents) } + /// Like `horizontal`, but will set up the spacing to match that of a normal label. + /// + /// In particular, the space between widgets is the same width as the space character. + /// + /// You can still add any widgets to the layout (not only Labels). + pub fn horizontal_for_text( + &mut self, + text_style: TextStyle, + add_contents: impl FnOnce(&mut Ui) -> R, + ) -> (R, Response) { + self.wrap(|ui| { + let font = &ui.fonts()[text_style]; + let row_height = font.row_height(); + let space_width = font.glyph_width(' '); + let style = ui.style_mut(); + style.spacing.interact_size.y = row_height; + style.spacing.item_spacing.x = space_width; + style.spacing.item_spacing.y = 0.0; + ui.horizontal(add_contents).0 + }) + } + /// Start a ui with horizontal layout that wraps to a new row /// when it reaches the right edge of the `max_size`. /// After you have called this, the function registers the contents as any other widget.