Skip to content

Commit

Permalink
#315 Include original relative_transform in serialized output.
Browse files Browse the repository at this point in the history
  • Loading branch information
John O'Neil committed Aug 25, 2023
1 parent 350879c commit 215b76a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions crates/figma_import/src/toolkit_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ pub struct ViewStyle {
pub stroke: Stroke,
pub opacity: Option<f32>,
pub transform: Option<LayoutTransform>,
pub relative_transform: Option<LayoutTransform>,
pub text_align: TextAlign,
pub text_align_vertical: TextAlignVertical,
pub text_overflow: TextOverflow,
Expand Down Expand Up @@ -558,6 +559,7 @@ impl Default for ViewStyle {
stroke: Stroke::default(),
opacity: None,
transform: None,
relative_transform: None,
text_align: TextAlign::Left,
text_align_vertical: TextAlignVertical::Top,
text_overflow: TextOverflow::Clip,
Expand Down Expand Up @@ -645,6 +647,9 @@ impl ViewStyle {
if self.transform != other.transform {
delta.transform = other.transform;
}
if self.relative_transform != other.relative_transform {
delta.relative_transform = other.relative_transform;
}
if self.text_align != other.text_align {
delta.text_align = other.text_align;
}
Expand Down
25 changes: 23 additions & 2 deletions crates/figma_import/src/transform_flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,16 @@ fn compute_background(
}

// We have a 1:1 correspondence between Nodes and Views which is super nice.
// NOTE: We carry round the immediate parent AND a relative_transform parent as we visit nodes.
// The reason for this is that in some cases the `relative_transform` data is NOT relative to
// the immediate parent. Please see:
// https://www.figma.com/plugin-docs/api/properties/nodes-relativetransform/#:~:text=The%20relative%20transform%20of%20a,group%20or%20a%20boolean%20operation.
// So in other words, `parent` will always be this node's immediate parent. But `relative_transform_parent` may
// be a parent further up the tree.
fn visit_node(
node: &Node,
parent: Option<&Node>,
relative_transform_parent: Option<&Node>,
component_map: &HashMap<String, Component>,
component_set_map: &HashMap<String, ComponentSet>,
component_context: &mut ComponentContext,
Expand Down Expand Up @@ -615,7 +622,7 @@ fn visit_node(

// Check relative transform to see if there is a rotation.
if let Some(transform) = node.relative_transform {
let parent_bounding_box = parent.and_then(|p| p.absolute_bounding_box);
let parent_bounding_box = relative_transform_parent.and_then(|p| p.absolute_bounding_box);
let bounds = node.absolute_bounding_box;

fn get(transform: [[Option<f32>; 3]; 2], a: usize, b: usize) -> f32 {
Expand All @@ -633,6 +640,10 @@ fn visit_node(
get(transform, 0, 2),
get(transform, 1, 2),
);

// Provide an unaltered transform from the last relevant parent.
style.relative_transform = Some(r.clone());
// And an additional transform with translation removed.
let r = r.post_translate(euclid::vec3(
-(bounds.x() - parent_bounds.x()),
-(bounds.y() - parent_bounds.y()),
Expand Down Expand Up @@ -1228,15 +1239,24 @@ fn visit_node(
RenderMethod::None,
);

// If the current node is a Group or a Boolean operation, it does
// not become the new parent used for `relative_transform` calculations.
let relative_transform_node = match node.data {
NodeData::Group { .. } => relative_transform_parent,
NodeData::BooleanOperation { .. } => relative_transform_parent,
_ => Some(node),
};

// Iterate over our visible children, but not vectors because they always
// present their childrens content themselves (e.g.: they are boolean products
// present their children's content themselves (e.g.: they are boolean products
// of their children).
if node.vector().is_none() {
for child in node.children.iter() {
if child.visible {
view.add_child(visit_node(
child,
Some(node),
relative_transform_node,
component_map,
component_set_map,
component_context,
Expand Down Expand Up @@ -1267,6 +1287,7 @@ pub fn create_component_flexbox(
visit_node(
component,
None,
None,
component_map,
component_set_map,
component_context,
Expand Down

0 comments on commit 215b76a

Please sign in to comment.