Skip to content

Commit

Permalink
Read min/max from figma
Browse files Browse the repository at this point in the history
It used to be ignored
  • Loading branch information
yiqunw700 committed Sep 23, 2024
1 parent 7a5f080 commit 8eceaaf
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 6 deletions.
4 changes: 4 additions & 0 deletions crates/figma_import/src/figma_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,10 @@ pub struct Node {
pub stroke_cap: StrokeCap,
pub bound_variables: Option<BoundVariables>,
pub explicit_variable_modes: Option<HashMap<String, String>>,
pub min_width: Option<f32>,
pub min_height: Option<f32>,
pub max_width: Option<f32>,
pub max_height: Option<f32>,
}

impl Node {
Expand Down
28 changes: 24 additions & 4 deletions crates/figma_import/src/transform_flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ use dc_bundle::legacy_definition::view::text_style::{StyledTextRun, TextStyle};
use dc_bundle::legacy_definition::view::view::{RenderMethod, ScrollInfo, View};
use dc_bundle::legacy_definition::view::view_style::ViewStyle;
use log::error;

use unicode_segmentation::UnicodeSegmentation;

// If an Auto content preview widget specifies a "Hug contents" sizing policy, this
Expand Down Expand Up @@ -116,6 +117,13 @@ fn compute_layout(
style.layout_style.bounding_box.height = bounds.height();
}

if let Some(max_width) = node.max_width {
style.layout_style.max_width = DimensionProto::new_points(max_width);
}
if let Some(max_height) = node.max_height {
style.layout_style.max_height = DimensionProto::new_points(max_height);
}

// Frames can implement Auto Layout on their children.
if let Some(frame) = node.frame() {
style.layout_style.position_type = PositionType::Relative;
Expand Down Expand Up @@ -311,20 +319,32 @@ fn compute_layout(
style.layout_style.height = DimensionProto::new_auto();
}
if let Some(bounds) = node.absolute_bounding_box {
if !hug_width {
if let Some(min_width) = node.min_width {
error!("2. Node has min_width {:?}", min_width);
style.layout_style.min_width = DimensionProto::new_points(min_width)
} else if !hug_width {
style.layout_style.min_width = DimensionProto::new_points(bounds.width().ceil());
}
if !hug_height {
if let Some(min_height) = node.min_height {
error!("2. Node has min_height {:?}", min_height);
style.layout_style.min_height = DimensionProto::new_points(min_height)
} else if !hug_height {
style.layout_style.min_height = DimensionProto::new_points(bounds.height().ceil());
}
}

if let Some(size) = &node.size {
if size.is_valid() {
if !hug_width {
if let Some(min_width) = node.min_width {
error!("3. Node has min_width {:?}", min_width);
style.layout_style.min_width = DimensionProto::new_points(min_width)
} else if !hug_width {
style.layout_style.min_width = DimensionProto::new_points(size.x());
}
if !hug_height {
if let Some(min_height) = node.min_height {
error!("3. Node has min_height {:?}", min_height);
style.layout_style.min_height = DimensionProto::new_points(min_height)
} else if !hug_height {
style.layout_style.min_height = DimensionProto::new_points(size.y());
}
// Set fixed vector size
Expand Down
Binary file modified crates/figma_import/tests/layout-unit-tests.dcf
Binary file not shown.
15 changes: 14 additions & 1 deletion crates/layout/src/layout_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,43 @@
use crate::into_taffy::{IntoTaffy, TryIntoTaffy};
use dc_bundle::definition::element::DimensionExt;
use dc_bundle::legacy_definition::layout::layout_style::LayoutStyle;
use log::error;

impl TryIntoTaffy<taffy::prelude::Style> for &LayoutStyle {
type Error = dc_bundle::Error;
fn try_into_taffy(self) -> Result<taffy::prelude::Style, Self::Error> {
let mut tstyle = taffy::prelude::Style::default();

tstyle.padding = (&self.padding).try_into_taffy()?;

tstyle.flex_grow = self.flex_grow;
tstyle.flex_shrink = self.flex_shrink;
tstyle.flex_basis = (&self.flex_basis).try_into_taffy()?;
tstyle.gap.width = (&self.item_spacing).into_taffy();
tstyle.gap.height = (&self.item_spacing).into_taffy();

tstyle.align_content = Some((&self.align_content).into_taffy());
error!("Into taffy align_content {:?}", &self.align_content);
tstyle.justify_content = Some((&self.justify_content).into_taffy());
error!("Into taffy justify_content {:?}", &self.justify_content);
tstyle.align_items = Some((&self.align_items).into_taffy());
error!("Into taffy align_items {:?}", &self.align_items);
tstyle.flex_direction = (&self.flex_direction).into_taffy();
error!("Into taffy flex_direction {:?}", &self.flex_direction);
tstyle.align_self = (&self.align_self).into_taffy();
error!("Into taffy align_self {:?}", &self.align_self);

tstyle.size.width = (&self.width).try_into_taffy()?;
error!("Into taffy width {:?}", &self.width);
tstyle.size.height = (&self.height).try_into_taffy()?;
error!("Into taffy height {:?}", &self.height);
tstyle.min_size.width = (&self.min_width).try_into_taffy()?;
error!("Into taffy min_width {:?}", &self.min_width);
tstyle.min_size.height = (&self.min_height).try_into_taffy()?;
error!("Into taffy min_height {:?}", &self.min_height);
tstyle.max_size.width = (&self.max_width).try_into_taffy()?;
error!("Into taffy max_width {:?}", &self.max_width);
tstyle.max_size.height = (&self.max_height).try_into_taffy()?;
error!("Into taffy max_height {:?}", &self.max_height);

// If we have a fixed size, use the bounding box since that takes into
// account scale and rotation, and disregard min/max sizes.
Expand All @@ -49,11 +60,13 @@ impl TryIntoTaffy<taffy::prelude::Style> for &LayoutStyle {
tstyle.size.width = taffy::prelude::Dimension::Points(self.bounding_box.width);
tstyle.min_size.width = taffy::prelude::Dimension::Auto;
tstyle.max_size.width = taffy::prelude::Dimension::Auto;
error!("Into taffy override max/min width to auto");
}
if self.height.is_points()? {
tstyle.size.height = taffy::prelude::Dimension::Points(self.bounding_box.height);
tstyle.min_size.height = taffy::prelude::Dimension::Auto;
tstyle.max_size.height = taffy::prelude::Dimension::Auto;
error!("Into taffy override max/min height to auto");
}

tstyle.position = (&self.position_type).into_taffy();
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ val EXAMPLES: ArrayList<Triple<String, @Composable () -> Unit, String?>> =
Triple("HelloVersion", { HelloVersion() }, HelloVersionDoc.javaClass.name),
// Alphabetically ordered and trying to put similar tests together...
Triple("Alignment", { AlignmentTest() }, AlignmentTestDoc.javaClass.name),
Triple("AutoLayout", { AutoLayoutTest() }, AutoLayoutTestDoc.javaClass.name),
Triple("Battleship", { BattleshipTest() }, BattleshipDoc.javaClass.name),
Triple("Blend Modes", { BlendModeTest() }, BlendModeTestDoc.javaClass.name),
Triple("Component Replace", { ComponentReplaceTest() }, ComponentReplaceDoc.javaClass.name),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.android.designcompose.testapp.validation.examples

import androidx.compose.runtime.Composable
import com.android.designcompose.annotation.DesignComponent
import com.android.designcompose.annotation.DesignDoc

@DesignDoc(id = "ArHVZAQIsKf2B9mATnVvLX")
interface AutoLayoutTest {
@DesignComponent(node = "#root") fun main()
}

@Composable
fun AutoLayoutTest() {
AutoLayoutTestDoc.main()
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class InternalGradlePlugin : Plugin<Project> {
test.inputs.properties(mapOf("isFetch" to isFetch))
test.outputs.doNotCacheIf("Always fetch DCF files") { isFetch.get() }


test.doFirst {
if (isFetch.get()) {
test.useJUnit {
Expand Down
Binary file not shown.

0 comments on commit 8eceaaf

Please sign in to comment.