From f60d329363f865b1bdcb9b6c3fdc15c51e68b2ea Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 10:39:22 -0800 Subject: [PATCH] Graphs: rewritten axis line paint and no-op reorders keep what a kind keeps paint.setFill, paint.setStroke, addon.color.fillToStroke and addon.color.strokeToFill on an axis line, tick mark or radar ring that already showed the paint they write stored nothing, so a paint its kind kept from another line came back when the graph was drawn again. The paint written to the line's topmost visible fill or stroke row is now stored for its kind; its other attributes stay as the kind keeps them. That capture runs after the change-based one, so the frontmost written line sets the paint its kind keeps. An appearance.moveItem that left a graph label's rows or Contents row where they were stored that label's rows for its kind, replacing the rows the kind kept from another label, and recorded an undo step. A reorder now keeps rows only for the labels and lines whose stack it changed. --- crates/engine/src/cmd/addons/color.rs | 3 +- crates/engine/src/cmd/appearance.rs | 17 ++++++-- crates/engine/src/cmd/graph.rs | 6 +-- crates/engine/src/cmd/graph/series.rs | 33 ++++++++++++++++ crates/engine/src/cmd/graph/tests.rs | 57 +++++++++++++++++++++++++++ crates/engine/src/cmd/paint.rs | 4 +- docs/mcp.md | 9 ++++- 7 files changed, 118 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/cmd/addons/color.rs b/crates/engine/src/cmd/addons/color.rs index 8462e35207..f5a51beabb 100644 --- a/crates/engine/src/cmd/addons/color.rs +++ b/crates/engine/src/cmd/addons/color.rs @@ -260,7 +260,8 @@ fn copy_paint(s: &mut Session, p: &Value, to_stroke: bool) -> Result { let series = super::super::appearance::capture_series_paint_writes(&before, d, &ids, &written); let labels = super::super::graph::capture_label_styles(d, &ids, ©_run); let painted: Vec = written.iter().map(|(id, _)| *id).collect(); - let lines = super::super::graph::capture_line_changes(&before, d, &painted); + // A line keeps the paint written to it, also when it already showed it, and the rest stays as its kind keeps it. + let lines = super::super::graph::capture_line_changes(&before, d, &painted) | super::super::graph::capture_line_paint_writes(d, &written); Ok(objects + usize::from(series || labels || lines)) })?; } diff --git a/crates/engine/src/cmd/appearance.rs b/crates/engine/src/cmd/appearance.rs index d61fc78d52..568f6ab50b 100644 --- a/crates/engine/src/cmd/appearance.rs +++ b/crates/engine/src/cmd/appearance.rs @@ -690,12 +690,14 @@ pub(crate) fn edit_items_then( rows.push(*id); } } - let more = after(&before, d, &edited)?; // A graph line keeps only the attributes of its rows that the edit changed: the others stay as its kind keeps // them, which the line may not show yet. rows.retain(|id| !super::graph::is_graph_line(d, *id)); let labels = super::graph::capture_label_rows(d, &rows); let lines = super::graph::capture_line_changes(&before, d, &edited); + // `after` runs last, so what it keeps for a graph line (a paint written to a line that already showed it) is not + // replaced by the change of a line behind it. + let more = after(&before, d, &edited)?; Ok(count_changed(d, &before, &edited) + usize::from(more || labels || lines)) })?; Ok(aimed) @@ -712,6 +714,13 @@ fn capture_row_edits(before: &vectorcraft_doc::Document, d: &mut vectorcraft_doc labels || lines } +/// [`capture_row_edits`] for a reorder: only the graph labels and lines among `ids` whose rows it changed keep them, so +/// a reorder that leaves a label's or line's stack as it was keeps what its kind keeps from another label or line. +fn capture_reorder_edits(before: &vectorcraft_doc::Document, d: &mut vectorcraft_doc::Document, ids: &[NodeId]) -> bool { + let moved: Vec = ids.iter().copied().filter(|id| d.node(*id).map(|n| &n.appearance) != before.node(*id).map(|n| &n.appearance)).collect(); + capture_row_edits(before, d, &moved) +} + /// The first of `ids` that [`edit_items`] aimed at `item` with `of` changes, and the item it changes there. Read it /// before the edit: once edited, a row that was equal to a row of the art of `of` no longer is. pub(crate) fn first_edited(s: &Session, item: ItemTarget, of: Option, ids: &[NodeId]) -> Option<(NodeId, ItemTarget)> { @@ -1457,7 +1466,7 @@ fn move_item(s: &mut Session, p: &Value) -> Result { } let kept = super::graph::capture_series_opacity(d, &top_opacity_changed(&before, d, &ids)); let painted = super::graph::capture_series_top_paints(d, &top_paint_changed(&before, d, &ids)); - let labels = capture_row_edits(&before, d, &ids); + let labels = capture_reorder_edits(&before, d, &ids); Ok(count_changed(d, &before, &ids) + usize::from(kept || painted || labels)) })?; // The moved row stays the active one; the rows it passed shift by one. @@ -1503,7 +1512,7 @@ fn move_items(s: &mut Session, p: &Value, ids: &[NodeId], to: usize) -> Result Result { } slot.ok_or_else(|| bad("appearance.moveItem", "only groups, layers, type and images have a Contents (Characters) row"))?; // A graph label's kind keeps where its rows paint relative to the characters. - let labels = capture_row_edits(&before, d, ids); + let labels = capture_reorder_edits(&before, d, ids); Ok(count_changed(d, &before, ids) + usize::from(labels)) })?; Ok(json!({ "contents": slot })) diff --git a/crates/engine/src/cmd/graph.rs b/crates/engine/src/cmd/graph.rs index d1be18ff64..6e15090100 100644 --- a/crates/engine/src/cmd/graph.rs +++ b/crates/engine/src/cmd/graph.rs @@ -50,9 +50,9 @@ use series::default_series_color; pub(crate) use series::{ LabelTextChange, capture_label_added_rows, capture_label_anti_alias, capture_label_changes, capture_label_object_opacity, capture_label_paragraphs, capture_label_rows, capture_label_styles, capture_label_text, capture_line_changes, capture_line_fill_stroke_rows, - capture_line_rows, capture_series_object_opacity, capture_series_opacity, capture_series_top_paints, is_design_art, is_graph_line, - label_object_opacity_differs, labels_in, paint_targets, plain_labels, series_group_marks, series_group_marks_where, series_groups_among, - series_marks_in, series_object_opacity_differs, shown_paint, + capture_line_paint_writes, capture_line_rows, capture_series_object_opacity, capture_series_opacity, capture_series_top_paints, is_design_art, + is_graph_line, label_object_opacity_differs, labels_in, paint_targets, plain_labels, series_group_marks, series_group_marks_where, + series_groups_among, series_marks_in, series_object_opacity_differs, shown_paint, }; #[cfg(test)] diff --git a/crates/engine/src/cmd/graph/series.rs b/crates/engine/src/cmd/graph/series.rs index d21e555588..1ee708e00e 100644 --- a/crates/engine/src/cmd/graph/series.rs +++ b/crates/engine/src/cmd/graph/series.rs @@ -1367,6 +1367,39 @@ pub(crate) fn capture_line_changes(before: &Document, doc: &mut Document, ids: & changed } +/// Keep, for every line of their kind, the paint an edit wrote to row `at` of graph line `id` for each `(id, at)` in +/// `written` when that row is the line's topmost visible fill or stroke, also when the line already showed that paint: +/// the paint replaces the one its kind keeps from another line. The other attributes stay as the kind keeps them. Lines +/// of a kind take the frontmost one's paint. Called inside the same edit, so they undo together. Returns whether kept +/// rows changed. +pub(crate) fn capture_line_paint_writes(doc: &mut Document, written: &[(NodeId, usize)]) -> bool { + let ids: Vec = written.iter().map(|(id, _)| *id).collect(); + let mut changed = false; + for (id, graph, line) in lines_of(doc, &ids) { + let Some(n) = doc.node(id) else { continue }; + let items = &n.appearance.items; + let wrote = |fill: bool| items.iter().rposition(|it| it.is_fill() == fill && it.visible()).is_some_and(|at| written.contains(&(id, at))); + let (fill, stroke) = (wrote(true), wrote(false)); + if !fill && !stroke { + continue; + } + let shown = label_rows(n).unwrap_or_default().paint; + 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()); + if fill { + rows.paint.fill = shown.fill; + } + if stroke { + rows.paint.stroke = shown.stroke; + } + 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() diff --git a/crates/engine/src/cmd/graph/tests.rs b/crates/engine/src/cmd/graph/tests.rs index 42a7f2a46c..b8d431dfd0 100644 --- a/crates/engine/src/cmd/graph/tests.rs +++ b/crates/engine/src/cmd/graph/tests.rs @@ -8056,6 +8056,63 @@ fn default_fill_and_stroke_on_an_axis_line_that_shows_it_is_kept() { assert!(looks.iter().all(|l| (l.2 - 0.5).abs() < 1e-6), "the kind keeps its 50% opacity: {looks:?}"); } +/// A stroke paint written to an axis line that already shows it is kept for every axis line, in place of the stroke the +/// kind keeps from another line. +#[test] +fn a_stroke_written_to_an_axis_line_that_shows_it_is_kept() { + let cases = [("paint.setStroke", json!({"color": "#000000"})), ("addon.color.fillToStroke", json!({}))]; + every_case(cases, |(cmd, params)| { + 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.setFill", &json!({"ids": [lines[1].id.0], "color": "#000000"})).unwrap(); + 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.0 == "#000000"), "{cmd}: {looks:?}"); + }); +} + +/// A reorder that leaves an axis line's rows as they were keeps what its kind keeps from another line and is no edit. +#[test] +fn a_reorder_that_changes_nothing_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 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 stroke = top_row(&s, lines[1].id, false); + let undo = s.doc().unwrap().history.undo.len(); + s.execute("appearance.moveItem", &json!({"ids": [lines[1].id.0], "from": stroke, "to": stroke})).unwrap(); + s.execute("appearance.moveItem", &json!({"ids": [lines[1].id.0], "indices": [stroke], "to": stroke})).unwrap(); + assert_eq!(s.doc().unwrap().history.undo.len(), undo, "nothing changed"); + let looks = redrawn_line_looks(&mut s, id); + assert!(looks.iter().all(|l| l.0 == "#ff0000"), "{looks:?}"); +} + +/// A Contents row move that leaves a label's stack as it was keeps the rows its kind keeps from another label and is no +/// edit. +#[test] +fn a_reorder_that_changes_nothing_on_a_label_keeps_the_rows_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 v = text_id(&s, id, "Axes", "4"); + s.execute("appearance.addStroke", &json!({"ids": [v.0]})).unwrap(); + s.execute("appearance.setItem", &json!({"ids": [v.0], "index": 0, "color": "#0000ff", "weight": 2})).unwrap(); + let zero = text_id(&s, id, "Axes", "0"); + let undo = s.doc().unwrap().history.undo.len(); + s.execute("appearance.moveItem", &json!({"ids": [zero.0], "from": "contents", "to": 0})).unwrap(); + assert_eq!(s.doc().unwrap().history.undo.len(), undo, "nothing changed"); + let looks = redrawn_label_rows(&mut s, id); + assert_eq!(rows_of(&looks, "0"), (vec![(false, "#0000ff".into(), 1.0, 2.0)], 1.0), "every value axis number: {looks:?}"); +} + #[test] fn pie_names_and_wedge_labels_keep_their_style() { let mut s = Session::new(); diff --git a/crates/engine/src/cmd/paint.rs b/crates/engine/src/cmd/paint.rs index 6990524df6..a4249020e5 100644 --- a/crates/engine/src/cmd/paint.rs +++ b/crates/engine/src/cmd/paint.rs @@ -649,6 +649,8 @@ pub(crate) fn apply_paint(s: &mut Session, p: &Value, paint: Paint, fill: bool) }, |before, d, changed| { let series = super::appearance::capture_series_paint_writes(before, d, changed, &written.borrow()); + // A graph line keeps the paint written to it, also when it already showed it. + let lines = super::graph::capture_line_paint_writes(d, &written.borrow()); let labels = super::graph::capture_label_styles(d, &characters.borrow(), &|st| { if fill { st.fill = unplaced(&paint); @@ -659,7 +661,7 @@ pub(crate) fn apply_paint(s: &mut Session, p: &Value, paint: Paint, fill: bool) st.stroke = unplaced(&paint); } }); - Ok(series || labels) + Ok(series || lines || labels) }, )?; // Rejected edits leave the next shape's appearance and active proxy untouched. diff --git a/docs/mcp.md b/docs/mcp.md index f499e39f82..aceded6680 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -936,7 +936,9 @@ opacity and blend mode only when it also applies the object's transparency; the and leaves the other kind of row and the object opacity and blend mode the kind keeps. When the graph is drawn again, every label of that kind gets what is stored, with a placed gradient fitted to each label, and the graph saves it as `valuesRows`, `categoriesRows` and `legendRows` in its label styles. Rows below the topmost one of each kind, a row's effects, other stroke options (caps, joins, dashes) and effects applied with `effect.*` are not stored. When labels of one kind are edited together, the -last one in the order of the targets (`ids` as given, otherwise paint order) sets what the kind keeps. +last one in the order of the targets (`ids` as given, otherwise paint order) sets what the kind keeps. An +`appearance.moveItem` that leaves a label's rows and its Contents row where they were stores nothing and records no +undo step. `appearance.copyFrom` onto the whole text of a label also stores the character attributes it applies and, from type, the paragraph attributes. `paint.setFill` and `paint.setStroke` store a character fill or stroke when the paint goes to the label's characters (the whole label, or all of its text highlighted with the Type tool), and the row when it goes to @@ -954,7 +956,10 @@ store only the object opacity and blend mode. `appearance.clear`, `appearance.tr `graphicStyle.redefine`, and `appearance.copyFrom` with every attribute picked up and applied, store a line's rows even when the line already showed them. The other commands, an `appearance.copyFrom` of some attributes included, store only what they change on a line's rows; the kind keeps its other attributes, also when the line still -shows different ones until the graph is drawn again. An `appearance.copyFrom` that does not apply the object's +shows different ones until the graph is drawn again. `paint.setFill`, `paint.setStroke`, `addon.color.fillToStroke` +and `addon.color.strokeToFill` also store the paint they write on a line's topmost visible fill or stroke row when the +line already showed it. An `appearance.moveItem` that leaves a line's rows as they were stores nothing and records no +undo step. An `appearance.copyFrom` that does not apply the object's transparency, and `graphicStyle.apply` with `add`, leave the object opacity and blend mode a kind keeps as they are, also when the line still shows other ones. `graphicStyle.apply` with `add` stores only the kinds of row that are visible in the style. `appearance.copyFrom` with `append` stores only the kinds of row it adds that are visible in the