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 ++++++++++++++ core/src/size.rs | 27 +++++++++++++++++++++++++++ native/src/widget/button.rs | 9 ++++++--- 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 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 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 6c0b8f6e..e927998c 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -426,12 +426,15 @@ 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.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( .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( -- cgit From ea4b3cd6aeb3c4dcb5113389c85f577fd3714682 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Thu, 27 Oct 2022 12:10:47 -0700 Subject: Fix text input padding --- native/src/widget/text_input.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index c2d25520..6ac4a2dd 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -350,15 +350,21 @@ 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.constrain(text.size(), limits.max()); - 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]) + let size = limits.pad(padding).resolve(text.size()).pad(padding); + + layout::Node::with_children(size, vec![text]) } /// Processes an [`Event`] and updates the [`State`] of a [`TextInput`] -- cgit From 7476663069572adec25161b46c26570f864f736f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 8 Nov 2022 03:56:05 +0100 Subject: Rename `Padding::constrain` to `fit` --- core/src/padding.rs | 4 ++-- native/src/widget/button.rs | 2 +- native/src/widget/container.rs | 2 +- native/src/widget/text_input.rs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core/src/padding.rs b/core/src/padding.rs index 64c95c89..ad5d1f0f 100644 --- a/core/src/padding.rs +++ b/core/src/padding.rs @@ -74,8 +74,8 @@ impl Padding { self.left + self.right } - /// Constrains the padding to fit between the inner & outer [`Size`] - pub fn constrain(self, inner: Size, outer: Size) -> Self { + /// 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 { diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index e927998c..01b528ec 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -430,7 +430,7 @@ pub fn layout( let mut content = layout_content(renderer, &limits.pad(padding)); - let padding = padding.constrain(content.size(), limits.max()); + let padding = padding.fit(content.size(), limits.max()); content.move_to(Point::new(padding.left.into(), padding.top.into())); diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index cc886dcb..1c060375 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -297,7 +297,7 @@ pub fn layout( let mut content = layout_content(renderer, &limits.pad(padding).loose()); - let padding = padding.constrain(content.size(), limits.max()); + let padding = padding.fit(content.size(), limits.max()); let size = limits.pad(padding).resolve(content.size()); diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 6ac4a2dd..a71c3b63 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -358,7 +358,7 @@ where let mut text = layout::Node::new(text_limits.resolve(Size::ZERO)); - let padding = padding.constrain(text.size(), limits.max()); + let padding = padding.fit(text.size(), limits.max()); text.move_to(Point::new(padding.left.into(), padding.top.into())); -- cgit From 914f0993428c752937d8db0a70a48f6f6f29c839 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 8 Nov 2022 04:04:01 +0100 Subject: Rearrange `layout` code to improve readability --- native/src/widget/button.rs | 4 +--- native/src/widget/container.rs | 2 -- native/src/widget/text_input.rs | 4 +--- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index 01b528ec..1582188b 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -429,13 +429,11 @@ pub fn layout( let limits = limits.width(width).height(height); 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); content.move_to(Point::new(padding.left.into(), padding.top.into())); - 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 1c060375..10a80b58 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -296,9 +296,7 @@ pub fn layout( .height(height); 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())); diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index a71c3b63..dfc49a8d 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -357,13 +357,11 @@ where 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); text.move_to(Point::new(padding.left.into(), padding.top.into())); - let size = limits.pad(padding).resolve(text.size()).pad(padding); - layout::Node::with_children(size, vec![text]) } -- cgit From 24d031b51c85507199b0e33e44c5a871882f6b32 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 8 Nov 2022 04:11:06 +0100 Subject: Cast to `u16` first then divide by `2` in `Padding::fit` --- core/src/padding.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/padding.rs b/core/src/padding.rs index ad5d1f0f..8d701f80 100644 --- a/core/src/padding.rs +++ b/core/src/padding.rs @@ -79,10 +79,10 @@ impl Padding { 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), + 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), } } } -- cgit From 04087b2a867520cfc43a648fa9e93cee2a6daf3c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 8 Nov 2022 04:11:45 +0100 Subject: Remove redundant `std::convert` namespace in `padding` --- core/src/padding.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/padding.rs b/core/src/padding.rs index 8d701f80..140ad8ee 100644 --- a/core/src/padding.rs +++ b/core/src/padding.rs @@ -87,7 +87,7 @@ impl Padding { } } -impl std::convert::From for Padding { +impl From for Padding { fn from(p: u16) -> Self { Padding { top: p, @@ -98,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], @@ -109,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], -- cgit