* 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 "common.h"
#include "function.h"
#include "napi/native_api.h"
#include <arkui/native_interface.h>
#include <arkui/native_key_event.h>
#include <arkui/native_node.h>
#include <arkui/native_node_napi.h>
#include <arkui/native_type.h>
#include <cstdint>
#include <cstdio>
#include <hilog/log.h>
#include <iostream>
#include <string>
#include "manager.h"
static ArkUI_NativeNodeAPI_1* nodeAPI = nullptr;
namespace NativeXComponentSample {
const unsigned int LOG_PRINT_DOMAIN = 0xFF00;
Manager Manager::manager_;
Manager::~Manager()
{
DisposeRootNode();
}
void Manager::DisposeRootNode()
{
if (contentHandle_ != nullptr && rootNode_ != nullptr) {
OH_ArkUI_NodeContent_RemoveNode(contentHandle_, rootNode_);
nodeAPI->disposeNode(rootNode_);
rootNode_ = nullptr;
}
}
void SampleEntry(ArkUI_NodeContentHandle contentHandle)
{
auto column = nodeAPI->createNode(ARKUI_NODE_COLUMN);
SetWidthPercent(column, 1);
SetHeightPercent(column, 1);
SetColumnJustifyContent(column, ARKUI_FLEX_ALIGNMENT_START);
SetColumnAlignItem(column, ARKUI_HORIZONTAL_ALIGNMENT_START);
FirstModule(column);
OH_ArkUI_NodeContent_AddNode(contentHandle, column);
}
napi_value Manager::CreateNativeNode(napi_env env, napi_callback_info info)
{
if ((env == nullptr) || (info == nullptr)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", "CreateNativeNode env or info is null");
return nullptr;
}
size_t argc = 1;
napi_value args[1] = { nullptr };
if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", "CreateNativeNode napi_get_cb_info failed");
return nullptr;
}
if (argc < 1) {
napi_throw_type_error(env, NULL, "Wrong number of arguments");
return nullptr;
}
ArkUI_NodeContentHandle contentHandle;
if (OH_ArkUI_GetNodeContentFromNapiValue(env, args[0], &contentHandle) != napi_ok) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager",
"CreateNativeNode GetNodeContentFromNapiValue failed");
return nullptr;
}
OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeAPI);
auto manager = Manager::GetInstance();
manager->SetContentHandle(contentHandle);
if (nodeAPI != nullptr && nodeAPI->createNode != nullptr && nodeAPI->addChild != nullptr) {
SampleEntry(contentHandle);
OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager", "Input event sample UI created successfully");
}
return nullptr;
}
napi_value Manager::DestroyNativeNode(napi_env env, napi_callback_info info)
{
if ((env == nullptr) || (info == nullptr)) {
OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager", "DestroyNativeNode env or info is null");
return nullptr;
}
auto manager = Manager::GetInstance();
if (manager != nullptr) {
manager->DisposeRootNode();
}
return nullptr;
}
}