Skip to content

Commit

Permalink
Refactor: Move meter data to definition package (#1782)
Browse files Browse the repository at this point in the history
<!-- start git-machete generated -->

# Based on PR #1774

## Chain of upstream PRs as of 2024-11-14

* PR #1774:
  `main` ← `wb/froeht/protoconv-layout-positioning`

  * **PR #1782 (THIS ONE)**:
`wb/froeht/protoconv-layout-positioning` ← `wb/froeht/1731-meter-data`

<!-- end git-machete generated -->

Moves the meter data types from the legacy_definition package to the
definition package.
This change simplifies the codebase by removing the redundant
legacy_definition package.

Additionally, this change refactors the DesignCompose code to use the
new meter data types.
  • Loading branch information
timothyfroehlich authored Nov 15, 2024
1 parent 6960284 commit 3709859
Show file tree
Hide file tree
Showing 47 changed files with 156 additions and 164 deletions.
1 change: 0 additions & 1 deletion crates/dc_bundle/src/legacy_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use crate::definition::element::VariableMap;
use crate::legacy_definition::element::node::NodeQuery;

pub mod element;
pub mod plugin;
pub mod view;

/// EncodedImageMap contains a mapping from ImageKey to network bytes. It can create an
Expand Down
17 changes: 0 additions & 17 deletions crates/dc_bundle/src/legacy_definition/plugin.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/dc_bundle/src/legacy_definition/view/node_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::definition::modifier::{
BlendMode, FilterOp, TextAlign, TextAlignVertical, TextOverflow,
};
use crate::definition::modifier::{BoxShadow, LayoutTransform, TextShadow};
use crate::legacy_definition::plugin::meter_data::MeterData;
use crate::definition::plugin::meter_data::MeterData;
use serde::{Deserialize, Serialize};

#[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion crates/figma_import/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ mod extended_layout_schema;
mod fetch;
mod figma_schema;
mod image_context;
pub mod meter_schema;
pub mod reaction_schema;
pub mod toolkit_style;
pub mod tools;
mod transform_flexbox;
mod variable_utils;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
/*
* Copyright 2024 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.
*/
// 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.

//! `toolkit_style` contains all of the style-related types that `toolkit_schema::View`
//! uses.
use crate::figma_schema;

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RotationMeterData {
pub struct RotationMeterJson {
pub enabled: bool,
pub start: f32,
pub end: f32,
Expand All @@ -28,7 +31,7 @@ pub struct RotationMeterData {

#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ArcMeterData {
pub struct ArcMeterJson {
pub enabled: bool,
pub start: f32,
pub end: f32,
Expand All @@ -39,7 +42,7 @@ pub struct ArcMeterData {

#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ProgressBarMeterData {
pub struct ProgressBarMeterJson {
pub enabled: bool,
pub discrete: bool,
pub discrete_value: f32,
Expand All @@ -53,7 +56,7 @@ pub struct ProgressBarMeterData {

#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ProgressMarkerMeterData {
pub struct ProgressMarkerMeterJson {
pub enabled: bool,
pub discrete: bool,
pub discrete_value: f32,
Expand All @@ -69,22 +72,23 @@ pub struct ProgressMarkerMeterData {
pub end_y: f32,
}

// Schema for progress vector data that we write to serialized data
// Schema for progress vector data that we read from Figma plugin data
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ProgressVectorMeterData {
pub struct ProgressVectorMeterJson {
pub enabled: bool,
pub discrete: bool,
pub discrete_value: f32,
pub paths: Vec<figma_schema::Path>,
}

// Schema for dials & gauges data that we write to serialized data
// Schema for dials & gauges Figma plugin data
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum MeterData {
ArcData(ArcMeterData),
RotationData(RotationMeterData),
ProgressBarData(ProgressBarMeterData),
ProgressMarkerData(ProgressMarkerMeterData),
ProgressVectorData(ProgressVectorMeterData),
pub enum MeterJson {
ArcData(ArcMeterJson),
RotationData(RotationMeterJson),
ProgressBarData(ProgressBarMeterJson),
ProgressMarkerData(ProgressMarkerMeterJson),
ProgressVectorData(ProgressVectorMeterJson),
}
14 changes: 5 additions & 9 deletions crates/figma_import/src/reflection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,19 @@ pub fn registry() -> serde_reflection::Result<serde_reflection::Registry> {
.trace_type::<dc_bundle::definition::layout::GridLayoutType>(&samples)
.expect("couldn't trace GridLayoutType");
tracer
.trace_type::<dc_bundle::legacy_definition::plugin::meter_data::RotationMeterData>(&samples)
.trace_type::<dc_bundle::definition::plugin::RotationMeterData>(&samples)
.expect("couldn't trace RotationMeterData");
tracer
.trace_type::<dc_bundle::legacy_definition::plugin::meter_data::ArcMeterData>(&samples)
.trace_type::<dc_bundle::definition::plugin::ArcMeterData>(&samples)
.expect("couldn't trace ArcMeterData");
tracer
.trace_type::<dc_bundle::legacy_definition::plugin::meter_data::ProgressBarMeterData>(
&samples,
)
.trace_type::<dc_bundle::definition::plugin::ProgressBarMeterData>(&samples)
.expect("couldn't trace ProgressBarMeterData");
tracer
.trace_type::<dc_bundle::legacy_definition::plugin::meter_data::ProgressVectorMeterData>(
&samples,
)
.trace_type::<dc_bundle::definition::plugin::ProgressVectorMeterData>(&samples)
.expect("couldn't trace ProgressVectorMeterData");
tracer
.trace_type::<dc_bundle::legacy_definition::plugin::meter_data::MeterData>(&samples)
.trace_type::<dc_bundle::definition::plugin::meter_data::MeterData>(&samples)
.expect("couldn't trace MeterData");
tracer
.trace_type::<dc_bundle::definition::layout::PositionType>(&samples)
Expand Down
62 changes: 0 additions & 62 deletions crates/figma_import/src/toolkit_style.rs

This file was deleted.

85 changes: 69 additions & 16 deletions crates/figma_import/src/transform_flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
use std::collections::HashMap;
use std::f32::consts::PI;

use crate::toolkit_style::MeterDataSchema;
use crate::meter_schema::MeterJson;

use crate::Error;
use crate::{
component_context::ComponentContext,
extended_layout_schema::{ExtendedAutoLayout, LayoutType, SizePolicy},
figma_schema,
image_context::ImageContext,
variable_utils::{bound_variables_color, FromFigmaVar},
};
use crate::{figma_schema, Error};
use dc_bundle::definition::element::{
view_shape, DimensionProto, DimensionRect, DimensionRectExt, FontFeature, FontStyle,
FontWeight, Size, ViewShape,
FontWeight, Path, Size, ViewShape,
};

use crate::figma_schema::LayoutPositioning;
Expand All @@ -39,7 +40,6 @@ use dc_bundle::definition::element::dimension_proto::Dimension;
use dc_bundle::definition::element::line_height::LineHeight;
use dc_bundle::definition::element::num_or_var::NumOrVarType;
use dc_bundle::definition::element::view_shape::RoundRect;
use dc_bundle::definition::element::Path;
use dc_bundle::definition::element::{
background, stroke_weight, Background, StrokeAlign, StrokeWeight,
};
Expand All @@ -52,8 +52,12 @@ use dc_bundle::definition::modifier::{
filter_op, BoxShadow, FilterOp, TextAlign, TextAlignVertical, TextOverflow, TextShadow,
};
use dc_bundle::definition::modifier::{BlendMode, LayoutTransform};
use dc_bundle::definition::plugin::FrameExtras;
use dc_bundle::definition::plugin::{
ArcMeterData, FrameExtras, ProgressBarMeterData, ProgressMarkerMeterData,
ProgressVectorMeterData, RotationMeterData,
};

use dc_bundle::definition::plugin::meter_data::MeterData;
use dc_bundle::legacy_definition::view::component::ComponentInfo;
use dc_bundle::legacy_definition::view::text_style::{StyledTextRun, TextStyle};
use dc_bundle::legacy_definition::view::view::{RenderMethod, ScrollInfo, View};
Expand Down Expand Up @@ -1562,18 +1566,67 @@ fn visit_node(
// a type of meter (dials/gauges/progress bars)
if let Some(vsw_data) = plugin_data {
if let Some(data) = vsw_data.get("vsw-meter-data") {
let meter_data: Option<MeterDataSchema> = serde_json::from_str(data.as_str()).ok();
if let Some(meter_data) = meter_data {
if let MeterDataSchema::ProgressVectorData(vector_data) = &meter_data {
// If this is a progress vector node, we read in data as a ProgressVectorMeterDataSchema,
// which contains vector drawing instructions as a string. We convert this into vector
// drawing instructions in a ProgressVectorMeterData struct and replace the normal stroke
// vector data with it.
if vector_data.enabled {
stroke_paths = vector_data.paths.iter().filter_map(parse_path).collect();
}
let meter_data = serde_json::from_str::<MeterJson>(data.as_str());
match meter_data {
Ok(meter_data) => {
style.node_style.meter_data = Some(match meter_data {
MeterJson::ArcData(data) => MeterData::ArcData(ArcMeterData {
enabled: data.enabled,
start: data.start,
end: data.end,
discrete: data.discrete,
discrete_value: data.discrete_value,
corner_radius: data.corner_radius,
}),
MeterJson::RotationData(data) => {
MeterData::RotationData(RotationMeterData {
enabled: data.enabled,
start: data.start,
end: data.end,
discrete: data.discrete,
discrete_value: data.discrete_value,
})
}
MeterJson::ProgressBarData(data) => {
MeterData::ProgressBarData(ProgressBarMeterData {
enabled: data.enabled,
discrete: data.discrete,
discrete_value: data.discrete_value,
vertical: data.vertical,
end_x: data.end_x,
end_y: data.end_y,
})
}
MeterJson::ProgressMarkerData(data) => {
MeterData::ProgressMarkerData(ProgressMarkerMeterData {
enabled: data.enabled,
discrete: data.discrete,
discrete_value: data.discrete_value,
vertical: data.vertical,
start_x: data.start_x,
end_x: data.end_x,
start_y: data.start_y,
end_y: data.end_y,
})
}
MeterJson::ProgressVectorData(data) => {
// ProgressVectors contain vector drawing instructions as a string. We convert this into vector
// drawing instructions in a ProgressVectorMeterData struct and replace the normal stroke
// vector data with it.
if data.enabled {
stroke_paths = data.paths.iter().filter_map(parse_path).collect();
}
MeterData::ProgressVectorData(ProgressVectorMeterData {
enabled: data.enabled,
discrete: data.discrete,
discrete_value: data.discrete_value,
})
}
})
}
Err(e) => {
error!("Failed to parse meter data: {}", e);
}
style.node_style.meter_data = Some(meter_data.into());
}
}
}
Expand Down
Binary file modified crates/figma_import/tests/layout-unit-tests.dcf
Binary file not shown.
Loading

0 comments on commit 3709859

Please sign in to comment.