Skip to content

fix(html): keep menu shortcuts working over a page that failed to load - #1176

Open
pavel-alay wants to merge 1 commit into
umputun:masterfrom
pavel-alay:fix-failed-page-keys
Open

pavel-alay wants to merge 1 commit into
umputun:masterfrom
pavel-alay:fix-failed-page-keys

Conversation

@pavel-alay

Copy link
Copy Markdown

Problem

A page that fails to load can leave its window deaf to every menu shortcut. Repro: open a page whose file lives in a temp dir (session overlay open --html $TMPDIR/x.html --companion), restart the Mac or delete the file, relaunch, select the page session. The panel shows "The page could not be loaded", and the command palette, Go to Session, next/previous session and the other menu chords do nothing in that window. Only ⌘W works.

Cause

Selecting the session makes the failed page's HtmlOverlayWebView first responder. When the page failed before WebKit started its web content process (a file page without --cwd whose file is gone fails in loadOriginal before any load), or after that process terminated, WKWebView still claims every key equivalent in performKeyEquivalent and then drops it, because there is no process to send it to. ⌘W survives only because CustomCommandRunner matches close_session while a page holds focus.

Fix

  • HtmlOverlayWebView.becomeFirstResponder refuses while the page's load state is failed. It reads the model live through HtmlOverlayPage (loadFailed): a restored page fails inside makeNSView, so updateNSView still holds the loading value and no copy taken there would be current. becomeFirstResponder is the gate because NSWindow.makeFirstResponder never asks acceptsFirstResponder.
  • HtmlWebViewHost.updateNSView gives up focus when a focused page fails, and re-arms so a reload that loads takes the keyboard back.

The keys then go to the menu as in any window without a focused view. The hidden terminal under the page does not get them. A replica page on a viewing Mac goes through the same view and model, so the remote path needs nothing extra.

Tests

  • HtmlOverlayViewTests.testAFailedPageLeavesMenuChordsToTheMenuUntilAReloadLoadsIt: a restored page with a missing file refuses focusCover, a ⌘K key equivalent is not swallowed, a reload takes focus back, and a web content process termination gives it up again. Fails on master (⌘K swallowed), passes here.
  • make test-app (2752 tests) and make lint pass. swift test has one failure, RemoteSessionTests.controlReachesTheCliWithEveryArgumentIntactUnderAnyLoginShell with /opt/homebrew/bin/fish; this change touches no agtermCore code. Likely the local fish config, not confirmed.
  • Checked by hand in an isolated Debug instance with the restart repro above.

A page that failed before WebKit started its web content process (a restored
file page whose file is gone, or a page whose process terminated) still took
first responder when its session was selected. WebKit then claimed every key
equivalent and dropped it, so the palette, session navigation and every other
menu chord did nothing in that window; only close_session got through, via
CustomCommandRunner.

HtmlOverlayWebView.becomeFirstResponder now refuses while the page's model
load state is failed, read live through the page because a restored page fails
inside makeNSView, before any view update sees it. HtmlWebViewHost gives up
focus when a focused page fails and takes it back once a reload loads.
@pavel-alay
pavel-alay marked this pull request as ready for review December 6, 2026 14:34
@pavel-alay
pavel-alay requested a review from umputun as a code owner December 6, 2026 14:34

@umputun umputun left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fix works for menu chords, but refusing focus on every failed page breaks the Esc close path.

  • agterm/Views/HtmlOverlayView.swift:234 + agterm/Views/HtmlOverlayWebView.swift:69 - a viewer (text, log, markdown, image, pdf) whose reload fails no longer closes on Esc or ⌘., both just beep. onCancel needs the web view to be first responder, and the page gives it up as soon as it fails. Repro: open a text file as a viewer, delete the file, reload, press Esc. On master it closes, as site/docs.html says.
  • same lines - with the window itself as first responder, macOS 26 turns an unclaimed ⌘. into NSWindow.cancelOperation:, which takes a full-screen window out of full screen (#753). I couldn't test it on 26, on 27 both keys only beep.

what the fix needs to keep true: menu chords reach the menu over a failed page, a failed viewer still closes on Esc and ⌘., and neither key reaches the window's cancelOperation:. One way that fits: keep the web view focusable (drop the becomeFirstResponder guard and the resign in updateNSView), return false from performKeyEquivalent while loadFailed is true so the menu gets the chord, and send Esc and an unclaimed ⌘. from keyDown straight to the existing cancelOperation/onCancel path, since WebKit drops key events when no web content process runs. Pls cover Esc and ⌘. on a failed viewer with no document, plus ⌘K reaching the menu.

two small ones:

  • the loadFailed doc comment and the new control-api.md sentence say the same thing, keep it in one place and have the other point to it.
  • the loadFailed closure spells out the id-to-page lookup that already exists in a few places (e.g. AppStore.replicaPageState). Pls add one agtermCore helper next to htmlOverlaySlot that returns the page's HtmlOverlay for an id and use it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants