diff --git a/ArkUIKit/StyledStringSample/entry/src/main/cpp/manager.cpp b/ArkUIKit/StyledStringSample/entry/src/main/cpp/manager.cpp index 050ff9aa6d0548cfc3c3444b872e3d5eff60a59b..15f7363b81ea3c4140aa7779e1f30718c65a65f0 100644 --- a/ArkUIKit/StyledStringSample/entry/src/main/cpp/manager.cpp +++ b/ArkUIKit/StyledStringSample/entry/src/main/cpp/manager.cpp @@ -105,20 +105,25 @@ static void HandleStyledStringSerialize(ArkUI_StyledString *styledString) OH_ArkUI_StyledString_Descriptor_Destroy(unmarshalDescriptor); } } + +NodeManager &NodeManager::GetInstance() +{ + static NodeManager instance; + return instance; +} + +void NodeManager::SetContentHandle(ArkUI_NodeContentHandle contentHandle) { contentHandle_ = contentHandle; } void NodeManager::CreateNativeNode() { - if (!xComponent_) { + if (!contentHandle_) { return; } - - // 获取NodeAPI接口 ArkUI_NativeNodeAPI_1 *nodeApi = reinterpret_cast( OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1")); if (nodeApi == nullptr) { return; } - // 创建容器与文本组件 ArkUI_NodeHandle column = CreateColumnContainer(nodeApi); ArkUI_NodeHandle text = CreateStyledText(nodeApi); @@ -168,18 +173,7 @@ void NodeManager::CreateNativeNode() OH_ArkUI_StyledString_Destroy(styledString); OH_Drawing_DestroyTypographyStyle(typographyStyle); OH_Drawing_DestroyFontCollection(fontCollection); - OH_NativeXComponent_AttachNativeRootNode(xComponent_, column); -} - -// 单例与XComponent设置 -NodeManager &NodeManager::GetInstance() -{ - static NodeManager instance; - return instance; -} - -void NodeManager::SetXComponent(OH_NativeXComponent *xComponent) -{ - xComponent_ = xComponent; + // Column作为ContentSlot子组件 + OH_ArkUI_NodeContent_AddNode(contentHandle_, column); } -} // namespace NativeNode::Manager \ No newline at end of file +} // namespace NativeNode::Manager diff --git a/ArkUIKit/StyledStringSample/entry/src/main/cpp/manager.h b/ArkUIKit/StyledStringSample/entry/src/main/cpp/manager.h index ec67c3bfff967de3f71b394942d43a194f621db6..40d7565e58b7363d5d78d39301b6e699bb56f5e6 100644 --- a/ArkUIKit/StyledStringSample/entry/src/main/cpp/manager.h +++ b/ArkUIKit/StyledStringSample/entry/src/main/cpp/manager.h @@ -20,6 +20,7 @@ #include #include + namespace NativeNode::Manager { using EventCallback = std::function; @@ -27,12 +28,13 @@ class NodeManager { public: ~NodeManager() = default; static NodeManager& GetInstance(); - void SetXComponent(OH_NativeXComponent* xComponent); + void SetContentHandle(ArkUI_NodeContentHandle contentHandle); void CreateNativeNode(); private: NodeManager() = default; - OH_NativeXComponent* xComponent_; + ArkUI_NodeContentHandle contentHandle_; std::unordered_map callbackMap_; }; } -#endif //CAPI_MANAGER_H \ No newline at end of file + +#endif //CAPI_MANAGER_H diff --git a/ArkUIKit/StyledStringSample/entry/src/main/cpp/napi_init.cpp b/ArkUIKit/StyledStringSample/entry/src/main/cpp/napi_init.cpp index 7435be4951c65dc734686ed1b73a3c62003fc31d..8de3f3d6c4047f660641e603fe3dc3375cb99f61 100644 --- a/ArkUIKit/StyledStringSample/entry/src/main/cpp/napi_init.cpp +++ b/ArkUIKit/StyledStringSample/entry/src/main/cpp/napi_init.cpp @@ -13,27 +13,23 @@ * limitations under the License. */ #include "manager.h" +#include #include +#include #include -static OH_NativeXComponent* GetXComponent(napi_env env, napi_value exports) +static napi_value createNativeNode(napi_env env, napi_callback_info info) { - if ((env == nullptr) || (exports == nullptr)) { - return nullptr; - } - napi_value exportInstance = nullptr; - if (napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance) != napi_ok) { + size_t argc = 1; + napi_value args[1] = { nullptr }; + if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) { + OH_LOG_Print(LOG_APP, LOG_ERROR, 0xFF00, "StyledStringNDK", "CreateNativeNode napi_get_cb_info failed"); return nullptr; } - OH_NativeXComponent* xComp = nullptr; - if (napi_unwrap(env, exportInstance, reinterpret_cast(&xComp)) != napi_ok) { - return nullptr; - } - return xComp; -} - -static napi_value createNativeNode(napi_env env, napi_callback_info info) -{ + // 获取NodeContent + ArkUI_NodeContentHandle contentHandle; + OH_ArkUI_GetNodeContentFromNapiValue(env, args[0], &contentHandle); + NativeNode::Manager::NodeManager::GetInstance().SetContentHandle(contentHandle); NativeNode::Manager::NodeManager::GetInstance().CreateNativeNode(); return nullptr; } @@ -45,10 +41,6 @@ static napi_value Init(napi_env env, napi_value exports) { "createNativeNode", nullptr, createNativeNode, nullptr, nullptr, nullptr, napi_default, nullptr } }; napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); - auto xComponent = GetXComponent(env, exports); - if (xComponent) { - NativeNode::Manager::NodeManager::GetInstance().SetXComponent(xComponent); - } return exports; } EXTERN_C_END @@ -66,4 +58,4 @@ static napi_module demoModule = { extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); -} \ No newline at end of file +} diff --git a/ArkUIKit/StyledStringSample/entry/src/main/cpp/types/libentry/Index.d.ts b/ArkUIKit/StyledStringSample/entry/src/main/cpp/types/libentry/Index.d.ts index c09127fac743a5190aedf27d6e07056b20076bbd..da6cd329155de245079074d62cd65bdb08919a65 100644 --- a/ArkUIKit/StyledStringSample/entry/src/main/cpp/types/libentry/Index.d.ts +++ b/ArkUIKit/StyledStringSample/entry/src/main/cpp/types/libentry/Index.d.ts @@ -12,4 +12,5 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -export const createNativeNode: () => void; \ No newline at end of file + +export const createNativeNode: (content: object) => void; \ No newline at end of file diff --git a/ArkUIKit/StyledStringSample/entry/src/main/ets/pages/Index.ets b/ArkUIKit/StyledStringSample/entry/src/main/ets/pages/Index.ets index 5f10ce0e02428ec6c03f84b80a1a73230142699d..4193211d18d31821f4c1a9a5e62a1faa0d462258 100644 --- a/ArkUIKit/StyledStringSample/entry/src/main/ets/pages/Index.ets +++ b/ArkUIKit/StyledStringSample/entry/src/main/ets/pages/Index.ets @@ -13,24 +13,24 @@ * limitations under the License. */ import testNapi from 'libentry.so'; +import { NodeContent } from '@kit.ArkUI'; @Entry @Component struct Index { + private nodeContent: Content = new NodeContent(); build() { Row() { Column() { - XComponent({ - id: "xComponent", - type: XComponentType.NODE, - libraryname: "entry" - }).onAppear(()=> { - testNapi.createNativeNode() - }) + ContentSlot(this.nodeContent) } .width('100%') } .height('100%') } -} \ No newline at end of file + + aboutToAppear(): void { + testNapi.createNativeNode(this.nodeContent); + } +}