Add ui.centered

This commit is contained in:
Emil Ernerfeldt 2022-11-08 01:07:51 +01:00
parent c3edc1b88e
commit 5515d2db77
3 changed files with 21 additions and 1 deletions

View file

@ -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 `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)). * 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)). * 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 🔧 ### 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)). * 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)).

View file

@ -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)] #[inline(always)]
pub fn top_down_justified(halign: Align) -> Self { pub fn top_down_justified(halign: Align) -> Self {
Self::top_down(halign).with_cross_justify(true) 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 /// For when you want to add a single widget to a layout, and that widget
/// should use up all available space. /// should use up all available space.
#[inline(always)] #[inline(always)]

View file

@ -2007,6 +2007,11 @@ impl Ui {
InnerResponse::new(inner, self.interact(rect, child_ui.id, Sense::hover())) 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<R>(&mut self, add_contents: impl FnOnce(&mut Self) -> R) -> InnerResponse<R> {
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. /// This will make the next added widget centered and justified in the available space.
pub fn centered_and_justified<R>( pub fn centered_and_justified<R>(
&mut self, &mut self,