From 2c103f8654943c773b6de3c70eb2927e92219422 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Thu, 27 Oct 2022 11:48:42 -0700 Subject: Constrain padding to inner & outer sizes --- core/src/padding.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'core/src/padding.rs') 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 for Padding { -- cgit