M3.44: graphic styles keep opacity and blend, capture groups and type, add on Alt-click

Styles now store the object's opacity, blend mode, isolate and knockout and apply them.
New Graphic Style from a group without its own paint captures its topmost object's
appearance, and from type its characters' fill and stroke. Names are made unique.
graphicStyle.apply takes ids and add (Alt-click in the panel adds the style on top of the
existing appearance). graphicStyle.list reports every style. Applying links objects to a
stable style id (Node.graphic_style), which the management commands build on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bflatastic
2026-10-04 23:01:10 +07:00
co-authored by Claude Opus 5.5
parent 6bd27a05da
commit eb83efdb19
6 changed files with 864 additions and 106 deletions
+382 -31
View File
@@ -1,34 +1,271 @@
//! The Graphic Styles panel: apply, new, delete and duplicate.
//! The Graphic Styles panel: new, apply (replace or add), link, redefine, break link, rename,
//! delete, duplicate, sort and list.
//!
//! Applying a style links the targeted objects to it ([`Node::graphic_style`]). An object stays
//! linked while it keeps the style's look; editing its appearance or transparency breaks the link
//! ([`in_sync`]), and Redefine Graphic Style updates only the objects still linked.
use std::collections::HashMap;
use serde_json::{Value, json};
use vectorcraft_doc::{Appearance, DEFAULT_GRAPHIC_STYLE, Document, GraphicStyle, Node, NodeId, NodeKind};
use super::*;
use crate::EngineError;
pub fn specs() -> Vec<CommandSpec> {
vec![
cmd!("graphicStyle.apply", "Apply Graphic Style", ["Window", "Graphic Styles"], None, "{name}", has_selection, style_apply),
cmd!("graphicStyle.new", "New Graphic Style", ["Window", "Graphic Styles"], None, "{name?} from the selection", has_selection, style_new),
cmd!("graphicStyle.delete", "Delete Graphic Style", ["Window", "Graphic Styles"], None, "{name}", has_doc, style_delete),
cmd!("graphicStyle.duplicate", "Duplicate Graphic Style", ["Window", "Graphic Styles"], None, "{name}", has_doc, style_duplicate),
cmd!(
"graphicStyle.apply",
"Apply Graphic Style",
["Window", "Graphic Styles"],
None,
"{name, add?: bool, ids?} give the objects (default: the selection; a group's contents) the style's appearance, and themselves its opacity, blend mode, isolate and knockout, and link them to it; add: true (Alt-click) adds its fills, strokes and effects on top of the existing appearance instead and unlinks",
has_doc,
style_apply
),
cmd!(
"graphicStyle.new",
"New Graphic Style",
["Window", "Graphic Styles"],
None,
"{name?, id?} a style from object `id` or the first selected object: its appearance, opacity, blend mode, isolate and knockout (a group without fills or strokes of its own lends its topmost object's, type its characters'); links the object. Names are made unique → {name}",
has_doc,
style_new
),
cmd!(
"graphicStyle.delete",
"Delete Graphic Style",
["Window", "Graphic Styles"],
None,
"{name} | {names: [name…]} delete styles; their objects keep their look but are unlinked",
has_doc,
style_delete
),
cmd!("graphicStyle.duplicate", "Duplicate Graphic Style", ["Window", "Graphic Styles"], None, "{name} → {name}", has_doc, style_duplicate),
cmd!(
query "graphicStyle.list",
"List Graphic Styles",
[],
None,
"{} → {styles: [{id, name, fill, stroke, strokeWidth, fills, strokes, effects: [effect id…], opacity: 0..100, blend, isolate, knockout, linked: [object ids]}], selected: the style the first selected object is linked to, or null}",
has_doc,
style_list
),
cmd!(
"graphicStyle.redefine",
"Redefine Graphic Style",
["Window", "Appearance"],
None,
"{name?, id?} replace the style (default: the one the object was last linked to) with the look of object `id` or the first selected object; the objects still linked to it update, and that object becomes linked → {name}",
has_doc,
style_redefine
),
cmd!(
"graphicStyle.breakLink",
"Break Link to Graphic Style",
["Window", "Graphic Styles"],
None,
"{ids?} unlink the objects (default: the selection) from their graphic style; they keep their look",
has_doc,
style_break_link
),
cmd!(
"graphicStyle.rename",
"Rename Graphic Style",
["Window", "Graphic Styles"],
None,
"{name, to} (Graphic Style Options; names are unique and linked objects stay linked) → {name}",
has_doc,
style_rename
),
cmd!(
query "graphicStyle.unused",
"Select All Unused",
["Window", "Graphic Styles"],
None,
"{} → {names: [the styles no object is linked to]} (the panel selects them)",
has_doc,
style_unused
),
cmd!(
"graphicStyle.sortByName",
"Sort by Name",
["Window", "Graphic Styles"],
None,
"{} sort the styles by name (the Default Graphic Style stays first)",
has_doc,
style_sort
),
]
}
// ---------- links ----------
/// Does a style applied to `n` (`top`: `n` is the target itself) descend into its children? Groups
/// and layers style their contents, and so do other containers (blends…) inside them, while a
/// compound path takes it as a whole, as with paint commands ([`leaf_targets`]).
fn descends(n: &Node, top: bool) -> bool {
match n.kind {
NodeKind::Group { .. } | NodeKind::Layer { .. } => true,
NodeKind::Compound { .. } => false,
_ => !top && n.is_container(),
}
}
/// The objects whose appearance a style applied to `n` sets: `n`, or its painted contents.
fn painted<'a>(n: &'a Node, top: bool, out: &mut Vec<&'a Node>) {
match n.children() {
Some(ch) if descends(n, top) => ch.iter().for_each(|c| painted(c, false, out)),
_ => out.push(n),
}
}
/// [`painted`], mutably (copying only the nodes on the way).
fn paint_mut(n: &mut Node, top: bool, f: &mut impl FnMut(&mut Node)) {
if !descends(n, top) {
return f(n);
}
for c in n.children_mut().into_iter().flatten() {
paint_mut(std::sync::Arc::make_mut(c), false, f);
}
}
/// Does `n` still have style `g`'s look (its transparency, and its appearance on every painted
/// object)? Linked objects that don't are no longer linked.
pub(crate) fn in_sync(n: &Node, g: &GraphicStyle) -> bool {
let mut leaves = vec![];
painted(n, true, &mut leaves);
g.transparency_matches(n) && leaves.iter().all(|l| l.appearance == g.appearance)
}
/// The style `n` is linked to (and still looks like).
pub(crate) fn linked_style<'a>(d: &'a Document, n: &Node) -> Option<&'a GraphicStyle> {
d.graphic_style_by_id(n.graphic_style?).filter(|g| in_sync(n, g))
}
/// Objects anywhere in the art that `f` accepts.
fn objects_where(d: &Document, mut f: impl FnMut(&Node) -> bool) -> Vec<NodeId> {
let mut ids = vec![];
for l in &d.layers {
l.walk(&mut |n| {
if f(n) {
ids.push(n.id);
}
});
}
ids
}
/// The objects linked to each style id.
fn links(d: &Document) -> HashMap<u32, Vec<NodeId>> {
let mut out: HashMap<u32, Vec<NodeId>> = HashMap::new();
for l in &d.layers {
l.walk(&mut |n| {
if let Some(g) = linked_style(d, n) {
out.entry(g.id).or_default().push(n.id);
}
});
}
out
}
/// The appearance a new or redefined style takes from `n`: its own. A group without fills or
/// strokes of its own lends those of its topmost painted object, and type its characters' fill
/// and stroke.
fn captured(n: &Node) -> Appearance {
let mut ap = n.appearance.clone();
if !ap.items.is_empty() {
return ap;
}
match &n.kind {
NodeKind::Text(t) => {
if let Some(r) = t.runs.first() {
ap.items = Appearance::basic(r.style.fill.clone(), r.style.stroke.clone(), r.style.stroke_width).items;
}
}
_ => {
let mut leaves = vec![];
painted(n, true, &mut leaves);
if let Some(top) = leaves.last().filter(|l| l.id != n.id) {
// The topmost object's own effects apply before the group's.
let top = captured(top);
ap.items = top.items;
ap.effects.splice(0..0, top.effects);
}
}
}
ap
}
/// Give `id` style `g` (its painted objects the appearance, itself the transparency) and link it.
fn apply_style(d: &mut Document, id: NodeId, g: &GraphicStyle) {
if let Some(n) = d.node_mut(id) {
paint_mut(n, true, &mut |l| l.appearance = g.appearance.clone());
g.apply_transparency(n);
n.graphic_style = Some(g.id);
}
}
fn set_link(d: &mut Document, ids: &[NodeId], link: Option<u32>) {
for id in ids {
if let Some(n) = d.node_mut(*id) {
n.graphic_style = link;
}
}
}
fn style_index(d: &Document, name: &str) -> Result<usize> {
d.graphic_style_index(name).ok_or_else(|| EngineError::Other(format!("no graphic style `{name}`")))
}
/// The object a style is made from: `id`, else the first selected object.
fn source<'a>(s: &'a Session, p: &Value, cmd: &str) -> Result<(NodeId, &'a Node)> {
let id = match id_param(p, "id") {
Some(id) => id,
None => super::edit::selected_roots(s)?.first().copied().ok_or_else(|| bad(cmd, "select an object (or pass `id`)"))?,
};
Ok((id, s.doc()?.doc.node(id).ok_or_else(|| bad(cmd, format!("no object {}", id.0)))?))
}
impl Session {
/// The graphic style of the first selected object: the one it was last linked to, and whether
/// it is still linked (still has that style's look).
pub fn selection_graphic_style(&self) -> Option<(&GraphicStyle, bool)> {
let st = self.active()?;
let n = st.doc.node(*st.selection.objects.first()?)?;
let g = st.doc.graphic_style_by_id(n.graphic_style?)?;
Some((g, in_sync(n, g)))
}
}
// ---------- commands ----------
fn style_apply(s: &mut Session, p: &Value) -> Result<Value> {
let name = str_param(p, "name").ok_or_else(|| bad("graphicStyle.apply", "missing name"))?;
let ap = s
.doc()?
.doc
.graphic_styles
.iter()
.find(|g| g.name == name)
.map(|g| g.appearance.clone())
.ok_or_else(|| EngineError::Other(format!("no graphic style `{name}`")))?;
let ids = paint_targets(s, p)?;
let i = style_index(&s.doc()?.doc, name)?;
let add = bool_or(p, "add", false);
let ids = super::appearance::appearance_targets(s, p)?;
if ids.is_empty() {
return Err(bad("graphicStyle.apply", "nothing selected (or pass `ids`)"));
}
s.edit("Apply Graphic Style", |d, _| {
for id in &ids {
if let Some(n) = d.node_mut(*id) {
n.appearance = ap.clone();
if add {
let ap = d.graphic_styles[i].appearance.clone();
for id in &ids {
if let Some(n) = d.node_mut(*id) {
paint_mut(n, true, &mut |l| {
l.appearance.items.extend(ap.items.iter().cloned());
l.appearance.effects.extend(ap.effects.iter().cloned());
});
n.graphic_style = None;
}
}
} else {
d.graphic_style_id(i);
let g = d.graphic_styles[i].clone();
for id in &ids {
apply_style(d, *id, &g);
}
}
Ok(())
@@ -37,23 +274,37 @@ fn style_apply(s: &mut Session, p: &Value) -> Result<Value> {
}
fn style_new(s: &mut Session, p: &Value) -> Result<Value> {
let st = s.doc()?;
let id = *st.selection.objects.first().unwrap();
let ap = st.doc.node(id).map(|n| n.appearance.clone()).unwrap_or_default();
let name = str_param(p, "name").map(str::to_string).unwrap_or_else(|| format!("Graphic Style {}", st.doc.graphic_styles.len() + 1));
let (src, n) = source(s, p, "graphicStyle.new")?;
let d = &s.doc()?.doc;
let name = match str_param(p, "name").map(str::trim).filter(|n| !n.is_empty()) {
Some(n) => unique_name(n, |x| d.graphic_style(x).is_some()),
None => d.new_graphic_style_name(),
};
let g = GraphicStyle { id: d.next_graphic_style_id(), ..GraphicStyle::of(name.clone(), captured(n), n) };
s.edit("New Graphic Style", |d, _| {
d.graphic_styles.push(vectorcraft_doc::GraphicStyle { name: name.clone(), appearance: ap });
set_link(d, &[src], Some(g.id));
d.graphic_styles.push(g);
Ok(())
})?;
Ok(json!({ "name": name }))
}
fn style_delete(s: &mut Session, p: &Value) -> Result<Value> {
let name = str_param(p, "name").ok_or_else(|| bad("graphicStyle.delete", "missing name"))?.to_string();
s.edit("Delete Graphic Style", |d, _| {
let before = d.graphic_styles.len();
d.graphic_styles.retain(|g| g.name != name);
if d.graphic_styles.len() == before { Err(EngineError::Other(format!("no graphic style `{name}`"))) } else { Ok(()) }
let names: Vec<String> = match (p.get("names").and_then(Value::as_array), str_param(p, "name")) {
(Some(a), _) => a.iter().filter_map(Value::as_str).map(str::to_string).collect(),
(None, Some(n)) => vec![n.to_string()],
(None, None) => return Err(bad("graphicStyle.delete", "missing name")),
};
s.edit(if names.len() > 1 { "Delete Graphic Styles" } else { "Delete Graphic Style" }, |d, _| {
for name in &names {
let i = style_index(d, name)?;
let id = d.graphic_styles.remove(i).id;
if id != 0 {
let ids = objects_where(d, |n| n.graphic_style == Some(id));
set_link(d, &ids, None);
}
}
Ok(())
})?;
ok()
}
@@ -61,12 +312,112 @@ fn style_delete(s: &mut Session, p: &Value) -> Result<Value> {
fn style_duplicate(s: &mut Session, p: &Value) -> Result<Value> {
let name = str_param(p, "name").ok_or_else(|| bad("graphicStyle.duplicate", "missing name"))?.to_string();
let new = s.edit("Duplicate Graphic Style", |d, _| {
let pos = d.graphic_styles.iter().position(|g| g.name == name).ok_or_else(|| EngineError::Other(format!("no graphic style `{name}`")))?;
let mut g = d.graphic_styles[pos].clone();
let nm = unique_name(&format!("{name} copy"), |n| d.graphic_styles.iter().any(|x| x.name == n));
g.name = nm.clone();
let pos = style_index(d, &name)?;
let nm = unique_name(&format!("{name} copy"), |n| d.graphic_style(n).is_some());
let g = GraphicStyle { name: nm.clone(), id: d.next_graphic_style_id(), ..d.graphic_styles[pos].clone() };
d.graphic_styles.insert(pos + 1, g);
Ok(nm)
})?;
Ok(json!({"name": new}))
}
fn style_list(s: &mut Session, _: &Value) -> Result<Value> {
let d = &s.doc()?.doc;
let links = links(d);
let styles: Vec<Value> = d
.graphic_styles
.iter()
.map(|g| {
let ap = &g.appearance;
let count = |fill: bool| ap.items.iter().filter(|i| i.is_fill() == fill).count();
json!({
"id": g.id,
"name": g.name,
"fill": ap.fill_paint().label(),
"stroke": ap.stroke_paint().label(),
"strokeWidth": ap.stroke_width(),
"fills": count(true),
"strokes": count(false),
"effects": ap.effects.iter().map(|e| e.id.as_str()).collect::<Vec<_>>(),
"opacity": (g.opacity * 100.0).round(),
"blend": g.blend.label(),
"isolate": g.isolate,
"knockout": g.knockout,
"linked": links.get(&g.id).map(|v| v.iter().map(|i| i.0).collect::<Vec<_>>()).unwrap_or_default(),
})
})
.collect();
let selected = s.selection_graphic_style().filter(|(_, linked)| *linked).map(|(g, _)| g.name.clone());
Ok(json!({ "styles": styles, "selected": selected }))
}
fn style_redefine(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "graphicStyle.redefine";
let (src, n) = source(s, p, cmd)?;
let d = &s.doc()?.doc;
let i = match str_param(p, "name") {
Some(name) => style_index(d, name)?,
None => n
.graphic_style
.and_then(|id| d.graphic_style_by_id(id))
.and_then(|g| d.graphic_style_index(&g.name))
.ok_or_else(|| bad(cmd, "the object has no graphic style (pass `name`)"))?,
};
let look = GraphicStyle::of(String::new(), captured(n), n);
let name = d.graphic_styles[i].name.clone();
s.edit(&format!("Redefine Graphic Style \u{201c}{name}\u{201d}"), |d, _| {
let id = d.graphic_style_id(i);
let old = std::mem::replace(&mut d.graphic_styles[i], GraphicStyle { name: name.clone(), id, ..look });
// Objects still linked follow the new definition; edited ones are unlinked (they keep
// their look), and the source keeps its own.
let linked = objects_where(d, |n| n.graphic_style == Some(id) && n.id != src);
let (follow, edited): (Vec<NodeId>, Vec<NodeId>) = linked.into_iter().partition(|nid| d.node(*nid).is_some_and(|n| in_sync(n, &old)));
set_link(d, &edited, None);
let g = d.graphic_styles[i].clone();
for nid in follow {
apply_style(d, nid, &g);
}
set_link(d, &[src], Some(id));
Ok(())
})?;
Ok(json!({ "name": name }))
}
fn style_break_link(s: &mut Session, p: &Value) -> Result<Value> {
let ids = super::appearance::appearance_targets(s, p)?;
s.edit("Break Link to Graphic Style", |d, _| {
set_link(d, &ids, None);
Ok(())
})?;
ok()
}
fn style_rename(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "graphicStyle.rename";
let name = str_param(p, "name").ok_or_else(|| bad(cmd, "missing name"))?.to_string();
let to = str_param(p, "to").map(str::trim).filter(|t| !t.is_empty()).ok_or_else(|| bad(cmd, "missing `to`"))?.to_string();
s.edit("Graphic Style Options", |d, _| {
let i = style_index(d, &name)?;
if to != name && d.graphic_style(&to).is_some() {
return Err(EngineError::Other(format!("a graphic style named `{to}` already exists")));
}
d.graphic_styles[i].name = to.clone();
Ok(())
})?;
Ok(json!({ "name": to }))
}
fn style_unused(s: &mut Session, _: &Value) -> Result<Value> {
let d = &s.doc()?.doc;
let links = links(d);
let names: Vec<&str> = d.graphic_styles.iter().filter(|g| !links.contains_key(&g.id)).map(|g| g.name.as_str()).collect();
Ok(json!({ "names": names }))
}
fn style_sort(s: &mut Session, _: &Value) -> Result<Value> {
s.edit("Sort by Name", |d, _| {
d.graphic_styles.sort_by_cached_key(|g| (g.name != DEFAULT_GRAPHIC_STYLE, g.name.to_lowercase()));
Ok(())
})?;
ok()
}
+2
View File
@@ -772,6 +772,8 @@ mod tests_prefs;
#[cfg(test)]
mod tests_strokegeom;
#[cfg(test)]
mod tests_styles;
#[cfg(test)]
mod tests_swatches;
#[cfg(test)]
mod tests_textedit;
+220
View File
@@ -0,0 +1,220 @@
//! Graphic styles: transparency, capture from groups and type, add-apply, unique names, links
//! (redefine, break link, delete, rename), Select All Unused, Sort by Name and Select > Same.
use serde_json::{Value, json};
use vectorcraft_color::{BlendMode, Color, Paint};
use vectorcraft_doc::{GraphicStyle, Node};
use super::*;
fn session() -> Session {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 600, "height": 400})).unwrap();
s
}
fn run(s: &mut Session, id: &str, p: Value) -> Value {
s.execute(id, &p).unwrap_or_else(|e| panic!("{id} {p}: {e}"))
}
/// A selected 50×50 rectangle at `x` with the default appearance.
fn rect(s: &mut Session, x: f64) -> NodeId {
let id = NodeId(run(s, "shape.rectangle", json!({"x": x, "y": 10, "width": 50, "height": 50}))["id"].as_u64().unwrap());
run(s, "select.set", json!({"ids": [id.0]}));
id
}
fn node(s: &Session, id: NodeId) -> Node {
s.doc().unwrap().doc.node(id).unwrap().clone()
}
fn style(s: &Session, name: &str) -> GraphicStyle {
s.doc().unwrap().doc.graphic_style(name).unwrap_or_else(|| panic!("no style {name}")).clone()
}
fn fill(s: &Session, id: NodeId) -> Paint {
node(s, id).appearance.fill_paint()
}
fn set_fill(s: &mut Session, id: NodeId, hex: &str) {
run(s, "paint.setFill", json!({"color": hex, "ids": [id.0]}));
}
fn linked(s: &mut Session, name: &str) -> Vec<u64> {
let l = run(s, "graphicStyle.list", json!({}));
let st = l["styles"].as_array().unwrap().iter().find(|g| g["name"] == name).unwrap_or_else(|| panic!("{name} not listed")).clone();
st["linked"].as_array().unwrap().iter().map(|v| v.as_u64().unwrap()).collect()
}
#[test]
fn styles_carry_opacity_and_blend_through_a_save() {
let mut s = session();
let a = rect(&mut s, 10.0);
run(&mut s, "transparency.set", json!({"item": null, "opacity": 50, "blend": "multiply", "knockout": true}));
run(&mut s, "graphicStyle.new", json!({"name": "Half"}));
let g = style(&s, "Half");
assert_eq!((g.opacity, g.blend, g.knockout), (0.5, BlendMode::Multiply, true));
let b = rect(&mut s, 100.0);
run(&mut s, "graphicStyle.apply", json!({"name": "Half"}));
let nb = node(&s, b);
assert_eq!((nb.opacity, nb.blend, nb.knockout), (0.5, BlendMode::Multiply, true));
let l = run(&mut s, "graphicStyle.list", json!({}));
let half = l["styles"].as_array().unwrap().iter().find(|g| g["name"] == "Half").unwrap();
assert_eq!((half["opacity"].as_f64(), half["blend"].as_str()), (Some(50.0), Some("Multiply")));
assert_eq!(linked(&mut s, "Half"), vec![a.0, b.0]);
assert_eq!(l["selected"], "Half");
// Native round trip: the style's transparency, its id and the objects' links survive.
let doc = s.doc().unwrap().doc.clone();
let back = vectorcraft_format::load(&vectorcraft_format::save(&doc, false)).unwrap();
assert_eq!(back.graphic_styles, doc.graphic_styles);
assert_eq!(back.node(b).unwrap().graphic_style, Some(g.id));
// Styles and objects from files without links still load (and get an id when first linked).
let old: GraphicStyle = serde_json::from_value(json!({"name": "Old", "appearance": {"items": []}})).unwrap();
assert_eq!((old.id, old.opacity, old.blend, old.isolate), (0, 1.0, BlendMode::Normal, false));
let mut d = (*doc).clone();
d.graphic_styles.push(old);
let i = d.graphic_styles.len() - 1;
assert_eq!(d.graphic_style_id(i), d.graphic_styles.iter().map(|g| g.id).max().unwrap());
assert!(!serde_json::to_string(&GraphicStyle::new("Plain", Default::default())).unwrap().contains("opacity"));
}
#[test]
fn a_style_from_a_group_or_type_is_not_empty() {
let mut s = session();
let a = rect(&mut s, 10.0);
let b = rect(&mut s, 100.0);
run(&mut s, "select.set", json!({"ids": [a.0, b.0]}));
run(&mut s, "paint.setFill", json!({"color": "#ff0000"}));
let g = NodeId(run(&mut s, "object.group", json!({}))["id"].as_u64().unwrap());
assert!(node(&s, g).appearance.items.is_empty());
run(&mut s, "graphicStyle.new", json!({"name": "Group Look"}));
let gs = style(&s, "Group Look");
assert_eq!(gs.appearance.fill_paint(), Paint::solid(Color::from_hex("#ff0000").unwrap()));
// Both members share the look, so the group is linked.
assert_eq!(linked(&mut s, "Group Look"), vec![g.0]);
let t = NodeId(run(&mut s, "text.create", json!({"x": 10, "y": 200, "text": "Hi"}))["id"].as_u64().unwrap());
run(&mut s, "select.set", json!({"ids": [t.0]}));
run(&mut s, "graphicStyle.new", json!({"name": "Type Look"}));
assert!(!style(&s, "Type Look").appearance.fill_paint().is_none());
// Applying to a group styles its contents and links the group.
let c = rect(&mut s, 200.0);
run(&mut s, "graphicStyle.apply", json!({"name": "Black Outline", "ids": [g.0]}));
assert_eq!(node(&s, a).appearance, style(&s, "Black Outline").appearance);
assert_eq!(node(&s, g).graphic_style, Some(style(&s, "Black Outline").id));
assert_ne!(node(&s, c).appearance, style(&s, "Black Outline").appearance);
}
#[test]
fn alt_apply_adds_the_style_on_top() {
let mut s = session();
rect(&mut s, 10.0);
run(&mut s, "appearance.clear", json!({}));
run(&mut s, "paint.setFill", json!({"color": "#00ff00"}));
run(&mut s, "effect.apply", json!({"effect": "distort.roughen"}));
run(&mut s, "graphicStyle.new", json!({"name": "Rough"}));
let rough = style(&s, "Rough");
let b = rect(&mut s, 100.0);
let before = node(&s, b).appearance;
run(&mut s, "graphicStyle.apply", json!({"name": "Rough", "add": true}));
let after = node(&s, b).appearance;
assert_eq!(after.items.len(), before.items.len() + rough.appearance.items.len());
assert_eq!(after.items[..before.items.len()], before.items[..]);
assert_eq!(after.items.last(), rough.appearance.items.last());
assert_eq!(after.effects.len(), before.effects.len() + 1);
assert_eq!(node(&s, b).graphic_style, None);
// One undo step.
run(&mut s, "edit.undo", json!({}));
assert_eq!(node(&s, b).appearance, before);
}
#[test]
fn style_names_are_unique() {
let mut s = session();
rect(&mut s, 10.0);
assert_eq!(run(&mut s, "graphicStyle.new", json!({"name": "X"}))["name"], "X");
assert_eq!(run(&mut s, "graphicStyle.new", json!({"name": "X"}))["name"], "X 2");
let n = s.doc().unwrap().doc.graphic_styles.len();
assert_eq!(run(&mut s, "graphicStyle.new", json!({}))["name"], format!("Graphic Style {}", n + 1));
assert!(s.execute("graphicStyle.rename", &json!({"name": "X 2", "to": "X"})).is_err());
assert!(s.execute("graphicStyle.rename", &json!({"name": "X 2", "to": " "})).is_err());
assert_eq!(run(&mut s, "graphicStyle.rename", json!({"name": "X 2", "to": "Y"}))["name"], "Y");
assert_eq!(run(&mut s, "graphicStyle.duplicate", json!({"name": "Y"}))["name"], "Y copy");
assert_ne!(style(&s, "Y copy").id, style(&s, "Y").id);
}
/// Three rectangles: `a` (the source of style "S"), `b` and `c`, all linked to "S".
fn linked_three(s: &mut Session) -> (NodeId, NodeId, NodeId) {
let a = rect(s, 10.0);
run(s, "graphicStyle.new", json!({"name": "S"}));
let b = rect(s, 100.0);
let c = rect(s, 200.0);
run(s, "graphicStyle.apply", json!({"name": "S", "ids": [b.0, c.0]}));
assert_eq!(linked(s, "S"), vec![a.0, b.0, c.0]);
(a, b, c)
}
#[test]
fn redefine_updates_every_linked_object() {
let mut s = session();
let (a, b, c) = linked_three(&mut s);
set_fill(&mut s, a, "#0000ff");
// Editing the source broke its link, but it remembers the style Redefine replaces.
assert_eq!(linked(&mut s, "S"), vec![b.0, c.0]);
run(&mut s, "select.set", json!({"ids": [a.0]}));
assert_eq!(s.selection_graphic_style().map(|(g, linked)| (g.name.clone(), linked)), Some(("S".into(), false)));
run(&mut s, "graphicStyle.redefine", json!({}));
let blue = Paint::solid(Color::from_hex("#0000ff").unwrap());
assert_eq!((fill(&s, b), fill(&s, c)), (blue.clone(), blue));
assert_eq!(linked(&mut s, "S"), vec![a.0, b.0, c.0]);
run(&mut s, "edit.undo", json!({}));
assert_eq!(fill(&s, b), Paint::solid(Color::WHITE));
}
#[test]
fn redefine_leaves_unlinked_and_edited_objects_alone() {
let mut s = session();
let (a, b, c) = linked_three(&mut s);
run(&mut s, "graphicStyle.breakLink", json!({"ids": [c.0]}));
assert_eq!(node(&s, c).graphic_style, None);
let d = rect(&mut s, 300.0);
run(&mut s, "graphicStyle.apply", json!({"name": "S"}));
set_fill(&mut s, d, "#00ff00");
set_fill(&mut s, a, "#0000ff");
run(&mut s, "graphicStyle.redefine", json!({"name": "S", "id": a.0}));
assert_eq!(fill(&s, b), Paint::solid(Color::from_hex("#0000ff").unwrap()));
assert_eq!(fill(&s, c), Paint::solid(Color::WHITE));
// The edited object keeps its look and is unlinked.
assert_eq!(fill(&s, d), Paint::solid(Color::from_hex("#00ff00").unwrap()));
assert_eq!(node(&s, d).graphic_style, None);
assert!(s.execute("graphicStyle.redefine", &json!({"id": c.0})).is_err());
}
#[test]
fn deleting_a_style_unlinks_and_renaming_keeps_links() {
let mut s = session();
let (_, b, c) = linked_three(&mut s);
run(&mut s, "graphicStyle.rename", json!({"name": "S", "to": "T"}));
assert_eq!(linked(&mut s, "T").len(), 3);
let look = node(&s, c).appearance;
run(&mut s, "graphicStyle.delete", json!({"name": "T"}));
assert_eq!((node(&s, b).graphic_style, node(&s, c).graphic_style), (None, None));
assert_eq!(node(&s, c).appearance, look);
}
#[test]
fn unused_styles_and_sort_by_name() {
let mut s = session();
linked_three(&mut s);
let unused = run(&mut s, "graphicStyle.unused", json!({}));
let names: Vec<&str> = unused["names"].as_array().unwrap().iter().filter_map(Value::as_str).collect();
assert!(names.contains(&"Sunshine") && !names.contains(&"S"));
run(&mut s, "graphicStyle.delete", json!({"names": names}));
assert_eq!(s.doc().unwrap().doc.graphic_styles.len(), 1);
run(&mut s, "edit.undo", json!({}));
run(&mut s, "graphicStyle.sortByName", json!({}));
let names: Vec<String> = s.doc().unwrap().doc.graphic_styles.iter().map(|g| g.name.clone()).collect();
assert_eq!(names, ["Default Graphic Style", "Black Outline", "Heavy Ink", "S", "Sunshine"]);
}