Skip to content

Commit ab55e12

Browse files
cortinicofacebook-github-bot
authored andcommitted
Expose UnstableReactLegacyComponentDescriptor inside rrc_legacyviewmanagerinterop
Summary: This adds the `UnstableReactLegacyComponentDescriptor`, part of the Fabric Interop Layer for Android. Summary of the changes are: * Expose a new `rrc_legacyviewmanagerinterop` native module via prefab * Extend default App Cmake setup so `rrc_legacyviewmanagerinterop` is exposed by default * Add a sample legacy component inside RN Tester Changelog: [Internal] [Changed] - Expose UnstableReactLegacyComponentDescriptor inside react/renderer/core Reviewed By: cipolleschi Differential Revision: D43731219 fbshipit-source-id: ee26d7e9d7eff5ef6a3e22853f8ea363b9198567
1 parent b72dc7f commit ab55e12

File tree

10 files changed

+189
-8
lines changed

10 files changed

+189
-8
lines changed

ReactAndroid/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ final def preparePrefab = tasks.register("preparePrefab", PreparePrefabHeadersTa
120120
"rrc_view",
121121
new Pair("../ReactCommon/react/renderer/components/view/", "react/renderer/components/view/")
122122
),
123+
new PrefabPreprocessingEntry(
124+
"rrc_legacyviewmanagerinterop",
125+
new Pair("../ReactCommon/react/renderer/components/legacyviewmanagerinterop/", "react/renderer/components/legacyviewmanagerinterop/")
126+
),
123127
new PrefabPreprocessingEntry(
124128
"jsi",
125129
new Pair("../ReactCommon/jsi/", "")
@@ -485,6 +489,7 @@ android {
485489
"rrc_image",
486490
"rrc_root",
487491
"rrc_view",
492+
"rrc_legacyviewmanagerinterop",
488493
"jsi",
489494
"glog",
490495
"fabricjni",
@@ -586,6 +591,9 @@ android {
586591
rrc_view {
587592
headers(new File(prefabHeadersDir, "rrc_view").absolutePath)
588593
}
594+
rrc_legacyviewmanagerinterop {
595+
headers(new File(prefabHeadersDir, "rrc_legacyviewmanagerinterop").absolutePath)
596+
}
589597
jsi {
590598
headers(new File(prefabHeadersDir, "jsi").absolutePath)
591599
}

ReactAndroid/cmake-utils/ReactNative-application.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ add_library(folly_runtime ALIAS ReactAndroid::folly_runtime)
6969
add_library(react_nativemodule_core ALIAS ReactAndroid::react_nativemodule_core)
7070
add_library(react_render_imagemanager ALIAS ReactAndroid::react_render_imagemanager)
7171
add_library(rrc_image ALIAS ReactAndroid::rrc_image)
72+
add_library(rrc_legacyviewmanagerinterop ALIAS ReactAndroid::rrc_legacyviewmanagerinterop)
7273

7374
find_package(fbjni REQUIRED CONFIG)
7475
add_library(fbjni ALIAS fbjni::fbjni)
@@ -91,6 +92,7 @@ target_link_libraries(${CMAKE_PROJECT_NAME}
9192
react_render_mapbuffer # prefab ready
9293
rrc_image # prefab ready
9394
rrc_view # prefab ready
95+
rrc_legacyviewmanagerinterop # prefab ready
9496
runtimeexecutor # prefab ready
9597
turbomodulejsijni # prefab ready
9698
yoga) # prefab ready

ReactAndroid/src/main/jni/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ add_react_common_subdir(react/renderer/components/textinput)
8787
add_react_common_subdir(react/renderer/components/progressbar)
8888
add_react_common_subdir(react/renderer/components/root)
8989
add_react_common_subdir(react/renderer/components/image)
90+
add_react_common_subdir(react/renderer/components/legacyviewmanagerinterop)
9091
add_react_common_subdir(react/renderer/componentregistry/native)
9192
add_react_common_subdir(react/renderer/components/text)
9293
add_react_common_subdir(react/renderer/components/unimplementedview)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# This source code is licensed under the MIT license found in the
4+
# LICENSE file in the root directory of this source tree.
5+
6+
cmake_minimum_required(VERSION 3.13)
7+
set(CMAKE_VERBOSE_MAKEFILE on)
8+
9+
add_compile_options(
10+
-fexceptions
11+
-frtti
12+
-std=c++17
13+
-Wall
14+
-Wpedantic
15+
-Wno-gnu-zero-variadic-macro-arguments
16+
-DLOG_TAG=\"Fabric\")
17+
18+
file(GLOB rrc_legacyviewmanagerinterop_SRC CONFIGURE_DEPENDS *.cpp)
19+
add_library(rrc_legacyviewmanagerinterop SHARED ${rrc_legacyviewmanagerinterop_SRC})
20+
21+
target_include_directories(rrc_legacyviewmanagerinterop PUBLIC ${REACT_COMMON_DIR})
22+
23+
target_link_libraries(rrc_legacyviewmanagerinterop
24+
glog
25+
glog_init
26+
folly_runtime
27+
jsi
28+
react_render_core
29+
rrc_view
30+
yoga
31+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <react/renderer/components/view/ConcreteViewShadowNode.h>
11+
#include <react/renderer/components/view/ViewProps.h>
12+
#include <react/renderer/core/ConcreteComponentDescriptor.h>
13+
14+
namespace facebook {
15+
namespace react {
16+
17+
/*
18+
* Descriptor for <UnstableReactLegacyComponent> component.
19+
*
20+
* This component is part of the Fabric Interop Layer and is subject to future
21+
* changes (hence the "Unstable" prefix).
22+
*/
23+
template <const char *concreteComponentName>
24+
class UnstableLegacyViewManagerInteropComponentDescriptor
25+
: public ConcreteComponentDescriptor<
26+
ConcreteViewShadowNode<concreteComponentName, ViewProps>> {
27+
public:
28+
UnstableLegacyViewManagerInteropComponentDescriptor<concreteComponentName>(
29+
ComponentDescriptorParameters const &parameters)
30+
: ConcreteComponentDescriptor<
31+
ConcreteViewShadowNode<concreteComponentName, ViewProps>>(
32+
parameters) {}
33+
34+
private:
35+
};
36+
37+
} // namespace react
38+
} // namespace facebook
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
import type {HostComponent} from 'react-native';
12+
import type {ViewProps} from 'react-native/Libraries/Components/View/ViewPropTypes';
13+
import {requireNativeComponent} from 'react-native';
14+
15+
type NativeProps = $ReadOnly<{|
16+
...ViewProps,
17+
opacity?: number,
18+
color?: string,
19+
|}>;
20+
21+
export type MyLegacyViewType = HostComponent<NativeProps>;
22+
23+
export default (requireNativeComponent(
24+
'RNTMyLegacyNativeView',
25+
): HostComponent<NativeProps>);

packages/rn-tester/NativeComponentExample/js/MyNativeView.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111
import * as React from 'react';
1212
import {useRef, useState} from 'react';
13-
import {View, Button} from 'react-native';
13+
import {View, Button, Text} from 'react-native';
1414
import RNTMyNativeView, {
1515
Commands as RNTMyNativeViewCommands,
1616
} from './MyNativeViewNativeComponent';
17+
import RNTMyLegacyNativeView from './MyLegacyViewNativeComponent';
1718
import type {MyNativeViewType} from './MyNativeViewNativeComponent';
1819

1920
const colors = [
@@ -29,18 +30,27 @@ const colors = [
2930
export default function MyNativeView(props: {}): React.Node {
3031
const ref = useRef<React.ElementRef<MyNativeViewType> | null>(null);
3132
const [opacity, setOpacity] = useState(1.0);
33+
const [color, setColor] = useState('#000000');
3234
return (
3335
<View style={{flex: 1}}>
36+
<Text style={{color: 'red'}}>Fabric View</Text>
3437
<RNTMyNativeView ref={ref} style={{flex: 1}} opacity={opacity} />
38+
<Text style={{color: 'red'}}>Legacy View</Text>
39+
<RNTMyLegacyNativeView
40+
style={{flex: 1}}
41+
opacity={opacity}
42+
color={color}
43+
/>
3544
<Button
3645
title="Change Background"
3746
onPress={() => {
38-
if (ref.current) {
39-
RNTMyNativeViewCommands.callNativeMethodToChangeBackgroundColor(
40-
ref.current,
41-
colors[Math.floor(Math.random() * 5)],
42-
);
43-
}
47+
let newColor = colors[Math.floor(Math.random() * 5)];
48+
setColor(newColor);
49+
RNTMyNativeViewCommands.callNativeMethodToChangeBackgroundColor(
50+
// $FlowFixMe[incompatible-call]
51+
ref.current,
52+
newColor,
53+
);
4454
}}
4555
/>
4656
<Button

packages/rn-tester/android/app/src/main/java/com/facebook/react/uiapp/RNTesterApplication.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
import com.facebook.react.module.model.ReactModuleInfo;
2323
import com.facebook.react.module.model.ReactModuleInfoProvider;
2424
import com.facebook.react.shell.MainReactPackage;
25+
import com.facebook.react.uiapp.component.MyLegacyViewManager;
2526
import com.facebook.react.uiapp.component.MyNativeViewManager;
2627
import com.facebook.react.uimanager.ViewManager;
2728
import com.facebook.react.views.text.ReactFontManager;
2829
import com.facebook.soloader.SoLoader;
30+
import java.util.ArrayList;
2931
import java.util.Arrays;
3032
import java.util.Collections;
3133
import java.util.HashMap;
@@ -106,7 +108,10 @@ public List<NativeModule> createNativeModules(
106108
@Override
107109
public List<ViewManager> createViewManagers(
108110
@NonNull ReactApplicationContext reactContext) {
109-
return Collections.singletonList(new MyNativeViewManager());
111+
List<ViewManager> viewManagers = new ArrayList<>();
112+
viewManagers.add(new MyNativeViewManager());
113+
viewManagers.add(new MyLegacyViewManager(reactContext));
114+
return viewManagers;
110115
}
111116
});
112117
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.uiapp.component;
9+
10+
import android.graphics.Color;
11+
import androidx.annotation.NonNull;
12+
import androidx.annotation.Nullable;
13+
import com.facebook.react.bridge.ReactApplicationContext;
14+
import com.facebook.react.module.annotations.ReactModule;
15+
import com.facebook.react.uimanager.SimpleViewManager;
16+
import com.facebook.react.uimanager.ThemedReactContext;
17+
import com.facebook.react.uimanager.ViewProps;
18+
import com.facebook.react.uimanager.annotations.ReactProp;
19+
20+
/** Legacy View manager (non Fabric compatible) for {@link MyNativeView} components. */
21+
@ReactModule(name = MyLegacyViewManager.REACT_CLASS)
22+
public class MyLegacyViewManager extends SimpleViewManager<MyNativeView> {
23+
24+
public static final String REACT_CLASS = "RNTMyLegacyNativeView";
25+
private final ReactApplicationContext mCallerContext;
26+
27+
public MyLegacyViewManager(ReactApplicationContext reactContext) {
28+
mCallerContext = reactContext;
29+
}
30+
31+
@NonNull
32+
@Override
33+
public String getName() {
34+
return REACT_CLASS;
35+
}
36+
37+
@NonNull
38+
@Override
39+
protected MyNativeView createViewInstance(@NonNull ThemedReactContext reactContext) {
40+
MyNativeView view = new MyNativeView(reactContext);
41+
view.setBackgroundColor(Color.RED);
42+
return view;
43+
}
44+
45+
@Override
46+
@ReactProp(name = ViewProps.OPACITY, defaultFloat = 1.f)
47+
public void setOpacity(@NonNull MyNativeView view, float opacity) {
48+
super.setOpacity(view, opacity);
49+
}
50+
51+
@ReactProp(name = ViewProps.COLOR)
52+
public void setColor(@NonNull MyNativeView view, @Nullable String color) {
53+
view.setBackgroundColor(Color.parseColor(color));
54+
}
55+
}

packages/rn-tester/android/app/src/main/jni/OnLoad.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,21 @@
1313
#include <fbjni/fbjni.h>
1414
#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
1515
#include <react/renderer/components/AppSpecs/ComponentDescriptors.h>
16+
#include <react/renderer/components/legacyviewmanagerinterop/UnstableLegacyViewManagerInteropComponentDescriptor.h>
1617
#include <rncore.h>
1718

1819
namespace facebook {
1920
namespace react {
2021

22+
extern const char RNTMyNativeViewName[] = "RNTMyLegacyNativeView";
23+
2124
void registerComponents(
2225
std::shared_ptr<ComponentDescriptorProviderRegistry const> registry) {
2326
registry->add(concreteComponentDescriptorProvider<
2427
RNTMyNativeViewComponentDescriptor>());
28+
registry->add(concreteComponentDescriptorProvider<
29+
UnstableLegacyViewManagerInteropComponentDescriptor<
30+
RNTMyNativeViewName>>());
2531
}
2632

2733
std::shared_ptr<TurboModule> cxxModuleProvider(

0 commit comments

Comments
 (0)