Merge branch 'object-style-attributes' into object-styles-panel

This commit is contained in:
paaspaas00 committed 2026-10-21 13:07:06 +02:00
commit 6f40546167
2 files changed
+21 -7

No files matched your search

+14 -7
View File
@@ -1515,7 +1515,7 @@ fn create_object(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(f) = fill_param(CREATE, p, os.fill.as_ref())? {
os.fill = f;
}
if let Some(st) = group_param(CREATE, p, "stroke", os.stroke.as_ref().unwrap_or(&designcraft_doc::Stroke::default()))? {
if let Some(st) = stroke_param(CREATE, p, os.stroke.as_ref())? {
os.stroke = st;
}
if let Some(c) = corners_param(CREATE, p, os.corners.unwrap_or_default())? {
@@ -1535,6 +1535,18 @@ fn create_object(s: &mut Session, p: &Value) -> Result<Value> {
})
}
/// `p.stroke` merged over `base` (as [`group_param`]), refused when the weight isn't a number ≥ 0
/// or the tint isn't finite.
fn stroke_param(cmd: &str, p: &Value, base: Option<&designcraft_doc::Stroke>) -> Result<Option<Option<designcraft_doc::Stroke>>> {
let st = group_param(cmd, p, "stroke", base.unwrap_or(&designcraft_doc::Stroke::default()))?;
if let Some(Some(st)) = &st
&& !(st.weight.is_finite() && st.weight >= 0.0 && st.tint.is_finite())
{
return Err(bad(cmd, "stroke weight must be a number ≥ 0"));
}
Ok(st)
}
/// `p[key]`, an attribute group of a style, merged field by field over `base`: `None` when not
/// given, `Some(None)` for null (the style no longer sets it).
fn group_param<T: serde::Serialize + serde::de::DeserializeOwned>(cmd: &str, p: &Value, key: &str, base: &T) -> Result<Option<Option<T>>> {
@@ -1616,12 +1628,7 @@ fn edit_object(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(f) = fill_param(CMD, p, old.fill.as_ref())? {
new.fill = f;
}
if let Some(st) = group_param(CMD, p, "stroke", old.stroke.as_ref().unwrap_or(&designcraft_doc::Stroke::default()))? {
if let Some(st) = &st
&& !(st.weight.is_finite() && st.weight >= 0.0 && st.tint.is_finite())
{
return Err(bad(CMD, "stroke weight must be a number ≥ 0"));
}
if let Some(st) = stroke_param(CMD, p, old.stroke.as_ref())? {
new.stroke = st;
}
if let Some(c) = corners_param(CMD, p, old.corners.unwrap_or_default())? {
+7
View File
@@ -725,6 +725,13 @@ fn object_styles_carry_corners_and_their_stroke_and_corners_can_be_edited() {
assert!(s.execute("style.object.edit", &json!({"name": "Framed", "rename": "[Basic Text Frame]"})).is_err());
assert!(s.execute("style.object.edit", &json!({"name": "[Basic Graphics Frame]", "rename": "Mine"})).is_err());
assert!(s.execute("style.object.edit", &json!({"name": "Framed", "corners": {"size": "big"}})).is_err());
// A negative stroke weight is refused when creating a style as when editing one.
let styles = s.doc().unwrap().doc.styles.object.len();
for cmd in ["style.object.create", "style.object.edit"] {
let e = s.execute(cmd, &json!({"name": "Framed", "stroke": {"weight": -3}})).unwrap_err();
assert!(e.to_string().contains("stroke weight"), "{cmd}: {e}");
}
assert_eq!(s.doc().unwrap().doc.styles.object.len(), styles);
// null: the style stops setting corners, the items keep theirs.
s.execute("style.object.edit", &json!({"name": "Framed", "corners": null})).unwrap();
assert!(s.doc().unwrap().doc.styles.object_style("Framed").unwrap().corners.is_none());