move ANDROID_APP to bevy_window (#15585)

# Objective

- Remove dependency in bevy_asset to bevy_winit
- First step for #15565 

## Solution

- the static `ANDROID_APP` and the `android_activity` reexport are now
in `bevy_window`

## Migration Guide

If you use the `android_activity` reexport from
`bevy::winit::android_activity`, it is now in
`bevy:🪟:android_activity`. Same for the `ANDROID_APP` static
This commit is contained in:
François Mockers 2024-10-02 05:01:06 +02:00 committed by GitHub
parent 85dfd72631
commit 23b0dd6ffd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 20 additions and 16 deletions

View file

@ -47,7 +47,7 @@ thiserror = "1.0"
uuid = { version = "1.0", features = ["v4"] } uuid = { version = "1.0", features = ["v4"] }
[target.'cfg(target_os = "android")'.dependencies] [target.'cfg(target_os = "android")'.dependencies]
bevy_winit = { path = "../bevy_winit", version = "0.15.0-dev" } bevy_window = { path = "../bevy_window", version = "0.15.0-dev" }
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { version = "0.2" } wasm-bindgen = { version = "0.2" }

View file

@ -17,7 +17,7 @@ pub struct AndroidAssetReader;
impl AssetReader for AndroidAssetReader { impl AssetReader for AndroidAssetReader {
async fn read<'a>(&'a self, path: &'a Path) -> Result<impl Reader + 'a, AssetReaderError> { async fn read<'a>(&'a self, path: &'a Path) -> Result<impl Reader + 'a, AssetReaderError> {
let asset_manager = bevy_winit::ANDROID_APP let asset_manager = bevy_window::ANDROID_APP
.get() .get()
.expect("Bevy must be setup with the #[bevy_main] macro on Android") .expect("Bevy must be setup with the #[bevy_main] macro on Android")
.asset_manager(); .asset_manager();
@ -31,7 +31,7 @@ impl AssetReader for AndroidAssetReader {
async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<impl Reader + 'a, AssetReaderError> { async fn read_meta<'a>(&'a self, path: &'a Path) -> Result<impl Reader + 'a, AssetReaderError> {
let meta_path = get_meta_path(path); let meta_path = get_meta_path(path);
let asset_manager = bevy_winit::ANDROID_APP let asset_manager = bevy_window::ANDROID_APP
.get() .get()
.expect("Bevy must be setup with the #[bevy_main] macro on Android") .expect("Bevy must be setup with the #[bevy_main] macro on Android")
.asset_manager(); .asset_manager();
@ -47,7 +47,7 @@ impl AssetReader for AndroidAssetReader {
&'a self, &'a self,
path: &'a Path, path: &'a Path,
) -> Result<Box<PathStream>, AssetReaderError> { ) -> Result<Box<PathStream>, AssetReaderError> {
let asset_manager = bevy_winit::ANDROID_APP let asset_manager = bevy_window::ANDROID_APP
.get() .get()
.expect("Bevy must be setup with the #[bevy_main] macro on Android") .expect("Bevy must be setup with the #[bevy_main] macro on Android")
.asset_manager(); .asset_manager();
@ -76,7 +76,7 @@ impl AssetReader for AndroidAssetReader {
&'a self, &'a self,
path: &'a Path, path: &'a Path,
) -> std::result::Result<bool, AssetReaderError> { ) -> std::result::Result<bool, AssetReaderError> {
let asset_manager = bevy_winit::ANDROID_APP let asset_manager = bevy_window::ANDROID_APP
.get() .get()
.expect("Bevy must be setup with the #[bevy_main] macro on Android") .expect("Bevy must be setup with the #[bevy_main] macro on Android")
.asset_manager(); .asset_manager();

View file

@ -12,8 +12,8 @@ pub fn bevy_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
TokenStream::from(quote! { TokenStream::from(quote! {
#[no_mangle] #[no_mangle]
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
fn android_main(android_app: bevy::winit::android_activity::AndroidApp) { fn android_main(android_app: bevy::window::android_activity::AndroidApp) {
let _ = bevy::winit::ANDROID_APP.set(android_app); let _ = bevy::window::ANDROID_APP.set(android_app);
main(); main();
} }

View file

@ -29,6 +29,9 @@ serde = { version = "1.0", features = ["derive"], optional = true }
raw-window-handle = "0.6" raw-window-handle = "0.6"
smol_str = "0.2" smol_str = "0.2"
[target.'cfg(target_os = "android")'.dependencies]
android-activity = "0.6"
[lints] [lints]
workspace = true workspace = true

View file

@ -27,6 +27,9 @@ mod window;
pub use crate::raw_handle::*; pub use crate::raw_handle::*;
#[cfg(target_os = "android")]
pub use android_activity;
pub use event::*; pub use event::*;
pub use monitor::*; pub use monitor::*;
pub use system::*; pub use system::*;
@ -188,3 +191,9 @@ pub enum ExitCondition {
/// surprise your users. /// surprise your users.
DontExit, DontExit,
} }
/// [`AndroidApp`] provides an interface to query the application state as well as monitor events
/// (for example lifecycle and input events).
#[cfg(target_os = "android")]
pub static ANDROID_APP: std::sync::OnceLock<android_activity::AndroidApp> =
std::sync::OnceLock::new();

View file

@ -18,8 +18,6 @@ use bevy_derive::Deref;
use bevy_window::{RawHandleWrapperHolder, WindowEvent}; use bevy_window::{RawHandleWrapperHolder, WindowEvent};
use core::marker::PhantomData; use core::marker::PhantomData;
use winit::event_loop::EventLoop; use winit::event_loop::EventLoop;
#[cfg(target_os = "android")]
pub use winit::platform::android::activity as android_activity;
use bevy_a11y::AccessibilityRequested; use bevy_a11y::AccessibilityRequested;
use bevy_app::{App, Last, Plugin}; use bevy_app::{App, Last, Plugin};
@ -52,12 +50,6 @@ mod winit_config;
mod winit_monitors; mod winit_monitors;
mod winit_windows; mod winit_windows;
/// [`AndroidApp`] provides an interface to query the application state as well as monitor events
/// (for example lifecycle and input events).
#[cfg(target_os = "android")]
pub static ANDROID_APP: std::sync::OnceLock<android_activity::AndroidApp> =
std::sync::OnceLock::new();
/// A [`Plugin`] that uses `winit` to create and manage windows, and receive window and input /// A [`Plugin`] that uses `winit` to create and manage windows, and receive window and input
/// events. /// events.
/// ///
@ -119,7 +111,7 @@ impl<T: Event> Plugin for WinitPlugin<T> {
{ {
use winit::platform::android::EventLoopBuilderExtAndroid; use winit::platform::android::EventLoopBuilderExtAndroid;
let msg = "Bevy must be setup with the #[bevy_main] macro on Android"; let msg = "Bevy must be setup with the #[bevy_main] macro on Android";
event_loop_builder.with_android_app(ANDROID_APP.get().expect(msg).clone()); event_loop_builder.with_android_app(bevy_window::ANDROID_APP.get().expect(msg).clone());
} }
app.init_non_send_resource::<WinitWindows>() app.init_non_send_resource::<WinitWindows>()