bevy/crates/bevy_winit/src
Wei Xu fbc55b84e2
Update winit_runner to use spawn_app for wasm32 target (#16630)
# Objective

- Fixes #12562
- Fixes #12195

## Solution

- Use `spawn_app` instead of `run_app` for web platform in
`winit_runner` as suggested in the
[document](https://docs.rs/winit/latest/winit/platform/web/trait.EventLoopExtWebSys.html#tymethod.spawn_app)

## Testing

- Did you test these changes? If so, how?

Tested on web. Created a react app which renders the bevy WASM app and
returns the disposer to JS. Js will call the disposer on component
unmount to stop the app, the disposer sends a signal to a `signal`
receiver in rust which exits the app like this:

```rust
fn handle_stop_signal(
    signal: NonSendMut<StopSignalReceiver>,
    window_entities: Query<(Entity, &Window)>,
    mut event_writer: EventWriter<WindowCloseRequested>,
) {
    if let Ok(_) = signal.try_recv() {
        for (entity, _window) in window_entities.iter() {
            info!("closing window entity: {:x}", entity.to_bits());
            event_writer.send(WindowCloseRequested { window: entity });
        }
    }
}
```


- Are there any parts that need more testing?

  - No

- How can other people (reviewers) test your changes? Is there anything
specific they need to know?

- Are all resources released after stopping the app like this? The WASM
is still loaded, the LogPlugin complains on the logger
re-initialization, but it's a warning anyway.

- If relevant, what platforms did you test these changes on, and are
there any important ones you can't test?

- Tested the WASM version on web platform and the native version on
MacOS.

---------

Co-authored-by: Martín Maita <47983254+mnmaita@users.noreply.github.com>
2025-01-06 23:21:24 +00:00
..
accessibility.rs Replace bevy_a11y::Focus with InputFocus (#16863) 2024-12-18 00:16:19 +00:00
converters.rs Expose text field from winit in KeyboardInput (#16864) 2024-12-17 22:42:54 +00:00
cursor.rs Remove tracing re-export from bevy_utils (#17161) 2025-01-05 23:06:34 +00:00
lib.rs bevy_winit(emit raw winit events) (#15884) 2024-12-03 17:20:43 +00:00
state.rs Update winit_runner to use spawn_app for wasm32 target (#16630) 2025-01-06 23:21:24 +00:00
system.rs Remove tracing re-export from bevy_utils (#17161) 2025-01-05 23:06:34 +00:00
winit_config.rs Remove everything except Instant from bevy_utils::time (#17158) 2025-01-05 20:36:08 +00:00
winit_monitors.rs Simpler lint fixes: makes ci lints work but disables a lint for now (#15376) 2024-09-24 11:42:59 +00:00
winit_windows.rs can hide status bar on iOS (#17179) 2025-01-06 19:19:56 +00:00