diff --git a/CHANGELOG.md b/CHANGELOG.md index 73c5ae3e..9b3cf0b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ NOTE: [`epaint`](crates/epaint/CHANGELOG.md), [`eframe`](crates/eframe/CHANGELOG * Added `Key::Minus` and `Key::Equals` ([#2239](https://github.com/emilk/egui/pull/2239)). * Added `egui::gui_zoom` module with helpers for scaling the whole GUI of an app ([#2239](https://github.com/emilk/egui/pull/2239)). * You can now put one interactive widget on top of another, and only one will get interaction at a time ([#2244](https://github.com/emilk/egui/pull/2244)). +* Add `ui.centered`. ### Changed 🔧 * Panels always have a separator line, but no stroke on other sides. Their spacing has also changed slightly ([#2261](https://github.com/emilk/egui/pull/2261)). diff --git a/crates/egui/src/layout.rs b/crates/egui/src/layout.rs index 21ea0f78..81fd5ec4 100644 --- a/crates/egui/src/layout.rs +++ b/crates/egui/src/layout.rs @@ -196,7 +196,7 @@ impl Layout { } } - /// Top-down layout justifed so that buttons etc fill the full available width. + /// Top-down layout justified so that buttons etc fill the full available width. #[inline(always)] pub fn top_down_justified(halign: Align) -> Self { Self::top_down(halign).with_cross_justify(true) @@ -229,6 +229,20 @@ impl Layout { } } + /// For when you want to add a single widget to a layout, and that widget + /// should be centered horizontally and vertically. + #[inline(always)] + pub fn centered(main_dir: Direction) -> Self { + Self { + main_dir, + main_wrap: false, + main_align: Align::Center, + main_justify: false, + cross_align: Align::Center, + cross_justify: false, + } + } + /// For when you want to add a single widget to a layout, and that widget /// should use up all available space. #[inline(always)] diff --git a/crates/egui/src/ui.rs b/crates/egui/src/ui.rs index 4f367fc3..73b8681c 100644 --- a/crates/egui/src/ui.rs +++ b/crates/egui/src/ui.rs @@ -2007,6 +2007,11 @@ impl Ui { InnerResponse::new(inner, self.interact(rect, child_ui.id, Sense::hover())) } + /// This will make the next added widget centered in the available space. + pub fn centered(&mut self, add_contents: impl FnOnce(&mut Self) -> R) -> InnerResponse { + self.with_layout_dyn(Layout::centered(Direction::TopDown), Box::new(add_contents)) + } + /// This will make the next added widget centered and justified in the available space. pub fn centered_and_justified( &mut self,