diff --git a/crates/ui-contract/src/runtime/egui/access.rs b/crates/ui-contract/src/runtime/egui/access.rs index 8eee24e44..a996c6550 100644 --- a/crates/ui-contract/src/runtime/egui/access.rs +++ b/crates/ui-contract/src/runtime/egui/access.rs @@ -10,9 +10,7 @@ mod text; mod tree; use crate::runtime::pass_state; -#[cfg(feature = "testing")] -use egui::FullOutput; -use egui::{Context, Id, IdMap, IdSet, ViewportId}; +use egui::{Context, FullOutput, Id, IdMap, IdSet, ViewportId}; use std::collections::HashMap; use super::pass; @@ -200,3 +198,28 @@ fn hide_behind_modal(ctx: &Context, viewport: ViewportId, written: &IdSet) { pub(crate) fn tree_of(output: &mut FullOutput) -> Option { output.platform_output.accesskit_update.take().map(AccessTree::new) } + +/// Drops relations that name a node this update does not carry. +/// +/// A menu reports its popup open on a frame where the popup's own node is not emitted (the ordering +/// differs per menu), and `runtime::access::emit` puts that id in `controls`. A host's UIA adapter +/// unwraps every `controls` lookup (`accesskit_consumer`'s `Node::controls`), so one id with no node +/// in the same update aborts the app: photocraft#15597, where clicking Image, Edit or Layer — and on +/// one machine Filter — closed PhotoCraft on the click that opened the menu. +pub(crate) fn drop_dangling_relations(output: &mut FullOutput) { + let Some(update) = output.platform_output.accesskit_update.as_mut() else { return }; + prune_controls(update); +} + +/// The step itself: every `controls` id of `update` that names no node of `update` is dropped. Other +/// relations keep their ids: they are built from nodes this pass wrote. +pub fn prune_controls(update: &mut egui::accesskit::TreeUpdate) { + let ids: std::collections::HashSet = update.nodes.iter().map(|(id, _)| *id).collect(); + for (_, node) in update.nodes.iter_mut() { + if node.controls().iter().all(|id| ids.contains(id)) { + continue; + } + let kept: Vec = node.controls().iter().copied().filter(|id| ids.contains(id)).collect(); + node.set_controls(kept); + } +} diff --git a/crates/ui-contract/src/runtime/egui/plugin.rs b/crates/ui-contract/src/runtime/egui/plugin.rs index efa49785b..6ae26f33b 100644 --- a/crates/ui-contract/src/runtime/egui/plugin.rs +++ b/crates/ui-contract/src/runtime/egui/plugin.rs @@ -1,7 +1,7 @@ //! The plugin impl, its registration and hook order, and the plugin lock. use crate::runtime::frame; -use egui::{Context, Plugin, RawInput, Ui}; +use egui::{Context, FullOutput, Plugin, RawInput, Ui}; /// The runtime's plugin. egui holds a plugin's mutex while it calls a hook, so the struct holds no state: /// every record lives in context data behind its own lock. @@ -17,6 +17,10 @@ impl Plugin for RuntimePlugin { frame::input_hook(ctx, input); } + fn output_hook(&mut self, ctx: &Context, output: &mut FullOutput) { + let _ = ctx; + crate::runtime::egui::access::drop_dangling_relations(output); + } fn on_begin_pass(&mut self, ui: &mut Ui) { frame::begin_pass(ui); } diff --git a/crates/ui-contract/src/testing/access.rs b/crates/ui-contract/src/testing/access.rs index e3f440cc8..2d05eb316 100644 --- a/crates/ui-contract/src/testing/access.rs +++ b/crates/ui-contract/src/testing/access.rs @@ -3,7 +3,7 @@ use crate::runtime::access::{FieldLabel, scope}; use egui::{Context, Id}; -pub use crate::runtime::egui::access::{AccessTree, Declared, NodeKey}; +pub use crate::runtime::egui::access::{AccessTree, Declared, NodeKey, prune_controls}; /// The AccessKit tree a finished pass of a viewport produced, taken out of its output; empty when it made none. pub fn tree_of(output: &mut crate::runtime::egui::fixture::FullOutput) -> AccessTree { diff --git a/crates/ui-contract/tests/it/access/controls.rs b/crates/ui-contract/tests/it/access/controls.rs new file mode 100644 index 000000000..81e3435f0 --- /dev/null +++ b/crates/ui-contract/tests/it/access/controls.rs @@ -0,0 +1,37 @@ +//! Relations that name a node the update does not carry. + +use craft_ui::testing::access::prune_controls; +use craft_ui::testing::trial; +use egui::accesskit::{Node, NodeId, Role, TreeId, TreeUpdate}; + +fn update(controls: Vec, carried: Vec) -> TreeUpdate { + let mut node = Node::new(Role::Button); + node.set_controls(controls); + TreeUpdate { + nodes: carried.into_iter().map(|id| (id, Node::new(Role::Button))).chain(std::iter::once((NodeId(1), node))).collect(), + tree: None, + tree_id: TreeId::ROOT, + focus: NodeId(1), + } +} + +fn controls_of_first(tree: &TreeUpdate) -> Vec { + tree.nodes.iter().find(|(id, _)| *id == NodeId(1)).map(|(_, node)| node.controls().to_vec()).unwrap_or_default() +} + +/// A popup that reports itself open on a frame where its node is not emitted leaves its id in +/// `controls`; the host's UIA adapter unwraps that lookup and aborts (photocraft#15597). The id +/// that has a node stays. +#[trial] +fn controls_keep_the_ids_the_update_carries_and_drop_the_rest() { + let mut tree = update(vec![NodeId(2), NodeId(9)], vec![NodeId(2)]); + prune_controls(&mut tree); + assert_eq!(controls_of_first(&tree), vec![NodeId(2)], "the id without a node is gone, the one with a node stays"); +} + +#[trial] +fn controls_with_every_node_carried_are_left_alone() { + let mut tree = update(vec![NodeId(2)], vec![NodeId(2)]); + prune_controls(&mut tree); + assert_eq!(controls_of_first(&tree), vec![NodeId(2)]); +} diff --git a/crates/ui-contract/tests/it/access/mod.rs b/crates/ui-contract/tests/it/access/mod.rs index 67a826aac..3e9415678 100644 --- a/crates/ui-contract/tests/it/access/mod.rs +++ b/crates/ui-contract/tests/it/access/mod.rs @@ -1,6 +1,7 @@ //! The accessibility emitter: the label scope and merge rule, announcements, modal inertness, viewports. mod bounds; +mod controls; mod face_record; mod face_state; mod nodes;