From 92a8e047e05de3d0c9e0c0a7d99e21e66b3c7aba Mon Sep 17 00:00:00 2001 From: Zach Coleman Date: Wed, 22 Nov 2023 11:43:40 -0500 Subject: [PATCH] Add APP_PAUSED and APP_RESUMED MainLoop notifications --- doc/classes/MainLoop.xml | 5 +++-- doc/classes/Node.xml | 5 +++-- platform/iphone/app_delegate.mm | 8 ++++++++ platform/iphone/os_iphone.h | 3 +++ platform/iphone/os_iphone.mm | 20 ++++++++++++++++++++ 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/doc/classes/MainLoop.xml b/doc/classes/MainLoop.xml index fa0c2c449..9631c1390 100644 --- a/doc/classes/MainLoop.xml +++ b/doc/classes/MainLoop.xml @@ -201,11 +201,12 @@ Notification received from the OS when the app is resumed. - Specific to the Android platform. + Specific to mobile platforms. Notification received from the OS when the app is paused. - Specific to the Android platform. + Specific to mobile platforms. + [b]Note:[/b] On iOS, you only have approximately 5 seconds to finish a task started by this signal. If you go over this allotment, iOS will kill the app instead of pausing it. diff --git a/doc/classes/Node.xml b/doc/classes/Node.xml index 672e13e83..e985841eb 100644 --- a/doc/classes/Node.xml +++ b/doc/classes/Node.xml @@ -1085,11 +1085,12 @@ Notification received from the OS when the app is resumed. - Specific to the Android platform. + Specific to mobile platforms. Notification received from the OS when the app is paused. - Specific to the Android platform. + Specific to mobile platforms. + [b]Note:[/b] On iOS, you only have approximately 5 seconds to finish a task started by this signal. If you go over this allotment, iOS will kill the app instead of pausing it. Inherits pause mode from the node's parent. For the root node, it is equivalent to [constant PAUSE_MODE_STOP]. Default. diff --git a/platform/iphone/app_delegate.mm b/platform/iphone/app_delegate.mm index 3c1a77f26..fe70491b4 100644 --- a/platform/iphone/app_delegate.mm +++ b/platform/iphone/app_delegate.mm @@ -175,6 +175,14 @@ static ViewController *mainViewController = nil; OSIPhone::get_singleton()->on_focus_in(); } +- (void)applicationDidEnterBackground:(UIApplication *)application { + OSIPhone::get_singleton()->on_enter_background(); +} + +- (void)applicationWillEnterForeground:(UIApplication *)application { + OSIPhone::get_singleton()->on_exit_background(); +} + - (void)dealloc { self.window = nil; } diff --git a/platform/iphone/os_iphone.h b/platform/iphone/os_iphone.h index 236567ba4..cd55d7a01 100644 --- a/platform/iphone/os_iphone.h +++ b/platform/iphone/os_iphone.h @@ -194,6 +194,9 @@ public: void on_focus_out(); void on_focus_in(); + + void on_enter_background(); + void on_exit_background(); }; #endif // OS_IPHONE_H diff --git a/platform/iphone/os_iphone.mm b/platform/iphone/os_iphone.mm index 6a78e9518..53b334312 100644 --- a/platform/iphone/os_iphone.mm +++ b/platform/iphone/os_iphone.mm @@ -802,4 +802,24 @@ void OSIPhone::on_focus_in() { } } +void OSIPhone::on_enter_background() { + // Do not check for is_focused, because on_focus_out will always be fired first by applicationWillResignActive. + + if (get_main_loop()) { + get_main_loop()->notification(MainLoop::NOTIFICATION_APP_PAUSED); + } + + on_focus_out(); +} + +void OSIPhone::on_exit_background() { + if (!is_focused) { + on_focus_in(); + + if (get_main_loop()) { + get_main_loop()->notification(MainLoop::NOTIFICATION_APP_RESUMED); + } + } +} + #endif