#7822: Spec, plan and ADR for closing document windows (#7840)

Agreed rules for every close path (window close, File › Close, quit, one-document hosts, automation), who owns the Save prompt, and the order of work: craft-libs hidden root and test kit, repin, then the WordCraft fix. Adds Document window and Save prompt to the glossary.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Martin Ball
2026-11-26 00:19:22 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 77a530b179
commit a44dc74fc0
4 changed files with 127 additions and 0 deletions
+13
View File
@@ -66,3 +66,16 @@ _Avoid_: Tofu, box, notdef character
A picture the document refers to that the output cannot include: its part is absent, its data
cannot be decoded, its format is unsupported, or it is linked from outside the file.
_Avoid_: Broken image, dropped image
## Windows
**Document window**:
A top-level window showing one open document. A document can be shown in several document windows
at once (View › New Window); each window shows exactly one.
_Avoid_: Viewport (that is the framework's surface), root window, tab
**Save prompt**:
The question "Save changes?" asked when the last document window showing a document with unsaved
changes is about to close. It belongs to that window and appears there; while it is up, the other
windows wait.
_Avoid_: Close guard, save dialog, unsaved-changes dialog
+14
View File
@@ -0,0 +1,14 @@
# The root window hides instead of taking over another document
The first window an app opens is the framework's root window, and the app ends when it closes. WordCraft
used to keep it open when the user closed it with other document windows open: it switched to another
window's document and that window disappeared, so the window the user closed stayed and a different one
vanished (#7752). We decided instead that the root window hides (storytold/craft-libs#584) and adopts
nothing, so the window the user closes is always the one that goes, and the app ends only when the last
document window closes or the user quits.
## Considered options
- **Root takes over another document** (the old behaviour): no framework change, but it is the reported bug,
and the moved window loses its position and size.
- **Root shows no document and stays visible**: leaves an empty window the user did not ask for.
+38
View File
@@ -0,0 +1,38 @@
# Implementation plan: closing document windows
For [the specification](../specs/multi-window-close.md) (#7822, part of #7671). Steps run in order; each is
its own PR.
## 1. craft-libs prerequisites (base `next`)
- **Hidden root window** (storytold/craft-libs#584, [ADR 0001](../adr/0001-hidden-root-window.md)): the root
OS window can close or hide while document windows stay open, and adopts nothing.
- **Test kit with any number of windows** (C5-8): `craft_ui_test_kit::windows::Windows` keeps focus and close
events per child, so three-window tests can target one child.
- Not in scope: the cursor on non-root windows (C5-5, `cursor::image_target`) gets its own issue.
## 2. Repin craft-libs in WordCraft
Brings storytold/craft-libs#3290 (one modal stack across windows) and the two changes above. Stage 3 of
#7671 does this repin.
## 3. WordCraft PR (Fixes #7822)
Code is in `crates/ui-egui/src/` unless noted.
| Spec rule | Change |
|---|---|
| 1 | Drop the root taking over another document (`settle_windows` in `windows.rs`); closing the root window closes it through step 1's hidden root. Rewrite `closing_a_window_asks_first_and_the_root_adopts_another`. |
| 2 | `other_window_on_document` filters windows by document (C5-6). |
| 3, 9 | `app::Closing` records the window that owns the prompt. Pumping, cancelling and closing the prompt use that window's dialog host, not the active one. |
| 4, 9 | `run` and `close_decision` check `closing` first: a close while one is running raises the prompt and returns, never calls `begin_closing` again. |
| 9 | Clearing `closing.prompt` in `sync_document` and `apply_close_answer` either continues the close or ends it, so no `Hold` is left with no prompt. `apply_close_answer` matches the guard's document, not the active one. |
| 5 | Cancelling Save As during a close cancels the close or quit (`continue_after_save`). |
| 7 | On one-document-at-a-time hosts, the root close acts on the active entry, not `root_key()`; the fallback layer keeps its close button (C5-1..3). |
| 8 | `file.close` takes `save`; `close_refused` names both options. `ui.closeWindow` takes `window` and defaults to `active_viewport()` (`control/handler.rs`). Update `docs/control-protocol.md` and `docs/mcp.md`. |
| — | `queue_for_window` repaints the target window (C5-7). |
Tests: the table in the specification, in `windows/tests.rs` and `documents.rs`.
Checks: `cargo fmt --all --check`, `cargo clippy -p wordcraft-ui-egui --all-targets -- -D warnings`,
`cargo test -p wordcraft-ui-egui`, `cargo xtask layers`, `cargo xtask wasm`.
+62
View File
@@ -0,0 +1,62 @@
# Closing document windows
Specification for #7822 (part of #7671). Terms are defined in [GLOSSARY.md](../../GLOSSARY.md): **Document
window**, **Save prompt**. The decision on the root window is [ADR 0001](../adr/0001-hidden-root-window.md).
## Rules
1. **A close acts on what its window shows.** Closing a document window (its ✕, the OS close, Ctrl+W or
Ctrl+F4, File › Close) acts only on the document that window shows. No other window changes what it
shows, and no other window disappears. This holds for the first window opened too.
2. **Another window on the same document.** When another document window still shows the document, the
window closes without a Save prompt. The Save prompt is asked only when the last window showing a
document with unsaved changes closes.
3. **The Save prompt belongs to its window.** It appears in the window being closed. While it is up, the
other windows wait; clicking one of them brings the prompt to the front. Its answer is taken from it
whichever window is active.
4. **One close at a time.** A second close request made while a Save prompt is up brings that prompt to the
front and is otherwise ignored, not queued.
5. **Answers.**
- Save: the document is saved, then the window closes.
- Don't Save: the window closes without saving.
- Cancel: nothing closes.
- Save on a never-saved document opens Save As; cancelling Save As cancels the whole close (or quit).
6. **Quit.** Quit asks about each document with unsaved changes in turn, in a window showing it. Save and
Don't Save move to the next; Cancel stops the quit and leaves every remaining window as it was.
7. **One document at a time.** On a host that draws one document at a time (the web app, a full-screen
window, a maximized window on Wayland), ✕, Ctrl+W and File › Close act on the document on screen, and
the most recently used other document is shown next.
8. **Automation follows the same rules.** Over the control channel and MCP:
- `file.close` on a document with unsaved changes needs `save: true` (save, then close) or `force: true`
(close without saving). With neither it closes nothing and returns "unsaved changes: pass save or force".
A close that rule 2 lets through needs neither.
- `ui.closeWindow` takes an optional `window` id and otherwise closes the active window, never the root.
- Automation never leaves a Save prompt waiting for an answer nobody will give.
9. **Nothing gets stuck.** After any answer, cancel, or the prompt's window closing, the app accepts the
next close or quit. In particular:
- a second close cannot replace the first and leave its prompt on screen unanswered;
- clearing the prompt always ends or continues the close, never leaves it half-done;
- cancelling the prompt works whichever window is active;
- an answer given while another window is active is still read;
- an answer over the control channel from another window does not cancel the close.
## Tests
One test per rule and per stuck path; unit tests under 100 ms, app-harness tests under 1 s (#7671).
| Test | Rule |
|---|---|
| Closing the first window of three leaves the other two showing their own documents | 1 |
| Closing a middle window of three closes only its document | 1 |
| ✕ on one of two windows on a modified document asks nothing | 2 |
| ✕ on the last window of a modified document asks, in that window | 2, 3 |
| Clicking another window while the prompt is up raises the prompt | 3 |
| Answering the prompt while another window is active is read | 3, 9 |
| Ctrl+W and ✕ while a prompt is up leave one prompt, and its answer is read | 4, 9 |
| Save, Don't Save and Cancel each do what rule 5 says | 5 |
| Cancelling Save As cancels the close and the quit | 5, 6 |
| Quit with three modified documents asks three times; Cancel on the second keeps two | 6 |
| One-at-a-time host: ✕ closes the document on screen and shows the last used other one | 7 |
| `file.close` without `save`/`force` refuses; with each, saves or discards | 8 |
| `ui.closeWindow` closes the active window, or the one named by `window` | 8 |
| After each of the five stuck paths, a following close and quit both succeed | 9 |