C-API 版本使用XComponent总共分成了两个步骤:
-
createSurface的时候创建XComponentSurface;
-
startSurface的时候将CPP的XComponentSurface连接到ArkUI的Xcomponent上。
createSurface的时候主要做了以下的操作:
1.创建并将XComponentSurface记录到Map中:
void RNInstanceCAPI::createSurface(facebook::react::Tag surfaceId, std::string const& moduleName) {
m_surfaceById.emplace(
surfaceId,
XComponentSurface(
// ···
surfaceId,
moduleName)
);
}
2.在XComponentSurface中创建rootView,用于挂载C-API的组件,并在Surface上统一处理Touch事件:
XComponentSurface::XComponentSurface(
//···
SurfaceId surfaceId,
std::string const& appKey)
:
//···
m_nativeXComponent(nullptr),
m_rootView(nullptr),
m_surfaceHandler(SurfaceHandler(appKey, surfaceId)) {
m_scheduler->registerSurface(m_surfaceHandler);
m_rootView = componentInstanceFactory->create(
surfaceId, facebook::react::RootShadowNode::Handle(), "RootView");
m_componentInstanceRegistry->insert(m_rootView);
m_touchEventHandler = std::make_unique<SurfaceTouchEventHandler>(m_rootView);
}
startSurface的时候主要做了以下的操作:
- 在ArkTS侧创建XComponent,并设置id,type与libraryname属性。其中:
- id:组件的唯一标识,又由InstanceID和SurfaceID共同组成,记录了此XComponent属于哪一个Instance与Surface;
- type:node