fix(tabs): close by stable document id instead of vector index

Bug:
Message::TabClose carried a vector index captured when the view message
was built. Closing several tabs in one batch (Close All / Close Others
with unsaved-changes dialogs, or a queue of continuation updates) let an
earlier close shift the vector before a later TabClose(idx) was handled:
self.tabs.remove(idx) either panicked (index past the end) or closed a
different tab than the one the user confirmed.

Reproduce:
1. Open several drawings and modify each so they all prompt on close.
2. Use Close All, or close two tabs in quick succession so both messages
   are handled in one batch.
3. Before: panic in remove(idx), or the wrong tab closes while the tab
   the user chose stays open (or its work is lost).
4. After: the chosen tab closes; an id already closed by an earlier
   message is a no-op.

Fix:
TabClose now carries the tab's stable u64 id, resolved to a vector
position when the message is handled, not when it is built;
PendingClose::Tab stores ids the same way. All construction sites pass
tab.id (a raw usize index no longer type-checks), and
DocTabCloseAll/DocTabCloseOthers build their queues from live ids at
handling time. A missing id means the tab is already gone, so the
handler returns Task::none().
This commit is contained in:
Karim Jerbi committed 2026-09-30 19:15:49 +01:00
1 parent 9c4aa5f896
commit 2d6f4fcfdf
10 files changed
+178 -25

No files matched your search

+10 -4
View File
@@ -1438,8 +1438,10 @@ pub(super) struct OpenCADStudio {
/// What triggered the "unsaved changes" dialog.
#[derive(Debug, Clone)]
pub(super) enum PendingClose {
/// User tried to close the tab at this index.
Tab(usize),
/// User tried to close the tab with this document id. Ids stay valid
/// while the dialog is open; a vector index would go stale the moment
/// another tab closes and Discard would remove the wrong tab.
Tab(u64),
/// User tried to quit the application.
Quit,
}
@@ -2417,8 +2419,12 @@ pub enum Message {
to: usize,
after: bool,
},
/// Close the given tab index.
TabClose(usize),
/// Close the tab with this document id. Carries the stable id, not a
/// vector index: iced dispatches every message built from one view
/// snapshot before rebuilding it, so an index captured alongside other
/// messages can already be stale (and point at a different tab) by the
/// time it is handled.
TabClose(u64),
/// Save every drawing that already has a file path.
DocTabSaveAll,
/// Close every non-Start drawing tab.