diff options
author | 2022-10-27 11:48:42 -0700 | |
---|---|---|
committer | 2022-10-27 11:50:12 -0700 | |
commit | 2c103f8654943c773b6de3c70eb2927e92219422 (patch) | |
tree | eda48510b5a8530367297dc87d04302d6b3ed4b8 | |
parent | 82217947aa80287282ed6deb02d238a31303e0d6 (diff) | |
download | iced-2c103f8654943c773b6de3c70eb2927e92219422.tar.gz iced-2c103f8654943c773b6de3c70eb2927e92219422.tar.bz2 iced-2c103f8654943c773b6de3c70eb2927e92219422.zip |
Constrain padding to inner & outer sizes
-rw-r--r-- | core/src/padding.rs | 14 | ||||
-rw-r--r-- | core/src/size.rs | 27 | ||||
-rw-r--r-- | native/src/widget/button.rs | 9 | ||||
-rw-r--r-- | native/src/widget/container.rs | 10 |
4 files changed, 53 insertions, 7 deletions
diff --git a/core/src/padding.rs b/core/src/padding.rs index 22467d6b..64c95c89 100644 --- a/core/src/padding.rs +++ b/core/src/padding.rs @@ -1,3 +1,5 @@ +use crate::Size; + /// An amount of space to pad for each side of a box /// /// You can leverage the `From` trait to build [`Padding`] conveniently: @@ -71,6 +73,18 @@ impl Padding { pub fn horizontal(self) -> u16 { self.left + self.right } + + /// Constrains the padding to fit between the inner & outer [`Size`] + pub fn constrain(self, inner: Size, outer: Size) -> Self { + let available = (outer - inner).max(Size::ZERO); + + Padding { + top: self.top.min((available.height / 2.0) as u16), + right: self.right.min((available.width / 2.0) as u16), + bottom: self.bottom.min((available.height / 2.0) as u16), + left: self.left.min((available.width / 2.0) as u16), + } + } } impl std::convert::From<u16> for Padding { diff --git a/core/src/size.rs b/core/src/size.rs index 2db33a88..31f3171b 100644 --- a/core/src/size.rs +++ b/core/src/size.rs @@ -34,6 +34,22 @@ impl Size { height: self.height + padding.vertical() as f32, } } + + /// Returns the minimum of each component of this size and another + pub fn min(self, other: Self) -> Self { + Size { + width: self.width.min(other.width), + height: self.height.min(other.height), + } + } + + /// Returns the maximum of each component of this size and another + pub fn max(self, other: Self) -> Self { + Size { + width: self.width.max(other.width), + height: self.height.max(other.height), + } + } } impl From<[f32; 2]> for Size { @@ -68,3 +84,14 @@ impl From<Size> for Vector<f32> { Vector::new(size.width, size.height) } } + +impl std::ops::Sub for Size { + type Output = Size; + + fn sub(self, rhs: Self) -> Self::Output { + Size { + width: self.width - rhs.width, + height: self.height - rhs.height, + } + } +} diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 6c0b8f6e..e927998c 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -426,12 +426,15 @@ pub fn layout<Renderer>( padding: Padding, layout_content: impl FnOnce(&Renderer, &layout::Limits) -> layout::Node, ) -> layout::Node { - let limits = limits.width(width).height(height).pad(padding); + let limits = limits.width(width).height(height); + + let mut content = layout_content(renderer, &limits.pad(padding)); + + let padding = padding.constrain(content.size(), limits.max()); - let mut content = layout_content(renderer, &limits); content.move_to(Point::new(padding.left.into(), padding.top.into())); - let size = limits.resolve(content.size()).pad(padding); + let size = limits.pad(padding).resolve(content.size()).pad(padding); layout::Node::with_children(size, vec![content]) } diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index 2afad3f2..cc886dcb 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -293,11 +293,13 @@ pub fn layout<Renderer>( .max_width(max_width) .max_height(max_height) .width(width) - .height(height) - .pad(padding); + .height(height); - let mut content = layout_content(renderer, &limits.loose()); - let size = limits.resolve(content.size()); + let mut content = layout_content(renderer, &limits.pad(padding).loose()); + + let padding = padding.constrain(content.size(), limits.max()); + + let size = limits.pad(padding).resolve(content.size()); content.move_to(Point::new(padding.left.into(), padding.top.into())); content.align( |