Repository: bevyengine/bevy
File: crates/bevy_winit/src/state.rs (Rust)

Code before:
        create_window.apply(self.world_mut());

        // TODO: This is a workaround for https://github.com/bevyengine/bevy/issues/17488
        //       while preserving the iOS fix in https://github.com/bevyengine/bevy/pull/11245
        //       The monitor sync logic likely belongs in monitor event handlers and not here.
        #[cfg(not(target_os = "windows"))]
        self.redraw_requested(event_loop);

        // Have the startup behavior run in about_to_wait, which prevents issues with
        // invisible window creation. https://github.com/bevyengine/bevy/issues/18027
        #[cfg(target_os = "windows")]
        {
            if WINIT_WINDOWS.with_borrow(|winit_windows| {
                let headless = winit_windows.windows.is_empty();
                let exiting = self.app_exit.is_some();
                let reactive = matches!(self.update_mode, UpdateMode::Reactive { .. });
                let all_invisible = winit_windows
                    .windows
                    .iter()
                    .all(|(_, w)| !w.is_visible().unwrap_or(false));
                !exiting
                    && (self.startup_forced_updates > 0
                        || headless
                        || all_invisible
                        || reactive
                        || self.window_event_received)
            }) {
                self.redraw_requested(event_loop);
            }
        }
    }

    fn suspended(&mut self, _event_loop: &ActiveEventLoop) {
        // Mark the state as `WillSuspend`. This will let the schedule run one last time
        // before actually suspending to let the application react
        self.lifecycle = AppLifecycle::WillSuspend;
    }

    fn exiting(&mut self, _event_loop: &ActiveEventLoop) {
        let world = self.world_mut();
        world.clear_all();
    }
}

impl<M: Message> WinitAppRunnerState<M> {
    fn redraw_requested(&mut self, event_loop: &ActiveEventLoop) {
        let mut redraw_message_cursor = MessageCursor::<RequestRedraw>::default();
        let mut close_message_cursor = MessageCursor::<WindowCloseRequested>::default();

        let mut focused_windows_state: SystemState<(Res<WinitSettings>, Query<(Entity, &Window)>)> =
            SystemState::new(self.world_mut());

Review comment (suggestion):
```suggestion
            if !self.app_exit.is_some()
                && (self.startup_forced_updates > 0
                    || matches!(self.update_mode, UpdateMode::Reactive { .. })
                    || self.window_event_received
                    || headless_or_all_invisible())
```

Code after:
        create_window.apply(self.world_mut());

        // TODO: This is a workaround for https://github.com/bevyengine/bevy/issues/17488
        //       while preserving the iOS fix in https://github.com/bevyengine/bevy/pull/11245
        //       The monitor sync logic likely belongs in monitor event handlers and not here.
        #[cfg(not(target_os = "windows"))]
        self.redraw_requested(event_loop);

        // Have the startup behavior run in about_to_wait, which prevents issues with
        // invisible window creation. https://github.com/bevyengine/bevy/issues/18027
        #[cfg(target_os = "windows")]
        {
            fn headless_or_all_invisible() -> bool {
                WINIT_WINDOWS.with_borrow(|winit_windows| {
                    winit_windows
                        .windows
                        .iter()
                        .all(|(_, w)| !w.is_visible().unwrap_or(false))
                })
            }

            if !self.app_exit.is_some()
                && (self.startup_forced_updates > 0
                    || matches!(self.update_mode, UpdateMode::Reactive { .. })
                    || self.window_event_received
                    || headless_or_all_invisible())
            {
                self.redraw_requested(event_loop);
            }
        }
    }

    fn suspended(&mut self, _event_loop: &ActiveEventLoop) {
        // Mark the state as `WillSuspend`. This will let the schedule run one last time
        // before actually suspending to let the application react
        self.lifecycle = AppLifecycle::WillSuspend;
    }

    fn exiting(&mut self, _event_loop: &ActiveEventLoop) {
        let world = self.world_mut();
        world.clear_all();
    }
}

impl<M: Message> WinitAppRunnerState<M> {
    fn redraw_requested(&mut self, event_loop: &ActiveEventLoop) {
        let mut redraw_message_cursor = MessageCursor::<RequestRedraw>::default();
        let mut close_message_cursor = MessageCursor::<WindowCloseRequested>::default();

        let mut focused_windows_state: SystemState<(Res<WinitSettings>, Query<(Entity, &Window)>)> =
            SystemState::new(self.world_mut());