summaryrefslogtreecommitdiffstats
path: root/native/src/widget/container.rs
diff options
context:
space:
mode:
authorLibravatar Ben LeFevre <ben@lefev.dev>2020-11-23 17:19:21 +0000
committerLibravatar Héctor Ramón <hector@lich.io>2021-06-01 19:05:39 +0700
commitfe0a27c56d9d75fb521e69352259f1d737402a20 (patch)
treef7f77430b63983717036a81e734276123d139ca6 /native/src/widget/container.rs
parenta9eb591628017caaf7aa9af505d1206f7a143a9a (diff)
downloadiced-fe0a27c56d9d75fb521e69352259f1d737402a20.tar.gz
iced-fe0a27c56d9d75fb521e69352259f1d737402a20.tar.bz2
iced-fe0a27c56d9d75fb521e69352259f1d737402a20.zip
Add support for asymmetrical padding
Diffstat (limited to 'native/src/widget/container.rs')
-rw-r--r--native/src/widget/container.rs29
1 files changed, 18 insertions, 11 deletions
diff --git a/native/src/widget/container.rs b/native/src/widget/container.rs
index 69fe699b..cf5cb3dc 100644
--- a/native/src/widget/container.rs
+++ b/native/src/widget/container.rs
@@ -5,7 +5,8 @@ use crate::event::{self, Event};
use crate::layout;
use crate::overlay;
use crate::{
- Align, Clipboard, Element, Hasher, Layout, Length, Point, Rectangle, Widget,
+ Align, Clipboard, Element, Hasher, Layout, Length, Padding, Point,
+ Rectangle, Widget,
};
use std::u32;
@@ -15,7 +16,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,
+ padding: Padding,
width: Length,
height: Length,
max_width: u32,
@@ -36,7 +37,7 @@ where
T: Into<Element<'a, Message, Renderer>>,
{
Container {
- padding: 0,
+ padding: Padding::ZERO,
width: Length::Shrink,
height: Length::Shrink,
max_width: u32::MAX,
@@ -48,9 +49,14 @@ where
}
}
- /// Sets the padding of the [`Container`].
- pub fn padding(mut self, units: u16) -> Self {
- self.padding = units;
+ /// Sets the [`Padding`] of the [`Container`].
+ ///```ignore
+ /// Container::new(/*...*/).padding(20); // 20px on all sides
+ /// Container::new(/*...*/).padding([10, 20]); // top/bottom, left/right
+ /// Container::new(/*...*/).padding([5, 10, 15, 20]); // top, right, bottom, left
+ /// ```
+ pub fn padding<P: Into<Padding>>(mut self, padding: P) -> Self {
+ self.padding = padding.into();
self
}
@@ -127,23 +133,24 @@ 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)
- .pad(padding);
+ .pad(self.padding);
let mut content = self.content.layout(renderer, &limits.loose());
let size = limits.resolve(content.size());
- content.move_to(Point::new(padding, padding));
+ content.move_to(Point::new(
+ self.padding.left.into(),
+ self.padding.top.into(),
+ ));
content.align(self.horizontal_alignment, self.vertical_alignment, size);
- layout::Node::with_children(size.pad(padding), vec![content])
+ layout::Node::with_children(size.pad(self.padding), vec![content])
}
fn on_event(