Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Traitify" the Style struct #568

Merged
merged 13 commits into from
Jul 16, 2024
39 changes: 38 additions & 1 deletion examples/custom_tree_owned_partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ impl taffy::TraversePartialTree for Node {
}

impl taffy::LayoutPartialTree for Node {
fn get_style(&self, node_id: NodeId) -> &Style {
type CoreContainerStyle<'a> = &'a Style where
Self: 'a;

type CacheMut<'b> = &'b mut Cache where Self: 'b;

fn get_core_container_style(&self, node_id: NodeId) -> Self::CoreContainerStyle<'_> {
&self.node_from_id(node_id).style
}

Expand Down Expand Up @@ -162,6 +167,38 @@ impl taffy::LayoutPartialTree for Node {
}
}

impl taffy::LayoutFlexboxContainer for Node {
type FlexboxContainerStyle<'a> = &'a Style where
Self: 'a;

type FlexboxItemStyle<'a> = &'a Style where
Self: 'a;

fn get_flexbox_container_style(&self, node_id: NodeId) -> Self::FlexboxContainerStyle<'_> {
&self.node_from_id(node_id).style
}

fn get_flexbox_child_style(&self, child_node_id: NodeId) -> Self::FlexboxItemStyle<'_> {
&self.node_from_id(child_node_id).style
}
}

impl taffy::LayoutGridContainer for Node {
type GridContainerStyle<'a> = &'a Style where
Self: 'a;

type GridItemStyle<'a> = &'a Style where
Self: 'a;

fn get_grid_container_style(&self, node_id: NodeId) -> Self::GridContainerStyle<'_> {
&self.node_from_id(node_id).style
}

fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::GridItemStyle<'_> {
&self.node_from_id(child_node_id).style
}
}

fn main() -> Result<(), taffy::TaffyError> {
let mut root = Node::new_column(Style::DEFAULT);

Expand Down
39 changes: 38 additions & 1 deletion examples/custom_tree_owned_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ impl TraversePartialTree for StatelessLayoutTree {
impl TraverseTree for StatelessLayoutTree {}

impl LayoutPartialTree for StatelessLayoutTree {
fn get_style(&self, node_id: NodeId) -> &Style {
type CoreContainerStyle<'a> = &'a Style where
Self: 'a;

type CacheMut<'b> = &'b mut Cache where Self: 'b;

fn get_core_container_style(&self, node_id: NodeId) -> Self::CoreContainerStyle<'_> {
unsafe { &node_from_id(node_id).style }
}

Expand Down Expand Up @@ -166,6 +171,38 @@ impl LayoutPartialTree for StatelessLayoutTree {
}
}

impl taffy::LayoutFlexboxContainer for StatelessLayoutTree {
type FlexboxContainerStyle<'a> = &'a Style where
Self: 'a;

type FlexboxItemStyle<'a> = &'a Style where
Self: 'a;

fn get_flexbox_container_style(&self, node_id: NodeId) -> Self::FlexboxContainerStyle<'_> {
unsafe { &node_from_id(node_id).style }
}

fn get_flexbox_child_style(&self, child_node_id: NodeId) -> Self::FlexboxItemStyle<'_> {
unsafe { &node_from_id(child_node_id).style }
}
}

impl taffy::LayoutGridContainer for StatelessLayoutTree {
type GridContainerStyle<'a> = &'a Style where
Self: 'a;

type GridItemStyle<'a> = &'a Style where
Self: 'a;

fn get_grid_container_style(&self, node_id: NodeId) -> Self::GridContainerStyle<'_> {
unsafe { &node_from_id(node_id).style }
}

fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::GridItemStyle<'_> {
unsafe { &node_from_id(child_node_id).style }
}
}

impl RoundTree for StatelessLayoutTree {
fn get_unrounded_layout(&self, node_id: NodeId) -> &Layout {
unsafe { &node_from_id_mut(node_id).unrounded_layout }
Expand Down
39 changes: 38 additions & 1 deletion examples/custom_tree_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ impl taffy::TraversePartialTree for Tree {
impl taffy::TraverseTree for Tree {}

impl taffy::LayoutPartialTree for Tree {
fn get_style(&self, node_id: NodeId) -> &Style {
type CoreContainerStyle<'a> = &'a Style where
Self: 'a;

type CacheMut<'b> = &'b mut Cache where Self: 'b;

fn get_core_container_style(&self, node_id: NodeId) -> Self::CoreContainerStyle<'_> {
&self.node_from_id(node_id).style
}

Expand Down Expand Up @@ -175,6 +180,38 @@ impl taffy::LayoutPartialTree for Tree {
}
}

impl taffy::LayoutFlexboxContainer for Tree {
type FlexboxContainerStyle<'a> = &'a Style where
Self: 'a;

type FlexboxItemStyle<'a> = &'a Style where
Self: 'a;

fn get_flexbox_container_style(&self, node_id: NodeId) -> Self::FlexboxContainerStyle<'_> {
&self.node_from_id(node_id).style
}

fn get_flexbox_child_style(&self, child_node_id: NodeId) -> Self::FlexboxItemStyle<'_> {
&self.node_from_id(child_node_id).style
}
}

impl taffy::LayoutGridContainer for Tree {
type GridContainerStyle<'a> = &'a Style where
Self: 'a;

type GridItemStyle<'a> = &'a Style where
Self: 'a;

fn get_grid_container_style(&self, node_id: NodeId) -> Self::GridContainerStyle<'_> {
&self.node_from_id(node_id).style
}

fn get_grid_child_style(&self, child_node_id: NodeId) -> Self::GridItemStyle<'_> {
&self.node_from_id(child_node_id).style
}
}

impl taffy::RoundTree for Tree {
fn get_unrounded_layout(&self, node_id: NodeId) -> &Layout {
&self.node_from_id(node_id).unrounded_layout
Expand Down
Loading