From 3f77778c9e4bd2dea8b8939542ed899cf72fbea9 Mon Sep 17 00:00:00 2001 From: Kayshen-X Date: Tue, 12 May 2026 22:37:54 +0800 Subject: [PATCH] fix(shell): Open resets undo history + stale UI state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Codex caught: `apply_payload` only swapped pages + active page + cleared selection — it left `history.past` / `history.future` holding snapshots of the OLD doc, plus every UI slot that may carry a `NodeId` from the old tree (pen_in_progress, text_editing, layer_rename, color_picker target, property_focus, agent_settings_drag, layer_context_menu). After Open the user could Cmd+Z back into the previous doc, or a stale `pen_in_progress = Some(NodeId)` from before the load would point at a non-existent row on the next press. Wipe history both directions and clear every NodeId-carrying UI slot + drafts + open dropdowns so the loaded doc starts on a clean slate. --- crates/openpencil-desktop/src/persistence.rs | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/crates/openpencil-desktop/src/persistence.rs b/crates/openpencil-desktop/src/persistence.rs index 6bdf1b40b..a4ead7931 100644 --- a/crates/openpencil-desktop/src/persistence.rs +++ b/crates/openpencil-desktop/src/persistence.rs @@ -144,7 +144,29 @@ pub fn apply_payload(doc: &mut Document, payload: DocPayload) -> Result<(), Stri } else { doc.active_page_index = payload.active_page_index.min(doc.pages.len() - 1); } + // Open replaces the document tree — every reference to a NodeId + // from the previous doc would point at a dead row. Wipe the + // undo stack + any in-progress UI state so a stale `pen_in_progress + // = Some(NodeId(42))` left over from the old doc doesn't trigger + // a phantom pen render or a panic on the next press. doc.clear_selection(); + doc.history.past.clear(); + doc.history.future.clear(); + doc.ui.pen_in_progress = None; + doc.ui.pen_cursor_doc = None; + doc.ui.pending_pen_history = None; + doc.ui.text_editing = None; + doc.ui.layer_rename = None; + doc.ui.color_picker = None; + doc.ui.property_focus = None; + doc.ui.property_input_draft.clear(); + doc.ui.settings_input_draft.clear(); + doc.ui.agent_settings.focus = None; + doc.ui.agent_settings_drag = None; + doc.ui.layer_context_menu = None; + doc.ui.locale_picker_open = false; + doc.ui.shape_picker_open = false; + doc.ui.fill_type_picker_open = false; Ok(()) }