Skip to content

Commit 99d1fad

Browse files
committed
Android: rework backend to use android_activity crate
This updates the Android backend to use the android_activity create instead of ndk-glue. This solves a few issues: 1. The backend is agnostic of the application's choice of Activity base class 2. Winit is no longer responsible for handling any Java synchronization details, since these are encapsulated by the design of android_activity 3. The backend no longer depends on global / static getters for state such as the native_window() which puts it in a better position to support running multiple activities within a single Android process. 4. Redraw requests are flagged, not queued, in a way that avoids taking priority over user events (resolves rust-windowing#2299) Addresses: PR rust-windowing#1892 Addresses: PR rust-windowing#2307 Addresses: PR rust-windowing#2343 Addresses: rust-windowing#2293 Resolves: rust-windowing#2299
1 parent 427cda9 commit 99d1fad

File tree

4 files changed

+620
-392
lines changed

4 files changed

+620
-392
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ simple_logger = "2.1.0"
5858
[target.'cfg(target_os = "android")'.dependencies]
5959
# Coordinate the next winit release with android-ndk-rs: https://github.com/rust-windowing/winit/issues/1995
6060
ndk = "0.7.0"
61-
ndk-glue = "0.7.0"
61+
android-activity = { git = "https://github.com/rib/android-activity" }
6262

6363
[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies]
6464
objc = "0.2.7"

src/event_loop.rs

+3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ impl<T> EventLoopBuilder<T> {
9797
/// `WINIT_UNIX_BACKEND`. Legal values are `x11` and `wayland`.
9898
/// If it is not set, winit will try to connect to a Wayland connection, and if that fails,
9999
/// will fall back on X11. If this variable is set with any other value, winit will panic.
100+
/// - **Android:** Must be configured with an `AndroidApp` from `android_main()` by calling
101+
/// [`.with_android_app(app)`] before calling `.build()`.
100102
///
103+
/// [`.with_android_app(app)`]: crate::platform::android::EventLoopBuilderExtAndroid::with_android_app
101104
/// [`platform`]: crate::platform
102105
#[inline]
103106
pub fn build(&mut self) -> EventLoop<T> {

src/platform/android.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#![cfg(any(target_os = "android"))]
22

33
use crate::{
4-
event_loop::{EventLoop, EventLoopWindowTarget},
4+
event_loop::{EventLoop, EventLoopBuilder, EventLoopWindowTarget},
55
window::{Window, WindowBuilder},
66
};
77
use ndk::configuration::Configuration;
8-
use ndk_glue::Rect;
8+
9+
use android_activity as ndk_glue;
10+
use ndk_glue::{Rect, AndroidApp};
911

1012
/// Additional methods on [`EventLoop`] that are specific to Android.
1113
pub trait EventLoopExtAndroid {}
@@ -38,3 +40,17 @@ impl<T> EventLoopWindowTargetExtAndroid for EventLoopWindowTarget<T> {}
3840
pub trait WindowBuilderExtAndroid {}
3941

4042
impl WindowBuilderExtAndroid for WindowBuilder {}
43+
44+
pub trait EventLoopBuilderExtAndroid {
45+
/// Associates the `AndroidApp` that was passed to `android_main()` with the event loop
46+
///
47+
/// This must be called on Android since the `AndroidApp` is not global state.
48+
fn with_android_app(&mut self, app: AndroidApp) -> &mut Self;
49+
}
50+
51+
impl<T> EventLoopBuilderExtAndroid for EventLoopBuilder<T> {
52+
fn with_android_app(&mut self, app: AndroidApp) -> &mut Self {
53+
self.platform_specific.android_app = Some(app);
54+
self
55+
}
56+
}

0 commit comments

Comments
 (0)