From 906a798003ac301cd6733f4dbba0452c515ecb12 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 26 Aug 2021 21:32:11 +0200 Subject: [PATCH] Add Ui::horizontal_top Closes https://github.com/emilk/egui/issues/601 --- CHANGELOG.md | 3 +++ egui/src/ui.rs | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d68f3af7..30d0151b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ NOTE: [`eframe`](eframe/CHANGELOG.md), [`egui_web`](egui_web/CHANGELOG.md) and [ ## Unreleased +### Added ⭐ +* Add `Ui::horizontal_top`. + ### Fixed 🐛 * Fix `set_width/set_min_width/set_height/set_min_height/expand_to_include_x/expand_to_include_y`. * Make minimum grid column width propagate properly. diff --git a/egui/src/ui.rs b/egui/src/ui.rs index 3f65ff5c..39686d55 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -1420,6 +1420,8 @@ impl Ui { /// Centering is almost always what you want if you are /// planning to to mix widgets or use different types of text. /// + /// If you don't want the contents to be centered, use [`Self::horizontal_top`] instead. + /// /// The returned `Response` will only have checked for mouse hover /// but can be used for tooltips (`on_hover_text`). /// It also contains the `Rect` used by the horizontal layout. @@ -1436,6 +1438,22 @@ impl Ui { self.horizontal_with_main_wrap(false, add_contents) } + /// Like [`Self::horizontal`], but aligns content with top. + #[inline(always)] + pub fn horizontal_top( + &mut self, + add_contents: impl FnOnce(&mut Ui) -> R, + ) -> InnerResponse { + let initial_size = self.available_size_before_wrap(); + let layout = if self.placer.prefer_right_to_left() { + Layout::right_to_left() + } else { + Layout::left_to_right() + } + .with_cross_align(Align::Min); + self.allocate_ui_with_layout_dyn(initial_size, layout, Box::new(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.