Skip to content

Commit 377dfd1

Browse files
android host color scheme listener
1 parent 83588a2 commit 377dfd1

7 files changed

Lines changed: 42 additions & 3 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import com.facebook.react.devsupport.interfaces.PackagerStatusCallback;
7373
import com.facebook.react.devsupport.interfaces.PausedInDebuggerOverlayManager;
7474
import com.facebook.react.devsupport.interfaces.RedBoxHandler;
75+
import com.facebook.react.fabric.FabricUIManager;
7576
import com.facebook.react.interfaces.TaskInterface;
7677
import com.facebook.react.internal.AndroidChoreographerProvider;
7778
import com.facebook.react.internal.ChoreographerProvider;
@@ -865,6 +866,11 @@ public void onConfigurationChanged(Context updatedContext, @Nullable Configurati
865866

866867
if (appearanceModule != null) {
867868
appearanceModule.onConfigurationChanged(updatedContext);
869+
870+
UIManager uiManager = UIManagerHelper.getUIManager(currentReactContext, FABRIC);
871+
if (uiManager instanceof FabricUIManager) {
872+
((FabricUIManager) uiManager).onColorSchemeChanged(appearanceModule.getColorScheme());
873+
}
868874
}
869875
}
870876
}

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,16 @@ public void stopSurface(final int surfaceID) {
439439
mBinding.stopSurface(surfaceID);
440440
}
441441

442+
/**
443+
* Triggers re-resolution of conditional styles when the color scheme changes.
444+
*/
445+
public void onColorSchemeChanged(String colorScheme) {
446+
if (mBinding == null) {
447+
return;
448+
}
449+
mBinding.onColorSchemeChanged("dark".equals(colorScheme));
450+
}
451+
442452
@Override
443453
public void initialize() {
444454
mEventDispatcher.addBatchEventDispatchedListener(mBatchEventDispatchedListener);

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManagerBinding.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ internal class FabricUIManagerBinding : HybridClassBase() {
7979

8080
external fun driveCxxAnimations()
8181

82+
external fun onColorSchemeChanged(isDark: Boolean)
83+
8284
external fun driveAnimationBackend(frameTimeNanos: Long)
8385

8486
external fun drainPreallocateViewsQueue()

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ import com.facebook.react.runtime.internal.bolts.TaskCompletionSource
6868
import com.facebook.react.turbomodule.core.interfaces.CallInvokerHolder
6969
import com.facebook.react.uimanager.DisplayMetricsHolder
7070
import com.facebook.react.uimanager.PixelUtil
71+
import com.facebook.react.uimanager.UIManagerHelper
72+
import com.facebook.react.uimanager.common.UIManagerType
7173
import com.facebook.react.uimanager.events.BlackHoleEventDispatcher
7274
import com.facebook.react.uimanager.events.EventDispatcher
7375
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper
@@ -745,6 +747,13 @@ public class ReactHostImpl(
745747

746748
val appearanceModule = currentReactContext.getNativeModule(AppearanceModule::class.java)
747749
appearanceModule?.onConfigurationChanged(context)
750+
751+
// Initialize the color scheme for conditional styles (media query)
752+
if (appearanceModule != null) {
753+
(UIManagerHelper.getUIManager(currentReactContext, UIManagerType.FABRIC)
754+
as? FabricUIManager)
755+
?.onColorSchemeChanged(appearanceModule.getColorScheme())
756+
}
748757
}
749758
}
750759

packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricUIManagerBinding.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ void FabricUIManagerBinding::driveCxxAnimations() {
6161
scheduler->animationTick();
6262
}
6363

64+
void FabricUIManagerBinding::onColorSchemeChanged(jboolean isDarkColorScheme) {
65+
auto scheduler = getScheduler();
66+
if (!scheduler) {
67+
LOG(ERROR)
68+
<< "FabricUIManagerBinding::onColorSchemeChanged: scheduler disappeared";
69+
return;
70+
}
71+
scheduler->onColorSchemeDidChange(
72+
isDarkColorScheme ? ColorScheme::Dark : ColorScheme::Light);
73+
}
74+
6475
void FabricUIManagerBinding::driveAnimationBackend(jlong frameTimeNanos) {
6576
if (!animationChoreographer_) {
6677
LOG(ERROR)
@@ -860,6 +871,8 @@ void FabricUIManagerBinding::registerNatives() {
860871
"setPixelDensity", FabricUIManagerBinding::setPixelDensity),
861872
makeNativeMethod(
862873
"driveCxxAnimations", FabricUIManagerBinding::driveCxxAnimations),
874+
makeNativeMethod(
875+
"onColorSchemeChanged", FabricUIManagerBinding::onColorSchemeChanged),
863876
makeNativeMethod(
864877
"driveAnimationBackend",
865878
FabricUIManagerBinding::driveAnimationBackend),

packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricUIManagerBinding.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class FabricUIManagerBinding : public jni::HybridClass<FabricUIManagerBinding>,
127127

128128
void driveCxxAnimations();
129129

130+
void onColorSchemeChanged(jboolean isDarkColorScheme);
131+
130132
void driveAnimationBackend(jlong frameTimeNanos);
131133

132134
void drainPreallocateViewsQueue();

packages/react-native/ReactCommon/react/renderer/core/ShadowNode.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,6 @@ ShadowNode::ShadowNode(
132132
}
133133
propagateUncullableTraitsFromChildren();
134134
}
135-
// Unconditional, unlike the uncullable trait: this node's own contribution
136-
// is props-derived, and a clone may change props while keeping children
137-
// (e.g. a media query patch), which flips the self bit.
138135
propagateStyleConditionsTrait();
139136
}
140137

0 commit comments

Comments
 (0)