Graphs: an edit of some attributes on an axis line keeps the rest its kind keeps

An edit that changed only some attributes of a graph line, such as a
transparency-only Eyedropper pick, a cap, or a fill, kept that line's whole
fill and stroke rows for every line of its kind. A line that still showed
the default stroke until the graph was drawn again then replaced the stroke
its kind kept from another line. Such edits now keep only the attributes they
change; Clear Appearance, Move Appearance, graphic styles, and a pick of every
attribute or with append still keep the whole rows.
This commit is contained in:
dotCooCoo
2026-11-13 22:11:11 -08:00
parent f0a8c750a3
commit f1a79b92cc
7 changed files with 145 additions and 42 deletions
+49 -10
View File
@@ -645,15 +645,54 @@ pub(crate) fn capture_line_rows(doc: &mut Document, ids: &[NodeId]) -> bool {
store_line_rows(doc, ids, false)
}
/// Keep the object rows of the graph lines among `ids` whose rows an edit changed (`before`: the document before the
/// edit) for every line of their kind. Returns whether kept rows changed.
fn capture_line_changes(before: &Document, doc: &mut Document, ids: &[NodeId]) -> bool {
let changed: Vec<NodeId> = ids
.iter()
.copied()
.filter(|id| graph_line(doc, *id).is_some() && before.node(*id).map(label_rows) != doc.node(*id).map(label_rows))
.collect();
store_line_rows(doc, &changed, false)
/// Keep, for every line of their kind, the attributes of the object rows ([`label_rows`]) that an edit changed on the
/// graph lines among `ids` (`before`: the document before the edit). The attributes the edit left alone stay as the kind
/// keeps them, also when a line still shows other ones until the graph is drawn again. Lines of a kind that the edit
/// changed differently take the frontmost one's attributes. Called inside the same edit, so they undo together. Returns
/// whether kept rows changed.
pub(crate) fn capture_line_changes(before: &Document, doc: &mut Document, ids: &[NodeId]) -> bool {
let mut changed = false;
for (id, graph, line) in lines_of(doc, ids) {
let old = before.node(id).and_then(label_rows).unwrap_or_default();
let new = doc.node(id).and_then(label_rows).unwrap_or_default();
if old == new {
continue;
}
let Some(spec) = doc.node_mut(graph).and_then(|n| n.extras_if_any().and_then(|e| e.graph.as_deref_mut())) else { continue };
let slot = line_slot(&mut spec.label_styles, line);
let mut rows = slot.clone().unwrap_or_else(|| line.default_rows());
merge_row_edits(&mut rows, &old, &new);
let kept = (rows != line.default_rows()).then_some(rows);
changed |= *slot != kept;
*slot = kept;
}
changed
}
/// Is `id` a graph line: a path directly in a graph's Axes group?
pub(crate) fn is_graph_line(doc: &Document, id: NodeId) -> bool {
graph_line(doc, id).is_some()
}
/// Set each attribute of `rows` that differs between `old` and `new` as `new` has it.
fn merge_row_edits(rows: &mut vectorcraft_doc::LabelRows, old: &vectorcraft_doc::LabelRows, new: &vectorcraft_doc::LabelRows) {
fn take<T: PartialEq + Clone>(kept: &mut T, old: &T, new: &T) {
if old != new {
*kept = new.clone();
}
}
let (k, o, n) = (&mut rows.paint, &old.paint, &new.paint);
take(&mut k.fill, &o.fill, &n.fill);
take(&mut k.stroke, &o.stroke, &n.stroke);
take(&mut k.stroke_width, &o.stroke_width, &n.stroke_width);
take(&mut k.fill_opacity, &o.fill_opacity, &n.fill_opacity);
take(&mut k.stroke_opacity, &o.stroke_opacity, &n.stroke_opacity);
take(&mut k.fill_blend, &o.fill_blend, &n.fill_blend);
take(&mut k.stroke_blend, &o.stroke_blend, &n.stroke_blend);
take(&mut k.opacity, &o.opacity, &n.opacity);
take(&mut k.blend, &o.blend, &n.blend);
take(&mut rows.below, &old.below, &new.below);
take(&mut rows.stroke_under, &old.stroke_under, &new.stroke_under);
}
/// Would [`store_line_rows`] with `object_only` change what a graph keeps for the lines among `ids`? Reads the document
@@ -901,7 +940,7 @@ pub(crate) fn labels_in(doc: &Document, roots: &[NodeId]) -> Vec<NodeId> {
/// labels itself ([`picks_legend_fill`]), their fill is kept only when the legend labels among `ids` end up with one
/// fill and the edit named them: with `named`, only a legend label among `named` (an edit that reached the others
/// through the graph or a group leaves them the graph's black or white); without it, every label in `ids`. The graph
/// lines among `ids` whose rows changed keep them for every line of their kind ([`capture_line_rows`]), with or without
/// lines among `ids` whose rows changed keep what changed for every line of their kind ([`capture_line_changes`]), with or without
/// `rows`. Called inside the same edit, so they undo together. Returns whether what a graph keeps changed.
pub(crate) fn capture_label_changes(before: &Document, doc: &mut Document, ids: &[NodeId], rows: bool, named: Option<&[NodeId]>) -> bool {
let lines = capture_line_changes(before, doc, ids);
+47
View File
@@ -6193,6 +6193,53 @@ fn an_object_opacity_on_an_axis_line_keeps_the_stroke_its_kind_keeps() {
}
}
/// An Eyedropper pick that applies only the object's transparency to an axis line keeps the stroke its kind keeps, though
/// the line it is applied to still shows the default stroke.
#[test]
fn a_transparency_only_pick_on_an_axis_line_keeps_the_stroke_its_kind_keeps() {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
let id = graph(&mut s, "column");
let rect = NodeId(s.execute("shape.rectangle", &json!({"x": 600, "y": 600, "width": 10, "height": 10})).unwrap()["id"].as_u64().unwrap());
s.execute("transparency.set", &json!({"ids": [rect.0], "opacity": 50})).unwrap();
let lines = line_nodes(&s, id);
s.execute("paint.setStroke", &json!({"ids": [lines[0].id.0], "color": "#ff0000"})).unwrap();
assert_eq!(line_look(&line_nodes(&s, id)[1]).0, "#000000", "the other lines show black until the graph is drawn again");
let fill = json!({"color": false, "transparency": false, "overprint": false});
let stroke = json!({"color": false, "transparency": false, "overprint": false, "weight": false, "cap": false, "join": false, "miter": false, "dash": false});
let apply = json!({"appearance": {"transparency": true, "fill": fill, "stroke": stroke}, "character": false, "paragraph": false});
s.execute("appearance.copyFrom", &json!({"source": rect.0, "ids": [lines[1].id.0], "apply": apply})).unwrap();
let looks = redrawn_line_looks(&mut s, id);
assert!(looks.iter().all(|l| l.0 == "#ff0000" && (l.2 - 0.5).abs() < 1e-6), "{looks:?}");
}
/// An edit of some attributes on an axis line that still shows the default stroke keeps what it changes, and the stroke
/// its kind keeps from another line stays: a cap is not kept, so it changes nothing the kind keeps.
#[test]
fn an_edit_of_some_attributes_on_an_axis_line_keeps_the_rest_its_kind_keeps() {
let cases = [
("stroke.set", json!({"cap": "round"}), ""),
("paint.setFill", json!({"color": "#0000ff"}), "#0000ff"),
("appearance.setItem", json!({"index": 0, "color": "#00ff00"}), "#00ff00"),
];
for (cmd, params, fill) in cases {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
let id = graph(&mut s, "column");
let lines = line_nodes(&s, id);
s.execute("paint.setStroke", &json!({"ids": [lines[0].id.0], "color": "#ff0000"})).unwrap();
assert_eq!(line_look(&line_nodes(&s, id)[1]).0, "#000000", "{cmd}: the other lines show black until the graph is drawn again");
let mut p = params.clone();
p["ids"] = json!([lines[1].id.0]);
s.execute(cmd, &p).unwrap();
let looks = redrawn_line_looks(&mut s, id);
assert!(looks.iter().all(|l| *l == ("#ff0000".into(), 0.5, 1.0)), "{cmd}: {looks:?}");
let fills: Vec<String> =
line_nodes(&s, id).iter().map(|n| n.appearance.fill_paint().color().map(|c| c.to_hex()).unwrap_or_default()).collect();
assert!(fills.iter().all(|f| f == fill), "{cmd}: {fills:?}");
}
}
/// An opacity and a stroke weight given to a radar graph's ring are kept for its rings, and its spokes keep their own.
#[test]
fn transparency_and_stroke_weight_on_a_radar_ring_are_kept_for_its_rings() {