diff options
author | 2020-03-31 01:19:28 +0200 | |
---|---|---|
committer | 2020-03-31 01:21:51 +0200 | |
commit | 749a9588d738de9c3ae734e6f026768c77eb3cce (patch) | |
tree | 719597f4577c9329e738389847ff9224c2ac075d /native/src | |
parent | 6e9ab1cd6f5358d323040379e3aadbed2cc4f7f8 (diff) | |
download | iced-749a9588d738de9c3ae734e6f026768c77eb3cce.tar.gz iced-749a9588d738de9c3ae734e6f026768c77eb3cce.tar.bz2 iced-749a9588d738de9c3ae734e6f026768c77eb3cce.zip |
Implement `padding` support for `Container`
Diffstat (limited to 'native/src')
-rw-r--r-- | native/src/layout/flex.rs | 5 | ||||
-rw-r--r-- | native/src/widget/container.rs | 19 |
2 files changed, 18 insertions, 6 deletions
diff --git a/native/src/layout/flex.rs b/native/src/layout/flex.rs index 2f65f1c1..9da75a21 100644 --- a/native/src/layout/flex.rs +++ b/native/src/layout/flex.rs @@ -171,8 +171,5 @@ where let (width, height) = axis.pack(main - padding, cross); let size = limits.resolve(Size::new(width, height)); - Node::with_children( - Size::new(size.width + padding * 2.0, size.height + padding * 2.0), - nodes, - ) + Node::with_children(size.pad(padding), nodes) } diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs index 11ef23a9..34032c3a 100644 --- a/native/src/widget/container.rs +++ b/native/src/widget/container.rs @@ -13,6 +13,7 @@ use std::u32; /// It is normally used for alignment purposes. #[allow(missing_debug_implementations)] pub struct Container<'a, Message, Renderer: self::Renderer> { + padding: u16, width: Length, height: Length, max_width: u32, @@ -35,6 +36,7 @@ where T: Into<Element<'a, Message, Renderer>>, { Container { + padding: 0, width: Length::Shrink, height: Length::Shrink, max_width: u32::MAX, @@ -46,6 +48,14 @@ where } } + /// Sets the padding of the [`Container`]. + /// + /// [`Container`]: struct.Column.html + pub fn padding(mut self, units: u16) -> Self { + self.padding = units; + self + } + /// Sets the width of the [`Container`]. /// /// [`Container`]: struct.Container.html @@ -137,19 +147,23 @@ where renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { + let padding = f32::from(self.padding); + let limits = limits .loose() .max_width(self.max_width) .max_height(self.max_height) .width(self.width) - .height(self.height); + .height(self.height) + .pad(padding); let mut content = self.content.layout(renderer, &limits.loose()); let size = limits.resolve(content.size()); + content.move_to(Point::new(padding, padding)); content.align(self.horizontal_alignment, self.vertical_alignment, size); - layout::Node::with_children(size, vec![content]) + layout::Node::with_children(size.pad(padding), vec![content]) } fn on_event( @@ -191,6 +205,7 @@ where fn hash_layout(&self, state: &mut Hasher) { std::any::TypeId::of::<Container<'_, (), Renderer>>().hash(state); + self.padding.hash(state); self.width.hash(state); self.height.hash(state); self.max_width.hash(state); |