diff --git a/ArkUIKit/NativeNodeNapiSample/README.md b/ArkUIKit/NativeNodeNapiSample/README.md index 8b383ac33fb4ff815854a302c789ec8306872813..d9ac17e780155659b78ff3c0d817c5600762a68f 100644 --- a/ArkUIKit/NativeNodeNapiSample/README.md +++ b/ArkUIKit/NativeNodeNapiSample/README.md @@ -2,13 +2,14 @@ ## 介绍 -本示例展示了如何通过调用CAPI提供的相关方法来获取navigation组件等组件的相关属性和信息。 +本示例展示了如何通过调用CAPI提供的相关方法来获取navigation组件等组件的相关属性和信息,以及将ArkTS侧创建的DrawableDescriptor对象映射到native侧的ArkUI_DrawableDescriptor。 ## 效果预览 | 首页 | navigation页面 | | ---- | -------------------------------------------------------- | | | | +| | | ## 使用说明 @@ -25,6 +26,7 @@ entry/src/main/ets/ └── pages ├── Index.ets (获取导航页面) └── page_navigation.ets (获取导航页面) + └── GetDrawableDescriptor.ets (映射DrawableDescriptor页面) entry/src/main/ ├── cpp │ ├── types diff --git a/ArkUIKit/NativeNodeNapiSample/entry/src/main/cpp/manager.cpp b/ArkUIKit/NativeNodeNapiSample/entry/src/main/cpp/manager.cpp index 600bc784e3850a2622fceffe53a2e813ad49ceab..d2bb4e1e9b3f19c3161ba5326918529756b9ec18 100644 --- a/ArkUIKit/NativeNodeNapiSample/entry/src/main/cpp/manager.cpp +++ b/ArkUIKit/NativeNodeNapiSample/entry/src/main/cpp/manager.cpp @@ -65,6 +65,59 @@ napi_value Manager::CreateNativeNaviNode(napi_env env, napi_callback_info info) return nullptr; } +napi_value Manager::ProcessDrawable(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1] = {nullptr}; + // 获取JS层传入的参数 + napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + if (status != napi_ok || argc < 1) { + OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager", "获取参数失败"); + return nullptr; + } + napi_value resourceValue = args[0]; // 这是ArkTS侧传入的Resource对象 + // 核心接口:将napi_value转换为DrawableDescriptor + ArkUI_DrawableDescriptor* drawable = nullptr; + int32_t res = OH_ArkUI_GetDrawableDescriptorFromNapiValue(env, resourceValue, &drawable); + // 处理转换结果 + if (res != ArkUI_ErrorCode::ARKUI_ERROR_CODE_NO_ERROR) { + // 转换失败:根据错误码处理(参数无效等) + OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager", "Failed to get drawable descriptor"); + napi_throw_error(env, nullptr, "Failed to get drawable descriptor"); + return nullptr; + } + // 返回处理结果给JS层 + napi_value result; + napi_create_int32(env, res, &result); + return result; +} + +napi_value Manager::ProcessDrawable2(napi_env env, napi_callback_info info) +{ + size_t argc = 1; + napi_value args[1] = {nullptr}; + // 获取JS层传入的参数 + napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + if (status != napi_ok || argc < 1) { + OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager", "获取参数失败"); + return nullptr; + } + napi_value resourceValue = args[0]; // 这是ArkTS侧传入的Resource对象 + // 核心接口:将napi_value转换为DrawableDescriptor + ArkUI_DrawableDescriptor* drawable = nullptr; + int32_t res = OH_ArkUI_GetDrawableDescriptorFromResourceNapiValue (env, resourceValue, &drawable); + // 3. 处理转换结果 + if (res != ArkUI_ErrorCode::ARKUI_ERROR_CODE_NO_ERROR) { + // 转换失败:根据错误码处理(参数无效等) + OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager", "Failed to get drawable descriptor"); + return nullptr; + } + // 返回处理结果给JS层 + napi_value result; + napi_create_int32(env, res, &result); + return result; +} + napi_value createNativeRootAnimation1(napi_env env, napi_callback_info info) { size_t argc = 1; diff --git a/ArkUIKit/NativeNodeNapiSample/entry/src/main/cpp/manager.h b/ArkUIKit/NativeNodeNapiSample/entry/src/main/cpp/manager.h index 058227c4aafe9c8cd0ffd5af776ee628c6e57538..1e658f6a73cfe44e9c09ed19236ff206f7bad6df 100644 --- a/ArkUIKit/NativeNodeNapiSample/entry/src/main/cpp/manager.h +++ b/ArkUIKit/NativeNodeNapiSample/entry/src/main/cpp/manager.h @@ -72,6 +72,8 @@ public: } static napi_value CreateNativeNaviNode(napi_env env, napi_callback_info info); + static napi_value ProcessDrawable(napi_env env, napi_callback_info info); + static napi_value ProcessDrawable2(napi_env env, napi_callback_info info); private: static Manager manager_; std::shared_ptr root_; diff --git a/ArkUIKit/NativeNodeNapiSample/entry/src/main/cpp/napi_init.cpp b/ArkUIKit/NativeNodeNapiSample/entry/src/main/cpp/napi_init.cpp index 46c21ea762e753e85fd1f68329723e07dcb8a797..3e268376bd5b05fcbf4c983ae96ef92c3ace0ee7 100644 --- a/ArkUIKit/NativeNodeNapiSample/entry/src/main/cpp/napi_init.cpp +++ b/ArkUIKit/NativeNodeNapiSample/entry/src/main/cpp/napi_init.cpp @@ -59,6 +59,8 @@ static napi_value Init(napi_env env, napi_value exports) {"createNativeRootVisualEffects10", nullptr, createNativeRootVisualEffects10, nullptr, nullptr, nullptr, napi_default, nullptr}, {"destroyNativeRoot", nullptr, DestroyNativeRoot, nullptr, nullptr, nullptr, napi_default, nullptr}, + { "processDrawable", nullptr, Manager::ProcessDrawable, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "processDrawable2", nullptr, Manager::ProcessDrawable2, nullptr, nullptr, nullptr, napi_default, nullptr }, }; if (napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc) != napi_ok) { diff --git a/ArkUIKit/NativeNodeNapiSample/entry/src/main/cpp/types/libentry/Index.d.ts b/ArkUIKit/NativeNodeNapiSample/entry/src/main/cpp/types/libentry/Index.d.ts index a3516e2a9713f3ffba98be72270930b9a19b9281..e6ac5da6f61274d306880b25f421e5d68694c302 100644 --- a/ArkUIKit/NativeNodeNapiSample/entry/src/main/cpp/types/libentry/Index.d.ts +++ b/ArkUIKit/NativeNodeNapiSample/entry/src/main/cpp/types/libentry/Index.d.ts @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { DrawableDescriptor } from "@kit.ArkUI"; export const createNativeNaviNode: (content: Object) =>void; export const createNativeRootAnimation1: (content: Object) => void; @@ -29,4 +30,6 @@ export const createNativeRootVisualEffects7: (content: Object) => void; export const createNativeRootVisualEffects8: (content: Object) => void; export const createNativeRootVisualEffects9: (content: Object) => void; export const createNativeRootVisualEffects10: (content: Object) => void; -export const destroyNativeRoot: () => void; \ No newline at end of file +export const destroyNativeRoot: () => void; +export const processDrawable: (a: DrawableDescriptor) => number; +export const processDrawable2: (a: DrawableDescriptor) => number; \ No newline at end of file diff --git a/ArkUIKit/NativeNodeNapiSample/entry/src/main/ets/pages/GetDrawableDescriptor.ets b/ArkUIKit/NativeNodeNapiSample/entry/src/main/ets/pages/GetDrawableDescriptor.ets new file mode 100644 index 0000000000000000000000000000000000000000..b82dc779855c2b3c66d7c1187ada39c87a3aaebe --- /dev/null +++ b/ArkUIKit/NativeNodeNapiSample/entry/src/main/ets/pages/GetDrawableDescriptor.ets @@ -0,0 +1,72 @@ +/* + * 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 Napi from 'libentry.so'; +import { DrawableDescriptor } from '@kit.ArkUI'; + +@Entry +@Component +struct Index { + @State sr1:Resource = $r('app.string.GetDrawableDescriptor_result0'); + @State sr2:Resource = $r('app.string.GetDrawableDescriptor_result0'); + build() { + Row() { + Column() { + Text($r('app.string.GetDrawableDescriptor_label')) + .fontSize(20) + .margin(20) + + Text($r('app.string.GetDrawableDescriptor_label2')) + .fontSize(20) + .margin(20) + + Button($r('app.string.GetDrawableDescriptor_button1')) + .onClick(() => { + let resManager = this.getUIContext().getHostContext()?.resourceManager + let imageRes: DrawableDescriptor = (resManager?.getDrawableDescriptor($r('app.media.foreground').id, 120)) as DrawableDescriptor; + + let res1 = Napi.processDrawable(imageRes); + if (res1 === 0) { + this.sr1 = $r('app.string.GetDrawableDescriptor_result1'); + } else { + this.sr1 = $r('app.string.GetDrawableDescriptor_result2'); + } + }) + .margin({top:40,bottom:20}) + + Text(this.sr1) + .fontSize(16) + .margin(20) + + Button($r('app.string.GetDrawableDescriptor_button2')) + .onClick(() => { + let resManager = this.getUIContext().getHostContext()?.resourceManager + let imageRes: DrawableDescriptor = (resManager?.getDrawableDescriptor($r('app.media.foreground').id)) as DrawableDescriptor; + let res2 = Napi.processDrawable2(imageRes); + if (res2 === 0) { + this.sr2 = $r('app.string.GetDrawableDescriptor_result3'); + } else { + this.sr2 = $r('app.string.GetDrawableDescriptor_result4'); + } + }) + Text(this.sr2) + .fontSize(16) + .margin(20) + } + .width('100%') + } + .height('100%') + } +} \ No newline at end of file diff --git a/ArkUIKit/NativeNodeNapiSample/entry/src/main/ets/pages/Index.ets b/ArkUIKit/NativeNodeNapiSample/entry/src/main/ets/pages/Index.ets index 9f5f3be3dc82ffed370eb82499417288ed3a6427..437698a51e1574edf6cdf6bcefe7c4449dbc1cee 100644 --- a/ArkUIKit/NativeNodeNapiSample/entry/src/main/ets/pages/Index.ets +++ b/ArkUIKit/NativeNodeNapiSample/entry/src/main/ets/pages/Index.ets @@ -27,6 +27,7 @@ struct MenuIndex { [ { title: 'Navigation', url: 'pages/page_navigation' }, { title: 'add your router', url: 'pages/page_navigation' }, + { title: 'Mapping DrawableDescriptor', url: 'pages/GetDrawableDescriptor' }, { title: 'Animation', url: 'pages/page_animation' }, { title: 'Visual Effects', url: 'pages/page_visual_effects' }, ]; diff --git a/ArkUIKit/NativeNodeNapiSample/entry/src/main/resources/base/element/string.json b/ArkUIKit/NativeNodeNapiSample/entry/src/main/resources/base/element/string.json index f94595515a99e0c828807e243494f57f09251930..b825322fafee159a2ebce7d1f3553e3a3632205f 100644 --- a/ArkUIKit/NativeNodeNapiSample/entry/src/main/resources/base/element/string.json +++ b/ArkUIKit/NativeNodeNapiSample/entry/src/main/resources/base/element/string.json @@ -11,6 +11,42 @@ { "name": "EntryAbility_label", "value": "label" + }, + { + "name": "GetDrawableDescriptor_label", + "value": "映射 Drawable 示例" + }, + { + "name": "GetDrawableDescriptor_label2", + "value": "ArkTS侧创建的DrawableDescriptor映射到Native侧" + }, + { + "name": "GetDrawableDescriptor_button1", + "value": "方式1:GetDrawableDescriptorFromNapiValue" + }, + { + "name": "GetDrawableDescriptor_result1", + "value": "测试结果: 方式1映射DrawableDescriptor成功" + }, + { + "name": "GetDrawableDescriptor_result2", + "value": "测试结果: 方式1映射DrawableDescriptor失败" + }, + { + "name": "GetDrawableDescriptor_button2", + "value": "方式2:GetDrawableDescriptorFromResourceNapiValue" + }, + { + "name": "GetDrawableDescriptor_result3", + "value": "测试结果: 方式2映射DrawableDescriptor成功" + }, + { + "name": "GetDrawableDescriptor_result4", + "value": "测试结果: 方式2映射DrawableDescriptor失败" + }, + { + "name": "GetDrawableDescriptor_result0", + "value": "测试结果: " } ] } \ No newline at end of file diff --git a/ArkUIKit/NativeNodeNapiSample/entry/src/main/resources/base/profile/main_pages.json b/ArkUIKit/NativeNodeNapiSample/entry/src/main/resources/base/profile/main_pages.json index a57d496de775519bfcf75e2dcf1f23c8cde39801..cbdc0fdc3d3ca856720dfd2ada6ff99b8f0417cf 100644 --- a/ArkUIKit/NativeNodeNapiSample/entry/src/main/resources/base/profile/main_pages.json +++ b/ArkUIKit/NativeNodeNapiSample/entry/src/main/resources/base/profile/main_pages.json @@ -2,6 +2,7 @@ "src": [ "pages/Index", "pages/page_navigation", + "pages/GetDrawableDescriptor", "pages/page_animation", "pages/animation/animationIndex1", "pages/animation/animationIndex2", diff --git a/ArkUIKit/NativeNodeNapiSample/screenshots/mappingDescriptor.jpeg b/ArkUIKit/NativeNodeNapiSample/screenshots/mappingDescriptor.jpeg new file mode 100644 index 0000000000000000000000000000000000000000..608ee544124aa6765b21d7b99ff9b349a6040b1d Binary files /dev/null and b/ArkUIKit/NativeNodeNapiSample/screenshots/mappingDescriptor.jpeg differ diff --git a/ArkUIKit/NativeNodeNapiSample/screenshots/page_navigation.jpeg b/ArkUIKit/NativeNodeNapiSample/screenshots/page_navigation.jpeg index b33f5ad2181b8044f203a9de4471570e558ab1f8..22893826aafd4fa19003a49dba5a6b6f4c6f33f8 100644 Binary files a/ArkUIKit/NativeNodeNapiSample/screenshots/page_navigation.jpeg and b/ArkUIKit/NativeNodeNapiSample/screenshots/page_navigation.jpeg differ