整理完创建节点和组件代码

This commit is contained in:
JackLee 2026-02-25 21:31:47 +08:00
parent e69e157960
commit 77dd6edf5f
3 changed files with 208 additions and 203 deletions

View File

@ -45,14 +45,13 @@ ArkUI_NodeHandle xc;
int32_t NativeManager::hasDraw_ = 0; int32_t NativeManager::hasDraw_ = 0;
int32_t NativeManager::hasChangeColor_ = 0; int32_t NativeManager::hasChangeColor_ = 0;
NativeManager::~NativeManager() NativeManager::~NativeManager() {
{
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "~NativeManager"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "~NativeManager");
nativeXComponentMap_.clear(); nativeXComponentMap_.clear();
if (eglcore_ != nullptr) { if (eglcore_ != nullptr) {
delete eglcore_; delete eglcore_;
eglcore_ = nullptr; eglcore_ = nullptr;
} }
for (auto iter = pluginManagerMap_.begin(); iter != pluginManagerMap_.end(); ++iter) { for (auto iter = pluginManagerMap_.begin(); iter != pluginManagerMap_.end(); ++iter) {
if (iter->second != nullptr) { if (iter->second != nullptr) {
@ -62,35 +61,29 @@ NativeManager::~NativeManager()
} }
pluginManagerMap_.clear(); pluginManagerMap_.clear();
} }
//Surface回调事件 // Surface回调事件
void OnSurfaceCreatedNative(OH_ArkUI_SurfaceHolder *holder) void OnSurfaceCreatedNative(OH_ArkUI_SurfaceHolder *holder) {
{
auto window = OH_ArkUI_XComponent_GetNativeWindow(holder); // 获取native window auto window = OH_ArkUI_XComponent_GetNativeWindow(holder); // 获取native window
auto eglCore = reinterpret_cast<EGLCore*>(OH_ArkUI_SurfaceHolder_GetUserData(holder)); auto eglCore = reinterpret_cast<EGLCore *>(OH_ArkUI_SurfaceHolder_GetUserData(holder));
eglCore->EglContextInit(window, 800, 600); eglCore->EglContextInit(window, 800, 600);
} }
void OnSurfaceChangedNative(OH_ArkUI_SurfaceHolder *holder, uint64_t width, uint64_t height) void OnSurfaceChangedNative(OH_ArkUI_SurfaceHolder *holder, uint64_t width, uint64_t height) {
{ EGLCore *render = reinterpret_cast<EGLCore *>(OH_ArkUI_SurfaceHolder_GetUserData(holder));
EGLCore* render = reinterpret_cast<EGLCore*>(OH_ArkUI_SurfaceHolder_GetUserData(holder)); render->UpdateSize(width, height); // 设置绘制区域大小
render->UpdateSize(width, height); // 设置绘制区域大小 render->Draw(NativeManager::hasDraw_); // 绘制五角星
render->Draw(NativeManager::hasDraw_); // 绘制五角星
} }
void OnSurfaceDestroyedNative(OH_ArkUI_SurfaceHolder *holder) void OnSurfaceDestroyedNative(OH_ArkUI_SurfaceHolder *holder) {
{
OH_LOG_Print(LOG_APP, LOG_ERROR, 0xff00, "onBind", "on destroyed"); OH_LOG_Print(LOG_APP, LOG_ERROR, 0xff00, "onBind", "on destroyed");
EGLCore* render = reinterpret_cast<EGLCore*>(OH_ArkUI_SurfaceHolder_GetUserData(holder)); EGLCore *render = reinterpret_cast<EGLCore *>(OH_ArkUI_SurfaceHolder_GetUserData(holder));
render->Release(); // 销毁eglSurface相关资源 render->Release(); // 销毁eglSurface相关资源
} }
void OnSurfaceShowNative(OH_ArkUI_SurfaceHolder *holder) void OnSurfaceShowNative(OH_ArkUI_SurfaceHolder *holder) {
{
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "on surface show"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "on surface show");
} }
void OnSurfaceHideNative(OH_ArkUI_SurfaceHolder *holder) void OnSurfaceHideNative(OH_ArkUI_SurfaceHolder *holder) {
{
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "on surface hide"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "on surface hide");
} }
void OnFrameCallbackNative(ArkUI_NodeHandle node, uint64_t timestamp, uint64_t targetTimestamp) void OnFrameCallbackNative(ArkUI_NodeHandle node, uint64_t timestamp, uint64_t targetTimestamp) {
{
if (!NativeManager::surfaceHolderMap_.count(node)) { if (!NativeManager::surfaceHolderMap_.count(node)) {
return; return;
} }
@ -101,53 +94,7 @@ void OnFrameCallbackNative(ArkUI_NodeHandle node, uint64_t timestamp, uint64_t t
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "OnFrameCallback count = %{public}ld", count); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "OnFrameCallback count = %{public}ld", count);
} }
} }
void onEvent(ArkUI_NodeEvent *event) void OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window) {
{
auto eventType = OH_ArkUI_NodeEvent_GetEventType(event); // 获取组件事件类型
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "on event");
if (eventType == NODE_TOUCH_EVENT) {
ArkUI_NodeHandle handle = OH_ArkUI_NodeEvent_GetNodeHandle(event); // 获取触发该事件的组件对象
auto holder = NativeManager::surfaceHolderMap_[handle];
EGLCore* render = reinterpret_cast<EGLCore*>(OH_ArkUI_SurfaceHolder_GetUserData(holder));
render->Draw(NativeManager::hasDraw_); // 绘制五角星
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "on touch");
}
}
static std::string value2String(napi_env env, napi_value value)
{
size_t stringSize = 0;
napi_get_value_string_utf8(env, value, nullptr, 0, &stringSize);
std::string valueString;
valueString.resize(stringSize);
napi_get_value_string_utf8(env, value, &valueString[0], stringSize+1, &stringSize);
return valueString;
}
napi_value NativeManager::NapiDrawPattern(napi_env env, napi_callback_info info)
{
// [StartExclude plugin_draw_pattern]
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeManager", "NapiDrawPattern");
if ((env == nullptr) || (info == nullptr)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "NapiDrawPattern: env or info is null");
return nullptr;
}
// [EndExclude plugin_draw_pattern]
// 获取环境变量参数
napi_value thisArg;
if (napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr) != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "NapiDrawPattern: napi_get_cb_info fail");
return nullptr;
}
auto *pluginManger = NativeManager::GetInstance();
// 调用绘制方法
pluginManger->eglcore_->Draw(hasDraw_);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeManager", "render->eglCore_->Draw() executed");
return nullptr;
}
//XComponent回调事件
void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window)
{
// [StartExclude plugin_on_surface_created] // [StartExclude plugin_on_surface_created]
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", "OnSurfaceCreatedCB"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", "OnSurfaceCreatedCB");
int32_t ret; int32_t ret;
@ -166,8 +113,7 @@ void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window)
auto *pluginManger = NativeManager::GetInstance(); auto *pluginManger = NativeManager::GetInstance();
pluginManger->OnSurfaceCreated(component, window); pluginManger->OnSurfaceCreated(component, window);
} }
void OnSurfaceChangedCB(OH_NativeXComponent* component, void* window) void OnSurfaceChangedCB(OH_NativeXComponent *component, void *window) {
{
// [StartExclude plugin_on_surface_changed] // [StartExclude plugin_on_surface_changed]
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", "OnSurfaceChangedCB"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", "OnSurfaceChangedCB");
int32_t ret; int32_t ret;
@ -183,8 +129,7 @@ void OnSurfaceChangedCB(OH_NativeXComponent* component, void* window)
// 封装OnSurfaceChanged方法 // 封装OnSurfaceChanged方法
pluginManger->OnSurfaceChanged(component, window); pluginManger->OnSurfaceChanged(component, window);
} }
void OnSurfaceDestroyedCB(OH_NativeXComponent* component, void* window) void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window) {
{
// [StartExclude plugin_on_surface_destroyed] // [StartExclude plugin_on_surface_destroyed]
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceDestroyedCB"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "OnSurfaceDestroyedCB");
int32_t ret; int32_t ret;
@ -199,8 +144,7 @@ void OnSurfaceDestroyedCB(OH_NativeXComponent* component, void* window)
auto *pluginManger = NativeManager::GetInstance(); auto *pluginManger = NativeManager::GetInstance();
pluginManger->OnSurfaceDestroyed(component, window); pluginManger->OnSurfaceDestroyed(component, window);
} }
void DispatchTouchEventCB(OH_NativeXComponent* component, void* window) void DispatchTouchEventCB(OH_NativeXComponent *component, void *window) {
{
// [StartExclude plugin_dispatch_touch_event] // [StartExclude plugin_dispatch_touch_event]
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "DispatchTouchEventCB"); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Callback", "DispatchTouchEventCB");
int32_t ret; int32_t ret;
@ -215,112 +159,9 @@ void DispatchTouchEventCB(OH_NativeXComponent* component, void* window)
auto *pluginManger = NativeManager::GetInstance(); auto *pluginManger = NativeManager::GetInstance();
pluginManger->DispatchTouchEvent(component, window); pluginManger->DispatchTouchEvent(component, window);
} }
NativeManager::NativeManager() //NativeManager回调
{ void NativeManager::OnSurfaceCreated(OH_NativeXComponent *component, void *window) {
eglcore_ = new EGLCore(); OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", "PluginManager::OnSurfaceCreated");
callback_.OnSurfaceCreated = OnSurfaceCreatedCB;
callback_.OnSurfaceChanged = OnSurfaceChangedCB;
callback_.OnSurfaceDestroyed = OnSurfaceDestroyedCB;
callback_.DispatchTouchEvent = DispatchTouchEventCB;
}
ArkUI_NodeHandle CreateNodeHandle(const std::string &tag)
{
ArkUI_NodeHandle column = nodeAPI->createNode(ARKUI_NODE_RELATIVE_CONTAINER);
ArkUI_NumberValue value[] = {480};
ArkUI_NumberValue value1[] = {{.u32 = 15}, {.f32 = 15}};
ArkUI_AttributeItem item = {value, 1, "changeSize"};
ArkUI_AttributeItem item1 = {value1, 2};
nodeAPI->setAttribute(column, NODE_WIDTH, &item);
value[0].f32 = COLUMN_MARGIN;
nodeAPI->setAttribute(column, NODE_MARGIN, &item);
// 创建XComponent组件
xc = nodeAPI->createNode(ARKUI_NODE_XCOMPONENT);
// 设置XComponent组件属性
value[0].u32 = ARKUI_XCOMPONENT_TYPE_SURFACE;
nodeAPI->setAttribute(xc, NODE_XCOMPONENT_TYPE, &item);
nodeAPI->setAttribute(xc, NODE_XCOMPONENT_ID, &item);
nodeAPI->setAttribute(xc, NODE_XCOMPONENT_SURFACE_SIZE, &item1);
ArkUI_NumberValue focusable[] = {1};
focusable[0].i32 = 1;
ArkUI_AttributeItem focusableItem = {focusable, 1};
nodeAPI->setAttribute(xc, NODE_FOCUSABLE, &focusableItem);
ArkUI_NumberValue valueSize[] = {480};
ArkUI_AttributeItem itemSize = {valueSize, 1};
valueSize[0].f32 = XC_WIDTH;
nodeAPI->setAttribute(xc, NODE_WIDTH, &itemSize);
valueSize[0].f32 = XC_HEIGHT;
nodeAPI->setAttribute(xc, NODE_HEIGHT, &itemSize);
ArkUI_AttributeItem item2 = {value, 1, "ndkxcomponent"};
nodeAPI->setAttribute(xc, NODE_ID, &item2);
auto *nativeXComponent = OH_NativeXComponent_GetNativeXComponent(xc);
if (!nativeXComponent) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "GetNativeXComponent error");
return column;
}
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "GetNativeXComponent success");
// 注册XComponent回调函数
OH_NativeXComponent_RegisterCallback(nativeXComponent, &NativeManager::callback_);
auto typeRet = nodeAPI->getAttribute(xc, NODE_XCOMPONENT_TYPE);
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "xcomponent type: %{public}d",
typeRet->value[0].i32);
auto idRet = nodeAPI->getAttribute(xc, NODE_XCOMPONENT_ID);
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "xcomponent id: %{public}s",
idRet->string);
nodeAPI->addChild(column, xc);
return column;
}
napi_value NativeManager::createNativeNode(napi_env env, napi_callback_info info)
{
if ((env == nullptr) || (info == nullptr)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "CreateNativeNode env or info is null");
return nullptr;
}
size_t argCnt = 2;
napi_value args[2] = { nullptr, nullptr };
if (napi_get_cb_info(env, info, &argCnt, args, nullptr, nullptr) != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "CreateNativeNode napi_get_cb_info failed");
}
if (argCnt != ARG_CNT) {
napi_throw_type_error(env, NULL, "Wrong number of arguments");
return nullptr;
}
ArkUI_NodeContentHandle nodeContentHandle_ = nullptr;
OH_ArkUI_GetNodeContentFromNapiValue(env, args[0], &nodeContentHandle_);
nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1*>(
OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1")
);
std::string tag = value2String(env, args[1]);
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "tag=%{public}s", tag.c_str());
int32_t ret = OH_ArkUI_NodeContent_SetUserData(nodeContentHandle_, new std::string(tag));
if (ret != ARKUI_ERROR_CODE_NO_ERROR) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "setUserData failed error=%{public}d", ret);
}
if (nodeAPI != nullptr && nodeAPI->createNode != nullptr && nodeAPI->addChild != nullptr) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager",
"CreateNativeNode tag=%{public}s", tag.c_str());
auto nodeContentEvent = [](ArkUI_NodeContentEvent *event) {
ArkUI_NodeContentHandle handle = OH_ArkUI_NodeContentEvent_GetNodeContentHandle(event);
std::string *userDate = reinterpret_cast<std::string*>(OH_ArkUI_NodeContent_GetUserData(handle));
if (OH_ArkUI_NodeContentEvent_GetEventType(event) == NODE_CONTENT_EVENT_ON_ATTACH_TO_WINDOW) {
ArkUI_NodeHandle testNode;
if (userDate) {
testNode = CreateNodeHandle(*userDate);
delete userDate;
userDate = nullptr;
} else {
testNode = CreateNodeHandle("noUserData");
}
OH_ArkUI_NodeContent_AddNode(handle, testNode);
}
};
OH_ArkUI_NodeContent_RegisterCallback(nodeContentHandle_, nodeContentEvent);
}
return nullptr;
}
void NativeManager::OnSurfaceCreated(OH_NativeXComponent* component, void* window)
{
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", "NativeManager::OnSurfaceCreated");
int32_t ret; int32_t ret;
char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {}; char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {};
uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1; uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
@ -331,12 +172,10 @@ void NativeManager::OnSurfaceCreated(OH_NativeXComponent* component, void* windo
eglcore_->Background(); eglcore_->Background();
} }
} }
void NativeManager::OnSurfaceDestroyed(OH_NativeXComponent* component, void* window) void NativeManager::OnSurfaceDestroyed(OH_NativeXComponent *component, void *window) {
{ OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", "PluginManager::OnSurfaceDestroyed");
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", "NativeManager::OnSurfaceDestroyed");
} }
void NativeManager::DispatchTouchEvent(OH_NativeXComponent* component, void* window) void NativeManager::DispatchTouchEvent(OH_NativeXComponent *component, void *window) {
{
int32_t ret = OH_NativeXComponent_GetTouchEvent(component, window, &touchEvent_); int32_t ret = OH_NativeXComponent_GetTouchEvent(component, window, &touchEvent_);
if (ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { if (ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
float tiltX = 2.2; float tiltX = 2.2;
@ -349,8 +188,8 @@ void NativeManager::DispatchTouchEvent(OH_NativeXComponent* component, void* win
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native",
"Touch Info : x=%{public}f, y=%{public}f screenx=%{public}f, screeny=%{public}f," "Touch Info : x=%{public}f, y=%{public}f screenx=%{public}f, screeny=%{public}f,"
"type=%{public}d, force=%{public}f, tiltX=%{public}f, tiltY=%{public}f, toolType=%{public}d", "type=%{public}d, force=%{public}f, tiltX=%{public}f, tiltY=%{public}f, toolType=%{public}d",
touchEvent_.x, touchEvent_.y, touchEvent_.screenX, touchEvent_.x, touchEvent_.y, touchEvent_.screenX, touchEvent_.screenY, touchEvent_.type,
touchEvent_.screenY, touchEvent_.type, touchEvent_.force, tiltX, tiltY, toolType); touchEvent_.force, tiltX, tiltY, toolType);
if (touchEvent_.type == OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_UP) { if (touchEvent_.type == OH_NativeXComponent_TouchEventType::OH_NATIVEXCOMPONENT_UP) {
eglcore_->ChangeColor(hasChangeColor_); eglcore_->ChangeColor(hasChangeColor_);
} }
@ -374,14 +213,177 @@ void NativeManager::DispatchTouchEvent(OH_NativeXComponent* component, void* win
} }
} }
} }
void NativeManager::OnSurfaceChanged(OH_NativeXComponent* component, void* window) void NativeManager::OnSurfaceChanged(OH_NativeXComponent *component, void *window) {
{
int32_t ret = OH_NativeXComponent_GetXComponentSize(component, window, &width_, &height_); int32_t ret = OH_NativeXComponent_GetXComponentSize(component, window, &width_, &height_);
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native", OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "XComponent_Native",
"OnSurfaceChanged ret=%{public}d width=%{public}lu, height=%{public}lu", ret, width_, height_); "OnSurfaceChanged ret=%{public}d width=%{public}lu, height=%{public}lu", ret, width_, height_);
} }
napi_value NativeManager::SetFrameRate(napi_env env, napi_callback_info info) void onEvent(ArkUI_NodeEvent *event) {
{ auto eventType = OH_ArkUI_NodeEvent_GetEventType(event); // 获取组件事件类型
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "on event");
if (eventType == NODE_TOUCH_EVENT) {
ArkUI_NodeHandle handle = OH_ArkUI_NodeEvent_GetNodeHandle(event); // 获取触发该事件的组件对象
auto holder = NativeManager::surfaceHolderMap_[handle];
EGLCore *render = reinterpret_cast<EGLCore *>(OH_ArkUI_SurfaceHolder_GetUserData(holder));
render->Draw(NativeManager::hasDraw_); // 绘制五角星
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "onBind", "on touch");
}
}
static std::string value2String(napi_env env, napi_value value) {
size_t stringSize = 0;
napi_get_value_string_utf8(env, value, nullptr, 0, &stringSize);
std::string valueString;
valueString.resize(stringSize);
napi_get_value_string_utf8(env, value, &valueString[0], stringSize + 1, &stringSize);
return valueString;
}
napi_value NativeManager::NapiDrawPattern(napi_env env, napi_callback_info info) {
// [StartExclude plugin_draw_pattern]
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeManager", "NapiDrawPattern");
if ((env == nullptr) || (info == nullptr)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "NapiDrawPattern: env or info is null");
return nullptr;
}
// [EndExclude plugin_draw_pattern]
// 获取环境变量参数
napi_value thisArg;
if (napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, nullptr) != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "NapiDrawPattern: napi_get_cb_info fail");
return nullptr;
}
auto *pluginManger = NativeManager::GetInstance();
// 调用绘制方法
pluginManger->eglcore_->Draw(hasDraw_);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeManager", "render->eglCore_->Draw() executed");
return nullptr;
}
// XComponent回调事件
NativeManager::NativeManager() {
eglcore_ = new EGLCore();
callback_.OnSurfaceCreated = OnSurfaceCreatedCB;
callback_.OnSurfaceChanged = OnSurfaceChangedCB;
callback_.OnSurfaceDestroyed = OnSurfaceDestroyedCB;
callback_.DispatchTouchEvent = DispatchTouchEventCB;
}
// 创建节点组件
ArkUI_NodeHandle CreateNodeHandle(const std::string &tag) {
// 创建Node也名创建ROW Column等容器
ArkUI_NodeHandle nodeHandel = nodeAPI->createNode(ARKUI_NODE_RELATIVE_CONTAINER);
// 节点默认宽度or高度
ArkUI_NumberValue nodeSizeData[] = {800, 600};
ArkUI_NumberValue nodeMarginData[] = {{.u32 = 15}, {.f32 = 15}};
// ArkUI_AttributeItem
// 参数1:指向数值数组
// 参数2:数组元素个数
// 参数3:属性名(可隐藏)
ArkUI_AttributeItem nodeSizeItem = {nodeSizeData, 2};
ArkUI_AttributeItem nodeMarginItem = {nodeMarginData, 2};
// 设置Node宽度or高度
nodeAPI->setAttribute(nodeHandel, NODE_SIZE, &nodeSizeItem);
nodeAPI->setAttribute(nodeHandel, NODE_MARGIN, &nodeMarginItem);
// 创建XComponent组件
// 组件类型Item
ArkUI_NumberValue comTypeData[] = {ARKUI_XCOMPONENT_TYPE_SURFACE};
ArkUI_AttributeItem comTypeItem = {comTypeData, 1};
// 组件ID Item
ArkUI_AttributeItem comIdItem = {.string = tag.c_str(), .size = 1};
// 组件Surface Size
ArkUI_NumberValue surfaceSizeData[] = {800, 600};
ArkUI_AttributeItem surfaceSizeItem = {surfaceSizeData, 2};
// 创建组件
xc = nodeAPI->createNode(ARKUI_NODE_XCOMPONENT);
// 设置XComponent组件属性
nodeAPI->setAttribute(xc, NODE_XCOMPONENT_TYPE, &comTypeItem);
nodeAPI->setAttribute(xc, NODE_XCOMPONENT_ID, &comIdItem);
nodeAPI->setAttribute(xc, NODE_XCOMPONENT_SURFACE_SIZE, &surfaceSizeItem);
// 焦点设置
ArkUI_NumberValue focusable[] = {1};
focusable[0].i32 = 1;
ArkUI_AttributeItem focusableItem = {focusable, 1};
nodeAPI->setAttribute(xc, NODE_FOCUSABLE, &focusableItem);
// 设置组件Size
nodeAPI->setAttribute(xc, NODE_WIDTH, &surfaceSizeItem);
nodeAPI->setAttribute(xc, NODE_HEIGHT, &surfaceSizeItem);
// 节点ID
ArkUI_AttributeItem nodeIdItem = {.string = "ndkxcomponent", .size = 1};
nodeAPI->setAttribute(xc, NODE_ID, &nodeIdItem);
auto *nativeXComponent = OH_NativeXComponent_GetNativeXComponent(xc);
if (!nativeXComponent) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "GetNativeXComponent error");
return nodeHandel;
}
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeManager", "GetNativeXComponent success");
// 注册XComponent回调函数
OH_NativeXComponent_RegisterCallback(nativeXComponent, &NativeManager::callback_);
// 组件类型
auto comTypeRet = nodeAPI->getAttribute(xc, NODE_XCOMPONENT_TYPE);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeManager", "com type: %{public}d",
comTypeRet->value[0].i32);
// 组件ID
auto comIdRet = nodeAPI->getAttribute(xc, NODE_XCOMPONENT_ID);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeManager", "com id: %{public}s", comIdRet->string);
// 增加组件到节点
nodeAPI->addChild(nodeHandel, xc);
return nodeHandel;
}
// Native侧创建Node
napi_value NativeManager::createNativeNode(napi_env env, napi_callback_info info) {
if ((env == nullptr) || (info == nullptr)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "CreateNativeNode env or info is null");
return nullptr;
}
size_t argCnt = 2;
napi_value args[2] = {nullptr, nullptr};
if (napi_get_cb_info(env, info, &argCnt, args, nullptr, nullptr) != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "CreateNativeNode napi_get_cb_info failed");
}
if (argCnt != ARG_CNT) {
napi_throw_type_error(env, NULL, "Wrong number of arguments");
return nullptr;
}
ArkUI_NodeContentHandle _nodeContentHandle = nullptr;
// 获取ArkTS侧创建的NodeContent对象映射到Native侧的
OH_ArkUI_GetNodeContentFromNapiValue(env, args[0], &_nodeContentHandle);
// 查询指定的模块接口名
nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1 *>(
OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1"));
// 节点名
std::string node_id = value2String(env, args[1]);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "NativeManager", "CreateNativeNode_Node_ID:", node_id.c_str());
// 设置用户自定义数据
int32_t ret = OH_ArkUI_NodeContent_SetUserData(_nodeContentHandle, new std::string(node_id));
if (ret != ARKUI_ERROR_CODE_NO_ERROR) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "NativeManager", "setUserData failed error=%{public}d", ret);
}
if (nodeAPI != nullptr && nodeAPI->createNode != nullptr && nodeAPI->addChild != nullptr) {
auto nodeContentEvent = [](ArkUI_NodeContentEvent *event) {
// 获取Node连接事件的对应Node的Handle
ArkUI_NodeContentHandle handle = OH_ArkUI_NodeContentEvent_GetNodeContentHandle(event);
// 获取对应Handle的UserData
std::string *userDate = reinterpret_cast<std::string *>(OH_ArkUI_NodeContent_GetUserData(handle));
if (OH_ArkUI_NodeContentEvent_GetEventType(event) == NODE_CONTENT_EVENT_ON_ATTACH_TO_WINDOW) {
ArkUI_NodeHandle testNode;
if (userDate) {
testNode = CreateNodeHandle(*userDate);
delete userDate;
userDate = nullptr;
} else {
testNode = CreateNodeHandle("noUserData");
}
// 向NodeContent中添加子组件
OH_ArkUI_NodeContent_AddNode(handle, testNode);
}
};
OH_ArkUI_NodeContent_RegisterCallback(_nodeContentHandle, nodeContentEvent);
}
return nullptr;
}
//设置期望帧率
napi_value NativeManager::SetFrameRate(napi_env env, napi_callback_info info) {
size_t argc = 4; size_t argc = 4;
napi_value args[4] = {nullptr}; napi_value args[4] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
@ -400,8 +402,7 @@ napi_value NativeManager::SetFrameRate(napi_env env, napi_callback_info info)
OH_ArkUI_XComponent_SetExpectedFrameRateRange(node, range); // 设置期望帧率 OH_ArkUI_XComponent_SetExpectedFrameRateRange(node, range); // 设置期望帧率
return nullptr; return nullptr;
} }
napi_value NativeManager::SetNeedSoftKeyboard(napi_env env, napi_callback_info info) napi_value NativeManager::SetNeedSoftKeyboard(napi_env env, napi_callback_info info) {
{
size_t argc = 2; size_t argc = 2;
napi_value args[2] = {nullptr}; napi_value args[2] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
@ -419,4 +420,4 @@ napi_value NativeManager::SetNeedSoftKeyboard(napi_env env, napi_callback_info i
return nullptr; return nullptr;
} }
} // namespace NativeXComponentSample } // namespace NativeOpenCAX

View File

@ -26,20 +26,18 @@
#include "arkui/native_node.h" #include "arkui/native_node.h"
#include "arkui/native_node_napi.h" #include "arkui/native_node_napi.h"
// [Start plugin_manager_h_part]
// plugin_manager.h
namespace NativeOpenCAX { namespace NativeOpenCAX {
// [StartExclude plugin_manager_h_part]
constexpr const int FIRST_ARG = 1; constexpr const int FIRST_ARG = 1;
constexpr const int SECOND_ARG = 2; constexpr const int SECOND_ARG = 2;
constexpr const int THIRD_ARG = 3; constexpr const int THIRD_ARG = 3;
constexpr const int FRAME_COUNT = 50; constexpr const int FRAME_COUNT = 50;
// [Start plugin_manager_h]
// 在头文件中定义NativeManager类
// [EndExclude plugin_manager_h_part]
class NativeManager { class NativeManager {
public: public:
// [StartExclude plugin_manager_h_part]
static OH_NativeXComponent_Callback callback_; static OH_NativeXComponent_Callback callback_;
NativeManager(); NativeManager();
~NativeManager(); ~NativeManager();

View File

@ -26,6 +26,12 @@ namespace NativeOpenCAX {
/** /**
* Log print domain. * Log print domain.
*/ */
#define DOMAIN 0xD001000
//#define LOGD(fmt, ...) HILOG_DEBUG(DOMAIN, TAG, fmt, ##__VA_ARGS__)
//#define LOGI(fmt, ...) HILOG_INFO(DOMAIN, TAG, fmt, ##__VA_ARGS__)
//#define LOGW(fmt, ...) HILOG_WARN(DOMAIN, TAG, fmt, ##__VA_ARGS__)
//#define LOGE(fmt, ...) HILOG_ERROR(DOMAIN, TAG, fmt, ##__VA_ARGS__)
const unsigned int LOG_PRINT_DOMAIN = 0xFF00; const unsigned int LOG_PRINT_DOMAIN = 0xFF00;
} // namespace NativeXComponentSample } // namespace NativeXComponentSample
#endif // NATIVE_XCOMPONENT_COMMON_H #endif // NATIVE_XCOMPONENT_COMMON_H