Skip to content

Commit

Permalink
Convert ViewStyle to Proto
Browse files Browse the repository at this point in the history
Fixes #1736
  • Loading branch information
timothyfroehlich committed Nov 25, 2024
1 parent 2aca348 commit a4ef01a
Show file tree
Hide file tree
Showing 80 changed files with 901 additions and 846 deletions.
1 change: 1 addition & 0 deletions .idea/runConfigurations/Figma_Fetch_Tests.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

220 changes: 218 additions & 2 deletions crates/dc_bundle/src/definition/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
* limitations under the License.
*/
use crate::definition::element::line_height::LineHeightType;
use crate::definition::element::num_or_var::NumOrVarType;
use crate::definition::element::{
background, Background, FontStretch, FontStyle, FontWeight, LineHeight, NumOrVar, Size, Stroke,
TextDecoration,
};
use crate::definition::interaction::PointerEvents;
use crate::definition::layout::{FlexWrap, LayoutSizing, Overflow};
use crate::definition::layout::{FlexWrap, LayoutSizing, LayoutStyle, Overflow};
use crate::definition::modifier::{BlendMode, TextAlign, TextAlignVertical, TextOverflow};

include!(concat!(env!("OUT_DIR"), "/designcompose.definition.view.rs"));
Expand Down Expand Up @@ -73,3 +72,220 @@ impl NodeStyle {
}
}
}

impl ViewStyle {
pub fn new_default() -> Self {
Self {
layout_style: Some(LayoutStyle::new_default()),
node_style: Some(NodeStyle::new_default()),
}
}
pub fn node_style(&self) -> &NodeStyle {
self.node_style.as_ref().expect("NodeStyle is required.")
}
pub fn node_style_mut(&mut self) -> &mut NodeStyle {
self.node_style.as_mut().expect("NodeStyle is required.")
}
pub fn layout_style(&self) -> &LayoutStyle {
self.layout_style.as_ref().expect("LayoutStyle is required.")
}
pub fn layout_style_mut(&mut self) -> &mut LayoutStyle {
self.layout_style.as_mut().expect("LayoutStyle is required.")
}
}

impl ViewStyle {
/// Compute the difference between this style and the given style, returning a style
/// that can be applied to this style to make it equal the given style using apply_non_default.
pub fn difference(&self, other: &ViewStyle) -> ViewStyle {
let mut delta = ViewStyle::new_default();
if self.node_style().text_color != other.node_style().text_color {
delta.node_style_mut().text_color = other.node_style().text_color.clone();
}
if self.node_style().font_size != other.node_style().font_size {
delta.node_style_mut().font_size = other.node_style().font_size.clone();
}
if self.node_style().font_family != other.node_style().font_family {
delta.node_style_mut().font_family = other.node_style().font_family.clone();
}
if self.node_style().font_weight != other.node_style().font_weight {
delta.node_style_mut().font_weight = other.node_style().font_weight.clone();
}
if self.node_style().font_style != other.node_style().font_style {
delta.node_style_mut().font_style = other.node_style().font_style;
}
if self.node_style().text_decoration != other.node_style().text_decoration {
delta.node_style_mut().text_decoration = other.node_style().text_decoration;
}
if self.node_style().letter_spacing != other.node_style().letter_spacing {
delta.node_style_mut().letter_spacing = other.node_style().letter_spacing;
}
if self.node_style().font_stretch != other.node_style().font_stretch {
delta.node_style_mut().font_stretch = other.node_style().font_stretch.clone();
}
if self.node_style().backgrounds != other.node_style().backgrounds {
delta.node_style_mut().backgrounds = other.node_style().backgrounds.clone();
}
if self.node_style().box_shadows != other.node_style().box_shadows {
delta.node_style_mut().box_shadows = other.node_style().box_shadows.clone();
}
if self.node_style().stroke != other.node_style().stroke {
delta.node_style_mut().stroke = other.node_style().stroke.clone();
}
if self.node_style().opacity != other.node_style().opacity {
delta.node_style_mut().opacity = other.node_style().opacity;
}
if self.node_style().transform != other.node_style().transform {
delta.node_style_mut().transform = other.node_style().transform;
}
if self.node_style().relative_transform != other.node_style().relative_transform {
delta.node_style_mut().relative_transform = other.node_style().relative_transform;
}
if self.node_style().text_align != other.node_style().text_align {
delta.node_style_mut().text_align = other.node_style().text_align;
}
if self.node_style().text_align_vertical != other.node_style().text_align_vertical {
delta.node_style_mut().text_align_vertical = other.node_style().text_align_vertical;
}
if self.node_style().text_overflow != other.node_style().text_overflow {
delta.node_style_mut().text_overflow = other.node_style().text_overflow;
}
if self.node_style().text_shadow != other.node_style().text_shadow {
delta.node_style_mut().text_shadow = other.node_style().text_shadow.clone();
}
if self.node_style().node_size != other.node_style().node_size {
delta.node_style_mut().node_size = other.node_style().node_size.clone();
}
if self.node_style().line_height != other.node_style().line_height {
delta.node_style_mut().line_height = other.node_style().line_height.clone();
}
if self.node_style().line_count != other.node_style().line_count {
delta.node_style_mut().line_count = other.node_style().line_count;
}
if self.node_style().font_features != other.node_style().font_features {
delta.node_style_mut().font_features = other.node_style().font_features.clone();
}
if self.node_style().filters != other.node_style().filters {
delta.node_style_mut().filters = other.node_style().filters.clone();
}
if self.node_style().backdrop_filters != other.node_style().backdrop_filters {
delta.node_style_mut().backdrop_filters = other.node_style().backdrop_filters.clone();
}
if self.node_style().blend_mode != other.node_style().blend_mode {
delta.node_style_mut().blend_mode = other.node_style().blend_mode;
}
if self.node_style().hyperlinks != other.node_style().hyperlinks {
delta.node_style_mut().hyperlinks = other.node_style().hyperlinks.clone();
}
if self.node_style().display_type != other.node_style().display_type {
delta.node_style_mut().display_type = other.node_style().display_type;
}
if self.layout_style().position_type != other.layout_style().position_type {
delta.layout_style_mut().position_type = other.layout_style().position_type;
}
if self.layout_style().flex_direction != other.layout_style().flex_direction {
delta.layout_style_mut().flex_direction = other.layout_style().flex_direction;
}
if self.node_style().flex_wrap != other.node_style().flex_wrap {
delta.node_style_mut().flex_wrap = other.node_style().flex_wrap;
}
if self.node_style().grid_layout_type != other.node_style().grid_layout_type {
delta.node_style_mut().grid_layout_type = other.node_style().grid_layout_type;
}
if self.node_style().grid_columns_rows != other.node_style().grid_columns_rows {
delta.node_style_mut().grid_columns_rows = other.node_style().grid_columns_rows;
}
if self.node_style().grid_adaptive_min_size != other.node_style().grid_adaptive_min_size {
delta.node_style_mut().grid_adaptive_min_size =
other.node_style().grid_adaptive_min_size;
}
if self.node_style().grid_span_contents != other.node_style().grid_span_contents {
delta.node_style_mut().grid_span_contents =
other.node_style().grid_span_contents.clone();
}
if self.node_style().overflow != other.node_style().overflow {
delta.node_style_mut().overflow = other.node_style().overflow;
}
if self.node_style().max_children != other.node_style().max_children {
delta.node_style_mut().max_children = other.node_style().max_children;
}
if self.node_style().overflow_node_id != other.node_style().overflow_node_id {
delta.node_style_mut().overflow_node_id = other.node_style().overflow_node_id.clone();
}
if self.node_style().overflow_node_name != other.node_style().overflow_node_name {
delta.node_style_mut().overflow_node_name =
other.node_style().overflow_node_name.clone();
}
if self.layout_style().align_items != other.layout_style().align_items {
delta.layout_style_mut().align_items = other.layout_style().align_items;
}
if self.layout_style().align_content != other.layout_style().align_content {
delta.layout_style_mut().align_content = other.layout_style().align_content;
}
if self.layout_style().justify_content != other.layout_style().justify_content {
delta.layout_style_mut().justify_content = other.layout_style().justify_content;
}
if self.layout_style().top != other.layout_style().top {
delta.layout_style_mut().top = other.layout_style().top;
}
if self.layout_style().left != other.layout_style().left {
delta.layout_style_mut().left = other.layout_style().left;
}
if self.layout_style().bottom != other.layout_style().bottom {
delta.layout_style_mut().bottom = other.layout_style().bottom;
}
if self.layout_style().right != other.layout_style().right {
delta.layout_style_mut().right = other.layout_style().right;
}
if self.layout_style().margin != other.layout_style().margin {
delta.layout_style_mut().margin = other.layout_style().margin.clone();
}
if self.layout_style().padding != other.layout_style().padding {
delta.layout_style_mut().padding = other.layout_style().padding.clone();
}
if self.layout_style().item_spacing != other.layout_style().item_spacing {
delta.layout_style_mut().item_spacing = other.layout_style().item_spacing.clone();
}
if self.node_style().cross_axis_item_spacing != other.node_style().cross_axis_item_spacing {
delta.node_style_mut().cross_axis_item_spacing =
other.node_style().cross_axis_item_spacing;
}
if self.layout_style().flex_grow != other.layout_style().flex_grow {
delta.layout_style_mut().flex_grow = other.layout_style().flex_grow;
}
if self.layout_style().flex_shrink != other.layout_style().flex_shrink {
delta.layout_style_mut().flex_shrink = other.layout_style().flex_shrink;
}
if self.layout_style().flex_basis != other.layout_style().flex_basis {
delta.layout_style_mut().flex_basis = other.layout_style().flex_basis;
}
if self.layout_style().width != other.layout_style().width {
delta.layout_style_mut().width = other.layout_style().width;
}
if self.layout_style().height != other.layout_style().height {
delta.layout_style_mut().height = other.layout_style().height;
}
if self.layout_style().max_width != other.layout_style().max_width {
delta.layout_style_mut().max_width = other.layout_style().max_width;
}
if self.layout_style().max_height != other.layout_style().max_height {
delta.layout_style_mut().max_height = other.layout_style().max_height;
}
if self.layout_style().min_width != other.layout_style().min_width {
delta.layout_style_mut().min_width = other.layout_style().min_width;
}
if self.layout_style().min_height != other.layout_style().min_height {
delta.layout_style_mut().min_height = other.layout_style().min_height;
}
if self.node_style().aspect_ratio != other.node_style().aspect_ratio {
delta.node_style_mut().aspect_ratio = other.node_style().aspect_ratio;
}
if self.node_style().pointer_events != other.node_style().pointer_events {
delta.node_style_mut().pointer_events = other.node_style().pointer_events;
}
if self.node_style().meter_data != other.node_style().meter_data {
delta.node_style_mut().meter_data = other.node_style().meter_data.clone();
}
delta
}
}
1 change: 0 additions & 1 deletion crates/dc_bundle/src/legacy_definition/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@

pub mod component;
pub mod view;
pub mod view_style;
3 changes: 1 addition & 2 deletions crates/dc_bundle/src/legacy_definition/view/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* limitations under the License.
*/

use crate::definition::view::StyledTextRun;
use crate::legacy_definition::view::view_style::ViewStyle;
use crate::definition::view::{StyledTextRun, ViewStyle};
use serde::{Deserialize, Serialize};

/// Figma component properties can be "overridden" in the UI. These overrides
Expand Down
3 changes: 1 addition & 2 deletions crates/dc_bundle/src/legacy_definition/view/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ use crate::definition::interaction::Reaction;
use crate::definition::layout::OverflowDirection;
use crate::definition::plugin::FrameExtras;
use crate::definition::view::StyledTextRun;
use crate::definition::view::ViewStyle;
use crate::legacy_definition::view::component::ComponentInfo;
use crate::legacy_definition::view::view_style::ViewStyle;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::atomic::AtomicU16;

/// This enum may be used as a hint by the DesignCompose renderer implementation
/// to determine if it is important for the content to be rendered identically on different platforms.
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
Expand Down
Loading

0 comments on commit a4ef01a

Please sign in to comment.