S057: quit and window close run beforeunload through the Leave dialog

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
astar76 2026-11-14 07:16:15 -06:00
commit 1f9d3c9c3e
6 changed files with 156 additions and 6 deletions

View file

@ -26,7 +26,7 @@ Bugs the user and S048/S056 found that are small but visible every day.
## Steps
- [x] Wheel over floating egui surfaces scrolls them, never the page; Customize sheet scrolls to its last row; regression smoke (wheel over the sheet, assert scroll offset; page scroll unchanged).
- [x] Colour picker chips render (font fallback or no emoji).
- [ ] beforeunload on window close / quit through Nacre's dialog; smoke with a page that sets it.
- [x] beforeunload on window close / quit through Nacre's dialog; smoke with a page that sets it.
- [ ] Site colour from the favicon (dominant vivid colour, unit-tested on a few icons: red, blue, monochrome → none), applied when Colour source = Website; works on WebKit; `colours.txt` smoke updated; shots of two coloured sites and one monochrome site read.
- [ ] Hidden smokes launch, basics, input, chrome, newtab green; shots read.
- [ ] PROGRESS entry, INDEX line, tracker, commit `S057: …`, push.
@ -41,4 +41,4 @@ Bugs the user and S048/S056 found that are small but visible every day.
Which surfaces now take the wheel.
## Resume
Boxes 1-2 done (Area allocate_rect fix in customize.rs; 📋 aliased to content_copy in tools/icons.py). Next: box 3 beforeunload.
Boxes 1-3 done (beforeunload: App::begin_shutdown/shutdown_answered/cancel_shutdown in app.rs, smoke unload-quit.txt). Next: box 4 favicon colour.

View file

@ -856,7 +856,7 @@ impl App {
},
Command::Quit => self.quit(),
Command::RestartBrowser => {
self.quit();
self.quit_now();
if let Ok(exe) = std::env::current_exe() {
let mut command = std::process::Command::new(exe);
command.args(["--profile", &self.shared.paths.profile]);
@ -1509,6 +1509,7 @@ impl App {
} else {
// Stay: a close waiting for this answer is off.
self.windows.get_mut(&w).expect("found").tabs[i].closing = None;
self.cancel_shutdown();
request.deny();
}
},

View file

@ -195,6 +195,14 @@ impl Shared {
}
}
/// Quit (`window` None) or closing one window, while its pages run `beforeunload` (WebKit `try_close`). The tabs
/// stay until every page has answered: if one stays, the others are reloaded and nothing was closed.
pub struct Shutdown {
window: Option<WindowId>,
waiting: Vec<u64>,
answered: Vec<u64>,
}
pub struct App {
pub gtk: gtk::Application,
/// This app's own handle, for interface closures.
@ -225,6 +233,8 @@ pub struct App {
/// `Permissions::revision` the hub and the file were last updated for.
pub permissions_seen: u64,
pub exiting: bool,
/// A quit or window close waiting for the pages' `beforeunload`.
pub shutdown: Option<Shutdown>,
last_tick: Instant,
last_save: Instant,
last_slow: Instant,
@ -348,6 +358,7 @@ impl App {
applied: settings,
permissions_seen: 0,
exiting: false,
shutdown: None,
last_tick: Instant::now(),
last_save: Instant::now(),
last_slow: Instant::now() - Duration::from_secs(3600),
@ -698,6 +709,9 @@ impl App {
let tab = &self.windows[&w].tabs[i];
// Closed by the user (after beforeunload): kept in recently closed; by `window.close()`: not.
let (tab_id, remember) = (tab.id, tab.closing.is_some());
if remember && self.shutdown_answered(tab_id) {
return;
}
self.close_tab(w, tab_id, remember);
}
},
@ -992,7 +1006,13 @@ impl App {
let origin = Url::parse(&win.tabs[i].url).map(|u| u.origin().ascii_serialization()).unwrap_or_default();
win.tabs[i].dialog = Some(crate::tabs::Dialog::Simple(dialog, origin));
},
Control::LeavePage(request) => self.windows.get_mut(&w).expect("found").tabs[i].dialog = Some(crate::tabs::Dialog::LeavePage(request)),
Control::LeavePage(request) => {
self.windows.get_mut(&w).expect("found").tabs[i].dialog = Some(crate::tabs::Dialog::LeavePage(request));
// A quit or window close asks tab by tab: show the one that asks.
if self.shutdown.is_some() {
self.select_tab(w, i);
}
},
Control::ContextMenu(menu) => {
let win = self.windows.get_mut(&w).expect("found");
let tab = &mut win.tabs[i];
@ -1052,6 +1072,9 @@ impl App {
// A closing tab whose page doesn't answer (hung, or no close signal) goes after 3 s, unless it is asking.
let hung: Vec<(WindowId, u64)> = self.windows.iter().flat_map(|(w, win)| win.tabs.iter().filter(|t| t.dialog.is_none() && t.closing.is_some_and(|c| now.duration_since(c) > Duration::from_secs(3))).map(|t| (*w, t.id))).collect();
for (w, tab) in hung {
if self.shutdown_answered(tab) {
continue;
}
self.close_tab(w, tab, true);
}
// Blocked counts for the toolbar.
@ -1461,6 +1484,84 @@ impl App {
self.quit();
return;
}
if !self.begin_shutdown(Some(window)) {
self.close_window_now(window);
}
}
/// Asks every page of the window (all windows when None) to run `beforeunload`; false when none needs asking.
/// Pages that don't mind answer at once (`Event::Closed`), one that asks shows Nacre's "Leave this page?".
fn begin_shutdown(&mut self, window: Option<WindowId>) -> bool {
if self.shutdown.is_some() {
return true;
}
let mut waiting = Vec::new();
for (_, win) in self.windows.iter_mut().filter(|(id, _)| window.is_none_or(|w| w == **id)) {
for tab in win.tabs.iter_mut() {
if tab.closing.is_none()
&& tab.crashed.is_none()
&& tab.page.is_none()
&& let Some(view) = tab.web.as_ref().map(|web| web.webview.clone())
{
tab.closing = Some(Instant::now());
waiting.push(tab.id);
view.close();
}
}
}
if waiting.is_empty() {
return false;
}
if window.is_none() {
// The tabs stay in the session whatever the pages answer.
self.save_session();
}
self.shutdown = Some(Shutdown { window, waiting, answered: Vec::new() });
true
}
/// A page answered `beforeunload` with yes (or hung); when all have, the quit or window close goes on.
/// False when the tab isn't part of a shutdown.
fn shutdown_answered(&mut self, tab_id: u64) -> bool {
let Some(s) = self.shutdown.as_mut() else { return false };
let Some(at) = s.waiting.iter().position(|t| *t == tab_id) else { return false };
s.answered.push(s.waiting.remove(at));
if s.waiting.is_empty()
&& let Some(s) = self.shutdown.take()
{
for tab in self.windows.values_mut().flat_map(|w| w.tabs.iter_mut()) {
tab.closing = None;
}
match s.window {
Some(w) => self.close_window_now(w),
None => self.quit_now(),
}
}
true
}
/// A page said "Stay": the quit or window close is off, and the pages that had already said yes are reloaded.
pub fn cancel_shutdown(&mut self) {
let Some(s) = self.shutdown.take() else { return };
for tab in self.windows.values_mut().flat_map(|w| w.tabs.iter_mut()) {
if s.answered.contains(&tab.id) {
if let Some(web) = &tab.web {
web.webview.reload();
}
}
if s.answered.contains(&tab.id) || s.waiting.contains(&tab.id) {
tab.closing = None;
}
}
}
fn close_window_now(&mut self, window: WindowId) {
let Some(win) = self.windows.get(&window) else { return };
let normal_left = self.windows.values().filter(|w| w.kind == Kind::Normal).count();
if win.kind == Kind::Normal && normal_left <= 1 {
self.quit_now();
return;
}
if let Kind::Pip { window: source, tab } = win.kind.clone() {
self.exit_pip(source, tab);
}
@ -1481,7 +1582,18 @@ impl App {
}
}
/// Quit, after the pages had their say on `beforeunload`.
pub fn quit(&mut self) {
if self.exiting {
return;
}
if !self.begin_shutdown(None) {
self.quit_now();
}
}
/// Quit without asking the pages.
pub fn quit_now(&mut self) {
if self.exiting {
return;
}

View file

@ -134,7 +134,7 @@ fn main() {
});
gtk_app.run_with_args::<&str>(&[]);
if let Some(app) = keep.borrow_mut().take() {
app.borrow_mut().quit();
app.borrow_mut().quit_now();
}
}

View file

@ -816,7 +816,7 @@ impl App {
self.selftest_log(&result);
},
"log" => self.selftest_log(rest),
"quit" => self.quit(),
"quit" => self.quit_now(),
_ => self.selftest_log(&format!("unknown line: {line}")),
}
}

View file

@ -0,0 +1,37 @@
# Quit and closing a window run the page's beforeunload through Nacre's "Leave this page?" dialog (S057).
# Run: SMOKE_SIZE=1280x900 tools/smoke.sh tests/smoke/unload-quit.txt
# Expect: quit-ask shows the dialog; after Escape (stay) the tab is still there; quit again, answer Leave, the run
# ends by itself (the `log not reached` line never runs).
wait 3
resize 1280 900
set /theme/mode "dark"
open @PAGES@/dialogs.html
waitload
locate #t
wait 0.5
click @
wait 0.5
do quit
wait 1.2
shot quit-ask
expect /active/dialog true
key Escape
wait 0.5
expect /tab_count 1
status stayed
# Closing the window (last window: same as quit) asks too.
do close_window
wait 1.2
shot window-ask
expect /active/dialog true
key Escape
wait 0.5
expect /tab_count 1
status done
# Leave: the browser quits by itself.
do quit
wait 1
tap 700 542
wait 3
log not reached: the browser should have quit
quit