1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "core/interfaces/native/node/node_content_modifier.h"
16 
17 #include "base/error/error_code.h"
18 #include "base/utils/utils.h"
19 #include "core/components_ng/base/ui_node.h"
20 #include "core/components_ng/syntax/node_content.h"
21 #include "core/interfaces/arkoala/arkoala_api.h"
22 
23 namespace OHOS::Ace::NG {
24 namespace {
25 
AddChild(ArkUINodeContentHandle content,ArkUINodeHandle child)26 ArkUI_Int32 AddChild(ArkUINodeContentHandle content, ArkUINodeHandle child)
27 {
28     CHECK_NULL_RETURN(content, ERROR_CODE_PARAM_INVALID);
29     CHECK_NULL_RETURN(child, ERROR_CODE_PARAM_INVALID);
30     auto* nodeContent = reinterpret_cast<NodeContent*>(content);
31     auto* uiNode = reinterpret_cast<UINode*>(child);
32     nodeContent->AddNode(uiNode);
33     return ERROR_CODE_NO_ERROR;
34 }
35 
InsertChild(ArkUINodeContentHandle content,ArkUINodeHandle child,ArkUI_Int32 position)36 ArkUI_Int32 InsertChild(ArkUINodeContentHandle content, ArkUINodeHandle child, ArkUI_Int32 position)
37 {
38     CHECK_NULL_RETURN(content, ERROR_CODE_PARAM_INVALID);
39     CHECK_NULL_RETURN(child, ERROR_CODE_PARAM_INVALID);
40     auto* nodeContent = reinterpret_cast<NodeContent*>(content);
41     auto* uiNode = reinterpret_cast<UINode*>(child);
42     nodeContent->AddNode(uiNode, position);
43     return ERROR_CODE_NO_ERROR;
44 }
45 
RemoveChild(ArkUINodeContentHandle content,ArkUINodeHandle child)46 ArkUI_Int32 RemoveChild(ArkUINodeContentHandle content, ArkUINodeHandle child)
47 {
48     CHECK_NULL_RETURN(content, ERROR_CODE_PARAM_INVALID);
49     CHECK_NULL_RETURN(child, ERROR_CODE_PARAM_INVALID);
50     auto* nodeContent = reinterpret_cast<NodeContent*>(content);
51     auto* uiNode = reinterpret_cast<UINode*>(child);
52     nodeContent->RemoveNode(uiNode);
53     return ERROR_CODE_NO_ERROR;
54 }
55 
RegisterEvent(ArkUINodeContentHandle content,void * userData,void (* receiver)(ArkUINodeContentEvent * event))56 ArkUI_Int32 RegisterEvent(
57     ArkUINodeContentHandle content, void* userData, void (*receiver)(ArkUINodeContentEvent* event))
58 {
59     CHECK_NULL_RETURN(content, ERROR_CODE_PARAM_INVALID);
60     CHECK_NULL_RETURN(receiver, ERROR_CODE_PARAM_INVALID);
61     auto onAttach = [receiver, userData, content]() {
62         ArkUINodeContentEvent event { 0, userData, content };
63         receiver(&event);
64     };
65     auto onDetach = [receiver, userData, content]() {
66         ArkUINodeContentEvent event { 1, userData, content };
67         receiver(&event);
68     };
69     auto* nodeContent = reinterpret_cast<NodeContent*>(content);
70     nodeContent->SetAttachToMainTreeCallback(std::move(onAttach));
71     nodeContent->SetDetachFromMainTreeCallback(std::move(onDetach));
72     return ERROR_CODE_NO_ERROR;
73 }
74 
SetUserData(ArkUINodeContentHandle content,void * userData)75 ArkUI_Int32 SetUserData(ArkUINodeContentHandle content, void* userData)
76 {
77     CHECK_NULL_RETURN(content, ERROR_CODE_PARAM_INVALID);
78     auto* nodeContent = reinterpret_cast<NodeContent*>(content);
79     nodeContent->SetUserData(userData);
80     return ERROR_CODE_NO_ERROR;
81 }
82 
GetUserData(ArkUINodeContentHandle content)83 void* GetUserData(ArkUINodeContentHandle content)
84 {
85     CHECK_NULL_RETURN(content, nullptr);
86     auto* nodeContent = reinterpret_cast<NodeContent*>(content);
87     return nodeContent->GetUserData();
88 }
89 } // namespace
90 
91 namespace NodeModifier {
GetNodeContentModifier()92 const ArkUINodeContentModifier* GetNodeContentModifier()
93 {
94     static const ArkUINodeContentModifier modifier = { AddChild, InsertChild, RemoveChild, RegisterEvent, SetUserData,
95         GetUserData };
96     return &modifier;
97 }
98 
GetCJUINodeContentModifier()99 const CJUINodeContentModifier* GetCJUINodeContentModifier()
100 {
101     static const CJUINodeContentModifier modifier = { AddChild, InsertChild, RemoveChild, RegisterEvent, SetUserData,
102         GetUserData };
103     return &modifier;
104 }
105 } // namespace NodeModifier
106 } // namespace OHOS::Ace::NG
107