Skip to content

Commit

Permalink
Fallback to the first foregroundInactive window when there are no `…
Browse files Browse the repository at this point in the history
…foregroundActive` windows in RCTKeyWindow (#44167)

Summary:
Pull Request resolved: #44167

We received an issue for OSS where, when the main window is inactive and the system tries to present a dialog, the dialog is not presented in the right position on the screen.

This change introduce a fallback to the first inactive window (which is still visible on screen) and it fixes the issues.

[iOS][Changed] - Fallback to the first `foregroundInactive` window when there are no `foregroundActive` windows in RCTKeyWindow

Reviewed By: dmytrorykun

Differential Revision: D56354741

fbshipit-source-id: fa23131ecd40f6d91c705879a72890506ee21486
  • Loading branch information
cipolleschi committed May 1, 2024
1 parent 7a841db commit eaaf865
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions packages/react-native/React/Base/RCTUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -562,17 +562,37 @@ BOOL RCTRunningInAppExtension(void)
return nil;
}

for (UIScene *scene in RCTSharedApplication().connectedScenes) {
if (scene.activationState != UISceneActivationStateForegroundActive ||
![scene isKindOfClass:[UIWindowScene class]]) {
NSSet<UIScene *> *connectedScenes = RCTSharedApplication().connectedScenes;

UIScene *foregroundActiveScene;
UIScene *foregroundInactiveScene;

for (UIScene *scene in connectedScenes) {
if (![scene isKindOfClass:[UIWindowScene class]]) {
continue;
}
UIWindowScene *windowScene = (UIWindowScene *)scene;

for (UIWindow *window in windowScene.windows) {
if (window.isKeyWindow) {
return window;
}
if (scene.activationState == UISceneActivationStateForegroundActive) {
foregroundActiveScene = scene;
break;
}

if (!foregroundInactiveScene && scene.activationState == UISceneActivationStateForegroundInactive) {
foregroundInactiveScene = scene;
// no break, we can have the active scene later in the set.
}
}

UIScene *sceneToUse = foregroundActiveScene ? foregroundActiveScene : foregroundInactiveScene;
UIWindowScene *windowScene = (UIWindowScene *)sceneToUse;

if (@available(iOS 15.0, *)) {
return windowScene.keyWindow;
}

for (UIWindow *window in windowScene.windows) {
if (window.isKeyWindow) {
return window;
}
}

Expand Down

0 comments on commit eaaf865

Please sign in to comment.