mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 02:54:25 +00:00
Pen: the anchors Undo and Delete never take back are the draft's kept run, so a draft handed back by Undo loses its anchors
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user