Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Enhance mobile suspend MainLoop notifications #85229

Merged
merged 1 commit into from
Feb 7, 2024
Merged
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: 3 additions & 2 deletions doc/classes/MainLoop.xml
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,12 @@
</constant>
<constant name="NOTIFICATION_APP_RESUMED" value="1014">
Notification received from the OS when the app is resumed.
Specific to the Android platform.
Specific to mobile platforms.
</constant>
<constant name="NOTIFICATION_APP_PAUSED" value="1015">
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.
</constant>
</constants>
</class>
5 changes: 3 additions & 2 deletions doc/classes/Node.xml
Original file line number Diff line number Diff line change
Expand Up @@ -928,11 +928,12 @@
</constant>
<constant name="NOTIFICATION_APP_RESUMED" value="1014">
Notification received from the OS when the app is resumed.
Specific to the Android platform.
Specific to mobile platforms.
</constant>
<constant name="NOTIFICATION_APP_PAUSED" value="1015">
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.
</constant>
<constant name="PAUSE_MODE_INHERIT" value="0" enum="PauseMode">
Inherits pause mode from the node's parent. For the root node, it is equivalent to [constant PAUSE_MODE_STOP]. Default.
Expand Down
8 changes: 8 additions & 0 deletions platform/iphone/app_delegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ - (void)applicationDidBecomeActive:(UIApplication *)application {
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;
}
Expand Down
3 changes: 3 additions & 0 deletions platform/iphone/os_iphone.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ class OSIPhone : public OS_Unix {

void on_focus_out();
void on_focus_in();

void on_enter_background();
void on_exit_background();
};

#endif // IPHONE_ENABLED
Expand Down
20 changes: 20 additions & 0 deletions platform/iphone/os_iphone.mm
Original file line number Diff line number Diff line change
Expand Up @@ -861,4 +861,24 @@ void add_ios_init_callback(init_callback cb) {
}
}

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