penpot/render-wasm/src/wasm/layouts.rs
Alejandro Alonso afd4db776f Batch WASM shape upload to speed up page switches
Upload structural shape attrs (base, children, blur, shadows,
flex, layout-item) via multi-shape `_set_shapes_batch` FFI in
chunks of 512, then apply host attrs with use-shape selection.
2026-09-01 07:58:51 +02:00

99 lines
2.3 KiB
Rust

use crate::shapes::Sizing;
use crate::with_current_shape_mut;
use macros::ToJs;
mod align;
pub mod constraints;
pub mod flex;
mod grid;
pub use align::{
RawAlignContent, RawAlignItems, RawAlignSelf, RawJustifyContent, RawJustifyItems,
};
pub use flex::{RawFlexDirection, RawWrapType};
#[derive(Debug, Clone, PartialEq, Copy, ToJs)]
#[repr(u8)]
#[allow(dead_code)]
pub enum RawSizing {
Fill = 0,
Fix = 1,
Auto = 2,
}
impl From<u8> for RawSizing {
fn from(value: u8) -> Self {
unsafe { std::mem::transmute(value) }
}
}
impl From<RawSizing> for Sizing {
fn from(value: RawSizing) -> Self {
match value {
RawSizing::Fill => Sizing::Fill,
RawSizing::Fix => Sizing::Fix,
RawSizing::Auto => Sizing::Auto,
}
}
}
#[no_mangle]
pub extern "C" fn clear_shape_layout() {
with_current_shape_mut!(state, |shape: &mut Shape| {
shape.clear_layout();
});
}
#[no_mangle]
pub extern "C" fn set_layout_data(
margin_top: f32,
margin_right: f32,
margin_bottom: f32,
margin_left: f32,
h_sizing: u8,
v_sizing: u8,
has_max_h: bool,
max_h: f32,
has_min_h: bool,
min_h: f32,
has_max_w: bool,
max_w: f32,
has_min_w: bool,
min_w: f32,
align_self: u8,
is_absolute: bool,
z_index: i32,
) {
with_current_shape_mut!(state, |shape: &mut Shape| {
let h_sizing = RawSizing::from(h_sizing);
let v_sizing = RawSizing::from(v_sizing);
let max_h = has_max_h.then(|| max_h.max(0.01));
let min_h = has_min_h.then(|| min_h.clamp(0.01, max_h.unwrap_or(f32::INFINITY)));
let max_w = has_max_w.then(|| max_w.max(0.01));
let min_w = has_min_w.then(|| min_w.clamp(0.01, max_w.unwrap_or(f32::INFINITY)));
let z_index = if z_index != 0 { Some(z_index) } else { None };
let raw_align_self = align::RawAlignSelf::from(align_self);
let align_self = raw_align_self.try_into().ok();
shape.set_flex_layout_child_data(
margin_top,
margin_right,
margin_bottom,
margin_left,
h_sizing.into(),
v_sizing.into(),
max_h,
min_h,
max_w,
min_w,
align_self,
is_absolute,
z_index,
);
});
}