Merge branch 'red/t2' into fw/mac1

This commit is contained in:
aiongg
2026-11-16 21:35:40 +00:00
5 changed files with 107 additions and 19 deletions
+37 -5
View File
@@ -6,9 +6,12 @@
//! calls and paints [`points`](OutlineDraft::points) itself.
use craft_keymap::Mods;
use kurbo::Point;
use kurbo::{Point, Vec2};
use super::lock::polygon_step;
use super::lock::{Directions, polygon_step, round_to_direction};
/// Screen points a ⇧ freehand drag travels before it locks its direction.
const LOCK_DISTANCE: f64 = 3.0;
/// The most points [`OutlineDraft`]'s close reach measures the outline's box from.
const BOX_SAMPLES: usize = 64;
@@ -24,8 +27,8 @@ pub enum DraftKind {
/// switches to straight segments, where each click adds a vertex and a drag draws freehand
/// again; releasing ⌥ with the button up closes. ⌥ held at the press means subtract (the
/// tool's operation), not straight segments, until it is released and pressed again; with
/// nothing selected it starts straight segments at once. ⇧ puts each new point on the
/// horizontal, vertical or 45° line through the previous one.
/// nothing selected it starts straight segments at once. ⇧ puts each clicked vertex on the
/// horizontal, vertical or 45° line through the previous one, and holds a freehand drag to one such line.
#[default]
Mixed,
/// The Polygonal Lasso: each click adds a vertex, ⇧ (without ⌥) puts it in the nearest of
@@ -147,6 +150,8 @@ pub struct OutlineDraft {
alt_from_press: bool,
/// The document key the outline began under.
document: Option<u64>,
/// A ⇧ freehand drag's line: where it began and the unit direction it locked to (the nearest of eight).
lock: Option<(Point, Vec2)>,
}
impl OutlineDraft {
@@ -162,6 +167,7 @@ impl OutlineDraft {
polygonal: false,
alt_from_press: false,
document: None,
lock: None,
}
}
@@ -258,7 +264,7 @@ impl OutlineDraft {
return None;
}
let point = if self.kind != DraftKind::Polygonal || at.mods.alt {
let point = self.shift_step(at);
let point = self.drag_step(at);
self.append(point);
point
} else {
@@ -268,6 +274,30 @@ impl OutlineDraft {
Some(DraftEvent::Changed)
}
/// Where a freehand point of `at` goes. With ⇧ held a Mixed draft's drag follows one line: once the pointer has
/// travelled [`LOCK_DISTANCE`] screen points from the last vertex, the nearest of eight directions from there
/// is locked and every later point is the pointer's projection onto it, however the pointer wanders, until ⇧ is
/// released. Before the lock the point stays at the vertex. Otherwise [`shift_step`](Self::shift_step).
fn drag_step(&mut self, at: DraftPoint) -> Point {
if self.kind != DraftKind::Mixed || !at.mods.shift || self.polygonal {
self.lock = None;
return self.shift_step(at);
}
let Some(&last) = self.points.last() else { return at.at };
let (origin, direction) = match self.lock {
Some(lock) => lock,
None => {
let delta = at.at - last;
if delta.hypot() * at.zoom < LOCK_DISTANCE {
return last;
}
let direction = Vec2::from_angle(round_to_direction(delta.atan2(), Directions::Eight));
*self.lock.insert((last, direction))
}
};
origin + direction * (at.at - origin).dot(direction)
}
/// Where a new point of `at` goes: with ⇧, on the horizontal, vertical or 45° line through the previous point
/// ([`polygon_step`]), for a Mixed draft's every point and a Polygonal vertex placed without ⌥; `at` itself
/// otherwise, and for the first point.
@@ -302,6 +332,7 @@ impl OutlineDraft {
return None;
}
self.down = false;
self.lock = None;
if at.at.x.is_finite() && at.at.y.is_finite() {
self.cursor = Some(at.at);
}
@@ -451,5 +482,6 @@ impl OutlineDraft {
self.down = false;
self.polygonal = false;
self.alt_from_press = false;
self.lock = None;
}
}
+17
View File
@@ -66,3 +66,20 @@ fn shift_steps_keep_the_shorter_diagonal_offset() {
let back = click(shift(along.x - 9.0, 1.0));
assert!((back.x - (along.x - 6.0)).abs() < 1e-9 && back.y == 1.0, "{back:?}");
}
// A ⇧ freehand drag locks the nearest of eight directions once it has travelled a few screen points and stays on that
// line while the pointer wanders; the first point and the drag's earlier points are untouched.
#[test]
fn shift_holds_a_freehand_drag_to_one_line() {
let shift = |x: f64, y: f64| DraftPoint::new(Point::new(x, y), Mods::SHIFT, 1.0);
let mut draft = OutlineDraft::new(DraftKind::Mixed, REACH);
draft.press(at(10.0, 60.0, 1.0), true);
for (x, y) in [(30.0, 63.0), (50.0, 55.0), (80.0, 68.0), (100.0, 52.0)] {
draft.drag(shift(x, y));
}
let points = draft.points();
assert_eq!(points[0], Point::new(10.0, 60.0));
assert!(points.len() >= 3, "{points:?}");
assert!(points.iter().all(|p| p.y == 60.0), "the drag stays on the horizontal line through its start: {points:?}");
assert_eq!(points.last().map(|p| p.x), Some(100.0), "and follows the pointer along it");
}
+8 -1
View File
@@ -17,7 +17,8 @@ impl PenTool {
/// - on the first anchor of a draft with 3 or more anchors: closes at once (`closed` set, `Commit`, `Finished`);
/// the Pen closes on release instead;
/// - on another anchor: grabs it; past the click slop it follows the pointer and the draft reflows on every
/// move;
/// move. The first [`kept`](PenTool::kept) anchors are not grabbed: a press on one adds an anchor, and a double
/// click on one does nothing, except on the last of them, the end the draft carries on from;
/// - elsewhere: a new smooth anchor at the press, which follows the pointer while the button is down. The first
/// two anchors give straight segments; from three on the handles follow
/// [`carried_handles`](crate::geometry::curvature::carried_handles), which keeps the first
@@ -49,6 +50,12 @@ impl PenTool {
self.finish(PenEnd::Closed);
return ToolAction::Done;
}
// The carried anchors keep their handles: a press on one is a press on empty canvas, except a double click on
// the carried end, which toggles it, and on another carried anchor, which does nothing.
if pointer.clicks == 2 && under.is_some_and(|i| i + 1 < self.kept) {
return ToolAction::Done;
}
let under = under.filter(|&i| i >= self.kept || (i + 1 == self.kept && pointer.clicks == 2));
let index = match under {
Some(i) => {
if pointer.clicks == 2 {
+6 -10
View File
@@ -189,9 +189,8 @@ pub struct PenTool {
label: Arc<str>,
pub(super) drag: Option<Drag>,
pub(super) events: Vec<PenEvent>,
/// The anchors of the draft the host handed in: undo never removes them.
own: usize,
/// The leading anchors of the draft that keep their handles when the Curvature mode reflows it.
/// The leading anchors of the draft that belong to the path a press continued: they keep their handles when the
/// Curvature mode reflows the draft, and Undo, Backspace and Delete never take them back.
pub(super) kept: usize,
}
@@ -218,7 +217,6 @@ impl PenTool {
label: Arc::from(""),
drag: None,
events: Vec::new(),
own: 0,
kept: 0,
}
}
@@ -238,12 +236,10 @@ impl PenTool {
match draft.filter(|draft| !draft.knots.is_empty()) {
Some(draft) => {
self.shown = draft.clone();
self.own = draft.knots.len();
self.resume = true;
}
None => {
self.shown = EditSubpath::default();
self.own = 0;
self.kept = 0;
self.resume = false;
}
@@ -263,8 +259,9 @@ impl PenTool {
true
}
/// The number of leading anchors of the handed-in draft that keep their handles when the Curvature mode reflows
/// the draft (the anchors of the path a press continued). Reset to 0 when the draft ends.
/// The number of leading anchors of the handed-in draft that belong to the path a press continued: they keep their
/// handles when the Curvature mode reflows the draft, and Undo, Backspace and Delete never take them back (they
/// end the draft as discarded instead). Reset to 0 when the draft ends.
pub fn kept(&mut self, kept: usize) -> &mut Self {
self.kept = kept;
self
@@ -400,7 +397,6 @@ impl PenTool {
self.run = None;
self.drag = None;
self.resume = false;
self.own = 0;
self.kept = 0;
self.foreign = false;
}
@@ -426,7 +422,7 @@ impl PenTool {
}
fn remove_last_anchor(&mut self, reason: CancelReason) {
if self.own > 0 && self.shown.knots.len() <= self.own {
if self.kept > 0 && self.shown.knots.len() <= self.kept {
// Undo never eats the anchors of the path the draft resumed.
self.drop_run(reason);
return;
+39 -3
View File
@@ -254,13 +254,14 @@ fn space_moves_the_anchor_with_its_handles() {
pen.release(from + vec2(60.0, 50.0), Modifiers::NONE);
}
// A draft the host hands in gains one anchor; the first removal updates the draft, the next goes back to the handed-in
// path and ends the draft as discarded, never as an empty commit.
// A draft the host hands in as a resumed path (`kept` its anchors) gains one anchor; the first removal updates the
// draft, the next goes back to the handed-in path and ends the draft as discarded, never as an empty commit. A draft
// handed in with nothing kept (the path Undo gave back) loses its anchors one by one.
#[trial]
fn removing_a_resumed_drafts_own_anchor_restores_the_path_and_ends_the_draft() {
let mut pen = pen();
let own = EditSubpath::new(vec![Knot::corner(doc(60.0, 60.0)), Knot::corner(doc(160.0, 60.0))], false);
pen.fixture.tool_mut().draft(Some(PenDraft::Own(&own)));
pen.fixture.tool_mut().draft(Some(PenDraft::Own(&own))).kept(2);
pen.fixture.fixture().settle();
pen.sync();
assert_eq!(pen.kinds(), ["begin"], "the handed-in draft begins its gesture at once");
@@ -278,6 +279,18 @@ fn removing_a_resumed_drafts_own_anchor_restores_the_path_and_ends_the_draft() {
assert_eq!(pen.draft(), EditSubpath::default());
}
#[trial]
fn a_handed_in_draft_with_nothing_kept_loses_its_anchors_to_undo() {
let mut pen = pen();
let given_back = EditSubpath::new(vec![Knot::corner(doc(60.0, 60.0)), Knot::corner(doc(160.0, 60.0)), Knot::corner(doc(160.0, 160.0))], false);
pen.fixture.tool_mut().draft(Some(PenDraft::Own(&given_back)));
pen.fixture.fixture().settle();
assert!(pen.fixture.tool_mut().remove_last());
pen.sync();
assert_eq!(pen.draft().knots.len(), 2, "the handed-in anchors are the selected tool's to take back");
assert!(pen.log.iter().all(|e| !matches!(e, PenEvent::Finished(_))), "the draft stays open");
}
// ---- Curvature mode ----
// Curvature mode with `kept(2)`: a handed-in draft resumes, a new click keeps knots 0 and 1's handles but bends knot
@@ -319,6 +332,29 @@ fn a_curvature_resume_keeps_the_carried_knots_and_replace_updates_the_draft() {
pen.release(at, Modifiers::NONE);
}
// The carried anchors of a resumed path keep their handles: a double click on one that is not the carried end changes
// nothing, and a press on one adds an anchor instead of moving it.
#[trial]
fn carried_anchors_are_neither_toggled_nor_grabbed() {
let mut pen = pen();
let carried = EditSubpath::new(
vec![Knot::corner(doc(60.0, 60.0)), Knot::smooth(doc(160.0, 60.0), doc(180.0, 40.0)), Knot::corner(doc(160.0, 160.0))],
false,
);
{
let mut tool = pen.fixture.tool_mut();
tool.mode(PenMode::Curvature);
tool.kept(3);
tool.draft(Some(PenDraft::Own(&carried)));
}
pen.fixture.fixture().settle();
pen.click_doc(60.0, 60.0);
pen.click_doc(60.0, 60.0);
let draft = pen.draft();
assert_eq!(draft.knots[..3], carried.knots[..], "the carried anchors keep their handles and places");
assert!(draft.knots.len() > 3, "a press on a carried anchor adds an anchor");
}
/// Three clicks of the Curvature Pen far apart give three anchors, each click an `Update`: on its own, and with the
/// host handing the draft it holds back in before every frame, as an app's per-pass configuration does.
#[trial]