Skip to content

Commit f58c496

Browse files
Andrei Shikovfacebook-github-bot
authored andcommitted
Use context from entry point for prerendering
Summary: Some of the prerendered surfaces rely on Android context being present to have correct theming (e.g. for platform colors) and measurements of platform components. This change uses context provided to initialize the surface as themed context before view is attached. This way it is possible to configure theming with `ContextThemeWrapper` the same way as Litho does it for prerendering. The assumption is that any kind of customization done through Android theme will be applied from prerendering entry point as well. Changelog: [Internal] - Use context from surface for prerendering Reviewed By: mdvacca Differential Revision: D31906091 fbshipit-source-id: 344fc96eb2f85ba5b762bee64d1a29443b3fd1d3
1 parent d2c10da commit f58c496

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public <T extends View> int addRootView(
213213
ThemedReactContext reactContext =
214214
new ThemedReactContext(
215215
mReactApplicationContext, rootView.getContext(), reactRootView.getSurfaceID(), rootTag);
216-
mMountingManager.startSurface(rootTag, rootView, reactContext);
216+
mMountingManager.startSurface(rootTag, reactContext, rootView);
217217
String moduleName = reactRootView.getJSModuleName();
218218
if (ENABLE_FABRIC_LOGS) {
219219
FLog.d(TAG, "Starting surface for module: %s and reactTag: %d", moduleName, rootTag);
@@ -271,7 +271,7 @@ public <T extends View> int startSurface(
271271
if (ENABLE_FABRIC_LOGS) {
272272
FLog.d(TAG, "Starting surface for module: %s and reactTag: %d", moduleName, rootTag);
273273
}
274-
mMountingManager.startSurface(rootTag, rootView, reactContext);
274+
mMountingManager.startSurface(rootTag, reactContext, rootView);
275275

276276
// If startSurface is executed in the UIThread then, it uses the ViewportOffset from the View,
277277
// Otherwise Fabric relies on calling {@link Binding#setConstraints} method to update the
@@ -295,18 +295,14 @@ public <T extends View> int startSurface(
295295
return rootTag;
296296
}
297297

298-
public void startSurface(final SurfaceHandler surfaceHandler, final @Nullable View rootView) {
298+
public void startSurface(
299+
final SurfaceHandler surfaceHandler, final Context context, final @Nullable View rootView) {
299300
final int rootTag = ReactRootViewTagGenerator.getNextRootViewTag();
300301

301-
if (rootView == null) {
302-
mMountingManager.startSurface(rootTag);
303-
} else {
304-
Context context = rootView.getContext();
305-
ThemedReactContext reactContext =
306-
new ThemedReactContext(
307-
mReactApplicationContext, context, surfaceHandler.getModuleName(), rootTag);
308-
mMountingManager.startSurface(rootTag, rootView, reactContext);
309-
}
302+
ThemedReactContext reactContext =
303+
new ThemedReactContext(
304+
mReactApplicationContext, context, surfaceHandler.getModuleName(), rootTag);
305+
mMountingManager.startSurface(rootTag, reactContext, rootView);
310306

311307
surfaceHandler.setSurfaceId(rootTag);
312308
if (surfaceHandler instanceof SurfaceHandlerBinding) {

ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountingManager.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,27 +75,21 @@ public MountingManager(
7575
mMountItemExecutor = mountItemExecutor;
7676
}
7777

78-
/** Starts surface and attaches the root view. */
79-
@AnyThread
80-
public void startSurface(
81-
final int surfaceId, @NonNull final View rootView, ThemedReactContext themedReactContext) {
82-
SurfaceMountingManager mountingManager = startSurface(surfaceId);
83-
mountingManager.attachRootView(rootView, themedReactContext);
84-
}
85-
8678
/**
8779
* Starts surface without attaching the view. All view operations executed against that surface
8880
* will be queued until the view is attached.
8981
*/
9082
@AnyThread
91-
public SurfaceMountingManager startSurface(final int surfaceId) {
83+
public SurfaceMountingManager startSurface(
84+
final int surfaceId, ThemedReactContext reactContext, @Nullable View rootView) {
9285
SurfaceMountingManager surfaceMountingManager =
9386
new SurfaceMountingManager(
9487
surfaceId,
9588
mJSResponderHandler,
9689
mViewManagerRegistry,
9790
mRootViewManager,
98-
mMountItemExecutor);
91+
mMountItemExecutor,
92+
reactContext);
9993

10094
// There could technically be a race condition here if addRootView is called twice from
10195
// different threads, though this is (probably) extremely unlikely, and likely an error.
@@ -111,6 +105,11 @@ public SurfaceMountingManager startSurface(final int surfaceId) {
111105
}
112106

113107
mMostRecentSurfaceMountingManager = mSurfaceIdToManager.get(surfaceId);
108+
109+
if (rootView != null) {
110+
surfaceMountingManager.attachRootView(rootView, reactContext);
111+
}
112+
114113
return surfaceMountingManager;
115114
}
116115

@@ -314,8 +313,8 @@ public void updateProps(int reactTag, @Nullable ReadableMap props) {
314313
}
315314

316315
/**
317-
* Clears the JS Responder specified by {@link #setJSResponder(int, int, int, boolean)}. After
318-
* this method is called, all the touch events are going to be handled by JS.
316+
* Clears the JS Responder specified by {@link SurfaceMountingManager#setJSResponder}. After this
317+
* method is called, all the touch events are going to be handled by JS.
319318
*/
320319
@UiThread
321320
public void clearJSResponder() {

ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,15 @@ public SurfaceMountingManager(
7676
@NonNull JSResponderHandler jsResponderHandler,
7777
@NonNull ViewManagerRegistry viewManagerRegistry,
7878
@NonNull RootViewManager rootViewManager,
79-
@NonNull MountItemExecutor mountItemExecutor) {
79+
@NonNull MountItemExecutor mountItemExecutor,
80+
@NonNull ThemedReactContext reactContext) {
8081
mSurfaceId = surfaceId;
8182

8283
mJSResponderHandler = jsResponderHandler;
8384
mViewManagerRegistry = viewManagerRegistry;
8485
mRootViewManager = rootViewManager;
8586
mMountItemExecutor = mountItemExecutor;
87+
mThemedReactContext = reactContext;
8688
}
8789

8890
public boolean isStopped() {

0 commit comments

Comments
 (0)