fix(shell): Open resets undo history + stale UI state

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.
This commit is contained in:
Kayshen-X 2026-05-12 22:37:54 +08:00
parent 87df5afcbb
commit 3f77778c9e

View file

@ -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(())
}