Skip to content

Commit

Permalink
Initial refactor of API
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Sjölander committed Apr 5, 2019
1 parent 6a2b01f commit f3d2a5e
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 175 deletions.
228 changes: 115 additions & 113 deletions src/algo.rs

Large diffs are not rendered by default.

12 changes: 0 additions & 12 deletions src/layout.rs

This file was deleted.

5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
extern crate alloc;

pub mod geometry;
pub mod layout;
pub mod number;
pub mod result;
pub mod number;
pub mod style;
pub mod node;

mod algo;
mod ref_eq;
pub use crate::algo::compute;
65 changes: 65 additions & 0 deletions src/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};

use std::rc::Rc;
use std::rc::Weak;
use std::cell::RefCell;

use crate::style::*;
use crate::algo;
use crate::geometry::Size;
use crate::number::Number;
use crate::result::{Layout, Cache, Result};

type MeasureFunc = Box<Fn(Size<Number>) -> Result<Size<f32>>>;

pub(crate) struct InternalNode {
pub(crate) style: Style,
pub(crate) parents: Vec<Weak<RefCell<InternalNode>>>,
pub(crate) children: Vec<Rc<RefCell<InternalNode>>>,
pub(crate) measure: Option<MeasureFunc>,
pub(crate) layout_cache: RefCell<Option<Cache>>,
}

pub struct Node(Rc<RefCell<InternalNode>>);

impl Node {
pub fn new(style: Style, children: Vec<&Node>) -> Node {
let mut parent = Node(Rc::new(RefCell::new(InternalNode {
style: style,
parents: vec![],
children: vec![],
measure: None,
layout_cache: RefCell::new(None),
})));

for child in children {
parent.add_child(child);
}

parent
}

pub fn add_child(&mut self, child: &Node) {
child.0.borrow_mut().parents.push(Rc::downgrade(&self.0));
self.0.borrow_mut().children.push(Rc::clone(&child.0));
}

// pub fn children(&self) -> Vec<Node> {}

// pub fn mark_dirty(&mut self) {}

// pub fn set_style(&mut self, Style) {}

// pub fn set_children(&mut self, Vec<Node>) {}

// pub fn add_child(&mut self, Node) {}

// pub fn remove_child(&mut self, Node) -> Node {}

// pub fn replace_child(&mut self, Node, u32) -> Node {}

pub fn compute_layout(&self, size: Size<Number>) -> Result<Layout> {
algo::compute(&self.0.borrow(), size)
}
}
24 changes: 24 additions & 0 deletions src/result.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

use core::any::Any;

use crate::geometry::{Point, Size};
use crate::number::Number;
use crate::algo::ComputeResult;

pub type Result<T> = core::result::Result<T, Box<Any>>;

#[derive(Debug, Clone)]
pub struct Layout {
pub(crate) order: u32,
pub size: Size<f32>,
pub location: Point<f32>,
pub children: Vec<Layout>,
}

#[derive(Debug, Clone)]
pub struct Cache {
pub node_size: Size<Number>,
pub parent_size: Size<Number>,
pub perform_layout: bool,

pub result: ComputeResult,
}
54 changes: 7 additions & 47 deletions src/style.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::{vec, vec::Vec};

use crate::algo;
use crate::geometry::{Rect, Size};
use crate::number::Number;
use crate::result::Result;

#[derive(Copy, Clone, PartialEq, Debug)]
pub enum AlignItems {
Expand Down Expand Up @@ -206,96 +202,60 @@ impl Default for Size<Dimension> {
}
}

type MeasureFunc = Box<Fn(Size<Number>) -> Result<Size<f32>>>;

#[derive(Debug, Clone)]
pub struct LayoutCache {
pub node_size: Size<Number>,
pub parent_size: Size<Number>,
pub perform_layout: bool,

pub result: algo::ComputeResult,
}

pub struct Node {
#[derive(Debug)]
pub struct Style {
pub display: Display,

pub position_type: PositionType,
pub direction: Direction,
pub flex_direction: FlexDirection,

pub flex_wrap: FlexWrap,
pub overflow: Overflow,

pub align_items: AlignItems,
pub align_self: AlignSelf,
pub align_content: AlignContent,

pub justify_content: JustifyContent,

pub position: Rect<Dimension>,
pub margin: Rect<Dimension>,
pub padding: Rect<Dimension>,
pub border: Rect<Dimension>,

pub flex_grow: f32,
pub flex_shrink: f32,
pub flex_basis: Dimension,

pub size: Size<Dimension>,
pub min_size: Size<Dimension>,
pub max_size: Size<Dimension>,

pub aspect_ratio: Number,
pub measure: Option<MeasureFunc>,

pub children: Vec<Node>,

pub layout_cache: core::cell::RefCell<Option<LayoutCache>>,
}

impl Default for Node {
fn default() -> Node {
Node {
impl Default for Style {
fn default() -> Style {
Style {
display: Default::default(),

position_type: Default::default(),
direction: Default::default(),
flex_direction: Default::default(),

flex_wrap: Default::default(),
overflow: Default::default(),

align_items: Default::default(),
align_self: Default::default(),
align_content: Default::default(),

justify_content: Default::default(),

position: Default::default(),
margin: Default::default(),
padding: Default::default(),
border: Default::default(),

flex_grow: 0.0,
flex_shrink: 1.0,
flex_basis: Dimension::Auto,

size: Default::default(),
min_size: Default::default(),
max_size: Default::default(),

aspect_ratio: Default::default(),
measure: None,

children: vec![],

layout_cache: core::cell::RefCell::new(None),
}
}
}

impl Node {
impl Style {
pub(crate) fn min_main_size(&self, direction: FlexDirection) -> Dimension {
match direction {
FlexDirection::Row | FlexDirection::RowReverse => self.min_size.width,
Expand Down Expand Up @@ -359,7 +319,7 @@ impl Node {
}
}

pub(crate) fn align_self(&self, parent: &Node) -> AlignSelf {
pub(crate) fn align_self(&self, parent: &Style) -> AlignSelf {
if self.align_self == AlignSelf::Auto {
match parent.align_items {
AlignItems::FlexStart => AlignSelf::FlexStart,
Expand Down

0 comments on commit f3d2a5e

Please sign in to comment.