diff --git a/core/src/padding.rs b/core/src/padding.rs index 22467d6b99..140ad8eee2 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,9 +73,21 @@ impl Padding { pub fn horizontal(self) -> u16 { self.left + self.right } + + /// Fits the [`Padding`] between the provided `inner` and `outer` [`Size`]. + pub fn fit(self, inner: Size, outer: Size) -> Self { + let available = (outer - inner).max(Size::ZERO); + + Padding { + top: self.top.min((available.height as u16) / 2), + right: self.right.min((available.width as u16) / 2), + bottom: self.bottom.min((available.height as u16) / 2), + left: self.left.min((available.width as u16) / 2), + } + } } -impl std::convert::From for Padding { +impl From for Padding { fn from(p: u16) -> Self { Padding { top: p, @@ -84,7 +98,7 @@ impl std::convert::From for Padding { } } -impl std::convert::From<[u16; 2]> for Padding { +impl From<[u16; 2]> for Padding { fn from(p: [u16; 2]) -> Self { Padding { top: p[0], @@ -95,7 +109,7 @@ impl std::convert::From<[u16; 2]> for Padding { } } -impl std::convert::From<[u16; 4]> for Padding { +impl From<[u16; 4]> for Padding { fn from(p: [u16; 4]) -> Self { Padding { top: p[0], diff --git a/core/src/size.rs b/core/src/size.rs index 2db33a88a1..31f3171b66 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 for Vector { 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 6c0b8f6e79..1582188b62 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -426,12 +426,13 @@ pub fn layout( 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); - content.move_to(Point::new(padding.left.into(), padding.top.into())); + let mut content = layout_content(renderer, &limits.pad(padding)); + let padding = padding.fit(content.size(), limits.max()); + let size = limits.pad(padding).resolve(content.size()).pad(padding); - let size = limits.resolve(content.size()).pad(padding); + content.move_to(Point::new(padding.left.into(), padding.top.into())); layout::Node::with_children(size, vec![content]) } diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index 2afad3f2d3..10a80b583d 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -293,11 +293,11 @@ pub fn layout( .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.fit(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( diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index c2d2552083..dfc49a8d0a 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -350,15 +350,19 @@ where { let text_size = size.unwrap_or_else(|| renderer.default_size()); - let limits = limits + let text_limits = limits .pad(padding) .width(width) .height(Length::Units(text_size)); + let limits = limits.width(width).height(Length::Shrink); + + let mut text = layout::Node::new(text_limits.resolve(Size::ZERO)); + let padding = padding.fit(text.size(), limits.max()); + let size = limits.pad(padding).resolve(text.size()).pad(padding); - let mut text = layout::Node::new(limits.resolve(Size::ZERO)); text.move_to(Point::new(padding.left.into(), padding.top.into())); - layout::Node::with_children(text.size().pad(padding), vec![text]) + layout::Node::with_children(size, vec![text]) } /// Processes an [`Event`] and updates the [`State`] of a [`TextInput`]