🐛 Fix serialization of constraints (#11108)

This commit is contained in:
Belén Albeza 2026-08-06 15:49:04 +02:00 committed by GitHub
parent 314a2a245f
commit de8d8ca401
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 40 additions and 49 deletions

View File

@ -110,13 +110,9 @@
blend-mode (sr/translate-blend-mode (get shape :blend-mode)) blend-mode (sr/translate-blend-mode (get shape :blend-mode))
constraint-h (let [c (get shape :constraints-h)] constraint-h (let [c (get shape :constraints-h)]
(if (some? c) (sr/translate-constraint-h c))
(sr/translate-constraint-h c)
CONSTRAINT-NONE))
constraint-v (let [c (get shape :constraints-v)] constraint-v (let [c (get shape :constraints-v)]
(if (some? c) (sr/translate-constraint-v c))
(sr/translate-constraint-v c)
CONSTRAINT-NONE))
opacity (d/nilv (get shape :opacity) 1.0) opacity (d/nilv (get shape :opacity) 1.0)
rotation (d/nilv (get shape :rotation) 0.0) rotation (d/nilv (get shape :rotation) 0.0)

View File

@ -116,13 +116,13 @@
(defn translate-constraint-h (defn translate-constraint-h
[type] [type]
(let [values (unchecked-get wasm/serializers "constraint-h") (let [values (unchecked-get wasm/serializers "constraint-h")
default 5] ;; TODO: fix code in rust so we have a proper None variant default (unchecked-get values "none")]
(d/nilv (unchecked-get values (d/name type)) default))) (d/nilv (unchecked-get values (d/name type)) default)))
(defn translate-constraint-v (defn translate-constraint-v
[type] [type]
(let [values (unchecked-get wasm/serializers "constraint-v") (let [values (unchecked-get wasm/serializers "constraint-v")
default 5] ;; TODO: fix code in rust so we have a proper None variant default (unchecked-get values "none")]
(d/nilv (unchecked-get values (d/name type)) default))) (d/nilv (unchecked-get values (d/name type)) default)))
(defn translate-bool-type (defn translate-bool-type

View File

@ -7,11 +7,12 @@ use crate::with_current_shape_mut;
#[repr(u8)] #[repr(u8)]
#[allow(dead_code)] #[allow(dead_code)]
pub enum RawConstraintH { pub enum RawConstraintH {
Left = 0, None = 0,
Right = 1, Left = 1,
Leftright = 2, // odd casing to comply with cljs value Right = 2,
Center = 3, Leftright = 3, // odd casing to comply with cljs value
Scale = 4, Center = 4,
Scale = 5,
} }
impl From<u8> for RawConstraintH { impl From<u8> for RawConstraintH {
@ -20,14 +21,15 @@ impl From<u8> for RawConstraintH {
} }
} }
impl From<RawConstraintH> for ConstraintH { impl From<RawConstraintH> for Option<ConstraintH> {
fn from(value: RawConstraintH) -> Self { fn from(value: RawConstraintH) -> Self {
match value { match value {
RawConstraintH::Left => ConstraintH::Left, RawConstraintH::None => None,
RawConstraintH::Right => ConstraintH::Right, RawConstraintH::Left => Some(ConstraintH::Left),
RawConstraintH::Leftright => ConstraintH::LeftRight, RawConstraintH::Right => Some(ConstraintH::Right),
RawConstraintH::Center => ConstraintH::Center, RawConstraintH::Leftright => Some(ConstraintH::LeftRight),
RawConstraintH::Scale => ConstraintH::Scale, RawConstraintH::Center => Some(ConstraintH::Center),
RawConstraintH::Scale => Some(ConstraintH::Scale),
} }
} }
} }
@ -36,11 +38,12 @@ impl From<RawConstraintH> for ConstraintH {
#[repr(u8)] #[repr(u8)]
#[allow(dead_code)] #[allow(dead_code)]
pub enum RawConstraintV { pub enum RawConstraintV {
Top = 0, None = 0,
Bottom = 1, Top = 1,
Topbottom = 2, // odd casing to comply with cljs value Bottom = 2,
Center = 3, Topbottom = 3, // odd casing to comply with cljs value
Scale = 4, Center = 4,
Scale = 5,
} }
impl From<u8> for RawConstraintV { impl From<u8> for RawConstraintV {
@ -49,14 +52,15 @@ impl From<u8> for RawConstraintV {
} }
} }
impl From<RawConstraintV> for ConstraintV { impl From<RawConstraintV> for Option<ConstraintV> {
fn from(value: RawConstraintV) -> Self { fn from(value: RawConstraintV) -> Self {
match value { match value {
RawConstraintV::Top => ConstraintV::Top, RawConstraintV::None => None,
RawConstraintV::Bottom => ConstraintV::Bottom, RawConstraintV::Top => Some(ConstraintV::Top),
RawConstraintV::Topbottom => ConstraintV::TopBottom, RawConstraintV::Bottom => Some(ConstraintV::Bottom),
RawConstraintV::Center => ConstraintV::Center, RawConstraintV::Topbottom => Some(ConstraintV::TopBottom),
RawConstraintV::Scale => ConstraintV::Scale, RawConstraintV::Center => Some(ConstraintV::Center),
RawConstraintV::Scale => Some(ConstraintV::Scale),
} }
} }
} }
@ -64,16 +68,16 @@ impl From<RawConstraintV> for ConstraintV {
#[no_mangle] #[no_mangle]
pub extern "C" fn set_shape_constraint_h(constraint: u8) { pub extern "C" fn set_shape_constraint_h(constraint: u8) {
with_current_shape_mut!(state, |shape: &mut Shape| { with_current_shape_mut!(state, |shape: &mut Shape| {
let constraint = RawConstraintH::from(constraint); let constraint: Option<ConstraintH> = RawConstraintH::from(constraint).into();
shape.set_constraint_h(Some(constraint.into())); shape.set_constraint_h(constraint);
}); });
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn set_shape_constraint_v(constraint: u8) { pub extern "C" fn set_shape_constraint_v(constraint: u8) {
with_current_shape_mut!(state, |shape: &mut Shape| { with_current_shape_mut!(state, |shape: &mut Shape| {
let constraint = RawConstraintV::from(constraint); let constraint: Option<ConstraintV> = RawConstraintV::from(constraint).into();
shape.set_constraint_v(Some(constraint.into())); shape.set_constraint_v(constraint);
}); });
} }

View File

@ -14,7 +14,6 @@ use super::RawShapeType;
const FLAG_CLIP_CONTENT: u8 = 0b0000_0001; const FLAG_CLIP_CONTENT: u8 = 0b0000_0001;
const FLAG_HIDDEN: u8 = 0b0000_0010; const FLAG_HIDDEN: u8 = 0b0000_0010;
const CONSTRAINT_NONE: u8 = 0xFF;
const RAW_BASE_PROPS_SIZE: usize = std::mem::size_of::<RawBasePropsData>(); const RAW_BASE_PROPS_SIZE: usize = std::mem::size_of::<RawBasePropsData>();
@ -87,19 +86,11 @@ impl RawBasePropsData {
} }
fn constraint_h(&self) -> Option<ConstraintH> { fn constraint_h(&self) -> Option<ConstraintH> {
if self.constraint_h == CONSTRAINT_NONE { RawConstraintH::from(self.constraint_h).into()
None
} else {
Some(RawConstraintH::from(self.constraint_h).into())
}
} }
fn constraint_v(&self) -> Option<ConstraintV> { fn constraint_v(&self) -> Option<ConstraintV> {
if self.constraint_v == CONSTRAINT_NONE { RawConstraintV::from(self.constraint_v).into()
None
} else {
Some(RawConstraintV::from(self.constraint_v).into())
}
} }
} }
@ -219,10 +210,10 @@ mod tests {
bytes[33] = FLAG_CLIP_CONTENT | FLAG_HIDDEN; bytes[33] = FLAG_CLIP_CONTENT | FLAG_HIDDEN;
// blend_mode = Overlay (15) // blend_mode = Overlay (15)
bytes[34] = 15; bytes[34] = 15;
// constraint_h = Center (3) // constraint_h = Center (4)
bytes[35] = 3; bytes[35] = 4;
// constraint_v = Scale (4) // constraint_v = Scale (5)
bytes[36] = 4; bytes[36] = 5;
// opacity // opacity
bytes[40..44].copy_from_slice(&0.5_f32.to_le_bytes()); bytes[40..44].copy_from_slice(&0.5_f32.to_le_bytes());
// rotation // rotation