Skip to content

Commit 3a2fb42

Browse files
committed
Merge pull request #85100 from ztc0611/fix-ios-focus-mainloop-notifs
Enhance mobile suspend MainLoop notifications
2 parents d3f64bf + fc7a63c commit 3a2fb42

File tree

6 files changed

+57
-10
lines changed

6 files changed

+57
-10
lines changed

doc/classes/MainLoop.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,20 @@
119119
</constant>
120120
<constant name="NOTIFICATION_APPLICATION_RESUMED" value="2014">
121121
Notification received from the OS when the application is resumed.
122-
Specific to the Android platform.
122+
Specific to the Android and iOS platforms.
123123
</constant>
124124
<constant name="NOTIFICATION_APPLICATION_PAUSED" value="2015">
125125
Notification received from the OS when the application is paused.
126-
Specific to the Android platform.
126+
Specific to the Android and iOS platforms.
127+
[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.
127128
</constant>
128129
<constant name="NOTIFICATION_APPLICATION_FOCUS_IN" value="2016">
129130
Notification received from the OS when the application is focused, i.e. when changing the focus from the OS desktop or a thirdparty application to any open window of the Godot instance.
130-
Implemented on desktop platforms.
131+
Implemented on desktop and mobile platforms.
131132
</constant>
132133
<constant name="NOTIFICATION_APPLICATION_FOCUS_OUT" value="2017">
133134
Notification received from the OS when the application is defocused, i.e. when changing the focus from any open window of the Godot instance to the OS desktop or a thirdparty application.
134-
Implemented on desktop platforms.
135+
Implemented on desktop and mobile platforms.
135136
</constant>
136137
<constant name="NOTIFICATION_TEXT_SERVER_CHANGED" value="2018">
137138
Notification received when text server is changed.

doc/classes/Node.xml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,19 +1161,20 @@
11611161
</constant>
11621162
<constant name="NOTIFICATION_APPLICATION_RESUMED" value="2014">
11631163
Notification received from the OS when the application is resumed.
1164-
Implemented only on Android.
1164+
Specific to the Android and iOS platforms.
11651165
</constant>
11661166
<constant name="NOTIFICATION_APPLICATION_PAUSED" value="2015">
11671167
Notification received from the OS when the application is paused.
1168-
Implemented only on Android.
1168+
Specific to the Android and iOS platforms.
1169+
[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.
11691170
</constant>
11701171
<constant name="NOTIFICATION_APPLICATION_FOCUS_IN" value="2016">
1171-
Notification received from the OS when the application is focused, i.e. when changing the focus from the OS desktop or a third-party application to any open window of the Godot instance.
1172-
Implemented on desktop platforms.
1172+
Notification received from the OS when the application is focused, i.e. when changing the focus from the OS desktop or a thirdparty application to any open window of the Godot instance.
1173+
Implemented on desktop and mobile platforms.
11731174
</constant>
11741175
<constant name="NOTIFICATION_APPLICATION_FOCUS_OUT" value="2017">
1175-
Notification received from the OS when the application is defocused, i.e. when changing the focus from any open window of the Godot instance to the OS desktop or a third-party application.
1176-
Implemented on desktop platforms.
1176+
Notification received from the OS when the application is defocused, i.e. when changing the focus from any open window of the Godot instance to the OS desktop or a thirdparty application.
1177+
Implemented on desktop and mobile platforms.
11771178
</constant>
11781179
<constant name="NOTIFICATION_TEXT_SERVER_CHANGED" value="2018">
11791180
Notification received when the [TextServer] is changed.

platform/android/os_android.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,17 @@ void OS_Android::main_loop_end() {
324324

325325
void OS_Android::main_loop_focusout() {
326326
DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
327+
if (OS::get_singleton()->get_main_loop()) {
328+
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT);
329+
}
327330
audio_driver_android.set_pause(true);
328331
}
329332

330333
void OS_Android::main_loop_focusin() {
331334
DisplayServerAndroid::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_IN);
335+
if (OS::get_singleton()->get_main_loop()) {
336+
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN);
337+
}
332338
audio_driver_android.set_pause(false);
333339
}
334340

platform/ios/app_delegate.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ - (void)applicationDidBecomeActive:(UIApplication *)application {
167167
OS_IOS::get_singleton()->on_focus_in();
168168
}
169169

170+
- (void)applicationDidEnterBackground:(UIApplication *)application {
171+
OS_IOS::get_singleton()->on_enter_background();
172+
}
173+
174+
- (void)applicationWillEnterForeground:(UIApplication *)application {
175+
OS_IOS::get_singleton()->on_exit_background();
176+
}
177+
170178
- (void)dealloc {
171179
self.window = nil;
172180
}

platform/ios/os_ios.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ class OS_IOS : public OS_Unix {
129129

130130
void on_focus_out();
131131
void on_focus_in();
132+
133+
void on_enter_background();
134+
void on_exit_background();
132135
};
133136

134137
#endif // IOS_ENABLED

platform/ios/os_ios.mm

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,10 @@ void register_dynamic_symbol(char *name, void *address) {
601601
DisplayServerIOS::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
602602
}
603603

604+
if (OS::get_singleton()->get_main_loop()) {
605+
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT);
606+
}
607+
604608
[AppDelegate.viewController.godotView stopRendering];
605609

606610
audio_driver.stop();
@@ -615,10 +619,34 @@ void register_dynamic_symbol(char *name, void *address) {
615619
DisplayServerIOS::get_singleton()->send_window_event(DisplayServer::WINDOW_EVENT_FOCUS_IN);
616620
}
617621

622+
if (OS::get_singleton()->get_main_loop()) {
623+
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN);
624+
}
625+
618626
[AppDelegate.viewController.godotView startRendering];
619627

620628
audio_driver.start();
621629
}
622630
}
623631

632+
void OS_IOS::on_enter_background() {
633+
// Do not check for is_focused, because on_focus_out will always be fired first by applicationWillResignActive.
634+
635+
if (OS::get_singleton()->get_main_loop()) {
636+
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_PAUSED);
637+
}
638+
639+
on_focus_out();
640+
}
641+
642+
void OS_IOS::on_exit_background() {
643+
if (!is_focused) {
644+
on_focus_in();
645+
646+
if (OS::get_singleton()->get_main_loop()) {
647+
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_RESUMED);
648+
}
649+
}
650+
}
651+
624652
#endif // IOS_ENABLED

0 commit comments

Comments
 (0)