Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor function update_accessibility_nodes #10911

Merged
merged 11 commits into from
Dec 10, 2023

Conversation

Nilirad
Copy link
Contributor

@Nilirad Nilirad commented Dec 8, 2023

Objective

update_accessibility_nodes is one of the most nested functions in the entire Bevy repository, with a maximum of 9 levels of indentations. This PR refactors it down to 3 levels of indentations, while improving readability on other fronts. The result is a function that is actually understandable at a first glance.

PS: I read AccessKit's documentation, but I don't have experience with it. Therefore, naming of variables and subroutines may be a bit off.

PS2: I don't know if the test suite covers the functionality of this system, but since I've spent quite some time on it and the changes were simple, I'm pretty confident the refactor is functionally equivalent to the original.

Solution

I documented each change with a commit, but as a summary I did the following to reduce nesting:

  • I moved from if-let blocks to let-else statements where appropriate to reduce rightward shift
  • I extracted the closure body to a new function update_adapter
  • I factored out parts of update_adapter into new functions queue_node_for_update and add_children_nodes

Note for reviewers: GitHub's diff viewer is not the greatest in showing horizontal code shifts, therefore you may want to use a different tool like VSCode to review some commits, especially the second one (anyway, that commit is very straightforward, despite changing many lines).

@Nilirad Nilirad added A-Windowing Platform-agnostic interface layer to run your app in C-Code-Quality A section of code that is hard to understand or change labels Dec 8, 2023
@JustAnotherCodemonkey
Copy link

Haven't taken a closer look yet (I will when I can) but this looks really good! I definitely agree that this is far more readable and takes good advantage of the tools Rust gives us for avoiding things like this. Hope it get's merged soon!

@alice-i-cecile
Copy link
Member

@JustAnotherCodemonkey when you get a chance, I'd appreciate your review on this :) With two approvals I can merge this in <3

@JustAnotherCodemonkey
Copy link

@alice-i-cecile How do I do a review? I've never done this before and don't see a button.

Copy link

@JustAnotherCodemonkey JustAnotherCodemonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, very satisfying change to see. The code appears to have been successfully refactored into a far more readable state by removing the intimidating indentation and by breaking smaller tasks into separate functions.

@Nilirad Nilirad added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Dec 9, 2023
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Dec 10, 2023
Merged via the queue into bevyengine:main with commit 47fa620 Dec 10, 2023
23 checks passed
@mockersf
Copy link
Member

mockersf commented Jan 3, 2024

a negation was missed in this refactor: #11206

github-merge-queue bot pushed a commit that referenced this pull request Jan 3, 2024
# Objective

- Since #10911, example `button` crashes when clicking the button
```
thread 'main' panicked at .cargo/registry/src/index.crates.io-6f17d22bba15001f/accesskit_consumer-0.16.1/src/tree.rs:139:9:
assertion `left == right` failed
  left: 1
 right: 0
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Encountered a panic in system `bevy_winit::accessibility::update_accessibility_nodes`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!
```


## Solution

- Re-add lost negation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Windowing Platform-agnostic interface layer to run your app in C-Code-Quality A section of code that is hard to understand or change S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants