Constrain padding to inner & outer sizes

This commit is contained in:
Cory Forsstrom 2022-10-27 11:48:42 -07:00
parent 82217947aa
commit 2c103f8654
4 changed files with 53 additions and 7 deletions

View file

@ -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 {