Skip to content

Commit 955c983

Browse files
committed
feat(ohos): export prop handle c interface in hippy.h
1 parent 68604af commit 955c983

File tree

18 files changed

+813
-7
lines changed

18 files changed

+813
-7
lines changed

framework/examples/ohos-demo/src/main/cpp/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# limitations under the License.
1919
#
2020

21+
# 示例一:
2122
# 对于需要源码依赖 Hippy c++ 代码的特别需求,需要做2点:
2223
# 1 集成 Hippy c++ 源码,生成使用方自己的 so,类似下面配置。
2324
# 2 Hippy 内部不再编译 hippy so,注释掉 Hippy/framework/ohos/src/main/cpp/CMakeLists.txt 里内容。
@@ -49,3 +50,20 @@ set(PUBLIC_SOURCE_SET
4950
target_sources(${PROJECT_NAME} PRIVATE ${SOURCE_SET} PUBLIC ${PUBLIC_SOURCE_SET})
5051
5152
#]]
53+
54+
55+
# 示例二:
56+
# 下面是监听Hippy组件设置非内部属性的例子(外部判断是否自己关心的属性,用来处理上报等):
57+
58+
project(hippy_ex)
59+
cmake_minimum_required(VERSION 3.14)
60+
61+
add_library(${PROJECT_NAME} SHARED)
62+
63+
set(PUBLIC_SOURCE_SET
64+
hippy_extend/src/example_external_prop_handler.cc
65+
)
66+
target_sources(${PROJECT_NAME} PUBLIC ${PUBLIC_SOURCE_SET})
67+
68+
find_package(hippy)
69+
target_link_libraries(${PROJECT_NAME} PUBLIC hippy::hippy libhilog_ndk.z.so)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
*
3+
* Tencent is pleased to support the open source community by making
4+
* Hippy available.
5+
*
6+
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
7+
* All rights reserved.
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*
21+
*/
22+
23+
#include <hippy.h>
24+
#include <hr_any_data.h>
25+
#include <string>
26+
#include <hilog/log.h>
27+
28+
#define LOG_DOMAIN 0x0020 // 自定义domain值(16进制)
29+
#define LOG_TAG "MyApp" // 自定义TAG字符串
30+
31+
#ifdef __cplusplus
32+
extern "C" {
33+
#endif
34+
35+
bool ExampleRenderViewOnSetProp(void *arkui_handle, const char *propKey, HRAnyData propValue) {
36+
// 下面是示例代码:
37+
std::string value;
38+
if (HRAnyDataIsString(propValue)) {
39+
value = HRAnyDataGetString(propValue);
40+
} else if (HRAnyDataIsInt(propValue)) {
41+
int32_t t = 0;
42+
HRAnyDataGetInt(propValue, &t);
43+
value = std::to_string(t);
44+
} else if (HRAnyDataIsUint(propValue)) {
45+
uint32_t t = 0;
46+
HRAnyDataGetUint(propValue, &t);
47+
value = std::to_string(t);
48+
} else if (HRAnyDataIsDouble(propValue)) {
49+
double t = 0;
50+
HRAnyDataGetDouble(propValue, &t);
51+
value = std::to_string(t);
52+
} else if (HRAnyDataIsBool(propValue)) {
53+
bool t = false;
54+
HRAnyDataGetBool(propValue, &t);
55+
value = std::to_string(t);
56+
} else if (HRAnyDataIsArray(propValue)) {
57+
int arraySize = 0;
58+
HRAnyDataGetArraySize(propValue, &arraySize);
59+
value = "size:" + std::to_string(arraySize);
60+
if (arraySize > 0) {
61+
HRAnyData elementValue = nullptr;
62+
HRAnyDataGetArrayElement(propValue, &elementValue, 0);
63+
bool isStr = HRAnyDataIsString(elementValue);
64+
value += ",[0:";
65+
value += std::to_string(isStr);
66+
value += ",...]";
67+
}
68+
}
69+
OH_LOG_INFO(LOG_APP, "hippy demo, on set prop, key: %{public}s, value: %{public}s", propKey, value.c_str());
70+
return true;
71+
}
72+
73+
#ifdef __cplusplus
74+
}
75+
#endif
76+
77+
// 这里使用了全局函数的调用来注册prop handler,如果断点这里不执行,请检查对应so是否加载了
78+
auto RegisterHippyRenderViewPropHandlerOnLoad = []() {
79+
HRRenderViewSetExternalPropHandler(ExampleRenderViewOnSetProp, nullptr);
80+
return 0;
81+
}();

framework/examples/ohos-demo/src/main/cpp/hippy_extend/src/example_view_provider.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
*/
2222

23-
#include "renderer/api/hippy_view_provider.h"
23+
#include "renderer/api_internal/hippy_view_provider.h"
2424
#include "../include/example_view_a.h"
2525
#include "../include/example_view_b.h"
2626

framework/examples/ohos-demo/src/main/ets/entryability/EntryAbility.ets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import font from '@ohos.font';
2727
import { EngineInitParams } from 'hippy';
2828
import { BusinessError } from '@kit.BasicServicesKit';
2929
import fs from '@ohos.file.fs';
30+
import libHippyEx from 'libhippy_ex.so'
3031

3132
export default class EntryAbility extends UIAbility {
3233
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
@@ -36,6 +37,8 @@ export default class EntryAbility extends UIAbility {
3637
AppStorage.setOrCreate("libHippy", libHippy)
3738
AppStorage.setOrCreate("abilityContext", this.context)
3839

40+
AppStorage.setOrCreate("libHippyEx", libHippyEx)
41+
3942
let pageManagementItems:Object[] = []
4043
AppStorage.setOrCreate("pageManagementItems", pageManagementItems)
4144
}

framework/ohos/build-profile.json5

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"apiType": "stageMode",
33
"buildOption": {
4+
"nativeLib": {
5+
"headerPath": "./src/main/cpp/impl/renderer/native/include/renderer/api"
6+
},
47
"sourceOption": {
58
"workers": [
69
"./src/main/ets/support/workers/worker.ets",

framework/ohos/src/main/cpp/impl/renderer/native/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ set(PUBLIC_SOURCE_SET
149149
src/native_render_provider_capi.cc
150150
src/native_render_provider_napi.cc
151151
src/api/hippy_view_provider.cc
152-
src/api_c/hippy.cc
152+
src/api/hippy.cc
153+
src/api/hr_any_data.cc
154+
src/api_c/hippy_c.cc
153155
)
154156
target_sources(${PROJECT_NAME} PRIVATE ${SOURCE_SET} PUBLIC ${PUBLIC_SOURCE_SET})
155157
# endregion
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
*
3+
* Tencent is pleased to support the open source community by making
4+
* Hippy available.
5+
*
6+
* Copyright (C) 2019 THL A29 Limited, a Tencent company.
7+
* All rights reserved.
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*
21+
*/
22+
23+
#pragma once
24+
25+
#include "hr_any_data.h"
26+
27+
#ifdef __cplusplus
28+
extern "C" {
29+
#endif
30+
31+
/**
32+
* 自定义属性handler
33+
* @param arkui_handle view对应的ohos capi的handle,类型为ArkUI_NodeHandle
34+
* @param propKey 属性名称
35+
* @param propValue 属性值
36+
*/
37+
typedef bool (*HRRenderViewOnSetProp)(void *arkui_handle, const char *propKey, HRAnyData propValue);
38+
39+
/**
40+
* 自定义属性重置handler
41+
* @param arkui_handle view对应的ohos capi的handle,类型为ArkUI_NodeHandle
42+
* @param propKey 属性名称
43+
*/
44+
typedef bool (*HRRenderViewOnResetProp)(void *arkui_handle, const char *propKey);
45+
46+
/**
47+
* 设置自定义属性handler
48+
* @param handler
49+
*/
50+
void HRRenderViewSetExternalPropHandler(HRRenderViewOnSetProp set, HRRenderViewOnResetProp reset);
51+
52+
#ifdef __cplusplus
53+
}
54+
#endif

0 commit comments

Comments
 (0)