Screen-reader actions can target the wrong widget when IDs shift between frames

Open
#8,917 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
22/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
rust, wasm

Research direction

Start with the minimal reproduction in the issue and run it to see Confirm's click land on Cancel. Relevant code is in src/context.rs around the AccessKit action dispatch, src/ui.rs around UiBuilder::id_salt (scope_id.with(salt).with(next_auto_id_salt)), and src/containers/popup.rs where Popup::default_response_id derives the popup key. Done means the action is ignored or correctly routed when IDs shift, and menu_button stays open across a preceding insert or removal.

Written by the indexing model from the issue text.

Description

Description

In egui 0.37.0, automatically generated widget IDs can change between frames when widgets earlier in the same UI appear or disappear.

This creates two related problems:

  1. Accessibility actions can activate the wrong control. A screen reader identifies a widget in one frame, but its action is processed in a later frame. If widget IDs have shifted, the action can be delivered to a different control.
  2. Persistent menu state can be lost. An open menu can close unexpectedly when a preceding widget appears or disappears, even though the menu itself has not changed.

Although egui documents automatically generated IDs as unstable, accessibility action dispatch and menu state both rely on these IDs across frames.

This can result in unintended user actions rather than merely losing visual state.

Observed behaviour

We discovered this issue while testing SuperBlender's operation bar.

  1. A preview was updating, displaying a busy spinner before the Confirm and Cancel buttons.
  2. A screen reader identified the Confirm button.
  3. The preview finished, causing the spinner to disappear.
  4. The automatically generated widget IDs shifted.
  5. The screen reader's pending Confirm action was delivered to Cancel.

The operation was cancelled instead of confirmed.

We independently reproduced another symptom: an open menu closes when a widget appears or disappears before its button. This also affects mouse users.

Minimal reproduction

The following reproduces the accessibility action misrouting using plain egui, without SuperBlender application code.

use egui::accesskit::{Action, ActionRequest, TreeId};

let ctx = egui::Context::default();
let mut confirm = egui::Id::NULL;

for frame in 0..2 {
    let events = if frame == 1 {
        vec![egui::Event::AccessKitActionRequest(ActionRequest {
            action: Action::Click,
            target_tree: TreeId::ROOT,
            target_node: confirm.accesskit_id(),
            data: None,
        })]
    } else {
        Vec::new()
    };

    let _ = ctx.run_ui(
        egui::RawInput {
            events,
            ..Default::default()
        },
        |ui| {
            ui.horizontal(|ui| {
                if frame == 0 {
                    ui.spinner(); // background work, finished by frame 1
                }

                let response = ui.button("Confirm");

                if ui.button("Cancel").clicked() {
                    println!("the Confirm request activated Cancel");
                }

                if frame == 0 {
                    confirm = response.id;
                }
            });
        },
    );
}
Result

The Action::Click request targets the ID originally assigned to Confirm.

After the spinner disappears, that ID is assigned to Cancel instead.

The output is:

the Confirm request activated Cancel

The same underlying identity problem affects menu_button: opening a menu and then inserting or removing a widget before its button can cause the menu to close unexpectedly.

Technical investigation

1. Accessibility actions use widget IDs across frames

A widget's AccessKit node ID corresponds to its egui ID.

A screen reader can read the accessibility tree from one frame and submit an action that is handled in a subsequent frame.

In src/context.rs (around line 1769 in egui 0.37.0), egui processes the action using the target ID.

However, Ui::next_auto_id generates IDs based on widget placement. Its documentation explicitly notes that IDs can change when widgets are inserted or removed before them.

Consequently, an action can be delivered to a different widget that has inherited the previous ID.

2. Menu state also depends on these IDs

menu_button stores its open state using Popup::default_response_id.

In src/containers/popup.rs (around line 722):

response.id.with("popup")

When the button's automatically generated ID changes, the popup's state key changes too.

This causes an open menu to close when preceding UI content changes.

3. push_id does not resolve the problem

We initially attempted to stabilise affected controls using push_id.

However, a child created through UiBuilder::id_salt still incorporates the automatically generated ID salt:

scope_id.with(salt).with(next_auto_id_salt)

See src/ui.rs, approximately lines 258–259 in egui 0.37.0.

Therefore, inserting or removing earlier widgets can still shift child IDs inside the salted scope.

Using an explicit UiBuilder::scope_id avoids this problem by assigning a stable scope identity.

Expected behaviour

  • An accessibility action should never activate a different control merely because automatically generated IDs shifted between frames.
  • If an accessibility action refers to a widget that can no longer be identified safely, it should be ignored rather than redirected to an unrelated widget.
  • Persistent menu state should remain associated with the intended menu control.
  • The distinction between push_id and an explicitly stable scope_id should be documented clearly.

Actual behaviour

  • A screen-reader action intended for Confirm can activate Cancel.
  • An open menu can close unexpectedly when preceding widgets change.
  • push_id does not prevent either problem when child IDs remain dependent on automatic placement.

Workaround

In SuperBlender, we introduced a widgets::stable_ids helper that uses an explicit stable scope identity.

It now protects affected controls, including:

  • Confirm and Cancel
  • Preview details
  • Retry preview
  • UV tool selector

Two regression tests verify the behaviour.

Both tests fail if the stable-ID implementation is replaced with push_id alone.

This addresses the identified cases in our application, but other controls using automatically generated IDs may still be affected.

Possible improvements

Accessibility action validation

Before dispatching an accessibility action, verify that the widget associated with the target ID still corresponds to the node originally published to the accessibility client.

For example, a mismatch in role or label could cause the action to be rejected instead of delivered to another control.

Stable menu identities

Allow menu_button to use a stable explicit identity for its persistent state, potentially through an ID-salt API similar to ComboBox::from_id_salt.

Documentation

Clarify that push_id introduces an ID namespace but does not necessarily stabilise the automatically generated IDs of its children when preceding sibling structure changes.

Document the recommended approach for controls whose identities must persist across frames, particularly controls exposed through accessibility APIs.

Related work

The following egui pull requests address warnings about IDs changing between passes within a single frame:

  • #8316
  • #8543
  • #8670

They do not appear to address accessibility action routing or menu state across separate frames.

No duplicate report covering these specific symptoms was identified during our investigation.

Environment

  • egui: 0.37.0
  • accesskit: 0.25.2
  • Rust: 1.100.0
  • Reproduction: Plain egui, without SuperBlender-specific code
  • Originally discovered: Automated headless GUI testing in SuperBlender

Verification status

  • Accessibility action misrouting reproduced in plain egui.
  • Menu state loss reproduced independently.
  • push_id confirmed insufficient to stabilise the affected child IDs.
  • Explicit stable scope IDs resolve the identified cases in SuperBlender.
  • Regression tests added for the stable-ID behaviour.
  • Not yet verified against the latest egui main branch.
Dominant language
Rust
Stars
32.6k
Forks
2.5k
Avg merge
2d 1h
Merged PRs (30d)
71

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from emilk/egui

All issues in emilk/egui

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.