From 576c2fe4e163900b8a017f2fe69c07bab4eb8d14 Mon Sep 17 00:00:00 2001 From: Kayshen-X Date: Tue, 12 May 2026 21:32:07 +0800 Subject: [PATCH] fix(shell): pen-tool undo restores pre-pen state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `start_pen_path` was pushing the new Path node onto the page first and only then calling `snapshot_for_history()` — the snapshot ended up including the new node, so a subsequent undo restored "node already present" and the path stayed on screen. Take the snapshot before any mutation; finish_pen_path still gates the push on `anchor_count >= 2` so single-anchor paths don't pollute the undo stack. --- crates/openpencil-shell-core/src/document/pen.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/openpencil-shell-core/src/document/pen.rs b/crates/openpencil-shell-core/src/document/pen.rs index 369dfb64f..8b764775b 100644 --- a/crates/openpencil-shell-core/src/document/pen.rs +++ b/crates/openpencil-shell-core/src/document/pen.rs @@ -17,6 +17,10 @@ impl Document { let raw = (*next_id).max(safe); *next_id = raw.checked_add(1)?; let id = NodeId::new(raw); + // Snapshot BEFORE any mutation so a later undo restores the + // pre-pen document state — otherwise the snapshot already + // contains the new path node and undo is a no-op visually. + let pre = self.snapshot_for_history(); let mut node = Node::leaf(raw, NodeKind::Path, "Path") .with_stroke(crate::Color::BLACK, 2.0) .with_bounds(crate::Rect { @@ -27,7 +31,7 @@ impl Document { let active = self.active_page_index; let page = self.pages.get_mut(active)?; page.children.push(node); - self.ui.pending_pen_history = Some(self.snapshot_for_history()); + self.ui.pending_pen_history = Some(pre); self.ui.pen_in_progress = Some(id); self.set_single_selection(id); Some(id)