mirror of
https://github.com/penpot/penpot.git
synced 2025-05-13 15:36:39 +02:00
✨ Serialize layout data
This commit is contained in:
parent
b4f6177be7
commit
80d5272248
17 changed files with 1006 additions and 375 deletions
|
@ -1,3 +1,11 @@
|
|||
use super::Path;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Bool {
|
||||
pub bool_type: BoolType,
|
||||
pub path: Path,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum BoolType {
|
||||
Union,
|
||||
|
@ -6,6 +14,15 @@ pub enum BoolType {
|
|||
Exclusion,
|
||||
}
|
||||
|
||||
impl Default for Bool {
|
||||
fn default() -> Self {
|
||||
Bool {
|
||||
bool_type: BoolType::Union,
|
||||
path: Path::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u8> for BoolType {
|
||||
fn from(value: u8) -> Self {
|
||||
match value {
|
||||
|
|
23
render-wasm/src/shapes/corners.rs
Normal file
23
render-wasm/src/shapes/corners.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
use skia_safe::Point;
|
||||
|
||||
pub type CornerRadius = Point;
|
||||
pub type Corners = [CornerRadius; 4];
|
||||
|
||||
pub fn make_corners(raw_corners: (f32, f32, f32, f32)) -> Option<Corners> {
|
||||
let (r1, r2, r3, r4) = raw_corners;
|
||||
let are_straight_corners = r1.abs() <= f32::EPSILON
|
||||
&& r2.abs() <= f32::EPSILON
|
||||
&& r3.abs() <= f32::EPSILON
|
||||
&& r4.abs() <= f32::EPSILON;
|
||||
|
||||
if are_straight_corners {
|
||||
None
|
||||
} else {
|
||||
Some([
|
||||
(r1, r1).into(),
|
||||
(r2, r2).into(),
|
||||
(r3, r3).into(),
|
||||
(r4, r4).into(),
|
||||
])
|
||||
}
|
||||
}
|
8
render-wasm/src/shapes/frames.rs
Normal file
8
render-wasm/src/shapes/frames.rs
Normal file
|
@ -0,0 +1,8 @@
|
|||
use super::Corners;
|
||||
use super::Layout;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct Frame {
|
||||
pub corners: Option<Corners>,
|
||||
pub layout: Option<Layout>,
|
||||
}
|
|
@ -1,10 +1,4 @@
|
|||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Default)]
|
||||
pub struct Group {
|
||||
pub masked: bool,
|
||||
}
|
||||
|
||||
impl Group {
|
||||
pub fn new(masked: bool) -> Self {
|
||||
Group { masked }
|
||||
}
|
||||
}
|
||||
|
|
195
render-wasm/src/shapes/layouts.rs
Normal file
195
render-wasm/src/shapes/layouts.rs
Normal file
|
@ -0,0 +1,195 @@
|
|||
#![allow(dead_code)]
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Layout {
|
||||
FlexLayout(LayoutData, FlexData),
|
||||
GridLayout(LayoutData, GridData),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Direction {
|
||||
Row,
|
||||
Column,
|
||||
}
|
||||
|
||||
impl Direction {
|
||||
pub fn from_u8(value: u8) -> Self {
|
||||
match value {
|
||||
0 => Self::Row,
|
||||
1 => Self::Column,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum AlignItems {
|
||||
Start,
|
||||
End,
|
||||
Center,
|
||||
Stretch,
|
||||
}
|
||||
|
||||
impl AlignItems {
|
||||
pub fn from_u8(value: u8) -> Self {
|
||||
match value {
|
||||
0 => Self::Start,
|
||||
1 => Self::End,
|
||||
2 => Self::Center,
|
||||
3 => Self::Stretch,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum AlignContent {
|
||||
Start,
|
||||
End,
|
||||
Center,
|
||||
SpaceBetween,
|
||||
SpaceAround,
|
||||
SpaceEvenly,
|
||||
Stretch,
|
||||
}
|
||||
|
||||
impl AlignContent {
|
||||
pub fn from_u8(value: u8) -> Self {
|
||||
match value {
|
||||
0 => Self::Start,
|
||||
1 => Self::End,
|
||||
2 => Self::Center,
|
||||
3 => Self::SpaceBetween,
|
||||
4 => Self::SpaceAround,
|
||||
5 => Self::SpaceEvenly,
|
||||
6 => Self::Stretch,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum JustifyItems {
|
||||
Start,
|
||||
End,
|
||||
Center,
|
||||
Stretch,
|
||||
}
|
||||
|
||||
impl JustifyItems {
|
||||
pub fn from_u8(value: u8) -> Self {
|
||||
match value {
|
||||
0 => Self::Start,
|
||||
1 => Self::End,
|
||||
2 => Self::Center,
|
||||
3 => Self::Stretch,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum JustifyContent {
|
||||
Start,
|
||||
End,
|
||||
Center,
|
||||
SpaceBetween,
|
||||
SpaceAround,
|
||||
SpaceEvenly,
|
||||
Stretch,
|
||||
}
|
||||
|
||||
impl JustifyContent {
|
||||
pub fn from_u8(value: u8) -> Self {
|
||||
match value {
|
||||
0 => Self::Start,
|
||||
1 => Self::End,
|
||||
2 => Self::Center,
|
||||
3 => Self::SpaceBetween,
|
||||
4 => Self::SpaceAround,
|
||||
5 => Self::SpaceEvenly,
|
||||
6 => Self::Stretch,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum WrapType {
|
||||
Wrap,
|
||||
NoWrap,
|
||||
}
|
||||
|
||||
impl WrapType {
|
||||
pub fn from_u8(value: u8) -> Self {
|
||||
match value {
|
||||
0 => Self::Wrap,
|
||||
1 => Self::NoWrap,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct GridTrack {}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Sizing {
|
||||
Fill,
|
||||
Fix,
|
||||
Auto,
|
||||
}
|
||||
|
||||
impl Sizing {
|
||||
pub fn from_u8(value: u8) -> Self {
|
||||
match value {
|
||||
0 => Self::Fill,
|
||||
1 => Self::Fix,
|
||||
2 => Self::Auto,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct LayoutData {
|
||||
pub direction: Direction,
|
||||
pub align_items: AlignItems,
|
||||
pub align_content: AlignContent,
|
||||
pub justify_items: JustifyItems,
|
||||
pub justify_content: JustifyContent,
|
||||
pub padding_top: f32,
|
||||
pub padding_right: f32,
|
||||
pub padding_bottom: f32,
|
||||
pub padding_left: f32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct FlexData {
|
||||
pub row_gap: f32,
|
||||
pub column_gap: f32,
|
||||
pub wrap_type: WrapType,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct GridData {
|
||||
pub rows: Vec<GridTrack>,
|
||||
pub columns: Vec<GridTrack>,
|
||||
// layout-grid-cells ;; map of id->grid-cell
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct LayoutItem {
|
||||
pub margin_top: f32,
|
||||
pub margin_right: f32,
|
||||
pub margin_bottom: f32,
|
||||
pub margin_left: f32,
|
||||
pub h_sizing: Sizing,
|
||||
pub v_sizing: Sizing,
|
||||
pub max_h: Option<f32>,
|
||||
pub min_h: Option<f32>,
|
||||
pub max_w: Option<f32>,
|
||||
pub min_w: Option<f32>,
|
||||
pub is_absolute: bool,
|
||||
pub z_index: i32,
|
||||
}
|
|
@ -221,8 +221,8 @@ pub fn propagate_modifiers(state: &State, modifiers: Vec<TransformEntry>) -> Vec
|
|||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use crate::shapes::Type;
|
||||
use skia::Point;
|
||||
use crate::math::{Matrix, Point};
|
||||
use crate::shapes::*;
|
||||
|
||||
#[test]
|
||||
fn test_propagate_shape() {
|
||||
|
@ -235,7 +235,7 @@ mod tests {
|
|||
|
||||
let parent_id = Uuid::new_v4();
|
||||
let mut parent = Shape::new(parent_id);
|
||||
parent.set_shape_type(Type::Group);
|
||||
parent.set_shape_type(Type::Group(Group::default()));
|
||||
parent.add_child(child_id);
|
||||
parent.set_selrect(1.0, 1.0, 5.0, 5.0);
|
||||
shapes.insert(parent_id, parent.clone());
|
||||
|
|
6
render-wasm/src/shapes/rects.rs
Normal file
6
render-wasm/src/shapes/rects.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
use super::Corners;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct Rect {
|
||||
pub corners: Option<Corners>,
|
||||
}
|
|
@ -8,3 +8,11 @@ impl SVGRaw {
|
|||
SVGRaw { content: svg }
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for SVGRaw {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
content: String::from(""),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue