diff options
author | 2022-10-27 11:48:42 -0700 | |
---|---|---|
committer | 2022-10-27 11:50:12 -0700 | |
commit | 2c103f8654943c773b6de3c70eb2927e92219422 (patch) | |
tree | eda48510b5a8530367297dc87d04302d6b3ed4b8 /core/src/padding.rs | |
parent | 82217947aa80287282ed6deb02d238a31303e0d6 (diff) | |
download | iced-2c103f8654943c773b6de3c70eb2927e92219422.tar.gz iced-2c103f8654943c773b6de3c70eb2927e92219422.tar.bz2 iced-2c103f8654943c773b6de3c70eb2927e92219422.zip |
Constrain padding to inner & outer sizes
Diffstat (limited to 'core/src/padding.rs')
-rw-r--r-- | core/src/padding.rs | 14 |
1 files changed, 14 insertions, 0 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 { |