Skip to content

Keep unaccepted events across discarded passes - #8944

Open
simzy39 wants to merge 3 commits into
emilk:mainfrom
simzy39:retain-unconsumed-input-across-discard
Open

simzy39 wants to merge 3 commits into
emilk:mainfrom
simzy39:retain-unconsumed-input-across-discard

Conversation

@simzy39

@simzy39 simzy39 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Closes #8942

A discarded pass previously lost unhandled input before the next pass. This PR preserves unaccepted key, text, IME, clipboard, and AccessKit events in their original order.

Pointer, touch, wheel, zoom, modifier, and window-focus events are already incorporated into input state and are not replayed. Remaining smooth scroll input is carried as smooth_scroll_delta.

Implementation

  • InputState::events is now Events instead of Vec<Event>. Acceptance follows individual events through list mutations (remove, swap_remove, insert, retain, swap, reverse, rotate_*, sort_*, dedup*, drain, split_off, append, splice, truncate). Identical event values retain distinct identities.
  • Each event slot receives a globally unique monotonic internal id (EventId), so replacing the entire InputState::events collection (Events::from, core::mem::take, FromIterator, serde) cannot let stale navigation or Escape ids match unrelated events. Replacing an individual event (including via IndexMut or iter_mut) assigns a fresh unaccepted identity.
  • key_pressed, key_released, and num_presses remain observational. accept_key, accept_key_presses, accept_key_releases, accept_event, accept_index, and accept_accesskit_action keep events visible during the current pass while excluding them from replay; consume_* methods remove them immediately.
  • Replayed Event::Key events do not mutate keys_down or recompute repeat, preserving persistent keyboard state from pass 0 even when only a subset of key events is replayed (e.g. an accepted release, an accepted subsequent press, or a non-replayed WindowFocused(false)).
  • Keyboard (Space/Enter) and AccessKit Action::Click widget activations are accepted only when create_widget registers the widget, not during observational read_response / Ui::response queries. This prevents preliminary styling and popup reads from swallowing clicks while ensuring delivered clicks are not replayed.
  • Navigation tracks event identities rather than indices. An Escape that already cleared focus during the scan is accepted even when a later navigation key is applied. Applied AccessKit Action::Focus requests are accepted so a discarded pass does not replay them over a later focus change; focus requests for widgets not yet in the focus cache remain unaccepted for the next pass.
  • Built-in widgets, egui_extras's datepicker, and examples/keyboard_events accept or consume handled events. The wheel smoother advances only once per frame.

Breaking changes & migration

  • InputState::events changes from Vec<Event> to Events.
    • Events dereferences to [Event] and implements slice/index reads (Index<I: SliceIndex<[Event]>>), AsRef<[Event]>, Borrow<[Event]>, PartialEq with Vec<Event>/[Event]/&[Event], From<Vec<Event>>, From<Events> for Vec<Event>, into_vec(), FromIterator<Event>, IntoIterator (owned, &, &mut), Extend<Event>, and common Vec methods (new, with_capacity, capacity, reserve, as_slice, push, pop, insert, remove, swap_remove, clear, truncate, retain, swap, reverse, rotate_left, rotate_right, sort_*, dedup*, drain, split_off, append, splice, iter_mut).
    • Code that specifically requires &mut Vec<Event> or &mut [Event] needs to use Events methods directly (or convert with into_vec() / Events::from).
  • Swapping or modifying elements via mutable indexing (events[i] = ...) or iter_mut() resets those slots to fresh unaccepted events; use events.swap(a, b) or events.retain(...) to preserve acceptance across reorders or removals.
  • Application code that performs side effects after observational queries (key_pressed, key_released, num_presses, or raw events loops) should accept (accept_key, accept_key_presses, accept_key_releases, accept_event, accept_index) or consume (consume_key, consume_shortcut, remove_event) the handled events so they do not run again on a subsequent pass after request_discard.

Validation

On final commit 9f7580a9:

  • Local upstream checks:
    • egui: 158 library unit tests, 3 drop_zone + 1 ime integration tests, and 189 doctests passed (1 ignored).
    • egui_extras: 18 library unit tests, 4 table_sizing integration tests, and 7 doctests passed.
    • scripts/lint.py, cargo fmt --all -- --check, cargo doc (public and --document-private-items, RUSTDOCFLAGS="-D warnings"), feature-matrix checks (--no-default-features, --no-default-features --features serde, --all-features), and cargo clippy (debug for egui, egui_extras, keyboard_events; release for egui, -D warnings) all passed.
  • GitHub Actions CI (9f7580a9): all 25 technical checks passed (Run tests, Format + check, Check Windows, Check wasm32 + wasm-bindgen, Check wasm32+atomics, android, ios, build, typos, taplo, cargo-shear, check-binary-files, check-source-branch, and all 11 cargo-deny targets; only the maintainer label check fails pending a label).
  • Downstream integration testing (SuperBlender):
    • Tested modeler-desktop against the final PR changes: modeler-desktop compiled cleanly with Events, its existing regression test expecting dropped clicks failed specifically because press.undo.clicked() is now true on the kept pass, and a workaround-free test of history_bar (without SuperBlender's settle_history_click or history_target_of_events workaround) confirmed the Undo AccessKit click fires once both when omitted on pass 0 and when built on both passes. SuperBlender's files were restored unchanged afterward.
  • Earlier release micro-benchmark (measured on cfbc5f38 against upstream 35925b9, covering begin_pass, key_pressed, num_presses, and retain; median of 3 runs, within run-to-run noise):
Workload PR (cfbc5f38) Upstream (35925b9)
4 key events 503 ns 574 ns
32 key events 1953 ns 1875 ns
32 mixed events 2780 ns 2912 ns

Remaining limitations / out of scope

  • Pointer hit-testing still uses previous-pass widget geometry; pointer, touch, wheel, zoom, and modifier events are not replayed (unused smooth scroll is carried via smooth_scroll_delta).
  • On a single-pass frame without discard, an AccessKit Action::Focus request still only targets widgets already in focus_widgets_cache from the previous pass (across a discarded pass, an unmatched focus request remains unaccepted and applies once the widget is registered).
  • At most one keyboard navigation key (Arrow* / Tab) is applied per pass; earlier navigation keys in the same batch replay only when another pass runs.
  • Arbitrary application side effects after observational queries cannot be detected automatically and must explicitly accept or consume handled events.

AI assistance (OpenAI ChatGPT, xAI Grok, and Google Gemini / Antigravity) was used during investigation, implementation, testing, and review.

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Preview available at https://egui-pr-preview.github.io/pr/8944-retain-unconsumed-input-across-discard
Note that it might take a couple seconds for the update to show up after the preview_build workflow has completed.

View snapshot changes at kitdiff

@simzy39
simzy39 marked this pull request as ready for review November 25, 2026 05:21
@simzy39 simzy39 changed the title Keep unconsumed input across a discarded pass Keep unaccepted events across discarded passes Nov 25, 2026
A discarded pass dropped input that no widget had accepted. Key, text, IME,
clipboard, and accesskit events now remain for the next pass until a handler
accepts or consumes them. Pointer, wheel, zoom, and modifier events are still
applied as the pass starts. Unused scroll is carried as smooth_scroll_delta.

InputState::events is an Events list. Acceptance stays on each event through
removal, insertion, reordering, and the other list edits. Identical values
stay distinct. key_pressed, key_released, and num_presses only look. A handler
that acts calls accept_key or consume_key.

One navigation key is applied per pass. An Escape that already cleared focus
is accepted. The wheel smoother steps once per frame.
@simzy39
simzy39 force-pushed the retain-unconsumed-input-across-discard branch from bf2c70f to cfbc5f3 Compare November 25, 2026 07:23
Button styling reads the previous pass before the widget is allocated.
That read goes through get_response. Removing the click there dropped it
before the response the caller keeps was built, so a button click stayed
false. The click is reported on every read this pass and accepted when
the widget is registered, so a discarded pass does not replay it.
@simzy39
simzy39 marked this pull request as draft November 25, 2026 09:58
@simzy39

simzy39 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

What remains outstanding

  • Verify keyboard state during partial event replay.
  • Verify navigation identity after whole-collection replacement.

…ling

- Preserve `keys_down` and key `repeat` flags when replaying a partial
  subset of `Event::Key` events after a discarded pass, avoiding stuck
  keys (accepted release or non-replayed `WindowFocused(false)`) and
  dropped held keys (accepted subsequent press).
- Replace per-instance `Events` IDs with globally unique monotonic
  `EventId`s so whole-collection replacement cannot let stale navigation
  or Escape IDs match unrelated events; expand `Events` collection
  conveniences (`new`, `with_capacity`, `capacity`, `reserve`,
  `as_slice`, `into_vec`, `From<Events> for Vec<Event>`, `AsRef`,
  `Borrow`, `FromIterator`, `IntoIterator`, slice `Index`, `PartialEq`).
- Avoid accepting `Space`/`Enter` or AccessKit `Click` activations
  during observational `read_response`/`Ui::response` reads, accepting
  them only when `create_widget` registers the widget.
- Accept applied AccessKit `Action::Focus` requests so a discarded pass
  does not replay them over a later focus change, while leaving focus
  requests for unknown widgets unaccepted for the next pass.
- Add `InputState::accept_key_releases`, update docs and
  `examples/keyboard_events`, and add targeted regression tests.
@simzy39
simzy39 marked this pull request as ready for review November 26, 2026 02:38

This branch has not been deployed

No deployments
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.

Unconsumed input events are dropped when a pass calls request_discard

1 participant