plans: S070 media pass (now playing priority, previews, media keys, PiP, controls row)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
astar76 2026-11-18 06:33:53 -06:00
commit 41eb7d4c8f
9 changed files with 297 additions and 13 deletions

View file

@ -178,6 +178,12 @@ pub enum Command {
WindowDrag,
FocusPage,
CrashReload,
/// "This page isn't responding" → Wait: the bar goes away and comes back if the page still hasn't answered after
/// another `ui::dialogs::HANG_AFTER`.
HangWait(u64),
/// "This page isn't responding" → Reload page: kill its web process (every tab sharing it shows the crash page),
/// then load the page again.
HangReload(u64),
Quit,
RestartBrowser,
Toast(String),
@ -867,6 +873,25 @@ impl App {
}
}
},
Command::HangWait(tab_id) => {
if let Some((w, i)) = self.find_tab(tab_id) {
let tab = &mut self.windows.get_mut(&w).expect("found").tabs[i];
if tab.unresponsive.is_some() {
tab.unresponsive = Some(std::time::Instant::now());
}
}
},
Command::HangReload(tab_id) => {
if let Some((w, i)) = self.find_tab(tab_id) {
let tab = &mut self.windows.get_mut(&w).expect("found").tabs[i];
tab.unresponsive = None;
if let Some(web) = &tab.web {
eprintln!("tab {tab_id}: Reload page, its web process is killed");
web.webview.terminate();
web.webview.reload();
}
}
},
Command::Quit => self.quit(),
Command::RestartBrowser => {
self.quit_now();

View file

@ -608,6 +608,50 @@ impl App {
self.windows.get_mut(&w)?.tabs.get_mut(i)
}
/// WebKit's `is-web-process-responsive` changed, or (`answered`) a script sent to a page came back.
fn on_responsive(&mut self, id: ViewId, responsive: bool, answered: bool) {
let Some((w, i)) = self.find_webview(id) else { return };
let shown = self.windows[&w].active == i;
let tab = &mut self.windows.get_mut(&w).expect("found").tabs[i];
if responsive && !answered && !shown && tab.unresponsive.is_some() {
// WebKit calls a page responsive once its tab is hidden, busy or not: a script that comes back tells. A
// page still busy answers when it is done.
if let Some(web) = &tab.web {
let jobs = self.jobs_tx.clone();
web.webview.evaluate_javascript("0", move |_| {
let _ = jobs.send(Box::new(move |app: &mut App| app.on_responsive(id, true, true)));
});
}
return;
}
let url = tab.url.clone();
let line = match (responsive, tab.unresponsive) {
(false, None) => {
tab.unresponsive = Some(Instant::now());
format!("not responding {url}")
},
(true, Some(since)) => {
tab.unresponsive = None;
format!("responding again {url} after {:.1} s", since.elapsed().as_secs_f32())
},
_ => return,
};
eprintln!("{line}");
self.page_log(&line);
self.redraw(w);
}
/// A line in `pages.log` (crashes, pages that stopped answering), with the time; none in incognito.
fn page_log(&self, text: &str) {
if self.shared.incognito {
return;
}
use std::io::Write as _;
if let Ok(mut f) = std::fs::OpenOptions::new().create(true).append(true).open(self.shared.paths.crash_log()) {
let _ = writeln!(f, "{} {text}", util::format_stamp(util::now()));
}
}
pub fn redraw(&mut self, window: WindowId) {
if let Some(w) = self.windows.get(&window) {
w.redraw();
@ -727,21 +771,27 @@ impl App {
},
Event::Crashed(id, reason) => {
let url = self.tab_mut(id).map(|t| t.url.clone()).unwrap_or_default();
if !self.shared.incognito {
let line = format!("{} crash {url}: {reason}\n", util::format_stamp(util::now()));
use std::io::Write as _;
if let Ok(mut f) = std::fs::OpenOptions::new().create(true).append(true).open(self.shared.paths.crash_log()) {
let _ = f.write_all(line.as_bytes());
}
}
self.page_log(&format!("crash {url}: {reason}"));
let mut pip = None;
if let Some(tab) = self.tab_mut(id) {
tab.crashed = Some(reason);
tab.unresponsive = None;
tab.load_status = None;
// A dead page plays nothing.
tab.playing_audio = false;
tab.media = None;
pip = tab.pip_size.map(|_| tab.id);
}
if let Some((w, _)) = self.find_webview(id) {
// A dead page in the picture-in-picture window shows nothing: the tab takes its view back and
// shows the crash page.
if let Some(tab_id) = pip {
self.run(Command::ClosePip(tab_id));
}
self.redraw(w);
}
},
Event::Responsive(id, responsive) => self.on_responsive(id, responsive, false),
Event::Fullscreen(id, on) => {
if let Some((w, i)) = self.find_webview(id)
&& let Some(win) = self.windows.get_mut(&w)
@ -1020,6 +1070,7 @@ impl App {
tab.url = String::new();
tab.title = String::new();
tab.opener = Some(opener);
tab.process = self.windows[&w].tabs[i].process;
tab.load_status = Some(LoadState::Started);
tab.web = Some(Web { webview: view });
let win = self.windows.get_mut(&w).expect("found");

View file

@ -689,6 +689,8 @@ impl Engine {
hub.push(Event::Crashed(id, why.to_owned()));
});
let hub = self.hub.clone();
web.connect_is_web_process_responsive_notify(move |w| hub.push(Event::Responsive(id, w.is_web_process_responsive())));
let hub = self.hub.clone();
web.connect_enter_fullscreen(move |_| {
hub.push(Event::Fullscreen(id, true));
true

View file

@ -65,6 +65,8 @@ pub enum Event {
Closed(ViewId),
/// The web process died (crash, memory limit).
Crashed(ViewId, String),
/// WebKit's `is-web-process-responsive` changed (false: the page's process didn't answer in time).
Responsive(ViewId, bool),
Fullscreen(ViewId, bool),
/// A main-frame navigation waits for `NavigationRequest::allow`/`deny`.
Navigation(ViewId, NavigationRequest),

View file

@ -89,6 +89,11 @@ impl View {
self.web.stop_loading();
}
/// Kills the page's web process (and so every view sharing it); each gets `web-process-terminated`.
pub fn terminate(&self) {
self.web.terminate_web_process();
}
pub fn is_loading(&self) -> bool {
self.web.is_loading()
}

View file

@ -23,6 +23,8 @@
//! do <action> a shortcut action by its settings key, e.g. `do new_tab`
//! cmd <name> [arg] back, forward, reload, bookmark, install_app, pip, exitpip, free_memory, ping_managers,
//! update_lists, check_updates, close_tab, select <n>, zoom <step>,
//! hang_wait, hang_reload (the "isn't responding" bar's buttons), kill_page [n] (the n-th
//! tab's web process dies; default the active tab),
//! find <text>, download <url>, pause_download|resume_download|cancel_download [id] (newest),
//! import <n> (bookmarks, history, settings from the n-th found browser profile),
//! install_script <url> (a .user.js / .user.css), open_app [id] (default: the first installed app),
@ -454,6 +456,18 @@ impl App {
let tab = self.windows.get(&window).and_then(|w| w.tab()).map(|t| t.id).unwrap_or(0);
let page = self.windows.get(&window).and_then(|w| w.tab()).map(|t| t.url.clone()).unwrap_or_default();
let newest = self.shared.downloads.borrow().items.first().map(|d| d.id).unwrap_or(1);
if name == "kill_page" {
// The web process of the n-th tab (default: the active one) dies, as on a crash.
let view = self.windows.get(&window).and_then(|w| match arg.parse::<usize>() {
Ok(n) => w.tabs.get(n),
Err(_) => w.tab(),
});
match view.and_then(|t| t.web.as_ref()) {
Some(web) => web.webview.terminate(),
None => self.selftest_log("kill_page: no web page"),
}
return;
}
let command = match name {
"back" => Command::Back,
"forward" => Command::Forward,
@ -463,6 +477,8 @@ impl App {
"pip" => Command::EnterPip,
"exitpip" => Command::ClosePip(tab),
"free_memory" => Command::FreeMemory,
"hang_wait" => Command::HangWait(tab),
"hang_reload" => Command::HangReload(tab),
"ping_managers" => Command::PingManagers,
"update_lists" => Command::UpdateFilterLists,
"check_updates" => Command::CheckUpdates,
@ -898,7 +914,7 @@ impl App {
"active": i == w.active, "url": t.shown_url(), "title": t.display_title(), "loading": t.loading(),
"load_status": format!("{:?}", t.load_status), "has_web": t.web.is_some(), "frozen": t.frozen, "blocked": t.blocked,
"hidden_elements": t.hidden_elements, "pinned": t.pinned, "muted": t.muted, "playing_audio": t.playing_audio, "audible": t.audible(), "zoom": t.zoom,
"favicon": t.favicon.is_some(), "crashed": t.crashed, "can_back": t.can_go_back(), "can_forward": t.can_go_forward(),
"favicon": t.favicon.is_some(), "crashed": t.crashed, "unresponsive": t.unresponsive.map(|s| s.elapsed().as_secs_f32()), "can_back": t.can_go_back(), "can_forward": t.can_go_forward(),
"history_len": t.back_forward.len(), "find": [t.find.index, t.find.count], "prompts": t.prompts.len(),
"dialog": t.dialog.is_some(), "closing": t.closing.is_some(), "page": t.page.as_ref().map(|p| p.url()), "history_state": t.history_state.is_some(), "media": t.media.as_ref().map(|(m, _)| serde_json::json!({"title": m.title, "paused": m.paused, "duration": m.duration, "position": m.position})),
"theme_color": t.theme_color.map(crate::theme::to_hex), "favicon_colour": t.favicon_colour.map(crate::theme::to_hex), "site_colour": t.site_colour().map(crate::theme::to_hex), "pip": t.pip_size.is_some(),

View file

@ -225,6 +225,11 @@ pub struct Tab {
pub prompts: VecDeque<Prompt>,
pub dialog: Option<Dialog>,
pub crashed: Option<String>,
/// The page's web process stopped answering (WebKit `is-web-process-responsive`); since when.
pub unresponsive: Option<Instant>,
/// Tabs with the same value share a web process (a pop-up's related view shares its opener's): the tab id of the
/// first one.
pub process: u64,
/// The user closed the tab and its page runs `beforeunload` first (WebKit `try_close`); since when.
pub closing: Option<std::time::Instant>,
/// HTTPS-only upgraded this load from this URL.
@ -291,6 +296,8 @@ impl Tab {
dialog: None,
closing: None,
crashed: None,
unresponsive: None,
process: id,
upgraded_from: None,
redo: None,
cert_error: None,

View file

@ -67,6 +67,52 @@ fn dialog_actions(ui: &mut Ui, contents: impl FnOnce(&mut Ui)) {
});
}
/// How long a page's web process may go without answering before the "isn't responding" bar shows.
pub const HANG_AFTER: std::time::Duration = std::time::Duration::from_secs(5);
/// "This page isn't responding" over the active tab's page: Wait, or Reload page (kills its web process). Clears
/// itself when the page answers again (`Event::Responsive`).
pub fn hang_bar(root: &mut Ui, f: &mut Frame) {
let Some(tab) = f.tab() else { return };
let Some(since) = tab.unresponsive.filter(|_| tab.page.is_none() && tab.crashed.is_none() && tab.pip_size.is_none()) else { return };
let waited = since.elapsed();
if waited < HANG_AFTER {
root.ctx().request_repaint_after(HANG_AFTER - waited);
return;
}
let tab_id = tab.id;
// Pop-ups share their opener's web process: killing it reloads them too.
let others = f.tabs.iter().filter(|t| t.process == tab.process && t.id != tab_id && t.web.is_some()).count();
let text = match others {
0 => "This page isn't responding".to_owned(),
1 => "This page isn't responding. Reload page also stops 1 other tab that shares its process".to_owned(),
n => format!("This page isn't responding. Reload page also stops {n} other tabs that share its process"),
};
let p = f.palette();
let frame = egui::Frame::NONE.fill(p.surface_container_high).inner_margin(egui::Margin::symmetric(theme::GAP_L as i8, theme::GAP_S as i8));
egui::Panel::top("hang-bar").frame(frame).show(root, |ui| {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = theme::GAP_S;
ui.set_min_height(theme::CONTROL);
let (rect, _) = ui.allocate_exact_size(Vec2::splat(theme::ICON_LARGE), Sense::hover());
icon(ui, rect.center(), icons::INFO, theme::ICON, false, p.primary);
ui.add_space(theme::GAP_XS);
let mut buttons = ui.new_child(egui::UiBuilder::new().max_rect(ui.max_rect()).layout(egui::Layout::right_to_left(egui::Align::Center)));
buttons.spacing_mut().item_spacing.x = theme::GAP_S;
if button(&mut buttons, ButtonKind::Filled, None, "Reload page").clicked() {
f.commands.push(Command::HangReload(tab_id));
}
if button(&mut buttons, ButtonKind::Tonal, None, "Wait").clicked() {
f.commands.push(Command::HangWait(tab_id));
}
let used = buttons.min_rect().width();
let width = (ui.available_width() - used - theme::GAP_L).max(80.0);
let galley = super::menus::one_line(ui, &text, theme::font(theme::BODY, Weight::Medium), p.on_surface, width);
ui.add(egui::Label::new(galley));
});
});
}
pub fn prompt_bars(root: &mut Ui, f: &mut Frame) {
let Some(tab) = f.tab() else { return };
let Some(prompt) = tab.prompts.front() else { return };

View file

@ -182,6 +182,7 @@ pub fn draw(root: &mut egui::Ui, f: &mut Frame) {
chrome::bookmarks_bar(root, f);
}
}
dialogs::hang_bar(root, f);
dialogs::prompt_bars(root, f);
dialogs::find_bar(root, f);
}
@ -272,14 +273,12 @@ fn page_contents(ui: &mut egui::Ui, f: &mut Frame) {
ui.vertical_centered(|ui| {
ui.add_space(rect.height() * 0.25);
ui.heading("This page crashed");
ui.label("Something on the page broke the engine. Reloading usually fixes it.");
// Why: a crash, the memory limit, or the process stopped (Reload page on a busy page that shares it).
ui.label(format!("{reason} Reloading usually fixes it."));
ui.add_space(8.0);
if ui.button("Reload").clicked() {
f.send(Command::CrashReload);
}
ui.collapsing("Details", |ui| {
ui.monospace(reason);
});
});
return;
}