Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
});
Expand Down Expand Up @@ -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);
}
}
3 changes: 2 additions & 1 deletion src/style.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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,
}
2 changes: 2 additions & 0 deletions src/styles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ mod vertical_align;
pub use vertical_align::*;
mod horizontal_align;
pub use horizontal_align::*;
mod padding;
pub use padding::*;
19 changes: 19 additions & 0 deletions src/styles/padding.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
}