Skip to content

Commit 0e61435

Browse files
authored
mobile and webgpu: trigger redraw request when needed and improve window creation (#11245)
# Objective - Since #11227, Bevy doesn't work on mobile anymore. Windows are not created. ## Solution - Create initial window on mobile after the initial `Resume` event. macOS is included because it's excluded from the other initial window creation and I didn't want it to feel alone. Also, it makes sense. this is needed for Android https://github.com/bevyengine/bevy/blob/cfcb6885e3b475a93ec0fe7e88023ac0f354bbbf/crates/bevy_winit/src/lib.rs#L152 - request redraw during plugin initialisation (needed for WebGPU) - request redraw when receiving `AboutToWait` instead of at the end of the event handler. request to redraw during a `RedrawRequested` event are ignored on iOS
1 parent 06bf928 commit 0e61435

File tree

1 file changed

+56
-44
lines changed

1 file changed

+56
-44
lines changed

crates/bevy_winit/src/lib.rs

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use bevy_window::{PrimaryWindow, RawHandleWrapper};
4646
pub use winit::platform::android::activity as android_activity;
4747

4848
use winit::{
49-
event::{self, DeviceEvent, Event, StartCause, WindowEvent},
49+
event::{self, DeviceEvent, Event, WindowEvent},
5050
event_loop::{ControlFlow, EventLoop, EventLoopBuilder, EventLoopWindowTarget},
5151
};
5252

@@ -352,6 +352,7 @@ pub fn winit_runner(mut app: App) {
352352
app.finish();
353353
app.cleanup();
354354
}
355+
runner_state.redraw_requested = true;
355356

356357
if let Some(app_exit_events) = app.world.get_resource::<Events<AppExit>>() {
357358
if app_exit_event_reader.read(app_exit_events).last().is_some() {
@@ -360,45 +361,25 @@ pub fn winit_runner(mut app: App) {
360361
}
361362
}
362363
}
363-
runner_state.redraw_requested = false;
364364

365365
match event {
366-
Event::NewEvents(start_cause) => match start_cause {
367-
StartCause::Init => {
368-
#[cfg(any(target_os = "ios", target_os = "macos"))]
369-
{
370-
let (
371-
commands,
372-
mut windows,
373-
event_writer,
374-
winit_windows,
375-
adapters,
376-
handlers,
377-
accessibility_requested,
378-
) = create_window_system_state.get_mut(&mut app.world);
379-
380-
create_windows(
381-
event_loop,
382-
commands,
383-
windows.iter_mut(),
384-
event_writer,
385-
winit_windows,
386-
adapters,
387-
handlers,
388-
accessibility_requested,
389-
);
390-
391-
create_window_system_state.apply(&mut app.world);
366+
Event::AboutToWait => {
367+
if runner_state.redraw_requested {
368+
let (_, winit_windows, _, _) =
369+
event_writer_system_state.get_mut(&mut app.world);
370+
for window in winit_windows.windows.values() {
371+
window.request_redraw();
392372
}
393373
}
394-
_ => {
395-
if let Some(t) = runner_state.scheduled_update {
396-
let now = Instant::now();
397-
let remaining = t.checked_duration_since(now).unwrap_or(Duration::ZERO);
398-
runner_state.wait_elapsed = remaining.is_zero();
399-
}
374+
runner_state.redraw_requested = false;
375+
}
376+
Event::NewEvents(_) => {
377+
if let Some(t) = runner_state.scheduled_update {
378+
let now = Instant::now();
379+
let remaining = t.checked_duration_since(now).unwrap_or(Duration::ZERO);
380+
runner_state.wait_elapsed = remaining.is_zero();
400381
}
401-
},
382+
}
402383
Event::WindowEvent {
403384
event, window_id, ..
404385
} => {
@@ -682,7 +663,6 @@ pub fn winit_runner(mut app: App) {
682663
event: DeviceEvent::MouseMotion { delta: (x, y) },
683664
..
684665
} => {
685-
runner_state.redraw_requested = true;
686666
let (mut event_writers, ..) = event_writer_system_state.get_mut(&mut app.world);
687667
event_writers.mouse_motion.send(MouseMotion {
688668
delta: Vec2::new(x as f32, y as f32),
@@ -696,6 +676,44 @@ pub fn winit_runner(mut app: App) {
696676
runner_state.active = ActiveState::WillSuspend;
697677
}
698678
Event::Resumed => {
679+
#[cfg(any(target_os = "android", target_os = "ios", target_os = "macos"))]
680+
{
681+
if runner_state.active == ActiveState::NotYetStarted {
682+
let mut create_window_system_state: SystemState<(
683+
Commands,
684+
Query<(Entity, &mut Window)>,
685+
EventWriter<WindowCreated>,
686+
NonSendMut<WinitWindows>,
687+
NonSendMut<AccessKitAdapters>,
688+
ResMut<WinitActionHandlers>,
689+
ResMut<AccessibilityRequested>,
690+
)> = SystemState::from_world(&mut app.world);
691+
692+
let (
693+
commands,
694+
mut windows,
695+
event_writer,
696+
winit_windows,
697+
adapters,
698+
handlers,
699+
accessibility_requested,
700+
) = create_window_system_state.get_mut(&mut app.world);
701+
702+
create_windows(
703+
&event_loop,
704+
commands,
705+
windows.iter_mut(),
706+
event_writer,
707+
winit_windows,
708+
adapters,
709+
handlers,
710+
accessibility_requested,
711+
);
712+
713+
create_window_system_state.apply(&mut app.world);
714+
}
715+
}
716+
699717
let (mut event_writers, ..) = event_writer_system_state.get_mut(&mut app.world);
700718
match runner_state.active {
701719
ActiveState::NotYetStarted => {
@@ -706,6 +724,7 @@ pub fn winit_runner(mut app: App) {
706724
}
707725
}
708726
runner_state.active = ActiveState::Active;
727+
runner_state.redraw_requested = true;
709728
#[cfg(target_os = "android")]
710729
{
711730
// Get windows that are cached but without raw handles. Those window were already created, but got their
@@ -748,12 +767,6 @@ pub fn winit_runner(mut app: App) {
748767
}
749768
_ => (),
750769
}
751-
if runner_state.redraw_requested {
752-
let (_, winit_windows, _, _) = event_writer_system_state.get_mut(&mut app.world);
753-
for window in winit_windows.windows.values() {
754-
window.request_redraw();
755-
}
756-
}
757770
};
758771

759772
trace!("starting winit event loop");
@@ -820,8 +833,7 @@ fn run_app_update_if_should(
820833
app.update();
821834

822835
// decide when to run the next update
823-
let (config, windows) = focused_windows_state.get(&app.world);
824-
let focused = windows.iter().any(|window| window.focused);
836+
let (config, _) = focused_windows_state.get(&app.world);
825837
match config.update_mode(focused) {
826838
UpdateMode::Continuous => {
827839
runner_state.redraw_requested = true;

0 commit comments

Comments
 (0)