From 0b9508e67285fc973c5cba3957f281a30c66a7f6 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 10:30:30 -0800 Subject: [PATCH 1/4] craft-quit: add a README for the close hook, quit() and the Quit menu id The crate had only rustdoc. The README states where on_close_request goes in eframe::App::logic, the rules for the app's unsaved-changes check and prompt, what quit() does on each platform, and how an app with its own menu uses QUIT_MENU_ID and on_menu_event. The workspace README links to it. --- README.md | 2 +- crates/quit/README.md | 90 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 crates/quit/README.md diff --git a/README.md b/README.md index 9512980917..7bd3598bb5 100644 --- a/README.md +++ b/README.md @@ -346,7 +346,7 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit with a password. Salvages damaged files. Bounded and panic-free on hostile input. What an item means stays in each app. -- [`craft-quit`](crates/quit/src/lib.rs): native macOS quit, egui close handling and an optional +- [`craft-quit`](crates/quit/README.md): native macOS quit, egui close handling and an optional app menu. Unsaved-document decisions and save prompts stay in each app. - [`craft-rtf`](crates/rtf/README.md): Rich Text Format: a tokenizer, text decoding in the file's code pages diff --git a/crates/quit/README.md b/crates/quit/README.md new file mode 100644 index 0000000000..757327aa76 --- /dev/null +++ b/crates/quit/README.md @@ -0,0 +1,90 @@ +# craft-quit + +Quit a Craft app the platform's own way, and route an egui window close through that quit. On macOS +the quit is `-[NSApplication terminate:]`; on other platforms the crate does nothing and the window +closes as usual. Unsaved-document decisions and save prompts stay in each app. + +## Why macOS needs it + +An eframe app on macOS must not leave by closing its window. eframe drops the window while AppKit's +run loop is still running. In the next display cycle AppKit's Touch Bar finder tries to remove a KVO +observer from the view that is gone (`-[_NSTouchBarFinderObservation invalidate]`), which raises an +uncaught Objective-C exception, and the app crashes. `terminate:`, what ⌘Q does in winit's default +menu, ends the process before another display cycle runs. + +## Features + +| Feature | Adds | +| --- | --- | +| (none) | `quit()` | +| `egui` | `on_close_request`, `on_menu_event` and `QUIT_MENU_ID` | +| `menu` | `egui`, plus the deprecated `install_app_menu` (use `craft_menu::mac::install_app_menu`) | + +## Use + +Call `craft_quit::on_close_request(ctx)` once per frame, last in `eframe::App::logic`, after the +app's own close decision: + +```rust,ignore +fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { + if ctx.input(|i| i.viewport().close_requested()) + && self.has_unsaved_changes() + && !self.discard_confirmed + { + ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose); + self.show_save_prompt = true; + } + craft_quit::on_close_request(ctx); // last, so it sees the app's `CancelClose` +} + +fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) { + // ... the app's UI, including the prompt when show_save_prompt is true ... +} +``` + +When the root window was asked to close (the close button, the app's own `ViewportCommand::Close`, +the OS) and the app did not send `CancelClose` in the same frame, `on_close_request` cancels the +close and calls `quit()`. It returns whether it took the close over. It does nothing in a child +viewport, and it does nothing on platforms where `quit()` returns `false`. + +Rules for the app: + +- Keep the unsaved-changes check in `logic`, not in `ui`. `logic` runs before `ui`, so a check in + `ui` would come after the quit. eframe also calls `logic` while the window is minimized or hidden, + when `ui` does not run. +- Keep the prompt's state in the app and draw the prompt in `ui`. Send `ViewportCommand::Close` + only after Save has succeeded and cleared the dirty state, or after Don't Save has set the + confirmation flag. Cancel, a canceled file picker and a failed save keep the app open. +- Reset the prompt and confirmation state when the decision is canceled or a new document opens. +- Let an asynchronous save finish before sending Close. +- If the window is hidden, show it again before showing the prompt. +- Save what the app keeps in `eframe::App::on_exit`. winit's `applicationWillTerminate:` runs + eframe's exit handling before the process ends. Code after `eframe::run_native` in `main` does + not run on macOS; move it to `on_exit`. + +## `quit()` + +On macOS, `quit()` schedules `terminate:` on the main queue and returns `true`. The caller keeps its +window open, and the process ends once the current event has been handled. Call it while the event +loop is running (any eframe callback is). Return to the event loop before blocking: a blocking +native dialog opened in the same callback would run the quit while winit is still handling the +event, which panics. + +On other platforms `quit()` returns `false`. + +## A Quit menu item that asks first + +muda's `PredefinedMenuItem::quit` and winit's default app menu end the process without asking the +app. An app with its own native menu gives its Quit item the id `craft_quit::QUIT_MENU_ID` and +passes its menu events to `craft_quit::on_menu_event(ctx, id)`. For that id, `on_menu_event` sends +the root window `ViewportCommand::Close`, so the app's unsaved-changes check runs as it does for the +close button, and `on_close_request` then quits. It returns `false` for other ids and leaves them +to the caller. It can be called between frames; egui keeps the command for the next frame. + +An app without a menu of its own uses `craft_menu::mac::install_app_menu`, which builds a minimal +app menu with that Quit item. `craft_quit::install_app_menu` is deprecated in its favor. + +## Notes + +- No `unsafe`: the macOS calls go through objc2's safe API. +- The macOS dependencies (objc2, objc2-app-kit, dispatch2) are already in eframe apps' lock files. From 98eb2f740a9db13a792462377a3f3beed4b5a9e6 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 10:52:41 -0800 Subject: [PATCH 2/4] craft-quit: README states the platform, timing and error of the app menu install craft_menu::mac::install_app_menu exists only on macOS and needs a cfg guard. It is called once from the eframe app creator, and an error leaves winit's menu, whose Quit skips the unsaved-changes prompt. --- crates/quit/README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/quit/README.md b/crates/quit/README.md index 757327aa76..8a42780f05 100644 --- a/crates/quit/README.md +++ b/crates/quit/README.md @@ -82,7 +82,18 @@ close button, and `on_close_request` then quits. It returns `false` for other id to the caller. It can be called between frames; egui keeps the command for the next frame. An app without a menu of its own uses `craft_menu::mac::install_app_menu`, which builds a minimal -app menu with that Quit item. `craft_quit::install_app_menu` is deprecated in its favor. +app menu with that Quit item. `craft_menu::mac` exists only on macOS, so the call needs +`#[cfg(target_os = "macos")]`; other platforms need no call. Call it once, from the eframe app +creator: winit builds its default menu before that and replaces a menu installed earlier. The call +returns a `Result`. On error winit's menu stays, and ⌘Q quits without the unsaved-changes prompt; +the app decides whether that is fatal, and `?` passes the error out of the app creator: + +```rust,ignore +#[cfg(target_os = "macos")] +craft_menu::mac::install_app_menu(&cc.egui_ctx, "WordCraft")?; +``` + +`craft_quit::install_app_menu` is deprecated in its favor. ## Notes From 6b057b39731467a4fa0161155a4cb0c43b86b12a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 19:41:03 -0800 Subject: [PATCH 3/4] craft-quit: README describes the current API The README now covers quit(), the ui feature's intercept_close and on_menu_event, QUIT_MENU_ID, CloseGuard with its steps, and web::hold_leave on wasm32. The removed on_close_request, the egui and menu features and install_app_menu are no longer described; the app menu points to craft-menu. --- README.md | 5 +- crates/quit/README.md | 162 ++++++++++++++++++++++++------------------ 2 files changed, 96 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index 6bda79e270..db3378e37b 100644 --- a/README.md +++ b/README.md @@ -373,8 +373,9 @@ See the [release notes](CHANGELOG.md) for included functionality and known limit with a password. Salvages damaged files. Bounded and panic-free on hostile input. What an item means stays in each app. -- [`craft-quit`](crates/quit/README.md): native macOS quit, egui close handling and an optional - app menu. Unsaved-document decisions and save prompts stay in each app. +- [`craft-quit`](crates/quit/README.md): native macOS quit, a window close held for unsaved + documents, the Quit menu item id and the unsaved-changes question one document at a time + (`CloseGuard`). The prompt and the saving stay in each app. - [`craft-rtf`](crates/rtf/README.md): Rich Text Format: a tokenizer, text decoding in the file's code pages (`\uN`, `\'hh`, single-byte and East Asian multibyte pages), escaping and dates, and a reader and writer for a small neutral model (paragraphs, character formatting, outline levels, tables). diff --git a/crates/quit/README.md b/crates/quit/README.md index 8a42780f05..22417fe830 100644 --- a/crates/quit/README.md +++ b/crates/quit/README.md @@ -1,8 +1,9 @@ # craft-quit -Quit a Craft app the platform's own way, and route an egui window close through that quit. On macOS -the quit is `-[NSApplication terminate:]`; on other platforms the crate does nothing and the window -closes as usual. Unsaved-document decisions and save prompts stay in each app. +Quit a Craft app the platform's own way, hold a window close while documents are unsaved, and walk +the unsaved-changes question one document at a time. On macOS the quit is +`-[NSApplication terminate:]`; on other platforms `quit()` returns `false` and the app closes its +window as usual. The prompt itself and the saving stay in each app. ## Why macOS needs it @@ -12,88 +13,111 @@ observer from the view that is gone (`-[_NSTouchBarFinderObservation invalidate] uncaught Objective-C exception, and the app crashes. `terminate:`, what ⌘Q does in winit's default menu, ends the process before another display cycle runs. -## Features +## Features and targets -| Feature | Adds | +| Feature or target | Adds | | --- | --- | -| (none) | `quit()` | -| `egui` | `on_close_request`, `on_menu_event` and `QUIT_MENU_ID` | -| `menu` | `egui`, plus the deprecated `install_app_menu` (use `craft_menu::mac::install_app_menu`) | - -## Use - -Call `craft_quit::on_close_request(ctx)` once per frame, last in `eframe::App::logic`, after the -app's own close decision: - -```rust,ignore -fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { - if ctx.input(|i| i.viewport().close_requested()) - && self.has_unsaved_changes() - && !self.discard_confirmed - { - ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose); - self.show_save_prompt = true; - } - craft_quit::on_close_request(ctx); // last, so it sees the app's `CancelClose` -} - -fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) { - // ... the app's UI, including the prompt when show_save_prompt is true ... -} -``` - -When the root window was asked to close (the close button, the app's own `ViewportCommand::Close`, -the OS) and the app did not send `CancelClose` in the same frame, `on_close_request` cancels the -close and calls `quit()`. It returns whether it took the close over. It does nothing in a child -viewport, and it does nothing on platforms where `quit()` returns `false`. - -Rules for the app: - -- Keep the unsaved-changes check in `logic`, not in `ui`. `logic` runs before `ui`, so a check in - `ui` would come after the quit. eframe also calls `logic` while the window is minimized or hidden, - when `ui` does not run. -- Keep the prompt's state in the app and draw the prompt in `ui`. Send `ViewportCommand::Close` - only after Save has succeeded and cleared the dirty state, or after Don't Save has set the - confirmation flag. Cancel, a canceled file picker and a failed save keep the app open. -- Reset the prompt and confirmation state when the decision is canceled or a new document opens. -- Let an asynchronous save finish before sending Close. -- If the window is hidden, show it again before showing the prompt. -- Save what the app keeps in `eframe::App::on_exit`. winit's `applicationWillTerminate:` runs - eframe's exit handling before the process ends. Code after `eframe::run_native` in `main` does - not run on macOS; move it to `on_exit`. +| (always) | `quit()`, `QUIT_MENU_ID`, `CloseGuard`, `CloseScope`, `CloseAnswer` and `GuardStep` | +| `ui` | `intercept_close(ctx, unsaved)` and `on_menu_event(ctx, id)`, on an egui `Context` | +| `wasm32` | `web::hold_leave(unsaved)`, the browser's own leave prompt | ## `quit()` On macOS, `quit()` schedules `terminate:` on the main queue and returns `true`. The caller keeps its -window open, and the process ends once the current event has been handled. Call it while the event -loop is running (any eframe callback is). Return to the event loop before blocking: a blocking -native dialog opened in the same callback would run the quit while winit is still handling the -event, which panics. +window open, and the process ends once the current event has been handled. winit's +`applicationWillTerminate:` then runs eframe's exit handling, so the app saves what it keeps in +`eframe::App::on_exit`. Code after `eframe::run_native` in `main` does not run on macOS. -On other platforms `quit()` returns `false`. +Call it while the event loop is running (any eframe callback is). Return to the event loop before +blocking: a blocking native dialog opened in the same callback would run the quit while winit is +still handling the event, which panics. + +On other platforms `quit()` returns `false`, and the caller closes its window. + +## Holding a window close + +Call `craft_quit::intercept_close(ctx, unsaved)` each frame in `eframe::App::logic`, with whether any +document has unsaved changes. When the root window was asked to close (the close button, the OS, or +the Quit menu item) and `unsaved` is `true`, it sends `ViewportCommand::CancelClose` and +`ViewportCommand::Focus` and returns `true`. The app then starts a `CloseGuard` for +`CloseScope::Quit`. In every other case it returns `false` and the close goes on. It does nothing in +a child viewport. + +`intercept_close` does not call `quit()`. When it lets a close go on, the app calls `quit()` and, when +that returns `true`, sends `CancelClose` so that the window stays open until the process ends: + +```rust,ignore +fn logic(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { + // Called every frame, also while a guard is open, so a second close request is held too. + if craft_quit::intercept_close(ctx, self.has_unsaved_changes()) { + if self.guard.is_none() { + self.guard = Some(craft_quit::CloseGuard::new(craft_quit::CloseScope::Quit, self.modified_documents())); + } + } else if ctx.viewport_id() == egui::ViewportId::ROOT + && ctx.input(|i| i.viewport().close_requested()) + && craft_quit::quit() + { + ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose); + } +} +``` + +An app that also holds a close for another reason (a running export, for example) makes that +decision first and skips `quit()` in a frame where it sent `CancelClose` itself. + +Keep the check in `logic`, not in `ui`: eframe also calls `logic` while the window is minimized or +hidden, when `ui` does not run. If the window is hidden, show it again before showing the prompt. + +## The unsaved-changes question + +`CloseGuard` holds the decision for closing documents, closing all, quitting and Revert. It +does not use egui. `D` is the app's document id. `CloseGuard::new(scope, modified)` takes the +documents with unsaved changes in the order they are asked about. An empty list is already finished. + +The app shows the question for `current()` (craft-ui's `UnsavedChangesDialog`), passes the answer to +`answer(CloseAnswer)`, or to `answer_all` when "Apply to All" is ticked, and acts on the +`GuardStep` it gets back: + +| Step | The app | +| --- | --- | +| `Save(doc)` | Saves `doc` (Save As when it has no file), closes it once the save succeeds, then calls `saved()`. A canceled step such as Save As calls `save_failed()`; a write that failed calls `retry_after_failure()`. | +| `Discard(doc)` | Closes `doc` without saving, then calls `discarded()`. | +| `Ask(doc)` | Shows the question for `doc`. | +| `Cancelled` | Drops the guard. Nothing more closes, and a quit does not happen. | +| `Finished` | Finishes the operation. For `CloseScope::Quit`, it drops the guard and sends the root window `ViewportCommand::Close`; with nothing unsaved, `intercept_close` lets that close go on and the app calls `quit()` as above. | + +- For `CloseScope::Revert`, the prompt asks Revert and Cancel, and Revert answers + `CloseAnswer::DontSave`. On `Discard(doc)` the app reloads `doc` from its file instead of closing + it, then calls `discarded()`. +- `CloseAnswer::Cancel` ends the whole operation. Documents already closed stay closed. +- `answer_all` applies one answer to every remaining document, Save included. A canceled Save As + stops the operation. +- `retry_after_failure()` puts the document back at the front, clears the "Apply to All" answer and + asks about that document again. +- `forget(&doc)` drops a document that closed elsewhere while the guard ran. +- `remaining()` counts the documents still waiting, the current one included. The prompt shows + "Apply to All" when it is more than one. ## A Quit menu item that asks first muda's `PredefinedMenuItem::quit` and winit's default app menu end the process without asking the app. An app with its own native menu gives its Quit item the id `craft_quit::QUIT_MENU_ID` and passes its menu events to `craft_quit::on_menu_event(ctx, id)`. For that id, `on_menu_event` sends -the root window `ViewportCommand::Close`, so the app's unsaved-changes check runs as it does for the -close button, and `on_close_request` then quits. It returns `false` for other ids and leaves them -to the caller. It can be called between frames; egui keeps the command for the next frame. +the root window `ViewportCommand::Close`, even when called from a child viewport, so +`intercept_close` and the app's question run as they do for the close button. It returns `false` for +other ids and leaves them to the caller. It can be called between frames; egui keeps the command for +the next frame. -An app without a menu of its own uses `craft_menu::mac::install_app_menu`, which builds a minimal -app menu with that Quit item. `craft_menu::mac` exists only on macOS, so the call needs -`#[cfg(target_os = "macos")]`; other platforms need no call. Call it once, from the eframe app -creator: winit builds its default menu before that and replaces a menu installed earlier. The call -returns a `Result`. On error winit's menu stays, and ⌘Q quits without the unsaved-changes prompt; -the app decides whether that is fatal, and `?` passes the error out of the app creator: +craft-menu uses this id for its Quit item. An app without a menu of its own calls +`craft_menu::mac::install_app_menu` once from the eframe app creator; see +[craft-menu](../menu/README.md). -```rust,ignore -#[cfg(target_os = "macos")] -craft_menu::mac::install_app_menu(&cc.egui_ctx, "WordCraft")?; -``` +## Browser tabs -`craft_quit::install_app_menu` is deprecated in its favor. +A browser tab has no close request an app can answer. On `wasm32`, call +`craft_quit::web::hold_leave(unsaved)` each frame. While the last call passed `true`, the page's +`beforeunload` listener calls `prevent_default`, and the browser asks the user before leaving. The +first call adds the listener; a page without a window gets none, and a later call tries again. ## Notes From 9219c6c0eb7da3cc3c31597eb02c168ca5edd615 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 00:01:17 -0800 Subject: [PATCH 4/4] craft-quit: Note forget finishing the guard and the browser prompt being best effort --- crates/quit/README.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/quit/README.md b/crates/quit/README.md index 22417fe830..56f7e2cbf9 100644 --- a/crates/quit/README.md +++ b/crates/quit/README.md @@ -94,7 +94,9 @@ The app shows the question for `current()` (craft-ui's `UnsavedChangesDialog`), stops the operation. - `retry_after_failure()` puts the document back at the front, clears the "Apply to All" answer and asks about that document again. -- `forget(&doc)` drops a document that closed elsewhere while the guard ran. +- `forget(&doc)` drops a document that closed elsewhere while the guard ran. It returns no step. When + it removed the last waiting document and no save is in progress, `is_finished()` is `true`: the app then treats the operation + as `Finished` and does what the `Finished` row above says. - `remaining()` counts the documents still waiting, the current one included. The prompt shows "Apply to All" when it is more than one. @@ -116,8 +118,9 @@ craft-menu uses this id for its Quit item. An app without a menu of its own call A browser tab has no close request an app can answer. On `wasm32`, call `craft_quit::web::hold_leave(unsaved)` each frame. While the last call passed `true`, the page's -`beforeunload` listener calls `prevent_default`, and the browser asks the user before leaving. The -first call adds the listener; a page without a window gets none, and a later call tries again. +`beforeunload` listener calls `prevent_default`, and the browser may ask the user before leaving. +Browsers decide whether to show the prompt (some skip it when the page has had no user interaction), +and the prompt cannot be customized, so treat it as best effort. The first call adds the listener; a page without a window gets none, and a later call tries again. ## Notes