Skip to content

Commit

Permalink
Merge pull request #16 from ejjonny/reduce-api
Browse files Browse the repository at this point in the history
Reduce unnecessary api
  • Loading branch information
cyypherus authored Sep 7, 2024
2 parents 1cebcfe + b28eba6 commit 62b1745
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 178 deletions.
6 changes: 3 additions & 3 deletions src/anynode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ use crate::models::Area;
use std::{any::Any, fmt, rc::Rc};

type AnyDrawFn<State> = Rc<dyn Fn(&dyn Any, &mut State)>;
pub struct AnyNode<State> {
pub(crate) struct AnyNode<State> {
pub(crate) inner: Box<dyn Any>,
pub(crate) clone: fn(&Box<dyn Any>) -> Box<dyn Any>,
pub(crate) layout: fn(&mut dyn Any, Area),
pub(crate) draw: AnyDrawFn<State>,
}

impl<State> AnyNode<State> {
pub fn draw(&self, state: &mut State) {
pub(crate) fn draw(&self, state: &mut State) {
(self.draw)(&*self.inner, state)
}

pub fn layout(&mut self, available_area: Area) {
pub(crate) fn layout(&mut self, available_area: Area) {
(self.layout)(&mut *self.inner, available_area)
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/clone.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
use crate::{drawable::Drawable, layout::Node};
use crate::{drawable::Drawable, layout::NodeValue};

impl<State> Clone for Node<State> {
impl<State> Clone for NodeValue<State> {
fn clone(&self) -> Self {
match self {
Node::Padding { amounts, element } => Node::Padding {
NodeValue::Padding { amounts, element } => NodeValue::Padding {
amounts: *amounts,
element: element.clone(),
},
Node::Column { elements, spacing } => Node::Column {
NodeValue::Column { elements, spacing } => NodeValue::Column {
elements: elements.clone(),
spacing: *spacing,
},
Node::Row { elements, spacing } => Node::Row {
NodeValue::Row { elements, spacing } => NodeValue::Row {
elements: elements.clone(),
spacing: *spacing,
},
Node::Stack(elements) => Node::Stack(elements.clone()),
Node::Offset {
NodeValue::Stack(elements) => NodeValue::Stack(elements.clone()),
NodeValue::Offset {
offset_x,
offset_y,
element,
} => Node::Offset {
} => NodeValue::Offset {
offset_x: *offset_x,
offset_y: *offset_y,
element: element.clone(),
},
Node::Draw(drawable) => Node::Draw(Drawable {
NodeValue::Draw(drawable) => NodeValue::Draw(Drawable {
area: drawable.area,
draw: drawable.draw.clone(),
}),
Node::Explicit { options, element } => Node::Explicit {
NodeValue::Explicit { options, element } => NodeValue::Explicit {
options: *options,
element: element.clone(),
},
Node::Group(elements) => Node::Group(elements.clone()),
Node::Space => Node::Space,
Node::Scope { scoped } => Node::Scope {
NodeValue::Group(elements) => NodeValue::Group(elements.clone()),
NodeValue::Space => NodeValue::Space,
NodeValue::Scope { scoped } => NodeValue::Scope {
scoped: scoped.clone(),
},
Node::Empty => Node::Empty,
NodeValue::Empty => NodeValue::Empty,
}
}
}
26 changes: 13 additions & 13 deletions src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
use crate::layout::Node;
use crate::layout::NodeValue;
use std::fmt;

impl<State> fmt::Debug for Node<State> {
impl<State> fmt::Debug for NodeValue<State> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Node::Padding { amounts, element } => f
NodeValue::Padding { amounts, element } => f
.debug_struct("Padding")
.field("amounts", amounts)
.field("element", element)
.finish(),
Node::Column { elements, spacing } => f
NodeValue::Column { elements, spacing } => f
.debug_struct("Column")
.field("elements", elements)
.field("spacing", spacing)
.finish(),
Node::Row { elements, spacing } => f
NodeValue::Row { elements, spacing } => f
.debug_struct("Row")
.field("elements", elements)
.field("spacing", spacing)
.finish(),
Node::Stack(elements) => f.debug_tuple("Stack").field(elements).finish(),
Node::Group(elements) => f.debug_tuple("Group").field(elements).finish(),
Node::Offset {
NodeValue::Stack(elements) => f.debug_tuple("Stack").field(elements).finish(),
NodeValue::Group(elements) => f.debug_tuple("Group").field(elements).finish(),
NodeValue::Offset {
offset_x,
offset_y,
element,
Expand All @@ -31,15 +31,15 @@ impl<State> fmt::Debug for Node<State> {
.field("offset_y", offset_y)
.field("element", element)
.finish(),
Node::Draw(drawable) => f.debug_tuple("Draw").field(drawable).finish(),
Node::Explicit { options, element } => f
NodeValue::Draw(drawable) => f.debug_tuple("Draw").field(drawable).finish(),
NodeValue::Explicit { options, element } => f
.debug_struct("Explicit")
.field("options", options)
.field("element", element)
.finish(),
Node::Space => write!(f, "Space"),
Node::Empty => write!(f, "Empty"),
Node::Scope { scoped } => f.debug_struct("Scope").field("scoped", scoped).finish(),
NodeValue::Space => write!(f, "Space"),
NodeValue::Empty => write!(f, "Empty"),
NodeValue::Scope { scoped } => f.debug_struct("Scope").field("scoped", scoped).finish(),
}
}
}
6 changes: 3 additions & 3 deletions src/drawable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use std::{fmt, rc::Rc};
type DrawFn<State> = Rc<dyn Fn(Area, &'_ mut State)>;

#[derive(Clone)]
pub struct Drawable<State> {
pub area: Area,
pub(crate) struct Drawable<State> {
pub(crate) area: Area,
pub(crate) draw: DrawFn<State>,
}

impl<State> Drawable<State> {
pub fn draw(&self, area: Area, state: &mut State) {
pub(crate) fn draw(&self, area: Area, state: &mut State) {
if area.width > 0. && area.height > 0. {
(self.draw)(area, state);
}
Expand Down
72 changes: 38 additions & 34 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,39 @@ impl<State> Layout<State> {
impl<State> Layout<State> {
pub fn draw(&self, area: Area, state: &mut State) {
let mut layout = (self.tree)(state);
layout.layout(area);
layout.draw(state);
layout.inner.layout(area);
layout.inner.draw(state);
}
}

pub enum Node<State> {
pub struct Node<State> {
pub(crate) inner: NodeValue<State>,
}

pub(crate) enum NodeValue<State> {
Padding {
amounts: Padding,
element: Box<Node<State>>,
element: Box<NodeValue<State>>,
},
Column {
elements: Vec<Node<State>>,
elements: Vec<NodeValue<State>>,
spacing: f32,
},
Row {
elements: Vec<Node<State>>,
elements: Vec<NodeValue<State>>,
spacing: f32,
},
Stack(Vec<Node<State>>),
Group(Vec<Node<State>>),
Stack(Vec<NodeValue<State>>),
Group(Vec<NodeValue<State>>),
Offset {
offset_x: f32,
offset_y: f32,
element: Box<Node<State>>,
element: Box<NodeValue<State>>,
},
Draw(Drawable<State>),
Explicit {
options: Size,
element: Box<Node<State>>,
element: Box<NodeValue<State>>,
},
Empty,
Space,
Expand All @@ -50,30 +54,30 @@ pub enum Node<State> {
},
}

impl<State> Node<State> {
pub fn draw(&self, state: &mut State) {
impl<State> NodeValue<State> {
pub(crate) fn draw(&self, state: &mut State) {
match self {
Node::Draw(drawable) => drawable.draw(drawable.area, state),
Node::Padding { element, .. }
| Node::Explicit { element, .. }
| Node::Offset { element, .. } => {
NodeValue::Draw(drawable) => drawable.draw(drawable.area, state),
NodeValue::Padding { element, .. }
| NodeValue::Explicit { element, .. }
| NodeValue::Offset { element, .. } => {
element.draw(state);
}
Node::Stack(elements) => {
NodeValue::Stack(elements) => {
elements.iter().for_each(|el| el.draw(state));
}
Node::Column { elements, .. } | Node::Row { elements, .. } => {
NodeValue::Column { elements, .. } | NodeValue::Row { elements, .. } => {
elements.iter().rev().for_each(|el| el.draw(state));
}
Node::Space => (),
Node::Scope { scoped } => scoped.draw(state),
Node::Group(_) | Node::Empty => unreachable!(),
NodeValue::Space => (),
NodeValue::Scope { scoped } => scoped.draw(state),
NodeValue::Group(_) | NodeValue::Empty => unreachable!(),
}
}

pub fn layout(&mut self, available_area: Area) {
pub(crate) fn layout(&mut self, available_area: Area) {
match self {
Node::Padding {
NodeValue::Padding {
amounts,
element: child,
} => {
Expand All @@ -85,26 +89,26 @@ impl<State> Node<State> {
};
child.layout(inner_area);
}
Node::Column { elements, spacing } => {
NodeValue::Column { elements, spacing } => {
layout_axis(elements, spacing, available_area, Orientation::Vertical)
}
Node::Row { elements, spacing } => {
NodeValue::Row { elements, spacing } => {
layout_axis(elements, spacing, available_area, Orientation::Horizontal)
}
Node::Stack(children) => {
NodeValue::Stack(children) => {
for child in children {
child.layout(available_area)
}
}
Node::Draw(drawable) => {
NodeValue::Draw(drawable) => {
drawable.area = Area {
x: available_area.x,
y: available_area.y,
width: available_area.width.max(0.),
height: available_area.height.max(0.),
};
}
Node::Explicit {
NodeValue::Explicit {
options,
element: child,
} => {
Expand Down Expand Up @@ -163,7 +167,7 @@ impl<State> Node<State> {
height: explicit_height,
});
}
Node::Offset {
NodeValue::Offset {
offset_x,
offset_y,
element,
Expand All @@ -175,9 +179,9 @@ impl<State> Node<State> {
height: available_area.height,
});
}
Node::Space => (),
Node::Scope { scoped } => scoped.layout(available_area),
Node::Group(_) | Node::Empty => unreachable!(),
NodeValue::Space => (),
NodeValue::Scope { scoped } => scoped.layout(available_area),
NodeValue::Group(_) | NodeValue::Empty => unreachable!(),
}
}
}
Expand All @@ -188,15 +192,15 @@ enum Orientation {
}

fn layout_axis<State>(
elements: &mut [Node<State>],
elements: &mut [NodeValue<State>],
spacing: &f32,
available_area: Area,
orientation: Orientation,
) {
let sizes: Vec<Option<f32>> = elements
.iter()
.map(|element| match element {
Node::Explicit {
NodeValue::Explicit {
options,
element: _,
} => {
Expand Down
Loading

0 comments on commit 62b1745

Please sign in to comment.