diff --git a/crates/engine/src/cmd/graph/series.rs b/crates/engine/src/cmd/graph/series.rs index 1ee708e00e..fc187b365e 100644 --- a/crates/engine/src/cmd/graph/series.rs +++ b/crates/engine/src/cmd/graph/series.rs @@ -1359,7 +1359,11 @@ pub(crate) fn capture_line_changes(before: &Document, doc: &mut Document, ids: & 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()); + let was_unpainted = stroke_unpainted(&rows); merge_row_edits(&mut rows, &old, &new); + if old.paint.stroke != new.paint.stroke { + keep_painted_stroke_weight(&mut rows, was_unpainted, &new.paint); + } let kept = (rows != line.default_rows()).then_some(rows); changed |= *slot != kept; *slot = kept; @@ -1388,10 +1392,12 @@ pub(crate) fn capture_line_paint_writes(doc: &mut Document, written: &[(NodeId, 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; + rows.paint.fill = shown.fill.clone(); } if stroke { - rows.paint.stroke = shown.stroke; + let was_unpainted = stroke_unpainted(&rows); + rows.paint.stroke = shown.stroke.clone(); + keep_painted_stroke_weight(&mut rows, was_unpainted, &shown); } let kept = (rows != line.default_rows()).then_some(rows); changed |= *slot != kept; @@ -1400,6 +1406,24 @@ pub(crate) fn capture_line_paint_writes(doc: &mut Document, written: &[(NodeId, changed } +/// Give kept line rows whose stroke an edit painted the painted line's stroke weight (`shown`) when the kind kept no +/// stroke paint before the edit (`was_unpainted`) and keeps the zero weight a stroke set to None left. A non-zero kept +/// weight stays, and so do a zero weight kept with a painted stroke and a zero weight the painted line shows. +fn keep_painted_stroke_weight(rows: &mut vectorcraft_doc::LabelRows, was_unpainted: bool, shown: &vectorcraft_doc::SeriesPaint) { + if was_unpainted + && !stroke_unpainted(rows) + && rows.paint.stroke_width.is_some_and(|w| w <= 0.0) + && let Some(w) = shown.stroke_width.filter(|w| *w > 0.0) + { + rows.paint.stroke_width = Some(w); + } +} + +/// Do kept line rows keep no stroke paint? +fn stroke_unpainted(rows: &vectorcraft_doc::LabelRows) -> bool { + rows.paint.stroke.as_ref().is_none_or(Paint::is_none) +} + /// 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 b8d431dfd0..85917acafc 100644 --- a/crates/engine/src/cmd/graph/tests.rs +++ b/crates/engine/src/cmd/graph/tests.rs @@ -8077,6 +8077,56 @@ fn a_stroke_written_to_an_axis_line_that_shows_it_is_kept() { }); } +/// A stroke painted on an axis line after another axis line's stroke was set to None is kept for every axis line with +/// the painted line's weight, not the zero weight the unpainted stroke left on its kind. +#[test] +fn a_stroke_painted_after_an_axis_line_lost_its_stroke_keeps_a_weight() { + let cases = ["paint.setStroke", "addon.color.fillToStroke", "appearance.setItem"]; + every_case(cases, |cmd| { + 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); + let weight = line_look(&lines[1]).1; + assert!(weight > 0.0, "{cmd}: the line has a stroke weight: {weight}"); + s.execute("paint.setStroke", &json!({"ids": [lines[0].id.0], "none": true})).unwrap(); + let b = lines[1].id.0; + match *cmd { + "paint.setStroke" => s.execute(cmd, &json!({"ids": [b], "color": "#ff0000"})).unwrap(), + "addon.color.fillToStroke" => { + s.execute("paint.setFill", &json!({"ids": [b], "color": "#ff0000"})).unwrap(); + s.execute(cmd, &json!({"ids": [b]})).unwrap() + } + _ => s.execute(cmd, &json!({"ids": [b], "index": top_row(&s, lines[1].id, false), "color": "#ff0000"})).unwrap(), + }; + let looks = redrawn_line_looks(&mut s, id); + assert!(looks.iter().all(|l| l.0 == "#ff0000" && (l.1 - weight).abs() < 1e-9), "{cmd}: {looks:?}"); + }); +} + +/// A zero stroke weight set together with a stroke color on an axis line is kept for every axis line as set, also when +/// a stroke is painted on another axis line before the graph is drawn again. +#[test] +fn a_zero_weight_set_with_a_stroke_color_on_an_axis_line_is_kept() { + every_case([false, true], |repaint| { + 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], "none": true})).unwrap(); + let at = top_row(&s, lines[1].id, false); + s.execute("appearance.setItem", &json!({"ids": [lines[1].id.0], "index": at, "color": "#ff0000", "weight": 0})).unwrap(); + let color = if *repaint { + s.execute("paint.setStroke", &json!({"ids": [lines[2].id.0], "color": "#0000ff"})).unwrap(); + "#0000ff" + } else { + "#ff0000" + }; + let looks = redrawn_line_looks(&mut s, id); + assert!(looks.iter().all(|l| l.0 == color && l.1 == 0.0), "repaint {repaint}: {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() {