* 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 CAPI0805_2_ARKUICUSTOMCOLUMN_H
#define CAPI0805_2_ARKUICUSTOMCOLUMN_H
#include "ArkUINode.h"
namespace NativeModule {
class ArkUICustomColumnNode : public ArkUINode {
public:
ArkUICustomColumnNode()
: 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);
}
~ArkUICustomColumnNode() override
{
nativeModule_->removeNodeCustomEventReceiver(handle_, OnStaticCustomEvent);
nativeModule_->unregisterNodeCustomEvent(handle_, ARKUI_NODE_CUSTOM_EVENT_ON_MEASURE);
nativeModule_->unregisterNodeCustomEvent(handle_, ARKUI_NODE_CUSTOM_EVENT_ON_LAYOUT);
}
void SetPaddingCustom(int32_t padding)
{
padding_ = padding;
nativeModule_->markDirty(handle_, NODE_NEED_MEASURE);
}
void SetOffsetCustom(int32_t offsetX, int32_t offsetY)
{
offset_.x = offsetX;
offset_.y = offsetY;
nativeModule_->markDirty(handle_, NODE_NEED_LAYOUT);
}
void SetRegisterLayoutCallback()
{
OH_ArkUI_RegisterLayoutCallbackOnNodeHandle(handle_, this, OnLayoutCompleted);
OH_LOG_INFO(LOG_APP, "SetRegisterLayoutCallback");
}
void SetUnregisterLayoutCallback()
{
OH_ArkUI_UnregisterLayoutCallbackOnNodeHandle(handle_);
OH_LOG_INFO(LOG_APP, "SetUnregisterLayoutCallback");
}
private:
static void OnStaticCustomEvent(ArkUI_NodeCustomEvent *event)
{
auto customNode = reinterpret_cast<ArkUICustomColumnNode *>(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)
{
OH_LOG_INFO(LogType::LOG_APP, "OnMeasure");
auto layoutConstrain = OH_ArkUI_NodeCustomEvent_GetLayoutConstraintInMeasure(event);
auto childLayoutConstrain = OH_ArkUI_LayoutConstraint_Copy(layoutConstrain);
OH_ArkUI_LayoutConstraint_SetMaxHeight(childLayoutConstrain, MAX_SIZE);
OH_ArkUI_LayoutConstraint_SetMaxWidth(childLayoutConstrain, MAX_SIZE);
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 + DOUBLE * padding_, maxHeight + DOUBLE * padding_);
}
void OnLayout(ArkUI_NodeCustomEvent *event)
{
OH_LOG_INFO(LogType::LOG_APP, "OnLayout");
auto position = OH_ArkUI_NodeCustomEvent_GetPositionInLayout(event);
nativeModule_->setLayoutPosition(handle_, position.x + offset_.x, position.y + offset_.y);
auto totalSize = nativeModule_->getTotalChildCount(handle_);
auto selfSize = nativeModule_->getMeasuredSize(handle_);
float currentX = 0.0f;
float currentY = 0.0f;
float maxHeightInColumn = 0.0f;
for (uint32_t i = 0; i < totalSize; i++) {
auto child = nativeModule_->getChildAt(handle_, i);
auto childSize = nativeModule_->getMeasuredSize(child);
if (currentY + childSize.height > selfSize.height && currentY != 0) {
currentX += maxHeightInColumn;
currentY = 0;
maxHeightInColumn = 0;
}
nativeModule_->layoutNode(child, currentX, currentY);
currentY += childSize.height;
if (childSize.width > maxHeightInColumn) {
maxHeightInColumn = childSize.width;
}
}
}
static void OnLayoutCompleted(void *userData) { OH_LOG_INFO(LogType::LOG_APP, "OnLayoutCompleted"); }
int32_t padding_ = 100;
ArkUI_IntOffset offset_ = { 0, 0 };
static constexpr int DOUBLE = 2;
static constexpr int MAX_SIZE = 2;
};
}
#endif