From 3aa3143f70cdd15234ef4d0a23b30596a28c9ee0 Mon Sep 17 00:00:00 2001 From: Ryan <865833921@qq.com> Date: Wed, 10 Sep 2025 17:38:10 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Native=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/build-profile.json5 | 9 + entry/oh-package.json5 | 4 +- entry/src/main/cpp/CMakeLists.txt | 25 +++ .../main/cpp/classdef/include/ArkUIBaseNode.h | 62 +++++++ .../src/main/cpp/classdef/include/ArkUINode.h | 94 +++++++++++ .../main/cpp/classdef/include/ArkUITextNode.h | 40 +++++ .../classdef/include/NativeModuleInstance.h | 48 ++++++ .../main/cpp/classdef/src/ArkUIBaseNode.cpp | 28 ++++ entry/src/main/cpp/classdef/src/ArkUINode.cpp | 154 ++++++++++++++++++ .../main/cpp/classdef/src/ArkUITextNode.cpp | 59 +++++++ .../cpp/classdef/src/NativeModuleInstance.cpp | 20 +++ .../function/include/IntegratingWithArkts.h | 35 ++++ .../main/cpp/function/include/NativeEntry.h | 49 ++++++ .../cpp/function/src/IntegratingWithArkts.cpp | 68 ++++++++ entry/src/main/cpp/napi_init.cpp | 50 ++++++ entry/src/main/cpp/types/libentry/Index.d.ts | 20 +++ .../main/cpp/types/libentry/oh-package.json5 | 6 + .../main/ets/components/NativeTextAdapter.ets | 42 +++++ entry/src/main/ets/pages/Index.ets | 2 + 19 files changed, 814 insertions(+), 1 deletion(-) create mode 100644 entry/src/main/cpp/CMakeLists.txt create mode 100644 entry/src/main/cpp/classdef/include/ArkUIBaseNode.h create mode 100644 entry/src/main/cpp/classdef/include/ArkUINode.h create mode 100644 entry/src/main/cpp/classdef/include/ArkUITextNode.h create mode 100644 entry/src/main/cpp/classdef/include/NativeModuleInstance.h create mode 100644 entry/src/main/cpp/classdef/src/ArkUIBaseNode.cpp create mode 100644 entry/src/main/cpp/classdef/src/ArkUINode.cpp create mode 100644 entry/src/main/cpp/classdef/src/ArkUITextNode.cpp create mode 100644 entry/src/main/cpp/classdef/src/NativeModuleInstance.cpp create mode 100644 entry/src/main/cpp/function/include/IntegratingWithArkts.h create mode 100644 entry/src/main/cpp/function/include/NativeEntry.h create mode 100644 entry/src/main/cpp/function/src/IntegratingWithArkts.cpp create mode 100644 entry/src/main/cpp/napi_init.cpp create mode 100644 entry/src/main/cpp/types/libentry/Index.d.ts create mode 100644 entry/src/main/cpp/types/libentry/oh-package.json5 create mode 100644 entry/src/main/ets/components/NativeTextAdapter.ets diff --git a/entry/build-profile.json5 b/entry/build-profile.json5 index 4d61187..d348bc7 100644 --- a/entry/build-profile.json5 +++ b/entry/build-profile.json5 @@ -1,6 +1,15 @@ { "apiType": "stageMode", "buildOption": { + "externalNativeOptions": { + "path": "./src/main/cpp/CMakeLists.txt", + "arguments": "", + "cppFlags": "", + "abiFilters": [ + "arm64-v8a", + "x86_64" + ], + } }, "buildOptionSet": [ { diff --git a/entry/oh-package.json5 b/entry/oh-package.json5 index 248c3b7..49272b9 100644 --- a/entry/oh-package.json5 +++ b/entry/oh-package.json5 @@ -5,6 +5,8 @@ "main": "", "author": "", "license": "", - "dependencies": {} + "dependencies": { + "libentry.so": "file:./src/main/cpp/types/libentry" + } } diff --git a/entry/src/main/cpp/CMakeLists.txt b/entry/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000..a317557 --- /dev/null +++ b/entry/src/main/cpp/CMakeLists.txt @@ -0,0 +1,25 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.5.0) +project(NDKCreateUI) + +set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +set(CMAKE_CXX_STANDARD 17) + +if(DEFINED PACKAGE_FIND_FILE) + include(${PACKAGE_FIND_FILE}) +endif() + +include_directories(${NATIVERENDER_ROOT_PATH} + ${NATIVERENDER_ROOT_PATH}/classdef/include + ${NATIVERENDER_ROOT_PATH}/function/include) + +file(GLOB BASECLADD_SOURCES "./classdef/src/*.cpp") + +file(GLOB FUNCTION_SOURCES "./function/src/*.cpp") + +add_library(entry SHARED + napi_init.cpp + ${BASECLADD_SOURCES} + ${FUNCTION_SOURCES}) + +target_link_libraries(entry PUBLIC libace_napi.z.so libace_ndk.z.so libhilog_ndk.z.so libnative_drawing.so libuv.so) \ No newline at end of file diff --git a/entry/src/main/cpp/classdef/include/ArkUIBaseNode.h b/entry/src/main/cpp/classdef/include/ArkUIBaseNode.h new file mode 100644 index 0000000..6e7549e --- /dev/null +++ b/entry/src/main/cpp/classdef/include/ArkUIBaseNode.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NDKCREATEUI_ARKUIBASENODE_H +#define NDKCREATEUI_ARKUIBASENODE_H + +#include +#include +#include +#include "NativeModuleInstance.h" + +namespace NativeModule { +class ArkUIBaseNode { +public: + explicit ArkUIBaseNode(ArkUI_NodeHandle handle) + : handle_(handle), nativeModule_(NativeModuleInstance::GetInstance()->GetNativeNodeAPI()) {} + + virtual ~ArkUIBaseNode() + { + // Encapsulate destructor to implement the function of removing child nodes. + if (!children_.empty()) { + for (const auto &child : children_) { + nativeModule_->removeChild(handle_, child->GetHandle()); + } + children_.clear(); + } + // Encapsulate destructors to uniformly recycle node resources. + nativeModule_->disposeNode(handle_); + } + + void AddChild(const std::shared_ptr &child); + + std::list> GetChildren(); + + ArkUI_NodeHandle GetHandle() const; + +protected: + virtual void OnAddChild(const std::shared_ptr &child) {} + virtual void OnRemoveChild(const std::shared_ptr &child) {} + virtual void OnInsertChild(const std::shared_ptr &child, int32_t index) {} + + ArkUI_NodeHandle handle_; + ArkUI_NativeNodeAPI_1 *nativeModule_ = nullptr; + +private: + std::list> children_; +}; +} // namespace NativeModule + +#endif // NDKCREATEUI_ARKUIBASENODE_H \ No newline at end of file diff --git a/entry/src/main/cpp/classdef/include/ArkUINode.h b/entry/src/main/cpp/classdef/include/ArkUINode.h new file mode 100644 index 0000000..c28a5a0 --- /dev/null +++ b/entry/src/main/cpp/classdef/include/ArkUINode.h @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NDKCREATEUI_ARKUINODE_H +#define NDKCREATEUI_ARKUINODE_H + +#include "ArkUIBaseNode.h" +#include "NativeModuleInstance.h" + +namespace NativeModule { +class ArkUINode : public ArkUIBaseNode { +public: + explicit ArkUINode(ArkUI_NodeHandle handle) : ArkUIBaseNode(handle) + { + nativeModule_ = NativeModuleInstance::GetInstance()->GetNativeNodeAPI(); + // When an event is triggered, the corresponding event object needs to be obtained through a function. + // Here, the encapsulated class pointer is kept on the component by setting node custom data, + // which facilitates subsequent event distribution. + nativeModule_->setUserData(handle_, this); + // Register a node to listen for event receivers. + nativeModule_->addNodeEventReceiver(handle_, ArkUINode::NodeEventReceiver); + } + + ~ArkUINode() override + { + if (onClick_) { + nativeModule_->unregisterNodeEvent(handle_, NODE_ON_CLICK); + } + if (onTouch_) { + nativeModule_->unregisterNodeEvent(handle_, NODE_TOUCH_EVENT); + } + if (onDisappear_) { + nativeModule_->unregisterNodeEvent(handle_, NODE_EVENT_ON_DISAPPEAR); + } + if (onAppear_) { + nativeModule_->unregisterNodeEvent(handle_, NODE_EVENT_ON_APPEAR); + } + nativeModule_->removeNodeEventReceiver(handle_, ArkUINode::NodeEventReceiver); + } + + void SetWidth(float width); + + void SetPercentWidth(float percent); + + void SetHeight(float height); + + void SetPercentHeight(float percent); + + void SetBackgroundColor(uint32_t color); + + // Handle general events. + void RegisterOnClick(const std::function &onClick); + + // Node disappearance event. + void RegisterOnDisappear(const std::function &onDisappear); + + // Node occurrence event. + void RegisterOnAppear(const std::function &onAppear); + +protected: + // Pointer to the event listener function. + static void NodeEventReceiver(ArkUI_NodeEvent *event); + + void ProcessNodeEvent(ArkUI_NodeEvent *event); + + virtual void OnNodeEvent(ArkUI_NodeEvent *event) {} + + void OnAddChild(const std::shared_ptr &child) override; + + void OnRemoveChild(const std::shared_ptr &child) override; + + void OnInsertChild(const std::shared_ptr &child, int32_t index) override; + +private: + std::function onClick_; + std::function onDisappear_; + std::function onAppear_; + std::function onTouch_; +}; +} // namespace NativeModule + +#endif // NDKCREATEUI_ARKUINODE_H \ No newline at end of file diff --git a/entry/src/main/cpp/classdef/include/ArkUITextNode.h b/entry/src/main/cpp/classdef/include/ArkUITextNode.h new file mode 100644 index 0000000..0a03dcf --- /dev/null +++ b/entry/src/main/cpp/classdef/include/ArkUITextNode.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NDKCREATEUI_ARKUITEXTNODE_H +#define NDKCREATEUI_ARKUITEXTNODE_H + +#include "ArkUINode.h" + +namespace NativeModule { +class ArkUITextNode : public ArkUINode { +public: + ArkUITextNode() + : ArkUINode((NativeModuleInstance::GetInstance()->GetNativeNodeAPI())->createNode(ARKUI_NODE_TEXT)) {} + + // Text attribute NDK interface encapsulation. + void SetFontSize(float fontSize); + + void SetFontColor(uint32_t color); + + void SetTextContent(const std::string &content); + + void SetTextAlign(ArkUI_TextAlignment align); + + void SetVerticalAlign(); +}; +} // namespace NativeModule + +#endif // NDKCREATEUI_ARKUITEXTNODE_H \ No newline at end of file diff --git a/entry/src/main/cpp/classdef/include/NativeModuleInstance.h b/entry/src/main/cpp/classdef/include/NativeModuleInstance.h new file mode 100644 index 0000000..da2059e --- /dev/null +++ b/entry/src/main/cpp/classdef/include/NativeModuleInstance.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NDKCREATEUI_NATIVEMODULE_H +#define NDKCREATEUI_NATIVEMODULE_H + +#include +#include +#include + +namespace NativeModule { +class NativeModuleInstance { +public: + static NativeModuleInstance *GetInstance() + { + static NativeModuleInstance instance; + return &instance; + } + + NativeModuleInstance() + { + // Retrieve the function pointer structure object of the NDK interface for subsequent operations. + OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, arkUINativeNodeApi_); + if (!arkUINativeNodeApi_) { + return; + } + } + // Expose to other modules for use. + ArkUI_NativeNodeAPI_1 *GetNativeNodeAPI(); + +private: + ArkUI_NativeNodeAPI_1 *arkUINativeNodeApi_ = nullptr; +}; +} // namespace NativeModule + +#endif // NDKCREATEUI_NATIVEMODULE_H \ No newline at end of file diff --git a/entry/src/main/cpp/classdef/src/ArkUIBaseNode.cpp b/entry/src/main/cpp/classdef/src/ArkUIBaseNode.cpp new file mode 100644 index 0000000..dbe80db --- /dev/null +++ b/entry/src/main/cpp/classdef/src/ArkUIBaseNode.cpp @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ArkUIBaseNode.h" + +namespace NativeModule { +void ArkUIBaseNode::AddChild(const std::shared_ptr &child) +{ + children_.emplace_back(child); + OnAddChild(child); +} + +std::list> ArkUIBaseNode::GetChildren() { return children_; } + +ArkUI_NodeHandle ArkUIBaseNode::GetHandle() const { return handle_; } +} // namespace NativeModule \ No newline at end of file diff --git a/entry/src/main/cpp/classdef/src/ArkUINode.cpp b/entry/src/main/cpp/classdef/src/ArkUINode.cpp new file mode 100644 index 0000000..832b3d2 --- /dev/null +++ b/entry/src/main/cpp/classdef/src/ArkUINode.cpp @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ArkUINode.h" + +namespace NativeModule { +void ArkUINode::SetWidth(float width) +{ + assert(handle_); + ArkUI_NumberValue value[] = {{.f32 = width}}; + ArkUI_AttributeItem item = {value, 1}; + nativeModule_->setAttribute(handle_, NODE_WIDTH, &item); +} + +void ArkUINode::SetPercentWidth(float percent) +{ + assert(handle_); + ArkUI_NumberValue value[] = {{.f32 = percent}}; + ArkUI_AttributeItem item = {value, 1}; + nativeModule_->setAttribute(handle_, NODE_WIDTH_PERCENT, &item); +} + +void ArkUINode::SetHeight(float height) +{ + assert(handle_); + ArkUI_NumberValue value[] = {{.f32 = height}}; + ArkUI_AttributeItem item = {value, 1}; + nativeModule_->setAttribute(handle_, NODE_HEIGHT, &item); +} + +void ArkUINode::SetPercentHeight(float percent) +{ + assert(handle_); + ArkUI_NumberValue value[] = {{.f32 = percent}}; + ArkUI_AttributeItem item = {value, 1}; + nativeModule_->setAttribute(handle_, NODE_HEIGHT_PERCENT, &item); +} + +void ArkUINode::SetBackgroundColor(uint32_t color) +{ + assert(handle_); + ArkUI_NumberValue value[] = {{.u32 = color}}; + ArkUI_AttributeItem item = {value, 1}; + nativeModule_->setAttribute(handle_, NODE_BACKGROUND_COLOR, &item); +} + +// Handle general events. +void ArkUINode::RegisterOnClick(const std::function &onClick) +{ + assert(handle_); + onClick_ = onClick; + // 注册点击事件。 + nativeModule_->registerNodeEvent(handle_, NODE_ON_CLICK, 0, nullptr); +} + +// Node disappearance event. +void ArkUINode::RegisterOnDisappear(const std::function &onDisappear) +{ + if (!handle_) { + throw std::runtime_error("handle_ is null!"); + } + onDisappear_ = onDisappear; + // Register for uninstallation events. + nativeModule_->registerNodeEvent(handle_, NODE_EVENT_ON_DISAPPEAR, 0, nullptr); +} + +// Node occurrence event. +void ArkUINode::RegisterOnAppear(const std::function &onAppear) +{ + if (!handle_) { + throw std::runtime_error("handle_ is null!"); + } + onAppear_ = onAppear; + // Register mount event. + nativeModule_->registerNodeEvent(handle_, NODE_EVENT_ON_APPEAR, 0, nullptr); +} + +// Pointer to the event listener function. +void ArkUINode::NodeEventReceiver(ArkUI_NodeEvent *event) +{ + // Retrieve the UI component object where the event occurred. + auto nodeHandle = OH_ArkUI_NodeEvent_GetNodeHandle(event); + // Retrieve custom data held in the UI component object and return the wrapper class pointer. + auto *node = + reinterpret_cast(NativeModuleInstance::GetInstance()->GetNativeNodeAPI()->getUserData(nodeHandle)); + // Process events based on encapsulated class instance objects. + node->ProcessNodeEvent(event); +} + +void ArkUINode::ProcessNodeEvent(ArkUI_NodeEvent *event) +{ + auto eventType = OH_ArkUI_NodeEvent_GetEventType(event); + switch (eventType) { + case NODE_ON_CLICK: { + if (onClick_) { + onClick_(); + } + break; + } + case NODE_TOUCH_EVENT: { + if (onTouch_) { + auto *uiInputEvent = OH_ArkUI_NodeEvent_GetInputEvent(event); + float x = OH_ArkUI_PointerEvent_GetX(uiInputEvent); + float y = OH_ArkUI_PointerEvent_GetY(uiInputEvent); + auto type = OH_ArkUI_UIInputEvent_GetAction(uiInputEvent); + onTouch_(type, x, y); + } + } + case NODE_EVENT_ON_DISAPPEAR: { + if (onDisappear_) { + onDisappear_(); + } + break; + } + case NODE_EVENT_ON_APPEAR: { + if (onAppear_) { + onAppear_(); + } + break; + } + default: { + // Assign component specific events to subclasses for processing. + OnNodeEvent(event); + } + } +} + +void ArkUINode::OnAddChild(const std::shared_ptr &child) +{ + nativeModule_->addChild(handle_, child->GetHandle()); +} + +void ArkUINode::OnRemoveChild(const std::shared_ptr &child) +{ + nativeModule_->removeChild(handle_, child->GetHandle()); +} + +void ArkUINode::OnInsertChild(const std::shared_ptr &child, int32_t index) +{ + nativeModule_->insertChildAt(handle_, child->GetHandle(), index); +} +} // namespace NativeModule \ No newline at end of file diff --git a/entry/src/main/cpp/classdef/src/ArkUITextNode.cpp b/entry/src/main/cpp/classdef/src/ArkUITextNode.cpp new file mode 100644 index 0000000..8422bdb --- /dev/null +++ b/entry/src/main/cpp/classdef/src/ArkUITextNode.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "ArkUITextNode.h" + +namespace NativeModule { +// Text attribute NDK interface encapsulation. +void ArkUITextNode::SetFontSize(float fontSize) +{ + assert(handle_); + ArkUI_NumberValue value[] = {{.f32 = fontSize}}; + ArkUI_AttributeItem item = {value, 1}; + nativeModule_->setAttribute(handle_, NODE_FONT_SIZE, &item); +} + +void ArkUITextNode::SetFontColor(uint32_t color) +{ + assert(handle_); + ArkUI_NumberValue value[] = {{.u32 = color}}; + ArkUI_AttributeItem item = {value, 1}; + nativeModule_->setAttribute(handle_, NODE_FONT_COLOR, &item); +} + +void ArkUITextNode::SetTextContent(const std::string &content) +{ + assert(handle_); + ArkUI_AttributeItem item = {nullptr, 0, content.c_str()}; + nativeModule_->setAttribute(handle_, NODE_TEXT_CONTENT, &item); +} + +void ArkUITextNode::SetTextAlign(ArkUI_TextAlignment align) +{ + assert(handle_); + ArkUI_NumberValue value[] = {{.i32 = align}}; + ArkUI_AttributeItem item = {value, 1}; + nativeModule_->setAttribute(handle_, NODE_TEXT_ALIGN, &item); +} + +void ArkUITextNode::SetVerticalAlign() +{ + assert(handle_); + ArkUI_NumberValue value[] = {{.i32 = ARKUI_TEXT_VERTICAL_ALIGNMENT_BASELINE}}; + ArkUI_AttributeItem item = {value, 1}; + nativeModule_->setAttribute(handle_, NODE_TEXT_RADIAL_GRADIENT , &item); +} +} // namespace NativeModule \ No newline at end of file diff --git a/entry/src/main/cpp/classdef/src/NativeModuleInstance.cpp b/entry/src/main/cpp/classdef/src/NativeModuleInstance.cpp new file mode 100644 index 0000000..c27bc48 --- /dev/null +++ b/entry/src/main/cpp/classdef/src/NativeModuleInstance.cpp @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "NativeModuleInstance.h" + +namespace NativeModule { +ArkUI_NativeNodeAPI_1 *NativeModuleInstance::GetNativeNodeAPI() { return arkUINativeNodeApi_; } +} // namespace NativeModule \ No newline at end of file diff --git a/entry/src/main/cpp/function/include/IntegratingWithArkts.h b/entry/src/main/cpp/function/include/IntegratingWithArkts.h new file mode 100644 index 0000000..30dfdff --- /dev/null +++ b/entry/src/main/cpp/function/include/IntegratingWithArkts.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NDKCREATEUI_INTEGRATINGWITHARKTS_H +#define NDKCREATEUI_INTEGRATINGWITHARKTS_H + +#include +#include +#include "ArkUIBaseNode.h" + +namespace NativeModule { +constexpr int32_t List_NUM = 30; +constexpr int32_t TEXT_FONTSIZE = 16; +constexpr int32_t TEXT_HEIGHT = 100; + +napi_value CreateTextListNativeRoot(napi_env env, napi_callback_info info); + +napi_value DestroyTextListNativeRoot(napi_env env, napi_callback_info info); + +std::shared_ptr CreateTextListExample(); +} // namespace NativeModule + +#endif // NDKCREATEUI_INTEGRATINGWITHARKTS_H diff --git a/entry/src/main/cpp/function/include/NativeEntry.h b/entry/src/main/cpp/function/include/NativeEntry.h new file mode 100644 index 0000000..c744e84 --- /dev/null +++ b/entry/src/main/cpp/function/include/NativeEntry.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef NDKCREATEUI_NATIVEENTRY_H +#define NDKCREATEUI_NATIVEENTRY_H + +#include +#include "ArkUIBaseNode.h" + +namespace NativeModule { +// Manage the lifecycle and memory of native components. +class NativeEntry { +public: + // Set handle content. + void SetContentHandle(ArkUI_NodeContentHandle handle) { handle_ = handle; } + + void SetRootNode(const std::shared_ptr &baseNode) + { + root_ = baseNode; + // Add Native components to NodeContent for mounting display. + OH_ArkUI_NodeContent_AddNode(handle_, root_->GetHandle()); + } + + void DisposeRootNode() + { + // Uninstall components from NodeContent and destroy native components. + OH_ArkUI_NodeContent_RemoveNode(handle_, root_->GetHandle()); + root_.reset(); + } + +private: + std::shared_ptr root_; + ArkUI_NodeContentHandle handle_; +}; +} // namespace NativeModule + +#endif // NDKCREATEUI_NATIVEENTRY_H \ No newline at end of file diff --git a/entry/src/main/cpp/function/src/IntegratingWithArkts.cpp b/entry/src/main/cpp/function/src/IntegratingWithArkts.cpp new file mode 100644 index 0000000..abb83f4 --- /dev/null +++ b/entry/src/main/cpp/function/src/IntegratingWithArkts.cpp @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include "ArkUITextNode.h" +#include "NativeEntry.h" +#include "IntegratingWithArkts.h" +#include + +namespace NativeModule { +NativeEntry nativeEntry; + +napi_value CreateTextListNativeRoot(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1] = {nullptr}; + + // Get parameters passed in from JS. + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + + // Get NodeContent. + ArkUI_NodeContentHandle contentHandle; + OH_ArkUI_GetNodeContentFromNapiValue(env, args[0], &contentHandle); + nativeEntry.SetContentHandle(contentHandle); + + // Create a text list. + auto list = CreateTextListExample(); + // Keep the Native side objects in the management class and maintain their lifecycle. + nativeEntry.SetRootNode(list); + + return nullptr; +} + +std::shared_ptr CreateTextListExample() +{ + auto textNode = std::make_shared(); + textNode->SetTextContent(std::string("Hello World")); + textNode->SetFontSize(TEXT_FONTSIZE); + textNode->SetPercentWidth(1); + textNode->SetHeight(50); + textNode->SetBackgroundColor(0xFFfffacd); + textNode->SetTextAlign(ARKUI_TEXT_ALIGNMENT_CENTER); + if (OH_GetSdkApiVersion() >= 20) { + textNode->SetVerticalAlign(); + } + + return textNode; +} + +napi_value DestroyTextListNativeRoot(napi_env env, napi_callback_info info) +{ + nativeEntry.DisposeRootNode(); + return nullptr; +} +} // namespace NativeModule \ No newline at end of file diff --git a/entry/src/main/cpp/napi_init.cpp b/entry/src/main/cpp/napi_init.cpp new file mode 100644 index 0000000..a24e440 --- /dev/null +++ b/entry/src/main/cpp/napi_init.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "IntegratingWithArkts.h" + +EXTERN_C_START +// Registration of components related to the encapsulation list. +void RegisterListComponents(napi_env env, napi_value exports) +{ + napi_property_descriptor desc[] = { + {"CreateTextListNativeRoot", nullptr, NativeModule::CreateTextListNativeRoot, nullptr, nullptr, nullptr, + napi_default, nullptr}, + {"DestroyTextListNativeRoot", nullptr, NativeModule::DestroyTextListNativeRoot, nullptr, nullptr, nullptr, + napi_default, nullptr}, + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); +} + +static napi_value Init(napi_env env, napi_value exports) +{ + // Bind the creation and destruction components on the native side. + RegisterListComponents(env, exports); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "entry", + .nm_priv = ((void *)0), + .reserved = {0}, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); } \ No newline at end of file diff --git a/entry/src/main/cpp/types/libentry/Index.d.ts b/entry/src/main/cpp/types/libentry/Index.d.ts new file mode 100644 index 0000000..1cfa1a5 --- /dev/null +++ b/entry/src/main/cpp/types/libentry/Index.d.ts @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { NodeContent } from "@kit.ArkUI"; + +export const CreateTextListNativeRoot: (content: NodeContent) => void; + +export const DestroyTextListNativeRoot: () => void; \ No newline at end of file diff --git a/entry/src/main/cpp/types/libentry/oh-package.json5 b/entry/src/main/cpp/types/libentry/oh-package.json5 new file mode 100644 index 0000000..ea41072 --- /dev/null +++ b/entry/src/main/cpp/types/libentry/oh-package.json5 @@ -0,0 +1,6 @@ +{ + "name": "libentry.so", + "types": "./Index.d.ts", + "version": "1.0.0", + "description": "Please describe the basic information." +} \ No newline at end of file diff --git a/entry/src/main/ets/components/NativeTextAdapter.ets b/entry/src/main/ets/components/NativeTextAdapter.ets new file mode 100644 index 0000000..26bcb69 --- /dev/null +++ b/entry/src/main/ets/components/NativeTextAdapter.ets @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { NodeContent } from '@kit.ArkUI'; +import nativeNode from 'libentry.so'; + + +@Component +export struct NativeTextAdapter { + private rootSlot = new NodeContent(); + + aboutToAppear(): void { + nativeNode.CreateTextListNativeRoot(this.rootSlot); + } + + aboutToDisappear(): void { + nativeNode.DestroyTextListNativeRoot(); + } + + build() { + Column() { + Row() { + // Bind NodeContent and ContentSlot placeholder components. + ContentSlot(this.rootSlot) + } + } + .width('100%') + .height('30%') + } +} diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 8bb6047..2cf53e7 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -16,6 +16,7 @@ import { ScrollComponentAdapter } from '../components/ScrollComponentAdapter'; import { TextComponentAdapter } from '../components/TextComponentAdapter'; import { ActionBarAdapter } from '../components/ActionBarAdapter'; +import { NativeTextAdapter } from '../components/NativeTextAdapter'; @Entry @Component @@ -25,6 +26,7 @@ struct Index { ScrollComponentAdapter() TextComponentAdapter() ActionBarAdapter() + NativeTextAdapter() } .height('100%') .width('100%') -- Gitee From df651a7870856df22fce067060e976df7de34279 Mon Sep 17 00:00:00 2001 From: Ryan <865833921@qq.com> Date: Thu, 11 Sep 2025 14:45:55 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Native=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/cpp/CMakeLists.txt | 2 +- .../{ArkUITextNode.h => ArkUIButtonNode.h} | 12 ++++++------ ...{ArkUITextNode.cpp => ArkUIButtonNode.cpp} | 18 +++++++++--------- .../function/include/IntegratingWithArkts.h | 6 +++--- .../cpp/function/src/IntegratingWithArkts.cpp | 19 ++++++++++--------- entry/src/main/cpp/napi_init.cpp | 4 ++-- entry/src/main/cpp/types/libentry/Index.d.ts | 4 ++-- .../main/ets/components/NativeTextAdapter.ets | 4 ++-- 8 files changed, 35 insertions(+), 34 deletions(-) rename entry/src/main/cpp/classdef/include/{ArkUITextNode.h => ArkUIButtonNode.h} (82%) rename entry/src/main/cpp/classdef/src/{ArkUITextNode.cpp => ArkUIButtonNode.cpp} (73%) diff --git a/entry/src/main/cpp/CMakeLists.txt b/entry/src/main/cpp/CMakeLists.txt index a317557..eccba73 100644 --- a/entry/src/main/cpp/CMakeLists.txt +++ b/entry/src/main/cpp/CMakeLists.txt @@ -22,4 +22,4 @@ add_library(entry SHARED ${BASECLADD_SOURCES} ${FUNCTION_SOURCES}) -target_link_libraries(entry PUBLIC libace_napi.z.so libace_ndk.z.so libhilog_ndk.z.so libnative_drawing.so libuv.so) \ No newline at end of file +target_link_libraries(entry PUBLIC libace_napi.z.so libace_ndk.z.so libhilog_ndk.z.so libnative_drawing.so libuv.so libdeviceinfo_ndk.z.so) \ No newline at end of file diff --git a/entry/src/main/cpp/classdef/include/ArkUITextNode.h b/entry/src/main/cpp/classdef/include/ArkUIButtonNode.h similarity index 82% rename from entry/src/main/cpp/classdef/include/ArkUITextNode.h rename to entry/src/main/cpp/classdef/include/ArkUIButtonNode.h index 0a03dcf..a1804d7 100644 --- a/entry/src/main/cpp/classdef/include/ArkUITextNode.h +++ b/entry/src/main/cpp/classdef/include/ArkUIButtonNode.h @@ -13,16 +13,16 @@ * limitations under the License. */ -#ifndef NDKCREATEUI_ARKUITEXTNODE_H -#define NDKCREATEUI_ARKUITEXTNODE_H +#ifndef NDKCREATEUI_ARKUI_BUTTON_H +#define NDKCREATEUI_ARKUI_BUTTON_H #include "ArkUINode.h" namespace NativeModule { -class ArkUITextNode : public ArkUINode { +class ArkUIButtonNode : public ArkUINode { public: - ArkUITextNode() - : ArkUINode((NativeModuleInstance::GetInstance()->GetNativeNodeAPI())->createNode(ARKUI_NODE_TEXT)) {} + ArkUIButtonNode() + : ArkUINode((NativeModuleInstance::GetInstance()->GetNativeNodeAPI())->createNode(ARKUI_NODE_BUTTON)) {} // Text attribute NDK interface encapsulation. void SetFontSize(float fontSize); @@ -33,7 +33,7 @@ public: void SetTextAlign(ArkUI_TextAlignment align); - void SetVerticalAlign(); + void SetButtonType(int32_t buttonType); }; } // namespace NativeModule diff --git a/entry/src/main/cpp/classdef/src/ArkUITextNode.cpp b/entry/src/main/cpp/classdef/src/ArkUIButtonNode.cpp similarity index 73% rename from entry/src/main/cpp/classdef/src/ArkUITextNode.cpp rename to entry/src/main/cpp/classdef/src/ArkUIButtonNode.cpp index 8422bdb..2190ae1 100644 --- a/entry/src/main/cpp/classdef/src/ArkUITextNode.cpp +++ b/entry/src/main/cpp/classdef/src/ArkUIButtonNode.cpp @@ -14,11 +14,11 @@ */ #include -#include "ArkUITextNode.h" +#include "ArkUIButtonNode.h" namespace NativeModule { // Text attribute NDK interface encapsulation. -void ArkUITextNode::SetFontSize(float fontSize) +void ArkUIButtonNode::SetFontSize(float fontSize) { assert(handle_); ArkUI_NumberValue value[] = {{.f32 = fontSize}}; @@ -26,7 +26,7 @@ void ArkUITextNode::SetFontSize(float fontSize) nativeModule_->setAttribute(handle_, NODE_FONT_SIZE, &item); } -void ArkUITextNode::SetFontColor(uint32_t color) +void ArkUIButtonNode::SetFontColor(uint32_t color) { assert(handle_); ArkUI_NumberValue value[] = {{.u32 = color}}; @@ -34,14 +34,14 @@ void ArkUITextNode::SetFontColor(uint32_t color) nativeModule_->setAttribute(handle_, NODE_FONT_COLOR, &item); } -void ArkUITextNode::SetTextContent(const std::string &content) +void ArkUIButtonNode::SetTextContent(const std::string &content) { assert(handle_); ArkUI_AttributeItem item = {nullptr, 0, content.c_str()}; - nativeModule_->setAttribute(handle_, NODE_TEXT_CONTENT, &item); + nativeModule_->setAttribute(handle_, NODE_BUTTON_LABEL, &item); } -void ArkUITextNode::SetTextAlign(ArkUI_TextAlignment align) +void ArkUIButtonNode::SetTextAlign(ArkUI_TextAlignment align) { assert(handle_); ArkUI_NumberValue value[] = {{.i32 = align}}; @@ -49,11 +49,11 @@ void ArkUITextNode::SetTextAlign(ArkUI_TextAlignment align) nativeModule_->setAttribute(handle_, NODE_TEXT_ALIGN, &item); } -void ArkUITextNode::SetVerticalAlign() +void ArkUIButtonNode::SetButtonType(int32_t buttonType) { assert(handle_); - ArkUI_NumberValue value[] = {{.i32 = ARKUI_TEXT_VERTICAL_ALIGNMENT_BASELINE}}; + ArkUI_NumberValue value[] = {{.i32 = buttonType}}; ArkUI_AttributeItem item = {value, 1}; - nativeModule_->setAttribute(handle_, NODE_TEXT_RADIAL_GRADIENT , &item); + nativeModule_->setAttribute(handle_, NODE_BUTTON_TYPE , &item); } } // namespace NativeModule \ No newline at end of file diff --git a/entry/src/main/cpp/function/include/IntegratingWithArkts.h b/entry/src/main/cpp/function/include/IntegratingWithArkts.h index 30dfdff..edcecdf 100644 --- a/entry/src/main/cpp/function/include/IntegratingWithArkts.h +++ b/entry/src/main/cpp/function/include/IntegratingWithArkts.h @@ -25,11 +25,11 @@ constexpr int32_t List_NUM = 30; constexpr int32_t TEXT_FONTSIZE = 16; constexpr int32_t TEXT_HEIGHT = 100; -napi_value CreateTextListNativeRoot(napi_env env, napi_callback_info info); +napi_value CreateButtonNativeRoot(napi_env env, napi_callback_info info); -napi_value DestroyTextListNativeRoot(napi_env env, napi_callback_info info); +napi_value DestroyButtonNativeRoot(napi_env env, napi_callback_info info); -std::shared_ptr CreateTextListExample(); +std::shared_ptr CreateButtonExample(); } // namespace NativeModule #endif // NDKCREATEUI_INTEGRATINGWITHARKTS_H diff --git a/entry/src/main/cpp/function/src/IntegratingWithArkts.cpp b/entry/src/main/cpp/function/src/IntegratingWithArkts.cpp index abb83f4..e4d5cec 100644 --- a/entry/src/main/cpp/function/src/IntegratingWithArkts.cpp +++ b/entry/src/main/cpp/function/src/IntegratingWithArkts.cpp @@ -15,7 +15,7 @@ #include #include -#include "ArkUITextNode.h" +#include "ArkUIButtonNode.h" #include "NativeEntry.h" #include "IntegratingWithArkts.h" #include @@ -23,7 +23,7 @@ namespace NativeModule { NativeEntry nativeEntry; -napi_value CreateTextListNativeRoot(napi_env env, napi_callback_info info) +napi_value CreateButtonNativeRoot(napi_env env, napi_callback_info info) { size_t argc = 1; napi_value args[1] = {nullptr}; @@ -37,30 +37,31 @@ napi_value CreateTextListNativeRoot(napi_env env, napi_callback_info info) nativeEntry.SetContentHandle(contentHandle); // Create a text list. - auto list = CreateTextListExample(); + auto list = CreateButtonExample(); // Keep the Native side objects in the management class and maintain their lifecycle. nativeEntry.SetRootNode(list); return nullptr; } -std::shared_ptr CreateTextListExample() +std::shared_ptr CreateButtonExample() { - auto textNode = std::make_shared(); + auto textNode = std::make_shared(); textNode->SetTextContent(std::string("Hello World")); textNode->SetFontSize(TEXT_FONTSIZE); textNode->SetPercentWidth(1); textNode->SetHeight(50); - textNode->SetBackgroundColor(0xFFfffacd); textNode->SetTextAlign(ARKUI_TEXT_ALIGNMENT_CENTER); - if (OH_GetSdkApiVersion() >= 20) { - textNode->SetVerticalAlign(); + if (OH_GetSdkApiVersion() >= 19) { + textNode->SetButtonType(ARKUI_BUTTON_ROUNDED_RECTANGLE); + } else { + textNode->SetButtonType(ARKUI_BUTTON_TYPE_CAPSULE); } return textNode; } -napi_value DestroyTextListNativeRoot(napi_env env, napi_callback_info info) +napi_value DestroyButtonNativeRoot(napi_env env, napi_callback_info info) { nativeEntry.DisposeRootNode(); return nullptr; diff --git a/entry/src/main/cpp/napi_init.cpp b/entry/src/main/cpp/napi_init.cpp index a24e440..3dbe3f3 100644 --- a/entry/src/main/cpp/napi_init.cpp +++ b/entry/src/main/cpp/napi_init.cpp @@ -21,9 +21,9 @@ EXTERN_C_START void RegisterListComponents(napi_env env, napi_value exports) { napi_property_descriptor desc[] = { - {"CreateTextListNativeRoot", nullptr, NativeModule::CreateTextListNativeRoot, nullptr, nullptr, nullptr, + {"CreateButtonNativeRoot", nullptr, NativeModule::CreateButtonNativeRoot, nullptr, nullptr, nullptr, napi_default, nullptr}, - {"DestroyTextListNativeRoot", nullptr, NativeModule::DestroyTextListNativeRoot, nullptr, nullptr, nullptr, + {"DestroyButtonNativeRoot", nullptr, NativeModule::DestroyButtonNativeRoot, nullptr, nullptr, nullptr, napi_default, nullptr}, }; napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); diff --git a/entry/src/main/cpp/types/libentry/Index.d.ts b/entry/src/main/cpp/types/libentry/Index.d.ts index 1cfa1a5..213acec 100644 --- a/entry/src/main/cpp/types/libentry/Index.d.ts +++ b/entry/src/main/cpp/types/libentry/Index.d.ts @@ -15,6 +15,6 @@ import { NodeContent } from "@kit.ArkUI"; -export const CreateTextListNativeRoot: (content: NodeContent) => void; +export const CreateButtonNativeRoot: (content: NodeContent) => void; -export const DestroyTextListNativeRoot: () => void; \ No newline at end of file +export const DestroyButtonNativeRoot: () => void; \ No newline at end of file diff --git a/entry/src/main/ets/components/NativeTextAdapter.ets b/entry/src/main/ets/components/NativeTextAdapter.ets index 26bcb69..3036b78 100644 --- a/entry/src/main/ets/components/NativeTextAdapter.ets +++ b/entry/src/main/ets/components/NativeTextAdapter.ets @@ -22,11 +22,11 @@ export struct NativeTextAdapter { private rootSlot = new NodeContent(); aboutToAppear(): void { - nativeNode.CreateTextListNativeRoot(this.rootSlot); + nativeNode.CreateButtonNativeRoot(this.rootSlot); } aboutToDisappear(): void { - nativeNode.DestroyTextListNativeRoot(); + nativeNode.DestroyButtonNativeRoot(); } build() { -- Gitee From a4e6b00ed6e66d3198a8c9a38789c9d6429957ed Mon Sep 17 00:00:00 2001 From: Ryan <865833921@qq.com> Date: Thu, 11 Sep 2025 15:12:10 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Native=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 30 +++++++++++++++++-- ...extAdapter.ets => NativeButtonAdapter.ets} | 2 +- entry/src/main/ets/pages/Index.ets | 4 +-- 3 files changed, 31 insertions(+), 5 deletions(-) rename entry/src/main/ets/components/{NativeTextAdapter.ets => NativeButtonAdapter.ets} (96%) diff --git a/README.md b/README.md index 22a45f2..bc105f1 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,39 @@ # 多API版本兼容示例 ### 介绍 -通过@ohos.deviceInfo(设备信息)模块deviceInfo.sdkApiVersion和deviceInfo.distributionOSApiVersion获取系统SDK版本,以Text、Scroll和HdsActionBar组件在API 18和API 20的兼容为例,介绍多版本适配的实现方法。 +通过@ohos.deviceInfo(设备信息)模块deviceInfo.sdkApiVersion和deviceInfo.distributionOSApiVersion获取系统SDK版本,以Text、Scroll和HdsActionBar组件在API 18和API 20的兼容为例,介绍多版本适配的实现方法。在Native侧,通过deviceinfo的OH_GetSdkApiVersion获取系统SDK版本,以Button组件为例介绍多版本适配的实现方法。 ### 工程目录 ``` +|──entry/src/main/cpp +| |──classdef +| | |──include +| | | |──ArkUIBaseNode.h // 组件树操作的基类 +| | | |──ArkUINode.h // 通用组件的封装 +| | | |──ArkUIButtonNode.h // 实现按钮组件的封装类 +| | | └──NativeModuleInstance.h // ArkUI在Native侧模块的封装接口 +| | └──src +| | | |──ArkUIBaseNode.cpp // 组件树操作的基类 +| | | |──ArkUINode.cpp // 通用组件的封装 +| | | |──ArkUIButtonNode.cpp // 实现按钮组件的封装类 +| | | └──NativeModuleInstance.cpp // ArkUI在Native侧模块的封装接口 +| |──function +| | |──include +| | | |──IntegratingWithArkts.h // 接入ArkTS界面 +| | | └──NativeEntry.h // 管理Native组件生命周期 +| | └──src +| | | └──IntegratingWithArkts.cpp // 接入ArkTS界面 +| └──types +| | └──libentry +| | | |──Index.d.ts // Native侧接口导出声明文件 +| | | └──oh-package.json5 +| |──CMakeLists.txt // cmake配置文件 +| └──napi_init.cpp // 接口映射、模块注册 ├──entry/src/main/ets // 代码区 │ ├──components // 自定义组件 -│ │ ├──ActionBarAdapter.ets // HdsActionBar组件版本兼容示例 +│ │ ├──ActionBarAdapter.ets // HdsActionBar组件版本兼容示例 +│ │ ├──NativeButtonAdapter.ets // Button组件版本兼容示例 │ │ ├──ScrollComponentAdapter.ets // Scroll组件版本兼容示例 │ │ └──TextComponentAdapter.ets // Text组件版本兼容示例 │ ├──entryability @@ -27,6 +52,7 @@ * 以Text组件的marqueeOptions属性使用为例来展示API18的兼容,实现了跑马灯效果 * 以Scroll组件的maxZoomScale、minZoomScale和enableBouncesZoom为例子来展示API20的兼容,实现了图片缩放效果。 * 以HdsActionBar组件的使用例子来展示API20的兼容,实现可以展开和收起的ActionBar效果。 +* 以Native侧的Button组件为例,展示API20的兼容。 ### 相关权限 不涉及 diff --git a/entry/src/main/ets/components/NativeTextAdapter.ets b/entry/src/main/ets/components/NativeButtonAdapter.ets similarity index 96% rename from entry/src/main/ets/components/NativeTextAdapter.ets rename to entry/src/main/ets/components/NativeButtonAdapter.ets index 3036b78..fd99dd4 100644 --- a/entry/src/main/ets/components/NativeTextAdapter.ets +++ b/entry/src/main/ets/components/NativeButtonAdapter.ets @@ -18,7 +18,7 @@ import nativeNode from 'libentry.so'; @Component -export struct NativeTextAdapter { +export struct NativeButtonAdapter { private rootSlot = new NodeContent(); aboutToAppear(): void { diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 2cf53e7..90d1e6d 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -16,7 +16,7 @@ import { ScrollComponentAdapter } from '../components/ScrollComponentAdapter'; import { TextComponentAdapter } from '../components/TextComponentAdapter'; import { ActionBarAdapter } from '../components/ActionBarAdapter'; -import { NativeTextAdapter } from '../components/NativeTextAdapter'; +import { NativeButtonAdapter } from '../components/NativeButtonAdapter'; @Entry @Component @@ -26,7 +26,7 @@ struct Index { ScrollComponentAdapter() TextComponentAdapter() ActionBarAdapter() - NativeTextAdapter() + NativeButtonAdapter() } .height('100%') .width('100%') -- Gitee