Skip to content

Add WakeUp event to App #19212

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

IceSentry
Copy link
Contributor

Objective

  • The WakeUp event is never added to the app. If you need to use that event you currently need to add it yourself.

Solution

  • Add the WakeUp event to the App in the WinitPlugin

Testing

  • I tested the window_setting example and it compiled and worked

@IceSentry IceSentry added A-Windowing Platform-agnostic interface layer to run your app in C-Bug An unexpected or incorrect behavior D-Trivial Nice and easy! A great choice to get started with Bevy S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels May 14, 2025
@tychedelia tychedelia added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels May 14, 2025
Copy link
Contributor

@ickshonpe ickshonpe left a comment

Choose a reason for hiding this comment

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

This doesn't look right to me. From what I can tell the problem isn't that the WakeUp event registration is missing. It's that WakeUp derives Event but it's not actually used as a Bevy event. Instead it's dispatched directly to winit using EventLoopProxy::send_event.

I think instead either the Event derive needs to be removed for WakeUp or there should be a system that redispatches the events like:

fn redispatch_winit_event<E = Event>(
    mut events: EventReader<E>,
    event_loop_proxy: Res<EventLoopProxyWrapper<E>>,
) {
    for event in events.read() {
        let _ = event_loop_proxy.send_event(event.clone());
    }
}

And then in the WinitPlugin<T> builder it would add a redispatch_winit_event<T> system to the schedule.

@ickshonpe ickshonpe added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged C-Code-Quality A section of code that is hard to understand or change and removed S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it C-Bug An unexpected or incorrect behavior D-Trivial Nice and easy! A great choice to get started with Bevy labels May 14, 2025
@IceSentry
Copy link
Contributor Author

So, the reason I made this PR is that we have code at work that uses that WakeUp event and while migrating to bevy 0.15 I noticed I was getting a bunch of error about needing to add the Event to the App. Adding the event like this PR does fixed the issue. Looking at the code more, I agree that it doesn't really make sense since neither bevy nor my work code uses it as a bevy event. So I'm a bit confused why that fixed the issue and why I even received the error in the first place since I can't find anywhere that tries to send the event as a bevy event.

Looking at this more, it probably makes more sense to just remove the Event constraint on WakeUp since that doesn't seem to be the intended usage of it.

@IceSentry
Copy link
Contributor Author

IceSentry commented May 14, 2025

Okay, looking at the code more. I think I found where the send_event happens. It's inside ApplicationHandler::user_event(). It sends any user event emitted by winit back to bevy. The reason this is triggered by our work code is because we send a user event directly to the event loop proxy.

I don't know why that feature exists but I think doing that redispatch_winit_event will result in a feedback loop where it will send that user event then the ApplicationHandler will send the bevy event back in this will constantly happen in an inifinte loop.

So, with that taken into consideration, if we don't want to change/break any existing behaviour, we can't remove the derive and we can't add the redispatch system.

@IceSentry
Copy link
Contributor Author

I did more digging and I think I figured out what happened. The #15649 PR added a feature flag on this line https://github.com/bevyengine/bevy/pull/15649/files#diff-b7a9bee1adef16186c0b38f7f0d7969565519dba556c0ecd007632ac35725cf5L91 the feature flag was only about the cursor part but it was also added to the add_event which I believe should not be behind a feature flag. That would explain why our usage of this broke in 0.15 but was fine in 0.14.

So the fix seems to be to only add the feature flag on the appropriate part of that line instead of that entire line. I'll push this fix on this branch and see what people think.

@ickshonpe
Copy link
Contributor

ickshonpe commented May 14, 2025

Okay, looking at the code more. I think I found where the send_event happens. It's inside ApplicationHandler::user_event(). It sends any user event emitted by winit back to bevy. The reason this is triggered by our work code is because we send a user event directly to the event loop proxy.

I don't know why that feature exists but I think doing that redispatch_winit_event will result in a feedback loop where it will send that user event then the ApplicationHandler will send the bevy event back in this will constantly happen in an inifinte loop.

So, with that taken into consideration, if we don't want to change/break any existing behaviour, we can't remove the derive and we can't add the redispatch system.

Oh right that makes sense now.

My suspicion is that the ApplicationHandler trait is just implemented incorrectly? There doesn't seem to be any need to bounce events back from the user_event method.

Copy link
Contributor

@ickshonpe ickshonpe left a comment

Choose a reason for hiding this comment

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

Looks good.

It seems to me that there's no need for the WakeUp event, the type params on WinitAppRunnerState and WinitPlugin, or the user_event impl at all but cleaning any of that up isn't urgent.

@ickshonpe ickshonpe added C-Bug An unexpected or incorrect behavior S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed C-Code-Quality A section of code that is hard to understand or change S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels May 14, 2025
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-Bug An unexpected or incorrect behavior 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