mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Add ExtraWindowEventListener interface (#55721)
Summary: This PR introduces a `ExtraWindowEventListener` interface that allows native modules to be notified when new windows are created or destroyed (e.g. Modal dialogs). Third-party libraries can implement it, or emit window events through `ReactContext.onExtraWindowCreate` / `ReactContext.onExtraWindowDestroy`. This opens the door for libraries like `expo-navigation-bar` to build a proper `NavigationBar` module that stays in sync across all windows (including modals). ## Related issue - https://github.com/zoontek/react-native-navigation-bar/issues/4 ## Changelog: [ANDROID] [ADDED] - Add `ExtraWindowEventListener` interface to allow native modules to react to window creation / destruction (e.g. Modal dialogs) Pull Request resolved: https://github.com/facebook/react-native/pull/55721 Test Plan: Follow https://github.com/facebook/react-native/pull/56059 test plan Reviewed By: cortinico Differential Revision: D94871845 Pulled By: alanleedev fbshipit-source-id: b2c00950c3c60dbeb3d53520d95160b301829a5e
This commit is contained in:
1 parent
f8fa76f94c
commit
58fec1fa8d
7 files changed
+308
No files matched your search
@@ -943,6 +943,7 @@ public abstract class com/facebook/react/bridge/ReactContext : android/content/C
|
||||
protected field mInteropModuleRegistry Lcom/facebook/react/bridge/interop/InteropModuleRegistry;
|
||||
public fun <init> (Landroid/content/Context;)V
|
||||
public fun addActivityEventListener (Lcom/facebook/react/bridge/ActivityEventListener;)V
|
||||
public fun addExtraWindowEventListener (Lcom/facebook/react/interfaces/ExtraWindowEventListener;)V
|
||||
public fun addLifecycleEventListener (Lcom/facebook/react/bridge/LifecycleEventListener;)V
|
||||
public fun addWindowFocusChangeListener (Lcom/facebook/react/bridge/WindowFocusChangeListener;)V
|
||||
public fun assertOnJSQueueThread ()V
|
||||
@@ -986,6 +987,8 @@ public abstract class com/facebook/react/bridge/ReactContext : android/content/C
|
||||
public fun isOnNativeModulesQueueThread ()Z
|
||||
public fun isOnUiQueueThread ()Z
|
||||
public fun onActivityResult (Landroid/app/Activity;IILandroid/content/Intent;)V
|
||||
public fun onExtraWindowCreate (Landroid/view/Window;)V
|
||||
public fun onExtraWindowDestroy (Landroid/view/Window;)V
|
||||
public fun onHostDestroy ()V
|
||||
public fun onHostDestroy (Z)V
|
||||
public fun onHostPause ()V
|
||||
@@ -995,6 +998,7 @@ public abstract class com/facebook/react/bridge/ReactContext : android/content/C
|
||||
public fun onWindowFocusChange (Z)V
|
||||
public abstract fun registerSegment (ILjava/lang/String;Lcom/facebook/react/bridge/Callback;)V
|
||||
public fun removeActivityEventListener (Lcom/facebook/react/bridge/ActivityEventListener;)V
|
||||
public fun removeExtraWindowEventListener (Lcom/facebook/react/interfaces/ExtraWindowEventListener;)V
|
||||
public fun removeLifecycleEventListener (Lcom/facebook/react/bridge/LifecycleEventListener;)V
|
||||
public fun removeWindowFocusChangeListener (Lcom/facebook/react/bridge/WindowFocusChangeListener;)V
|
||||
public fun resetPerfStats ()V
|
||||
@@ -2305,6 +2309,11 @@ public final class com/facebook/react/fabric/mounting/SurfaceMountingManager {
|
||||
public final fun updateState (ILcom/facebook/react/uimanager/StateWrapper;)V
|
||||
}
|
||||
|
||||
public abstract interface class com/facebook/react/interfaces/ExtraWindowEventListener {
|
||||
public abstract fun onExtraWindowCreate (Landroid/view/Window;)V
|
||||
public abstract fun onExtraWindowDestroy (Landroid/view/Window;)V
|
||||
}
|
||||
|
||||
public abstract interface class com/facebook/react/interfaces/TaskInterface {
|
||||
public abstract fun getError ()Ljava/lang/Exception;
|
||||
public abstract fun getResult ()Ljava/lang/Object;
|
||||
@@ -4149,6 +4158,7 @@ public final class com/facebook/react/uimanager/ThemedReactContext : com/faceboo
|
||||
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;Landroid/content/Context;Ljava/lang/String;)V
|
||||
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;Landroid/content/Context;Ljava/lang/String;I)V
|
||||
public synthetic fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;Landroid/content/Context;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
|
||||
public fun addExtraWindowEventListener (Lcom/facebook/react/interfaces/ExtraWindowEventListener;)V
|
||||
public fun addLifecycleEventListener (Lcom/facebook/react/bridge/LifecycleEventListener;)V
|
||||
public fun destroy ()V
|
||||
public fun getCatalystInstance ()Lcom/facebook/react/bridge/CatalystInstance;
|
||||
@@ -4174,7 +4184,10 @@ public final class com/facebook/react/uimanager/ThemedReactContext : com/faceboo
|
||||
public fun hasNativeModule (Ljava/lang/Class;)Z
|
||||
public fun hasReactInstance ()Z
|
||||
public fun isBridgeless ()Z
|
||||
public fun onExtraWindowCreate (Landroid/view/Window;)V
|
||||
public fun onExtraWindowDestroy (Landroid/view/Window;)V
|
||||
public fun registerSegment (ILjava/lang/String;Lcom/facebook/react/bridge/Callback;)V
|
||||
public fun removeExtraWindowEventListener (Lcom/facebook/react/interfaces/ExtraWindowEventListener;)V
|
||||
public fun removeLifecycleEventListener (Lcom/facebook/react/bridge/LifecycleEventListener;)V
|
||||
}
|
||||
|
||||
|
||||
+36
@@ -15,6 +15,7 @@ import android.content.ContextWrapper;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Window;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import com.facebook.common.logging.FLog;
|
||||
@@ -25,6 +26,7 @@ import com.facebook.react.bridge.interop.InteropModuleRegistry;
|
||||
import com.facebook.react.bridge.queue.MessageQueueThread;
|
||||
import com.facebook.react.bridge.queue.ReactQueueConfiguration;
|
||||
import com.facebook.react.common.LifecycleState;
|
||||
import com.facebook.react.interfaces.ExtraWindowEventListener;
|
||||
import com.facebook.react.turbomodule.core.interfaces.CallInvokerHolder;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Collection;
|
||||
@@ -48,6 +50,8 @@ public abstract class ReactContext extends ContextWrapper {
|
||||
new CopyOnWriteArraySet<>();
|
||||
private final CopyOnWriteArraySet<ActivityEventListener> mActivityEventListeners =
|
||||
new CopyOnWriteArraySet<>();
|
||||
private final CopyOnWriteArraySet<ExtraWindowEventListener> mExtraWindowEventListeners =
|
||||
new CopyOnWriteArraySet<>();
|
||||
private final CopyOnWriteArraySet<WindowFocusChangeListener> mWindowFocusEventListeners =
|
||||
new CopyOnWriteArraySet<>();
|
||||
private final ScrollEndedListeners mScrollEndedListeners = new ScrollEndedListeners();
|
||||
@@ -246,6 +250,14 @@ public abstract class ReactContext extends ContextWrapper {
|
||||
mActivityEventListeners.remove(listener);
|
||||
}
|
||||
|
||||
public void addExtraWindowEventListener(ExtraWindowEventListener listener) {
|
||||
mExtraWindowEventListeners.add(listener);
|
||||
}
|
||||
|
||||
public void removeExtraWindowEventListener(ExtraWindowEventListener listener) {
|
||||
mExtraWindowEventListeners.remove(listener);
|
||||
}
|
||||
|
||||
public void addWindowFocusChangeListener(WindowFocusChangeListener listener) {
|
||||
mWindowFocusEventListeners.add(listener);
|
||||
}
|
||||
@@ -356,6 +368,30 @@ public abstract class ReactContext extends ContextWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
@ThreadConfined(UI)
|
||||
public void onExtraWindowCreate(Window window) {
|
||||
UiThreadUtil.assertOnUiThread();
|
||||
for (ExtraWindowEventListener listener : mExtraWindowEventListeners) {
|
||||
try {
|
||||
listener.onExtraWindowCreate(window);
|
||||
} catch (RuntimeException e) {
|
||||
handleException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ThreadConfined(UI)
|
||||
public void onExtraWindowDestroy(Window window) {
|
||||
UiThreadUtil.assertOnUiThread();
|
||||
for (ExtraWindowEventListener listener : mExtraWindowEventListeners) {
|
||||
try {
|
||||
listener.onExtraWindowDestroy(window);
|
||||
} catch (RuntimeException e) {
|
||||
handleException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ThreadConfined(UI)
|
||||
public void onWindowFocusChange(boolean hasFocus) {
|
||||
UiThreadUtil.assertOnUiThread();
|
||||
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.interfaces
|
||||
|
||||
import android.view.Window
|
||||
|
||||
/**
|
||||
* Listener for receiving extra window creation and destruction events.
|
||||
*
|
||||
* This allows modules to react to new windows being added or removed, such as Dialog windows
|
||||
* registered by Modal components. Modules like StatusBarModule can implement this interface to
|
||||
* apply their configuration to all active windows.
|
||||
*
|
||||
* Third-party libraries can both implement this listener and emit window events through
|
||||
* [ReactContext.onExtraWindowCreate] and [ReactContext.onExtraWindowDestroy].
|
||||
*/
|
||||
public interface ExtraWindowEventListener {
|
||||
|
||||
/** Called when a new [Window] is created (e.g. a Dialog window for a Modal). */
|
||||
public fun onExtraWindowCreate(window: Window)
|
||||
|
||||
/** Called when a [Window] is destroyed (e.g. on Dialog window dismiss). */
|
||||
public fun onExtraWindowDestroy(window: Window)
|
||||
}
|
||||
+18
@@ -11,6 +11,7 @@ package com.facebook.react.uimanager
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.view.Window
|
||||
import com.facebook.react.bridge.Callback
|
||||
import com.facebook.react.bridge.CatalystInstance
|
||||
import com.facebook.react.bridge.JavaScriptContextHolder
|
||||
@@ -22,6 +23,7 @@ import com.facebook.react.bridge.ReactContext
|
||||
import com.facebook.react.bridge.ScrollEndedListeners
|
||||
import com.facebook.react.bridge.UIManager
|
||||
import com.facebook.react.common.annotations.internal.LegacyArchitecture
|
||||
import com.facebook.react.interfaces.ExtraWindowEventListener
|
||||
import com.facebook.react.turbomodule.core.interfaces.CallInvokerHolder
|
||||
|
||||
/**
|
||||
@@ -67,6 +69,22 @@ public class ThemedReactContext(
|
||||
reactApplicationContext.removeLifecycleEventListener(listener)
|
||||
}
|
||||
|
||||
override fun addExtraWindowEventListener(listener: ExtraWindowEventListener) {
|
||||
reactApplicationContext.addExtraWindowEventListener(listener)
|
||||
}
|
||||
|
||||
override fun removeExtraWindowEventListener(listener: ExtraWindowEventListener) {
|
||||
reactApplicationContext.removeExtraWindowEventListener(listener)
|
||||
}
|
||||
|
||||
override fun onExtraWindowCreate(window: Window) {
|
||||
reactApplicationContext.onExtraWindowCreate(window)
|
||||
}
|
||||
|
||||
override fun onExtraWindowDestroy(window: Window) {
|
||||
reactApplicationContext.onExtraWindowDestroy(window)
|
||||
}
|
||||
|
||||
override fun hasCurrentActivity(): Boolean = reactApplicationContext.hasCurrentActivity()
|
||||
|
||||
override fun getCurrentActivity(): Activity? = reactApplicationContext.getCurrentActivity()
|
||||
|
||||
+4
@@ -197,6 +197,9 @@ public class ReactModalHostView(context: ThemedReactContext) :
|
||||
|
||||
dialog?.let { nonNullDialog ->
|
||||
if (nonNullDialog.isShowing) {
|
||||
nonNullDialog.window?.let { window ->
|
||||
(context as ThemedReactContext).onExtraWindowDestroy(window)
|
||||
}
|
||||
val dialogContext =
|
||||
ContextUtils.findContextOfType(nonNullDialog.context, Activity::class.java)
|
||||
if (dialogContext == null || !dialogContext.isFinishing) {
|
||||
@@ -341,6 +344,7 @@ public class ReactModalHostView(context: ThemedReactContext) :
|
||||
newDialog.show()
|
||||
updateSystemAppearance()
|
||||
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
|
||||
(context as ThemedReactContext).onExtraWindowCreate(window)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.interfaces
|
||||
|
||||
import android.view.Window
|
||||
import com.facebook.react.bridge.ReactApplicationContext
|
||||
import com.facebook.react.bridge.ReactTestHelper
|
||||
import com.facebook.testutils.shadows.ShadowSoLoader
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.kotlin.mock
|
||||
import org.mockito.kotlin.never
|
||||
import org.mockito.kotlin.times
|
||||
import org.mockito.kotlin.verify
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(shadows = [ShadowSoLoader::class])
|
||||
class ExtraWindowEventListenerTest {
|
||||
private lateinit var reactContext: ReactApplicationContext
|
||||
private lateinit var window: Window
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
reactContext = ReactTestHelper.createCatalystContextForTest()
|
||||
window = mock()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOnExtraWindowCreateNotifiesListener() {
|
||||
val listener: ExtraWindowEventListener = mock()
|
||||
|
||||
reactContext.addExtraWindowEventListener(listener)
|
||||
reactContext.onExtraWindowCreate(window)
|
||||
|
||||
verify(listener, times(1)).onExtraWindowCreate(window)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOnExtraWindowDestroyNotifiesListener() {
|
||||
val listener: ExtraWindowEventListener = mock()
|
||||
|
||||
reactContext.addExtraWindowEventListener(listener)
|
||||
reactContext.onExtraWindowDestroy(window)
|
||||
|
||||
verify(listener, times(1)).onExtraWindowDestroy(window)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMultipleListenersAreNotified() {
|
||||
val listener1: ExtraWindowEventListener = mock()
|
||||
val listener2: ExtraWindowEventListener = mock()
|
||||
|
||||
reactContext.addExtraWindowEventListener(listener1)
|
||||
reactContext.addExtraWindowEventListener(listener2)
|
||||
reactContext.onExtraWindowCreate(window)
|
||||
|
||||
verify(listener1, times(1)).onExtraWindowCreate(window)
|
||||
verify(listener2, times(1)).onExtraWindowCreate(window)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRemovedListenerIsNotNotified() {
|
||||
val listener: ExtraWindowEventListener = mock()
|
||||
|
||||
reactContext.addExtraWindowEventListener(listener)
|
||||
reactContext.removeExtraWindowEventListener(listener)
|
||||
reactContext.onExtraWindowCreate(window)
|
||||
|
||||
verify(listener, never()).onExtraWindowCreate(window)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOnlyRemovedListenerStopsReceivingEvents() {
|
||||
val listener1: ExtraWindowEventListener = mock()
|
||||
val listener2: ExtraWindowEventListener = mock()
|
||||
|
||||
reactContext.addExtraWindowEventListener(listener1)
|
||||
reactContext.addExtraWindowEventListener(listener2)
|
||||
reactContext.removeExtraWindowEventListener(listener1)
|
||||
reactContext.onExtraWindowDestroy(window)
|
||||
|
||||
verify(listener1, never()).onExtraWindowDestroy(window)
|
||||
verify(listener2, times(1)).onExtraWindowDestroy(window)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testListenerReceivesBothCreateAndDestroyEvents() {
|
||||
val listener: ExtraWindowEventListener = mock()
|
||||
|
||||
reactContext.addExtraWindowEventListener(listener)
|
||||
reactContext.onExtraWindowCreate(window)
|
||||
reactContext.onExtraWindowDestroy(window)
|
||||
|
||||
verify(listener, times(1)).onExtraWindowCreate(window)
|
||||
verify(listener, times(1)).onExtraWindowDestroy(window)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testNoListenersDoesNotCrash() {
|
||||
// Should not throw when no listeners are registered
|
||||
reactContext.onExtraWindowCreate(window)
|
||||
reactContext.onExtraWindowDestroy(window)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDuplicateAddIsIdempotent() {
|
||||
val listener: ExtraWindowEventListener = mock()
|
||||
|
||||
reactContext.addExtraWindowEventListener(listener)
|
||||
reactContext.addExtraWindowEventListener(listener)
|
||||
reactContext.onExtraWindowCreate(window)
|
||||
|
||||
// CopyOnWriteArraySet deduplicates, so listener should only be called once
|
||||
verify(listener, times(1)).onExtraWindowCreate(window)
|
||||
}
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.uimanager
|
||||
|
||||
import android.view.Window
|
||||
import com.facebook.react.bridge.ReactApplicationContext
|
||||
import com.facebook.react.bridge.ReactTestHelper
|
||||
import com.facebook.react.interfaces.ExtraWindowEventListener
|
||||
import com.facebook.testutils.shadows.ShadowSoLoader
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.kotlin.mock
|
||||
import org.mockito.kotlin.times
|
||||
import org.mockito.kotlin.verify
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(shadows = [ShadowSoLoader::class])
|
||||
class ThemedReactContextTest {
|
||||
private lateinit var reactApplicationContext: ReactApplicationContext
|
||||
private lateinit var themedReactContext: ThemedReactContext
|
||||
private lateinit var window: Window
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
reactApplicationContext = ReactTestHelper.createCatalystContextForTest()
|
||||
themedReactContext =
|
||||
ThemedReactContext(reactApplicationContext, RuntimeEnvironment.getApplication(), null, -1)
|
||||
window = mock()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAddExtraWindowEventListenerDelegatesToReactApplicationContext() {
|
||||
val listener: ExtraWindowEventListener = mock()
|
||||
|
||||
themedReactContext.addExtraWindowEventListener(listener)
|
||||
// Verify the listener was registered on the underlying context by dispatching an event
|
||||
reactApplicationContext.onExtraWindowCreate(window)
|
||||
|
||||
verify(listener, times(1)).onExtraWindowCreate(window)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testRemoveExtraWindowEventListenerDelegatesToReactApplicationContext() {
|
||||
val listener: ExtraWindowEventListener = mock()
|
||||
|
||||
themedReactContext.addExtraWindowEventListener(listener)
|
||||
themedReactContext.removeExtraWindowEventListener(listener)
|
||||
// After removal via ThemedReactContext, the listener should not be notified
|
||||
reactApplicationContext.onExtraWindowCreate(window)
|
||||
|
||||
verify(listener, times(0)).onExtraWindowCreate(window)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOnExtraWindowCreateDelegatesToReactApplicationContext() {
|
||||
val listener: ExtraWindowEventListener = mock()
|
||||
|
||||
reactApplicationContext.addExtraWindowEventListener(listener)
|
||||
// Dispatching via ThemedReactContext should reach listeners on the underlying context
|
||||
themedReactContext.onExtraWindowCreate(window)
|
||||
|
||||
verify(listener, times(1)).onExtraWindowCreate(window)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testOnExtraWindowDestroyDelegatesToReactApplicationContext() {
|
||||
val listener: ExtraWindowEventListener = mock()
|
||||
|
||||
reactApplicationContext.addExtraWindowEventListener(listener)
|
||||
// Dispatching via ThemedReactContext should reach listeners on the underlying context
|
||||
themedReactContext.onExtraWindowDestroy(window)
|
||||
|
||||
verify(listener, times(1)).onExtraWindowDestroy(window)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user