diff options
author | 2024-05-03 09:11:46 +0200 | |
---|---|---|
committer | 2024-05-03 09:11:46 +0200 | |
commit | 15057a05c118dafcb8cf90d4119e66caaa6026c5 (patch) | |
tree | 59248040104660f87fc4097a9a33408d9d37cd74 /widget/src/helpers.rs | |
parent | 1cefe6be210cdae8c6769673e8d23c6781a988f1 (diff) | |
download | iced-15057a05c118dafcb8cf90d4119e66caaa6026c5.tar.gz iced-15057a05c118dafcb8cf90d4119e66caaa6026c5.tar.bz2 iced-15057a05c118dafcb8cf90d4119e66caaa6026c5.zip |
Introduce `center` widget helper
... and also make `center_x` and `center_y` set
`width` and `height` to `Length::Fill`, respectively.
This targets the most common use case when centering
things and removes a bunch of boilerplate as a result.
Diffstat (limited to 'widget/src/helpers.rs')
-rw-r--r-- | widget/src/helpers.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 48c5dde4..fd8614f5 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -78,6 +78,27 @@ where Container::new(content) } +/// Creates a new [`Container`] that fills all the available space +/// and centers its contents inside. +/// +/// This is equivalent to: +/// ```rust,no_run +/// # use iced_widget::Container; +/// # fn container<A>(x: A) -> Container<'static, ()> { unreachable!() } +/// let centered = container("Centered!").center(); +/// ``` +/// +/// [`Container`]: crate::Container +pub fn center<'a, Message, Theme, Renderer>( + content: impl Into<Element<'a, Message, Theme, Renderer>>, +) -> Container<'a, Message, Theme, Renderer> +where + Theme: container::Catalog + 'a, + Renderer: core::Renderer, +{ + container(content).fill().center() +} + /// Creates a new [`Column`] with the given children. pub fn column<'a, Message, Theme, Renderer>( children: impl IntoIterator<Item = Element<'a, Message, Theme, Renderer>>, |