diff options
author | 2019-11-14 06:46:50 +0100 | |
---|---|---|
committer | 2019-11-14 06:46:50 +0100 | |
commit | bc8d347736ec997ec0e0c401289e2bc09e212b8a (patch) | |
tree | b98798c09a3aa914b7d0869fba0cfd3efff7754f /native/src/layout/node.rs | |
parent | 839e039dbf2fb89dcb8c141503740777d2af2eb3 (diff) | |
parent | 73f3c900071f950ea914652ca3f0002c1e173f61 (diff) | |
download | iced-bc8d347736ec997ec0e0c401289e2bc09e212b8a.tar.gz iced-bc8d347736ec997ec0e0c401289e2bc09e212b8a.tar.bz2 iced-bc8d347736ec997ec0e0c401289e2bc09e212b8a.zip |
Merge pull request #52 from hecrj/custom-layout-engine
Custom layout engine
Diffstat (limited to 'native/src/layout/node.rs')
-rw-r--r-- | native/src/layout/node.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/native/src/layout/node.rs b/native/src/layout/node.rs new file mode 100644 index 00000000..64ebf2d0 --- /dev/null +++ b/native/src/layout/node.rs @@ -0,0 +1,64 @@ +use crate::{Align, Rectangle, Size}; + +#[derive(Debug, Clone, Default)] +pub struct Node { + pub bounds: Rectangle, + children: Vec<Node>, +} + +impl Node { + pub fn new(size: Size) -> Self { + Self::with_children(size, Vec::new()) + } + + pub fn with_children(size: Size, children: Vec<Node>) -> Self { + Node { + bounds: Rectangle { + x: 0.0, + y: 0.0, + width: size.width, + height: size.height, + }, + children, + } + } + + pub fn size(&self) -> Size { + Size::new(self.bounds.width, self.bounds.height) + } + + pub fn bounds(&self) -> Rectangle { + self.bounds + } + + pub fn children(&self) -> &[Node] { + &self.children + } + + pub fn align( + &mut self, + horizontal_alignment: Align, + vertical_alignment: Align, + space: Size, + ) { + match horizontal_alignment { + Align::Start => {} + Align::Center => { + self.bounds.x += (space.width - self.bounds.width) / 2.0; + } + Align::End => { + self.bounds.x += space.width - self.bounds.width; + } + } + + match vertical_alignment { + Align::Start => {} + Align::Center => { + self.bounds.y += (space.height - self.bounds.height) / 2.0; + } + Align::End => { + self.bounds.y += space.height - self.bounds.height; + } + } + } +} |