Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/event-resumed-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": minor
---

`Event::Resumed` is now only emitted when the app is actually resumed (going back to foreground) so it won't be called on app startup.
5 changes: 5 additions & 0 deletions .changes/will-enter-foreground.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

Use WillEnterForeground instead of DidBecomeActive for Event::Resumed in iOS.
12 changes: 12 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,21 @@ pub enum Event<'a, T: 'static> {
UserEvent(T),

/// Emitted when the application has been suspended.
///
/// ## Platform-specific
///
/// - **Android**: This is triggered by `onPause` method of the Activity.
/// - **iOS**: This is triggered by `applicationWillResignActive` method of the UIApplicationDelegate.
/// - **Linux / macOS / Windows**: Unsupported.
Suspended,

/// Emitted when the application has been resumed.
///
/// ## Platform-specific
///
/// - **Android**: This is triggered by `onResume` method of the Activity. The first onResume() is ignored to match the iOS implementation, since that is called on activity creation.
/// - **iOS**: This is triggered by `applicationWillEnterForeground` method of the UIApplicationDelegate.
/// - **Linux / macOS / Windows**: Unsupported.
Resumed,

/// Emitted when all of the event loop's input events have been processed and redraw processing
Expand Down
15 changes: 12 additions & 3 deletions src/platform_impl/android/ndk_glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ use std::{
fs::File,
io::{BufRead, BufReader},
os::unix::prelude::*,
sync::{Arc, Condvar, Mutex, RwLock, RwLockReadGuard},
sync::{
atomic::{AtomicBool, Ordering},
Arc, Condvar, Mutex, RwLock, RwLockReadGuard,
},
thread,
};

/// Android pacakge name that could be used to reference classes
/// in the android project.
pub static PACKAGE: OnceCell<&str> = OnceCell::new();

/// Generate JNI compilant functions that are necessary for
/// Generate JNI compliant functions that are necessary for
/// building android apps with tao.
///
/// Arguments in order:
Expand Down Expand Up @@ -123,6 +126,7 @@ static WINDOW_MANAGER: StaticCell<Option<GlobalRef>> = StaticCell(RefCell::new(N
static INPUT_QUEUE: Lazy<RwLock<Option<InputQueue>>> = Lazy::new(|| Default::default());
static CONTENT_RECT: Lazy<RwLock<Rect>> = Lazy::new(|| Default::default());
static LOOPER: Lazy<Mutex<Option<ForeignLooper>>> = Lazy::new(|| Default::default());
static DID_RESUME: AtomicBool = AtomicBool::new(false);

pub fn window_manager() -> std::cell::Ref<'static, Option<GlobalRef>> {
WINDOW_MANAGER.0.borrow()
Expand Down Expand Up @@ -285,7 +289,12 @@ pub unsafe fn create(
}

pub unsafe fn resume(_: JNIEnv, _: JClass, _: JObject) {
wake(Event::Resume);
let did_resume = DID_RESUME.swap(true, Ordering::Relaxed);
// first Activity onResume() is called even after onCreate()
// to match the iOS implementation, we ignore the first resume event
if did_resume {
wake(Event::Resume);
}
}

pub unsafe fn pause(_: JNIEnv, _: JClass, _: JObject) {
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/ios/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
//!
//! This is how those event are represented in tao:
//!
//! - applicationDidBecomeActive is Resumed
//! - applicationWillEnterForeground is Resumed
//! - applicationWillResignActive is Suspended
//! - applicationWillTerminate is LoopDestroyed
//!
Expand Down
13 changes: 4 additions & 9 deletions src/platform_impl/ios/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,15 +611,14 @@ pub fn create_delegate_class() {
}
}

extern "C" fn did_become_active(_: &Object, _: Sel, _: id) {
unsafe { app_state::handle_nonuser_event(EventWrapper::StaticEvent(Event::Resumed)) }
}

extern "C" fn will_resign_active(_: &Object, _: Sel, _: id) {
unsafe { app_state::handle_nonuser_event(EventWrapper::StaticEvent(Event::Suspended)) }
}

extern "C" fn will_enter_foreground(_: &Object, _: Sel, _: id) {}
extern "C" fn will_enter_foreground(_: &Object, _: Sel, _: id) {
unsafe { app_state::handle_nonuser_event(EventWrapper::StaticEvent(Event::Resumed)) }
}

extern "C" fn did_enter_background(_: &Object, _: Sel, _: id) {}

extern "C" fn will_terminate(_: &Object, _: Sel, _: id) {
Expand Down Expand Up @@ -669,10 +668,6 @@ pub fn create_delegate_class() {
application_continue as extern "C" fn(_, _, _, _, _) -> _,
);

decl.add_method(
sel!(applicationDidBecomeActive:),
did_become_active as extern "C" fn(_, _, _),
);
decl.add_method(
sel!(applicationWillResignActive:),
will_resign_active as extern "C" fn(_, _, _),
Expand Down