diff --git a/src/engine.rs b/src/engine.rs index ea4b896..07a732a 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -139,7 +139,16 @@ impl LayoutEngine { }; // Recursively layout the child node - self.layout_node(tree, child, (width, height), child_x, child_y); + self.layout_node( + tree, + child, + ( + width - (style.padding.left + style.padding.right), + height - (style.padding.top + style.padding.bottom), + ), + child_x + style.padding.left, + child_y + style.padding.top, + ); // Update cursor position based on the direction cursor += match style.direction { diff --git a/src/lib.rs b/src/lib.rs index ce07ab2..b3a2718 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,6 +36,10 @@ mod tests { width: Units::Pixels(30.0), height: Units::Pixels(0.0), }, + padding: Padding { + top: 20.0, + ..Default::default() + }, direction: Direction::Row, ..Default::default() }); @@ -84,11 +88,11 @@ mod tests { assert_eq!(engine.computed[1].height, 300.0); assert_eq!(engine.computed[1].width, 450.0); assert_eq!(engine.computed[1].x, 0.0); - assert_eq!(engine.computed[1].y, 0.0); + assert_eq!(engine.computed[1].y, 20.0); assert_eq!(engine.computed[2].height, 300.0); assert_eq!(engine.computed[2].width, 200.0); - assert_eq!(engine.computed[2].y, 95.0); + assert_eq!(engine.computed[2].y, 105.0); assert_eq!(engine.computed[2].x, 500.0); } } diff --git a/src/style.rs b/src/style.rs index cf97a94..3cf2f21 100644 --- a/src/style.rs +++ b/src/style.rs @@ -1,4 +1,4 @@ -use crate::{Direction, HorizontalAlign, Margin, Size, VerticalAlign}; +use crate::{Direction, HorizontalAlign, Margin, Padding, Size, VerticalAlign}; /// Style of Node #[derive(Default, Clone, Debug)] @@ -9,6 +9,7 @@ pub struct Style { pub gap: Size, pub direction: Direction, pub margin: Margin, + pub padding: Padding, pub vertical_align: VerticalAlign, pub horizontal_align: HorizontalAlign, } diff --git a/src/styles/mod.rs b/src/styles/mod.rs index f735633..93f858a 100644 --- a/src/styles/mod.rs +++ b/src/styles/mod.rs @@ -8,3 +8,5 @@ mod vertical_align; pub use vertical_align::*; mod horizontal_align; pub use horizontal_align::*; +mod padding; +pub use padding::*; diff --git a/src/styles/padding.rs b/src/styles/padding.rs new file mode 100644 index 0000000..c238fa1 --- /dev/null +++ b/src/styles/padding.rs @@ -0,0 +1,19 @@ +#[derive(Clone, Copy, Debug)] +/// Space inside an node +pub struct Padding { + pub top: f32, + pub left: f32, + pub right: f32, + pub bottom: f32, +} + +impl Default for Padding { + fn default() -> Self { + Padding { + top: 0.0, + left: 0.0, + right: 0.0, + bottom: 0.0, + } + } +}