Skip to content

Web: keep the browser off chords an enabled command claims - #357

Merged
samitbasu merged 5 commits into
mainfrom
web-chords-343
Nov 18, 2026
Merged

samitbasu merged 5 commits into
mainfrom
web-chords-343

Conversation

@samitbasu

Copy link
Copy Markdown
Owner

Fixes #343

eframe 0.37 has WebOptions::should_prevent_default, but on_keydown never asks it about an egui::Event::Key. It uses a private hardcoded list (should_prevent_default_for_key), and upstream main does the same. So eggshell installs its own listener.

API for review

  • eggshell_ui::web::ClaimedChords: a cheap Clone handle (an Arc<Mutex<_>> kept in the egui context). It holds the chords the live tables' armed commands claimed in the last pass.
    • ClaimedChords::of(ctx: &egui::Context) -> ClaimedChords
    • ClaimedChords::claims(&self, pressed: egui::KeyboardShortcut) -> bool. This is true when a press of the chord would run a command, matched as dispatch matches it. Cut, copy and paste (Cmd+X/C/V) are never claimed: preventing those key presses would suppress the clipboard events they arrive as.
  • eggshell_ui::web::prevent_claimed_chords(canvas: &web_sys::HtmlCanvasElement, claimed: ClaimedChords) -> Result<(), JsValue> (wasm32 only). It adds a capture-phase keydown listener on the canvas's parent. The listener calls preventDefault, never stopPropagation, for a claimed chord pressed on the canvas or on eframe's text agent. Installing it is one line in the app creator:
    eggshell_ui::web::prevent_claimed_chords(&canvas, ClaimedChords::of(&cc.egui_ctx)).ok();
  • Problem::BrowserChord (new): in debug builds on the web, a command bound to a chord the browser keeps is reported. Those chords are Cmd/Ctrl+W, T, N, Q (with or without Shift), Ctrl+Tab, and Cmd+Shift+[ / ].

Shape

  • Dispatch and the claim snapshot share one walk of the live tables (Visit::Fire / Visit::Claim) and one per-command predicate (Command::armed). Enablement, yielding to a text field, modals and mounts therefore can't disagree between them.
  • The browser KeyboardEvent → egui chord mapping lives in one place (web::KeyDown). It mirrors eframe 0.37's modifiers_from_kb_event / translate_key / on_keydown's fallback to the key code, says so in a comment, and has tests that pin it.
  • New wasm-only dependencies: wasm-bindgen, web-sys. Both are already in the tree via eframe, so there are no new packages. Cargo.lock only gains the two dependency edges under eggshell-ui.
  • examples/common installs the listener for every web demo.

Behaviour changes

  • On the web, preventDefault is now called for key presses of chords that an enabled command claims.
  • Native behaviour is unchanged. The dispatch refactor is behaviour-preserving, and every existing test passes unchanged.

Tests

  • Through the real path (tests/it/commands.rs, claimed):
    • an enabled command's chord is claimed, and an unclaimed chord is not;
    • a disabled command's chord is not claimed;
    • a chord that yields while typing is not claimed while typing;
    • copy and paste are not claimed;
    • under a modal, only the modal's own chords are claimed.
  • Unit tests:
    • the browser-chord report fires on the web host in debug and not natively;
    • the reserved list itself;
    • the pinned key mappings: Cmd+G, Ctrl+Alt+G, Alt turning a key into a symbol, and shifted keys;
    • snapshot passes.
  • Headless Chrome (chromedriver) against cargo xtask web menu --release: a capture listener on window read event.defaultPrevented after dispatch.
    • Ctrl+Alt+G and Cmd+Alt+G (claimed by Show grid): true
    • Ctrl+Alt+H and Ctrl+G (unclaimed): false
  • cargo xtask ci: exit 0.

Manual check

This changes how keys behave in the browser, so it needs a manual try before approval. The build to try is the menu example: cargo xtask web menu. In Chrome, Ctrl/Cmd+Alt+G should toggle Show grid and do nothing in the browser.

🤖 Generated with Claude Code

samitbasu and others added 2 commits November 17, 2026 21:11
eframe 0.37 never consults WebOptions::should_prevent_default for key
presses (on_keydown uses a private hardcoded list), so a chord claimed by
a command also ran the browser's shortcut (Cmd+G ran Chrome's Find next).

- The draw now walks the live command tables twice through one visitor
  (Visit::Fire, Visit::Claim), so the chords it records as claimed are
  exactly the ones dispatch would fire: enabled, not yielding to a
  focused text field, in the live roots (modal-aware, mounts included).
- web::ClaimedChords: a cheap shared handle in the egui context holding
  the last pass's claimed chords (layouts drawn in one pass add up).
  Cut/copy/paste chords are never claimed: preventing their key press
  would suppress the clipboard events they arrive as.
- web::prevent_claimed_chords(&canvas, claimed) (wasm32): a capture-phase
  keydown listener on the canvas's parent that calls preventDefault (never
  stopPropagation) for a claimed chord pressed on the canvas or eframe's
  text agent. The KeyboardEvent -> egui chord mapping mirrors eframe
  0.37's modifiers_from_kb_event / translate_key in one place, with tests
  pinning it.
- examples/common installs it for every web demo.
- Problem::BrowserChord: in debug builds on the web, a command bound to a
  chord the browser keeps (Cmd/Ctrl+W, T, N, Q, Ctrl+Tab, Cmd+Shift+[ / ])
  is reported.

New wasm-only deps on wasm-bindgen and web-sys (already in the tree via
eframe; Cargo.lock only gains the two dependency edges).

Fixes #343

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PWiKKe8zfqGfVMCSC5L3up
Cargo.toml conflict: #339's Linux-only screenshot dependencies and this
branch's wasm-only wasm-bindgen/web-sys both kept, in separate target tables.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PWiKKe8zfqGfVMCSC5L3up
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PWiKKe8zfqGfVMCSC5L3up
samitbasu and others added 2 commits November 18, 2026 12:45
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PWiKKe8zfqGfVMCSC5L3up
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PWiKKe8zfqGfVMCSC5L3up
@samitbasu

Copy link
Copy Markdown
Owner Author

Approved by the user (2026-11-18). Gate (my run, merged with current main): cargo xtask ci exits 0. Merging.

@samitbasu
samitbasu merged commit bfdcd5d into main Nov 18, 2026
4 checks passed
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.

Web: a command's chord also triggers the browser's own shortcut (e.g. Cmd+G = Find next)

1 participant