Skip to content

Commit

Permalink
Upstream with_accessibility_parent with return value
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Jul 8, 2024
1 parent ab4648f commit 8e55da9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
6 changes: 4 additions & 2 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2864,7 +2864,7 @@ impl Context {
/// No locks are held while the given closure is called.
#[allow(clippy::unused_self)]
#[inline]
pub fn with_accessibility_parent(&self, _id: Id, f: impl FnOnce()) {
pub fn with_accessibility_parent<T>(&self, _id: Id, f: impl FnOnce() -> T) -> T {
// TODO(emilk): this isn't thread-safe - another thread can call this function between the push/pop calls
#[cfg(feature = "accesskit")]
self.frame_state_mut(|fs| {
Expand All @@ -2873,14 +2873,16 @@ impl Context {
}
});

f();
let result = f();

#[cfg(feature = "accesskit")]
self.frame_state_mut(|fs| {
if let Some(state) = fs.accesskit_state.as_mut() {
assert_eq!(state.parent_stack.pop(), Some(_id));
}
});

result
}

/// If AccessKit support is active for the current frame, get or create
Expand Down
10 changes: 1 addition & 9 deletions crates/egui/src/widgets/theme_switch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ mod space_allocation {

// Focusable elements always get an accessible node, so let's ensure that
// the parent is set correctly when the responses are created the first time.
with_accessibility_parent(ui, id, |ui| {
ui.ctx().clone().with_accessibility_parent(id, || {
let buttons = options
.into_iter()
.enumerate()
Expand Down Expand Up @@ -195,14 +195,6 @@ mod space_allocation {
buttons: usize,
}

// TODO: this is a workaround, maybe we can change the function on ctx?
fn with_accessibility_parent<T>(ui: &mut Ui, id: Id, f: impl FnOnce(&mut Ui) -> T) -> T {
let ctx = ui.ctx().clone();
let mut result = None;
ctx.with_accessibility_parent(id, || result = Some(f(ui)));
result.expect("with_accessibility_parent() always calls f()")
}

fn allocate_button<T>(
ui: &Ui,
remaining: &mut Rect,
Expand Down

0 comments on commit 8e55da9

Please sign in to comment.