Skip to content

Commit

Permalink
Move Stroke, Background to proto generated files (#1684)
Browse files Browse the repository at this point in the history
Created ProtoUtils.kt to hold some convenience functions for the
serde-generated proto-generated files. Created ProtoEnums.kt to convert
between enum values and hard coded integers whose association was lost
in the serdegen conversion. This can be removed and fixed when we
migrate to using proto-generated Kotlin. This changed required changing
usage of color_or_var::ColorOrVar to the outer struct ColorOrVar.

Fixes #1667, #1672
  • Loading branch information
rylin8 authored Sep 27, 2024
1 parent de9f0a5 commit 17e7bf7
Show file tree
Hide file tree
Showing 81 changed files with 552 additions and 410 deletions.
44 changes: 44 additions & 0 deletions crates/dc_bundle/src/definition/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::definition::element::path::WindingRule;
use crate::Error;
use std::fmt;
use std::fmt::{Debug, Display, Formatter};
use std::hash::{Hash, Hasher};

include!(concat!(env!("OUT_DIR"), "/designcompose.definition.element.rs"));

Expand Down Expand Up @@ -366,3 +367,46 @@ impl Path {
self
}
}

// Implement the Eq and Hash traits so that ImageKey can be used as a hash table key
impl Eq for ImageKey {}
impl Hash for ImageKey {
fn hash<H: Hasher>(&self, state: &mut H) {
self.key.hash(state);
}
}
impl ImageKey {
pub fn new(str: String) -> Self {
ImageKey { key: str }
}
}

impl Background {
pub fn new(bg_type: background::BackgroundType) -> Self {
Background { background_type: Some(bg_type) }
}
pub fn is_some(&self) -> bool {
if let Some(bg) = &self.background_type {
match bg {
background::BackgroundType::None(_) => false,
_ => true,
}
} else {
false
}
}
}

impl ColorOrVar {
pub fn new_color(color: Color) -> Self {
ColorOrVar { color_or_var_type: Some(color_or_var::ColorOrVarType::Color(color)) }
}
pub fn new_var(id: String, fallback: Option<Color>) -> Self {
ColorOrVar {
color_or_var_type: Some(color_or_var::ColorOrVarType::Var(color_or_var::ColorVar {
id,
fallback,
})),
}
}
}
6 changes: 6 additions & 0 deletions crates/dc_bundle/src/definition/modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@
include!(concat!(env!("OUT_DIR"), "/designcompose.definition.modifier.rs"));
pub mod affine_transform;
pub mod layout_transform;

impl FilterOp {
pub fn new(op_type: filter_op::FilterOpType) -> Self {
FilterOp { filter_op_type: Some(op_type) }
}
}
2 changes: 1 addition & 1 deletion crates/dc_bundle/src/legacy_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use std::sync::Arc;
// module for the generated protobuf files to `proto`, so that all of the protobuf-generated types
// inside `legacy_definition` must be prepended with `proto::`
pub(crate) use crate::definition as proto;
use crate::definition::element::ImageKey;
use crate::definition::element::VariableMap;
use crate::legacy_definition::element::background::ImageKey;
use crate::legacy_definition::element::node::NodeQuery;

pub mod element;
Expand Down
1 change: 0 additions & 1 deletion crates/dc_bundle/src/legacy_definition/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

pub mod background;
pub mod font;
pub mod geometry;
pub mod node;
Expand Down
102 changes: 0 additions & 102 deletions crates/dc_bundle/src/legacy_definition/element/background.rs

This file was deleted.

48 changes: 0 additions & 48 deletions crates/dc_bundle/src/legacy_definition/element/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

use crate::legacy_definition::element::background::Background;
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)]
Expand All @@ -28,50 +27,3 @@ impl Default for LineHeight {
LineHeight::Percent(1.0)
}
}

/// How is a stroke aligned to its containing box?
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)]
pub enum StrokeAlign {
/// The stroke is entirely within the containing view. The stroke's outer edge matches the
/// outer edge of the containing view.
Inside,
/// The stroke is centered on the edge of the containing view, and extends into the view
/// on the inside, and out of the view on the outside.
Center,
/// The stroke is entirely outside of the view. The stroke's inner edge is the outer edge
/// of the containing view.
Outside,
}

/// Stroke weight is either a uniform value for all sides, or individual
/// weights for each side.
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)]
pub enum StrokeWeight {
/// One weight is used for all sides.
Uniform(f32),
/// Individual weights for each side (typically only applied on boxes).
Individual { top: f32, right: f32, bottom: f32, left: f32 },
}

/// A stroke is similar to a border, except that it does not change layout (a border insets
/// the children by the border size), it may be inset, centered or outset from the view bounds
/// and there can be multiple strokes on a view.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Stroke {
/// The alignment of strokes on this view.
pub stroke_align: StrokeAlign,
/// The thickness of strokes on this view (in pixels).
pub stroke_weight: StrokeWeight,
/// The stroke colors/fills
pub strokes: Vec<Background>,
}

impl Default for Stroke {
fn default() -> Self {
Stroke {
stroke_align: StrokeAlign::Center,
stroke_weight: StrokeWeight::Uniform(0.0),
strokes: Vec::new(),
}
}
}
1 change: 0 additions & 1 deletion crates/dc_bundle/src/legacy_definition/modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@
*/

pub mod blend;
pub mod filter;
pub mod shadow;
pub mod text;
29 changes: 0 additions & 29 deletions crates/dc_bundle/src/legacy_definition/modifier/filter.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/dc_bundle/src/legacy_definition/modifier/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

use crate::definition::element::color_or_var::ColorOrVar;
use crate::definition::element::ColorOrVar;
use serde::{Deserialize, Serialize};

/// Shadows can be applied to the border box, or the stroke box.
Expand Down
13 changes: 8 additions & 5 deletions crates/dc_bundle/src/legacy_definition/view/node_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@
*/

use crate::definition::element::num_or_var::NumOrVar;
use crate::definition::element::{FontFeature, FontStyle, Hyperlink, Size, TextDecoration};
use crate::definition::element::Background;
use crate::definition::element::Stroke;
use crate::definition::element::{
background, FontFeature, FontStyle, Hyperlink, Size, TextDecoration,
};
use crate::definition::layout::FlexWrap;
use crate::definition::modifier::FilterOp;
use crate::definition::modifier::LayoutTransform;
use crate::legacy_definition::element::background::Background;
use crate::legacy_definition::element::font::{FontStretch, FontWeight};
use crate::legacy_definition::element::path::{LineHeight, Stroke};
use crate::legacy_definition::element::path::LineHeight;
use crate::legacy_definition::interaction::pointer::PointerEvents;
use crate::legacy_definition::layout::grid::{GridLayoutType, GridSpan};
use crate::legacy_definition::layout::positioning::{LayoutSizing, Overflow};
use crate::legacy_definition::modifier::blend::BlendMode;
use crate::legacy_definition::modifier::filter::FilterOp;
use crate::legacy_definition::modifier::shadow::{BoxShadow, TextShadow};
use crate::legacy_definition::modifier::text::{TextAlign, TextAlignVertical, TextOverflow};
use crate::legacy_definition::plugin::meter_data::MeterData;
Expand Down Expand Up @@ -109,7 +112,7 @@ pub struct NodeStyle {
impl Default for NodeStyle {
fn default() -> NodeStyle {
NodeStyle {
text_color: Background::None,
text_color: Background::new(background::BackgroundType::None(())),
font_size: NumOrVar::Num(18.0),
font_family: None,
font_weight: FontWeight::NORMAL,
Expand Down
8 changes: 5 additions & 3 deletions crates/dc_bundle/src/legacy_definition/view/text_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* limitations under the License.
*/

use crate::definition::element::color_or_var::ColorOrVar;
use crate::definition::element::num_or_var::NumOrVar;
use crate::definition::element::Color;
use crate::definition::element::ColorOrVar;
use crate::definition::element::{background, Background};
use crate::definition::element::{FontFeature, FontStyle, Hyperlink, TextDecoration};
use crate::legacy_definition::element::background::Background;
use crate::legacy_definition::element::font::{FontStretch, FontWeight};
use crate::legacy_definition::element::path::LineHeight;
use serde::{Deserialize, Serialize};
Expand All @@ -45,7 +45,9 @@ pub struct TextStyle {
impl Default for TextStyle {
fn default() -> Self {
TextStyle {
text_color: Background::Solid(ColorOrVar::Color(Color::BLACK)),
text_color: Background::new(background::BackgroundType::Solid(ColorOrVar::new_color(
Color::BLACK,
))),
font_size: NumOrVar::Num(18.0),
font_family: None,
font_weight: FontWeight::NORMAL,
Expand Down
2 changes: 1 addition & 1 deletion crates/figma_import/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::{
transform_flexbox::create_component_flexbox,
variable_utils::create_variable,
};
use dc_bundle::legacy_definition::element::background::ImageKey;
use dc_bundle::definition::element::ImageKey;
use dc_bundle::legacy_definition::view::component::{ComponentContentOverride, ComponentOverrides};
use dc_bundle::legacy_definition::view::view::{View, ViewData};
use dc_bundle::legacy_definition::EncodedImageMap;
Expand Down
4 changes: 2 additions & 2 deletions crates/figma_import/src/image_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::{
use crate::error::Error;
use crate::fetch::ProxyConfig;
use crate::figma_schema::{Paint, Transform};
use dc_bundle::legacy_definition::element::background::ImageKey;
use dc_bundle::definition::element::ImageKey;
use dc_bundle::legacy_definition::EncodedImageMap;
use image::DynamicImage;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -177,7 +177,7 @@ impl ImageContext {
url,
&self.proxy_config,
) {
url.unwrap_or(&None).as_ref().map(|url_string| ImageKey::new(url_string.clone()))
url.unwrap_or(&None).as_ref().map(|url_string| ImageKey { key: url_string.clone() })
} else {
None
}
Expand Down
3 changes: 1 addition & 2 deletions crates/figma_import/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ pub use fetch::{fetch_doc, ConvertRequest, ConvertResponse, ProxyConfig};
pub use image_context::ImageContextSession;

// Internal convenience
pub use dc_bundle::definition::element::Color;
pub use dc_bundle::legacy_definition::element::background::ImageKey;
pub use dc_bundle::definition::element::{Color, ImageKey};
pub use dc_bundle::legacy_definition::element::node::NodeQuery;
pub use dc_bundle::legacy_definition::view::view::View;
pub use dc_bundle::legacy_definition::view::view::ViewData;
Expand Down
Loading

0 comments on commit 17e7bf7

Please sign in to comment.