* 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 MYAPPLICATION_ARKUICUSTOMCONTAINERNODE_H
#define MYAPPLICATION_ARKUICUSTOMCONTAINERNODE_H
#include "ArkUINode.h"
namespace NativeModule {
class ArkUICustomContainerNode : public ArkUINode {
public:
ArkUICustomContainerNode()
: ArkUINode((NativeModuleInstance::GetInstance()->GetNativeNodeAPI())->createNode(ARKUI_NODE_CUSTOM))
{
nativeModule_->addNodeCustomEventReceiver(handle_, OnStaticCustomEvent);
nativeModule_->registerNodeCustomEvent(handle_, ARKUI_NODE_CUSTOM_EVENT_ON_MEASURE, 0, this);
nativeModule_->registerNodeCustomEvent(handle_, ARKUI_NODE_CUSTOM_EVENT_ON_LAYOUT, 0, this);
}
~ArkUICustomContainerNode() override
{
nativeModule_->removeNodeCustomEventReceiver(handle_, OnStaticCustomEvent);
nativeModule_->unregisterNodeCustomEvent(handle_, ARKUI_NODE_CUSTOM_EVENT_ON_MEASURE);
nativeModule_->unregisterNodeCustomEvent(handle_, ARKUI_NODE_CUSTOM_EVENT_ON_LAYOUT);
}
void SetPadding(int32_t padding)
{
padding_ = padding;
nativeModule_->markDirty(handle_, NODE_NEED_MEASURE);
}
private:
static void OnStaticCustomEvent(ArkUI_NodeCustomEvent *event)
{
auto customNode = reinterpret_cast<ArkUICustomContainerNode *>(OH_ArkUI_NodeCustomEvent_GetUserData(event));
auto type = OH_ArkUI_NodeCustomEvent_GetEventType(event);
switch (type) {
case ARKUI_NODE_CUSTOM_EVENT_ON_MEASURE:
customNode->OnMeasure(event);
break;
case ARKUI_NODE_CUSTOM_EVENT_ON_LAYOUT:
customNode->OnLayout(event);
break;
default:
break;
}
}
void OnMeasure(ArkUI_NodeCustomEvent *event)
{
auto layoutConstrain = OH_ArkUI_NodeCustomEvent_GetLayoutConstraintInMeasure(event);
auto childLayoutConstrain = OH_ArkUI_LayoutConstraint_Copy(layoutConstrain);
int32_t maxConstrain = 1000;
OH_ArkUI_LayoutConstraint_SetMaxHeight(childLayoutConstrain, maxConstrain);
OH_ArkUI_LayoutConstraint_SetMaxWidth(childLayoutConstrain, maxConstrain);
OH_ArkUI_LayoutConstraint_SetMinHeight(childLayoutConstrain, 0);
OH_ArkUI_LayoutConstraint_SetMinWidth(childLayoutConstrain, 0);
auto totalSize = nativeModule_->getTotalChildCount(handle_);
int32_t maxWidth = 0;
int32_t maxHeight = 0;
for (uint32_t i = 0; i < totalSize; i++) {
auto child = nativeModule_->getChildAt(handle_, i);
nativeModule_->measureNode(child, childLayoutConstrain);
auto size = nativeModule_->getMeasuredSize(child);
if (size.width > maxWidth) {
maxWidth = size.width;
}
if (size.height > maxHeight) {
maxHeight = size.height;
}
}
nativeModule_->setMeasuredSize(handle_, maxWidth + 2 * padding_, maxHeight + 2 * padding_);
}
void OnLayout(ArkUI_NodeCustomEvent *event)
{
auto position = OH_ArkUI_NodeCustomEvent_GetPositionInLayout(event);
nativeModule_->setLayoutPosition(handle_, position.x, position.y);
auto totalSize = nativeModule_->getTotalChildCount(handle_);
auto selfSize = nativeModule_->getMeasuredSize(handle_);
for (uint32_t i = 0; i < totalSize; i++) {
auto child = nativeModule_->getChildAt(handle_, i);
auto childSize = nativeModule_->getMeasuredSize(child);
nativeModule_->layoutNode(child, (selfSize.width - childSize.width) / 2,
(selfSize.height - childSize.height) / 2);
}
}
int32_t padding_ = 100;
};
}
#endif