diff --git a/crates/engine/src/cmd/gradient.rs b/crates/engine/src/cmd/gradient.rs index 5293343f2a..22c99d9d8e 100644 --- a/crates/engine/src/cmd/gradient.rs +++ b/crates/engine/src/cmd/gradient.rs @@ -529,8 +529,12 @@ fn apply_gradient_edit_in_model( } let recolor = p.get("stops").is_some() || bool_or(p, "reverse", false); if let Some(stops) = p.get("stops") { - gp.gradient.stops = parse_stops(stops)?; - gp.swatch = None; + let stops = parse_stops(stops)?; + // A paint whose stops stay as they are keeps its gradient swatch. + if stops != gp.gradient.stops { + gp.gradient.stops = stops; + gp.swatch = None; + } } if bool_or(p, "reverse", false) { // Midpoints mirrored with their segments. @@ -901,3 +905,32 @@ fn set_gradient_geom(s: &mut Session, p: &Value) -> Result { })?; Ok(json!({ "ids": targets.iter().map(|i| i.0).collect::>() })) } + +#[cfg(test)] +mod tests { + use super::*; + use vectorcraft_color::Color; + + fn linked(stops: &[(f32, &str)]) -> Paint { + let stops = stops.iter().map(|(offset, hex)| GradientStop::new(*offset, Color::from_hex(hex).unwrap())).collect(); + let mut paint = GradientPaint::new(Gradient::new(GradientKind::Linear, stops)); + paint.swatch = Some("Linked".into()); + Paint::Gradient(Box::new(paint)) + } + + fn swatch(paint: &Paint) -> Option<&str> { + match paint { + Paint::Gradient(g) => g.swatch.as_deref(), + _ => None, + } + } + + #[test] + fn writing_the_stops_a_paint_already_has_keeps_its_gradient_swatch() { + let paint = linked(&[(0.0, "#ffffff"), (1.0, "#000000")]); + let same = json!({"stops": [{"offset": 0, "color": "#ffffff"}, {"offset": 1, "color": "#000000"}]}); + assert_eq!(swatch(&apply_gradient_edit(&paint, &same, None).unwrap()), Some("Linked")); + let other = json!({"stops": [{"offset": 0, "color": "#ff0000"}, {"offset": 1, "color": "#000000"}]}); + assert_eq!(swatch(&apply_gradient_edit(&paint, &other, None).unwrap()), None, "new stops unlink the swatch"); + } +} diff --git a/crates/ui-egui/src/dialogs/gradient_stop.rs b/crates/ui-egui/src/dialogs/gradient_stop.rs index 3f0cf54d46..e3d9a5334b 100644 --- a/crates/ui-egui/src/dialogs/gradient_stop.rs +++ b/crates/ui-egui/src/dialogs/gradient_stop.rs @@ -786,8 +786,17 @@ pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { && !owner.key.point && let Some(index) = app.session.selected_stop() { - // The stop can cross its neighbor when its location changes. + // The stop can cross its neighbor when its location changes. A swatch picked for it moves with it. + let moved = owner.key.clone(); owner.key.index = index; + dialogs.with(|host| { + if let Some(popover) = host.get_mut_by_id::(id) + && let Some(picked) = &mut popover.picked_swatch + && picked.target == moved + { + picked.target.index = index; + } + }); } if let Some((name, color)) = filed.swatch && let Some(target) = selected(app) @@ -1063,6 +1072,26 @@ mod tests { assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal"), "the picked swatch moved before the other stays the one shown"); } + #[test] + fn the_swatches_view_keeps_the_picked_swatch_when_the_popover_moves_its_stop_past_a_neighbor() { + let mut app = app(); + let stops = json!([{"offset": 0, "color": "#ffffff"}, {"offset": 0.5, "color": "#008080"}, {"offset": 0.7, "color": "#000000"}]); + app.run("paint.setFill", json!({"gradient": {"stops": stops, "start": [100, 150], "end": [200, 150]}})).unwrap(); + app.run("gradient.selectStop", json!({"index": 1})).unwrap(); + for name in ["First teal", "Second teal"] { + app.run("swatch.new", json!({"name": name, "color": "#008080"})).unwrap(); + } + let mut panel = swatches_view(app); + panel.click("Second teal"); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal")); + panel.app().dialogs().unwrap().with(|host| { + host.get_mut_by_id::(dialog_id(KIND)).expect("the popover").filed.fields.location = Some(90.0); + }); + panel.frame(); + assert_eq!(panel.app().session.selected_stop(), Some(2), "the stop crossed its neighbor"); + assert_eq!(shown_swatch(&mut panel).as_deref(), Some("Second teal"), "the pick moved with its stop"); + } + #[test] fn the_swatches_view_keeps_the_picked_swatch_through_a_rename_in_a_batch() { let mut app = app();