Skip to content

Commit

Permalink
Support parsing corner radius from Figma as a variable hash map
Browse files Browse the repository at this point in the history
  • Loading branch information
rylin8 authored and timothyfroehlich committed Nov 12, 2024
1 parent bc7e0b7 commit 3bc306a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 6 deletions.
13 changes: 13 additions & 0 deletions crates/figma_import/src/figma_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,19 @@ impl BoundVariables {
}
None
}

pub(crate) fn get_var_from_hash(&self, hash_name: &str, var_name: &str) -> Option<String> {
let var_hash = self.variables.get(hash_name);
if let Some(var) = var_hash {
if let VariableAliasOrList::Map(map) = var {
let var = map.get(var_name);
if let Some(var) = var {
return Some(var.id.clone());
}
}
}
None
}
}

// We use the "untagged" serde attribute because the value of a variable is
Expand Down
43 changes: 37 additions & 6 deletions crates/figma_import/src/transform_flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1464,19 +1464,50 @@ fn visit_node(
// Collect the corner radii values to be saved into the view. If the corner radii are set
// to variables, they will be set to NumOrVar::Var. Otherwise they will be set to NumOrVar::Num.
let corner_radius = if let Some(vars) = &node.bound_variables {
let top_left = NumOrVarType::from_var(vars, "topLeftRadius", corner_radius_values[0]);
let top_right = NumOrVarType::from_var(vars, "topRightRadius", corner_radius_values[1]);
let bottom_right =
NumOrVarType::from_var(vars, "bottomRightRadius", corner_radius_values[2]);
let bottom_left = NumOrVarType::from_var(vars, "bottomLeftRadius", corner_radius_values[3]);
let corner_values = if vars.has_var("rectangleCornerRadii") {
(
NumOrVarType::from_var_hash(
vars,
"rectangleCornerRadii",
"RECTANGLE_TOP_LEFT_CORNER_RADIUS",
corner_radius_values[0],
),
NumOrVarType::from_var_hash(
vars,
"rectangleCornerRadii",
"RECTANGLE_TOP_RIGHT_CORNER_RADIUS",
corner_radius_values[1],
),
NumOrVarType::from_var_hash(
vars,
"rectangleCornerRadii",
"RECTANGLE_BOTTOM_LEFT_CORNER_RADIUS",
corner_radius_values[2],
),
NumOrVarType::from_var_hash(
vars,
"rectangleCornerRadii",
"RECTANGLE_BOTTOM_RIGHT_CORNER_RADIUS",
corner_radius_values[3],
),
)
} else {
(
NumOrVarType::from_var(vars, "topLeftRadius", corner_radius_values[0]),
NumOrVarType::from_var(vars, "topRightRadius", corner_radius_values[1]),
NumOrVarType::from_var(vars, "bottomRightRadius", corner_radius_values[2]),
NumOrVarType::from_var(vars, "bottomLeftRadius", corner_radius_values[3]),
)
};
if vars.has_var("topLeftRadius")
|| vars.has_var("topRightRadius")
|| vars.has_var("bottomRightRadius")
|| vars.has_var("bottomLeftRadius")
|| vars.has_var("rectangleCornerRadii")
{
has_corner_radius = true;
}
[top_left, top_right, bottom_right, bottom_left]
[corner_values.0, corner_values.1, corner_values.2, corner_values.3]
} else {
[
NumOrVarType::Num(corner_radius_values[0]),
Expand Down
29 changes: 29 additions & 0 deletions crates/figma_import/src/variable_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ pub(crate) trait FromFigmaVar<VarType> {
var_name: &str,
var_value: VarType,
) -> Self;

fn from_var_hash(
bound_variables: &figma_schema::BoundVariables,
hash_name: &str,
var_name: &str,
var_value: VarType,
) -> Self;
}
// Create a NumOrVar from Figma variable name and number value
impl FromFigmaVar<f32> for NumOrVarType {
Expand All @@ -46,6 +53,19 @@ impl FromFigmaVar<f32> for NumOrVarType {
NumOrVarType::Num(var_value)
}
}
fn from_var_hash(
bound_variables: &figma_schema::BoundVariables,
hash_name: &str,
var_name: &str,
var_value: f32,
) -> Self {
let var = bound_variables.get_var_from_hash(hash_name, var_name);
if let Some(var) = var {
NumOrVarType::Var(NumVar { id: var, fallback: var_value })
} else {
NumOrVarType::Num(var_value)
}
}
}
// Create a ColorOrVar from Figma variable name and color value
impl FromFigmaVar<&FloatColor> for ColorOrVar {
Expand All @@ -61,6 +81,15 @@ impl FromFigmaVar<&FloatColor> for ColorOrVar {
ColorOrVar::new_color(color.into())
}
}
fn from_var_hash(
_bound_variables: &figma_schema::BoundVariables,
_hash_name: &str,
_var_name: &str,
color: &FloatColor,
) -> Self {
// Currently, no color variables from a hash are yet supported
ColorOrVar::new_color(color.into())
}
}

// Create a VariableValue from figma_schema::VariableValue
Expand Down
Binary file not shown.

0 comments on commit 3bc306a

Please sign in to comment.