mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-16 12:03:31 +00:00
Merge pull request #7945 from dotCooCoo/feat/gradient-end-handle-length
Gradient tool: dragging the end handle changes only the length, Alt-drag the length and angle
This commit is contained in:
@@ -377,14 +377,15 @@ fn annotator_gestures_edit_the_gradient_as_single_undo_steps() {
|
||||
assert_eq!(stops.len(), 3);
|
||||
assert!((stops[1].offset - 0.3).abs() < 1e-6);
|
||||
assert_eq!(s.selected_stop(), Some(1));
|
||||
// Dragging the end handle changes only the end, as one undo step.
|
||||
// Dragging the end handle changes only the length (the end stays on the bar's line), as one
|
||||
// undo step.
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
gesture(
|
||||
&mut s,
|
||||
&[(PointerKind::Down, 200.0, 150.0), (PointerKind::Drag, 210.0, 160.0), (PointerKind::Drag, 220.0, 170.0), (PointerKind::Up, 220.0, 170.0)],
|
||||
);
|
||||
let g = fill_geom(&s, id);
|
||||
assert_eq!((g.start, g.end), (Point::new(100.0, 150.0), Point::new(220.0, 170.0)));
|
||||
assert_eq!((g.start, g.end), (Point::new(100.0, 150.0), Point::new(220.0, 150.0)));
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
// The selected stop takes Delete ahead of edit.clear; the object stays.
|
||||
|
||||
@@ -44,10 +44,15 @@ fn gradient_annotator_end_to_end() {
|
||||
let r = ok(&mut h, "press_key", json!({"key": "Delete"}));
|
||||
assert_eq!(r["handledBy"], "tool", "{r}");
|
||||
assert_eq!(offsets(&gradient(&h)), vec![0.0, 100.0]);
|
||||
// The end handle changes only the end.
|
||||
// The end handle changes only the length: the end stays on the bar's line.
|
||||
let end = json!({"events": [{"kind": "down", "x": 200, "y": 150}, {"kind": "drag", "x": 230, "y": 120}, {"kind": "up", "x": 230, "y": 120}]});
|
||||
ok(&mut h, "pointer_gesture", end);
|
||||
let g = gradient(&h).geom.unwrap();
|
||||
assert_eq!((g.start, g.end), (Point::new(100.0, 150.0), Point::new(230.0, 150.0)));
|
||||
// Alt-dragged, it changes the length and the angle together.
|
||||
let end = json!({"mods": {"alt": true}, "events": [{"kind": "down", "x": 230, "y": 150}, {"kind": "drag", "x": 230, "y": 120}, {"kind": "up", "x": 230, "y": 120}]});
|
||||
ok(&mut h, "pointer_gesture", end);
|
||||
let g = gradient(&h).geom.unwrap();
|
||||
assert_eq!((g.start, g.end), (Point::new(100.0, 150.0), Point::new(230.0, 120.0)));
|
||||
// Double-clicking a stop asks for its popover.
|
||||
let dbl = ok(&mut h, "pointer_gesture", json!({"events": [{"kind": "doubleclick", "x": 100, "y": 160}]}));
|
||||
|
||||
@@ -1120,14 +1120,15 @@ fn gradient_vector_ends_land_on_a_glyph_anchor() {
|
||||
let acts = tool.pointer(&c, &ev(PointerKind::Drag, near + Vec2::new(1.5, -1.0)));
|
||||
assert!(acts.is_empty(), "drag start, Snap to Glyph {glyphs}: {acts:?}");
|
||||
tool.pointer(&c, &ev(PointerKind::Up, near + Vec2::new(1.5, -1.0)));
|
||||
// The end handle of the rectangle's gradient dragged to the click lands on the anchor.
|
||||
// The end handle of the rectangle's gradient Alt-dragged to the click lands on the anchor.
|
||||
let c = ToolContext { snap_to_glyph: glyphs, ..cx(&d, &on_rect, &p) };
|
||||
let mut tool = GradientTool::default();
|
||||
// Hovering over the handle (on the rectangle's edge) shows no guides.
|
||||
tool.pointer(&c, &ev(PointerKind::Move, Point::new(400.0, 100.0)));
|
||||
assert!(!tool.overlays(&c).iter().any(|o| matches!(o, Overlay::Label { .. })), "hover over the handle");
|
||||
tool.pointer(&c, &ev(PointerKind::Down, Point::new(400.0, 100.0)));
|
||||
let v = preview(&tool.pointer(&c, &ev(PointerKind::Drag, click)));
|
||||
let alt = |kind, q: Point| PointerEvent { mods: crate::Mods { alt: true, ..Default::default() }, ..ev(kind, q) };
|
||||
tool.pointer(&c, &alt(PointerKind::Down, Point::new(400.0, 100.0)));
|
||||
let v = preview(&tool.pointer(&c, &alt(PointerKind::Drag, click)));
|
||||
assert_eq!(lands(at(&v["end"])), glyphs, "end handle, Snap to Glyph {glyphs}: {v}");
|
||||
tool.pointer(&c, &ev(PointerKind::Up, click));
|
||||
// The start handle dragged to the click lands on the anchor.
|
||||
|
||||
@@ -6,11 +6,14 @@
|
||||
//! pointer; a click inside selected art applies the gradient from that point.
|
||||
//!
|
||||
//! The annotator is a bar from a round start handle to a square end handle. Drag the start handle
|
||||
//! (or the bar) to move the gradient, the end handle to change its length and angle, and just past
|
||||
//! the end to rotate it. A vector drawn on the art, a click that applies one, and the dragged
|
||||
//! handles snap as drawn points do (the anchors and paths of the art, and with Snap to Glyph the
|
||||
//! guides of live type); Shift keeps the dragged end or handle at 45° steps from the Constrain Angle
|
||||
//! preference, snapping along that step. Colour stops sit under the bar: click the bar to add
|
||||
//! (or the bar) to move the gradient, the end handle to change its length, and just past the end
|
||||
//! to rotate it. The dragged end stays on the bar's line, never nearer the start than
|
||||
//! `MIN_LENGTH`; Alt-drag the end handle to change the length and angle together. A vector drawn
|
||||
//! on the art, a click that applies one, and the dragged handles snap as drawn points do (the
|
||||
//! anchors and paths of the art, and with Snap to Glyph the guides of live type; the end handle
|
||||
//! without Alt only onto points on the bar's line); Shift keeps the end of a drawn vector, the
|
||||
//! Alt-dragged end handle or the start handle at 45° steps from the Constrain Angle preference,
|
||||
//! snapping along that step. Colour stops sit under the bar: click the bar to add
|
||||
//! one, drag one to move it (Alt drags a copy), drag it off the bar to delete it, and drag a
|
||||
//! diamond above the bar to move a midpoint. Double-clicking a stop opens its popover. The
|
||||
//! selected stop (`gradient.selectStop`) is shared with the Gradient and Color panels: Delete or
|
||||
@@ -54,6 +57,9 @@ const LIGHT: [u8; 3] = [0xf0, 0xf0, 0xf0];
|
||||
// Annotator layout and hit radii, in screen pixels.
|
||||
/// Start and end handles.
|
||||
const HANDLE: f64 = 5.0;
|
||||
/// The end handle dragged without Alt stays at least this far from the start (or at the vector's
|
||||
/// own length when that is shorter).
|
||||
const MIN_LENGTH: f64 = 2.0 * HANDLE;
|
||||
/// The rotate zone reaches this far past the end handle.
|
||||
const ROTATE: f64 = 14.0;
|
||||
/// Stop chips sit this far below the bar.
|
||||
@@ -335,6 +341,19 @@ fn run_ticks(cx: &ToolContext, a: &Annotator) -> Vec<Overlay> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Where the end handle dragged to `p` lands when only the length changes: `p` projected onto the
|
||||
/// line of `geom`'s vector, on the side of its end and no nearer the start than `min` (or the
|
||||
/// vector's own length when that is shorter). None for a vector of no length.
|
||||
fn along_bar(geom: &GradientGeom, p: Point, min: f64) -> Option<Point> {
|
||||
let v = geom.end - geom.start;
|
||||
let len = v.hypot();
|
||||
if len < 1e-12 {
|
||||
return None;
|
||||
}
|
||||
let u = v / len;
|
||||
Some(geom.start + u * (p - geom.start).dot(u).max(min.min(len)))
|
||||
}
|
||||
|
||||
/// The vector a click applies to `id`: its current gradient's, else the default fit on its box.
|
||||
fn click_vector(cx: &ToolContext, id: NodeId) -> Option<Vec2> {
|
||||
let stroke = !cx.fill_active;
|
||||
@@ -444,7 +463,24 @@ impl GradientTool {
|
||||
let start = place(geom.start, geom.start + (p - g.at));
|
||||
("paint.setGradientGeom", Self::geom_params(cx, start, start + (geom.end - geom.start)))
|
||||
}
|
||||
Grab::End(geom) => ("paint.setGradientGeom", Self::geom_params(cx, geom.start, place(geom.start, p))),
|
||||
// Alt (or a vector of no length, which has no direction to keep) moves the end freely.
|
||||
Grab::End(geom) if mods.alt || geom.length() < 1e-12 => {
|
||||
("paint.setGradientGeom", Self::geom_params(cx, geom.start, place(geom.start, p)))
|
||||
}
|
||||
Grab::End(geom) => {
|
||||
// Only the length changes: the end slides along the bar's line. It snaps only to a
|
||||
// target on that line; elsewhere it follows the pointer, with no guides.
|
||||
let min = cx.tol(MIN_LENGTH);
|
||||
let q = snap.drag(cx, p, Some(&Leave::segment(cx, geom.start, false)));
|
||||
let end = match along_bar(geom, q, min) {
|
||||
Some(e) if e.distance(q) <= cx.tol(0.5) => e,
|
||||
_ => {
|
||||
snap.clear();
|
||||
along_bar(geom, p, min).unwrap_or(geom.end)
|
||||
}
|
||||
};
|
||||
("paint.setGradientGeom", Self::geom_params(cx, geom.start, end))
|
||||
}
|
||||
Grab::Rotate { geom, from } => {
|
||||
let dir = (Affine::rotate(-from) * (p - geom.start).to_point()).to_vec2();
|
||||
let dir = if mods.shift { constrain(dir) } else { dir };
|
||||
|
||||
@@ -111,8 +111,9 @@ fn the_end_handle_changes_only_the_end_and_the_start_moves_both() {
|
||||
let p = paint();
|
||||
let cx = cx(&d, &s, &p);
|
||||
let mut t = GradientTool::default();
|
||||
t.pointer(&cx, &ev(PointerKind::Down, Point::new(200.0, 150.0)));
|
||||
let v = preview(&t.pointer(&cx, &ev(PointerKind::Drag, Point::new(220.0, 170.0)))).clone();
|
||||
// Alt-drag moves the end freely: the length and the angle change together.
|
||||
t.pointer(&cx, &with(ALT, ev(PointerKind::Down, Point::new(200.0, 150.0))));
|
||||
let v = preview(&t.pointer(&cx, &with(ALT, ev(PointerKind::Drag, Point::new(220.0, 170.0))))).clone();
|
||||
assert_eq!((point(&v["start"]), point(&v["end"])), (Point::new(100.0, 150.0), Point::new(220.0, 170.0)));
|
||||
assert_eq!(t.pointer(&cx, &ev(PointerKind::Up, Point::new(220.0, 170.0))), vec![Action::Commit]);
|
||||
// The start handle moves the whole vector.
|
||||
@@ -121,6 +122,32 @@ fn the_end_handle_changes_only_the_end_and_the_start_moves_both() {
|
||||
assert_eq!((point(&v["start"]), point(&v["end"])), (Point::new(110.0, 140.0), Point::new(210.0, 140.0)));
|
||||
}
|
||||
|
||||
/// A plain drag of the end handle changes only the length: the end stays on the bar's line, with
|
||||
/// Shift too, never nearer the start than `MIN_LENGTH`, and it does not snap to a target off
|
||||
/// that line.
|
||||
#[test]
|
||||
fn a_plain_drag_of_the_end_handle_changes_only_the_length() {
|
||||
let (d, s) = graded(two());
|
||||
let p = paint();
|
||||
let cx = cx(&d, &s, &p);
|
||||
let mut t = GradientTool::default();
|
||||
let drag = |t: &mut GradientTool, m: Mods, to: Point| {
|
||||
t.pointer(&cx, &with(m, ev(PointerKind::Down, Point::new(200.0, 150.0))));
|
||||
let v = preview(&t.pointer(&cx, &with(m, ev(PointerKind::Drag, to)))).clone();
|
||||
let guides = t.overlays(&cx).iter().any(|o| matches!(o, Overlay::Label { .. }));
|
||||
t.pointer(&cx, &with(m, ev(PointerKind::Up, to)));
|
||||
(point(&v["start"]), point(&v["end"]), guides)
|
||||
};
|
||||
let start = Point::new(100.0, 150.0);
|
||||
assert_eq!(drag(&mut t, Mods::default(), Point::new(220.0, 170.0)), (start, Point::new(220.0, 150.0), false));
|
||||
assert_eq!(drag(&mut t, SHIFT, Point::new(230.0, 100.0)), (start, Point::new(230.0, 150.0), false));
|
||||
// Near the rectangle's corner, off the line: no snap.
|
||||
assert_eq!(drag(&mut t, Mods::default(), Point::new(202.0, 197.0)), (start, Point::new(202.0, 150.0), false));
|
||||
// Dragged back past the start, the end stops short of it.
|
||||
let (_, end, _) = drag(&mut t, Mods::default(), Point::new(60.0, 160.0));
|
||||
assert!(end.distance(Point::new(100.0 + cx.tol(MIN_LENGTH), 150.0)) < 1e-9, "{end:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn past_the_end_rotates_and_shift_snaps() {
|
||||
let (d, s) = graded(two());
|
||||
@@ -140,9 +167,9 @@ fn handles_snap_to_anchors_and_shift_uses_the_constrain_angle() {
|
||||
let p = paint();
|
||||
let mut cx = cx(&d, &s, &p);
|
||||
let mut t = GradientTool::default();
|
||||
// The end handle dragged near the rectangle's corner lands on it, with a guide label.
|
||||
t.pointer(&cx, &ev(PointerKind::Down, Point::new(200.0, 150.0)));
|
||||
let v = preview(&t.pointer(&cx, &ev(PointerKind::Drag, Point::new(202.0, 197.0)))).clone();
|
||||
// The end handle Alt-dragged near the rectangle's corner lands on it, with a guide label.
|
||||
t.pointer(&cx, &with(ALT, ev(PointerKind::Down, Point::new(200.0, 150.0))));
|
||||
let v = preview(&t.pointer(&cx, &with(ALT, ev(PointerKind::Drag, Point::new(202.0, 197.0))))).clone();
|
||||
assert_eq!(point(&v["end"]), Point::new(200.0, 200.0));
|
||||
assert!(t.overlays(&cx).iter().any(|o| matches!(o, Overlay::Label { text, .. } if text == "anchor")));
|
||||
t.pointer(&cx, &ev(PointerKind::Up, Point::new(202.0, 197.0)));
|
||||
@@ -156,12 +183,13 @@ fn handles_snap_to_anchors_and_shift_uses_the_constrain_angle() {
|
||||
let v = preview(&t.pointer(&cx2, &ev(PointerKind::Drag, Point::new(150.0, 130.0)))).clone();
|
||||
assert_eq!(point(&v["start"]), Point::new(100.0, 100.0));
|
||||
t.pointer(&cx2, &ev(PointerKind::Up, Point::new(150.0, 130.0)));
|
||||
// Constrain Angle 30: Shift gives 30 and 75 degrees (and no snapping).
|
||||
// Constrain Angle 30: Shift with Alt gives 30 and 75 degrees (and no snapping).
|
||||
cx.constrain_angle = 30.0;
|
||||
let angle = |v: &Value| GradientGeom { start: point(&v["start"]), end: point(&v["end"]), aspect: 1.0, focal: None }.angle_deg();
|
||||
let shift_alt = Mods { alt: true, ..SHIFT };
|
||||
for (to, want) in [(Point::new(190.0, 100.0), 30.0), (Point::new(125.0, 60.0), 75.0)] {
|
||||
t.pointer(&cx, &ev(PointerKind::Down, Point::new(200.0, 150.0)));
|
||||
let v = preview(&t.pointer(&cx, &with(SHIFT, ev(PointerKind::Drag, to)))).clone();
|
||||
t.pointer(&cx, &with(shift_alt, ev(PointerKind::Down, Point::new(200.0, 150.0))));
|
||||
let v = preview(&t.pointer(&cx, &with(shift_alt, ev(PointerKind::Drag, to)))).clone();
|
||||
assert!((angle(&v) - want).abs() < 1e-9, "{want}: {v}");
|
||||
t.pointer(&cx, &ev(PointerKind::Up, to));
|
||||
}
|
||||
|
||||
+5
-3
@@ -1421,9 +1421,11 @@ Drive a tool like a mouse:
|
||||
|
||||
Edit a gradient with the Gradient tool's annotator (the bar runs along the vector; stop chips sit 10 px under
|
||||
it, midpoint diamonds 7 px above): a click on the bar adds a stop, dragging a chip moves it (drag it off the bar to
|
||||
delete it, hold Alt to copy it), dragging the end handle changes the vector. Both ends of a vector drawn on the art,
|
||||
the start of one a click applies, and the dragged start and end handles snap as drawn points do; with Shift, the end
|
||||
or the handle keeps to 45° steps and snaps along them. The selected stop
|
||||
delete it, hold Alt to copy it), dragging the end handle changes the length (the end stays on the bar's line, at
|
||||
least 10 px from the start), and Alt-dragging it changes the length and angle together. Both ends of a vector drawn
|
||||
on the art, the start of one a click applies, and the dragged start and end handles snap as drawn points do (the end
|
||||
handle without Alt only onto points on the bar's line); with Shift, the end of a drawn vector, the Alt-dragged end
|
||||
handle or the start handle keeps to 45° steps and snaps along them. The selected stop
|
||||
(`gradient.selectStop`) takes Delete and ←/→:
|
||||
|
||||
```json
|
||||
|
||||
@@ -455,6 +455,7 @@ next unit in the order a right-click on a ruler lists them, from Feet back to Po
|
||||
| Gradient panel: Method, Classic or Perceptual (30.0) | done | Method menu under the Stroke buttons for linear and radial gradients (`paint.editGradient` `method`); [public source](https://helpx.adobe.com/illustrator/desktop/paint-and-fill/create-and-edit-gradients/control-perceptual-interpolation-in-gradients.html); `panels::gradient` test |
|
||||
| Gradient panel: Presets (30.0) | done | Presets menu by library (theme) plus Saved: applies a preset to the gradient behind the active proxy, keeping its placement, one undo step; panel menu Save Gradient Preset…, Delete Gradient Preset, Hide/Show Presets (`gradient.presets.*`); `panels::gradient` tests |
|
||||
| Gradient panel: delete a stop | done | Delete Stop at the right end of the ramp deletes the selected stop, and a stop dragged down off the ramp is deleted on release, never below two stops, one undo step each; [Gradient panel options](https://helpx.adobe.com/illustrator/desktop/paint-and-fill/create-and-edit-gradients/gradient-tool-and-panel.html); `panels::gradient` test |
|
||||
| Gradient tool: the annotator's end handle changes the length; Alt-drag changes the length and angle together | done | A plain drag keeps the end on the bar's line, at least 10 px from the start (VectorCraft's own minimum); Alt-drag moves it freely, Shift with Alt at 45° steps; [Default keyboard shortcuts](https://helpx.adobe.com/illustrator/using/default-keyboard-shortcuts.html); `xform::gradient` tests |
|
||||
| Workspaces (save, reset, switch); each remembers the layout changes made in it across switches and restarts until Reset <name> | done | `UiState::workspace_changes` |
|
||||
| Opening a file: after 0.4 s a progress window with the file name, a moving bar (reading counts no steps), how many more files wait, Cancel (Esc: the file doesn't open) and Continue in Background; never over another dialog or progress window | done | #5930; `opening::show`, `file.cancelOpen`, `file.openInBackground`. Save, Place and Close still show only the status bar's spinner (Place and Save run off the UI thread; Close is immediate) |
|
||||
| Window › Help Bar shows or hides the tool-help strip apart from the status bar; kept with the preferences and in saved workspaces; Presentation Mode hides both bars | done | `window.helpBar`; `help_bar_toggle_is_independent_and_saved_in_workspaces`, `help_bar_and_status_bar_render_independently`, `legacy_help_bar_visibility_follows_each_layouts_status_bar` |
|
||||
|
||||
Reference in New Issue
Block a user