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

Read min/max from figma #1679

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1028,6 +1028,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
1 change: 1 addition & 0 deletions crates/figma_import/src/tools/fetch_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ pub fn fetch_layout(args: Args) -> Result<(), ConvertError> {
eprintln!("Warning: {error}");
}

// Take the first argument as the root node
let stage = views.get(&NodeQuery::NodeName(args.nodes.get(0).expect("NOT EMPTY").to_string()));
if let Some(stage) = stage {
let mut id = 0;
Expand Down
39 changes: 30 additions & 9 deletions crates/figma_import/src/transform_flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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 @@ -119,6 +120,20 @@ fn compute_layout(
Some(Size { width: bounds.width(), 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);
}

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

// Frames can implement Auto Layout on their children.
if let Some(frame) = node.frame() {
style.layout_style.position_type = match frame.layout_positioning {
Expand Down Expand Up @@ -218,7 +233,7 @@ fn compute_layout(
}
figma_schema::LayoutSizingMode::Auto => DimensionProto::new_auto(),
};
if hug_width {
if hug_width && node.min_width.is_none() {
style.layout_style.min_width = DimensionProto::new_auto();
}
}
Expand All @@ -239,7 +254,7 @@ fn compute_layout(
}
figma_schema::LayoutSizingMode::Auto => DimensionProto::new_auto(),
};
if hug_height {
if hug_height && node.min_height.is_none() {
style.layout_style.min_height = DimensionProto::new_auto();
}
}
Expand Down Expand Up @@ -319,20 +334,20 @@ fn compute_layout(
style.layout_style.height = DimensionProto::new_auto();
}
if let Some(bounds) = node.absolute_bounding_box {
if !hug_width {
if !hug_width && node.min_width.is_none() {
style.layout_style.min_width = DimensionProto::new_points(bounds.width().ceil());
}
if !hug_height {
if !hug_height && node.min_height.is_none() {
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 !hug_width && node.min_width.is_none() {
style.layout_style.min_width = DimensionProto::new_points(size.x());
}
if !hug_height {
if !hug_height && node.min_height.is_none() {
style.layout_style.min_height = DimensionProto::new_points(size.y());
}
// Set fixed vector size
Expand Down Expand Up @@ -372,7 +387,9 @@ fn compute_layout(
style.layout_style.height = DimensionProto::new_auto();
}
figma_schema::TextAutoResize::WidthAndHeight => {
style.layout_style.min_width = DimensionProto::new_auto();
if node.min_width.is_none() {
style.layout_style.min_width = DimensionProto::new_auto();
}
style.layout_style.width = DimensionProto::new_auto();
style.layout_style.height = DimensionProto::new_auto();
}
Expand Down Expand Up @@ -455,7 +472,9 @@ fn compute_layout(
right / parent_bounds.width().ceil()
});
style.layout_style.width = DimensionProto::new_auto();
style.layout_style.min_width = DimensionProto::new_auto();
if node.min_width.is_none() {
style.layout_style.min_width = DimensionProto::new_auto();
}
}
}

Expand Down Expand Up @@ -501,7 +520,9 @@ fn compute_layout(
style.layout_style.top = DimensionProto::new_percent(top);
style.layout_style.bottom = DimensionProto::new_percent(bottom);
style.layout_style.height = DimensionProto::new_auto();
style.layout_style.min_height = DimensionProto::new_auto();
if node.min_height.is_none() {
style.layout_style.min_height = DimensionProto::new_auto();
}
}
}
}
Expand Down
Binary file modified crates/figma_import/tests/layout-unit-tests.dcf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ 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 MinMax",
{ AutoLayoutMinMaxTest() },
AutoLayoutMinMaxTestDoc.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 AutoLayoutMinMaxTest {
@DesignComponent(node = "#root") fun main()
}

@Composable
fun AutoLayoutMinMaxTest() {
AutoLayoutMinMaxTestDoc.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.
Binary file not shown.