diff --git a/crates/engine/src/gradient_fill_cmds.rs b/crates/engine/src/gradient_fill_cmds.rs index 05f88ce8cb..e5a9a0bbe6 100644 --- a/crates/engine/src/gradient_fill_cmds.rs +++ b/crates/engine/src/gradient_fill_cmds.rs @@ -222,8 +222,25 @@ pub fn apply_set(layer: &Layer, f: &Fill, canvas: Rect, p: &Value, fg: [f32; 4], } if from.is_some() || to.is_some() || (style_changed && !explicit_geometry) { let cur = if from.is_some() || to.is_some() { gf::handles(*style, *angle, *scale, *offset, frame) } else { old_handles }; - let (a, s, o) = gf::from_handles(*style, from.unwrap_or(cur.0), to.unwrap_or(cur.1), frame, *angle); + let (f0, t0) = (from.unwrap_or(cur.0), to.unwrap_or(cur.1)); + let (a, s, o) = gf::from_handles(*style, f0, t0, frame, *angle); (*angle, *scale, *offset) = (a, clamp_scale(s), o); + // Past the scale range the dragged handle stops; the other one stays where it is (#1243). + // (Linear centres on the midpoint, so a clamped scale would otherwise move both ends.) + let anchor = match (from, to) { + (Some(_), None) => Some((t0, false)), + (None, Some(_)) => Some((f0, true)), + _ => None, + }; + if let Some((pin, is_start)) = anchor + && *scale != s + { + let (f1, t1) = gf::handles(*style, *angle, *scale, *offset, frame); + let at = if is_start { f1 } else { t1 }; + let (dx, dy) = (pin[0] - at[0], pin[1] - at[1]); + let (w, h) = (frame.width().max(1) as f32, frame.height().max(1) as f32); + *offset = (offset.0 + dx / w, offset.1 + dy / h); + } } if let Some(v) = p.get("stops") { let arr = v.as_array().ok_or_else(|| bad(CMD, "`stops` is [[location, colour], …]"))?; diff --git a/crates/engine/src/gradient_fill_cmds/tests.rs b/crates/engine/src/gradient_fill_cmds/tests.rs index 769712085e..f8a6fa77bd 100644 --- a/crates/engine/src/gradient_fill_cmds/tests.rs +++ b/crates/engine/src/gradient_fill_cmds/tests.rs @@ -297,3 +297,29 @@ fn commands_fail_gracefully() { let g = s.execute(SET, json!({"scale": 1})).unwrap(); assert!((g["scale"].as_f64().unwrap() - 10.0).abs() < 1e-3, "{g}"); } + +/// Dragging one handle past the scale range stops it; the other handle stays put (#1243). +#[test] +fn a_clamped_handle_drag_leaves_the_other_handle_alone() { + let pt = |g: &Value, k: &str| [g[k][0].as_f64().unwrap(), g[k][1].as_f64().unwrap()]; + let close = |a: [f64; 2], b: [f64; 2]| (a[0] - b[0]).abs() < 1e-2 && (a[1] - b[1]).abs() < 1e-2; + for style in STYLES { + for (from, to, short, short_start) in [([14.0, 16.0], [34.0, 16.0], [15.0, 16.0], [33.0, 16.0]), ([24.0, 6.0], [24.0, 26.0], [24.0, 7.0], [24.0, 25.0])] + { + let mut s = session(8, "white"); + s.execute(CREATE, json!({"from": from, "to": to, "style": style})).unwrap(); + // End dragged almost onto the start: far below 10 %. + let g = s.execute(SET, json!({"to": short})).unwrap(); + assert!((g["scale"].as_f64().unwrap() - 10.0).abs() < 1e-3, "{style}: {g}"); + assert!(close(pt(&g, "from"), from), "{style}: start moved to {:?}", pt(&g, "from")); + // The end stops on the line towards the pointer. + let e = pt(&g, "to"); + assert!((e[0] - from[0]) * (short[0] - from[0]) >= 0.0 && (e[1] - from[1]) * (short[1] - from[1]) >= 0.0, "{style}: {e:?}"); + // And the same for the start handle. + let mut s = session(8, "white"); + s.execute(CREATE, json!({"from": from, "to": to, "style": style})).unwrap(); + let g = s.execute(SET, json!({"from": short_start})).unwrap(); + assert!(close(pt(&g, "to"), to), "{style}: end moved: {g}"); + } + } +}