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 "core/components_ng/syntax/node_content.h"
19
20 namespace OHOS::Ace::NG {
21 namespace {
22
AddChild(ArkUINodeContentHandle content,ArkUINodeHandle child)23 ArkUI_Int32 AddChild(ArkUINodeContentHandle content, ArkUINodeHandle child)
24 {
25 CHECK_NULL_RETURN(content, ERROR_CODE_PARAM_INVALID);
26 CHECK_NULL_RETURN(child, ERROR_CODE_PARAM_INVALID);
27 auto* nodeContent = reinterpret_cast<NodeContent*>(content);
28 auto* uiNode = reinterpret_cast<UINode*>(child);
29 nodeContent->AddNode(uiNode);
30 return ERROR_CODE_NO_ERROR;
31 }
32
InsertChild(ArkUINodeContentHandle content,ArkUINodeHandle child,ArkUI_Int32 position)33 ArkUI_Int32 InsertChild(ArkUINodeContentHandle content, ArkUINodeHandle child, ArkUI_Int32 position)
34 {
35 CHECK_NULL_RETURN(content, ERROR_CODE_PARAM_INVALID);
36 CHECK_NULL_RETURN(child, ERROR_CODE_PARAM_INVALID);
37 auto* nodeContent = reinterpret_cast<NodeContent*>(content);
38 auto* uiNode = reinterpret_cast<UINode*>(child);
39 nodeContent->AddNode(uiNode, position);
40 return ERROR_CODE_NO_ERROR;
41 }
42
RemoveChild(ArkUINodeContentHandle content,ArkUINodeHandle child)43 ArkUI_Int32 RemoveChild(ArkUINodeContentHandle content, ArkUINodeHandle child)
44 {
45 CHECK_NULL_RETURN(content, ERROR_CODE_PARAM_INVALID);
46 CHECK_NULL_RETURN(child, ERROR_CODE_PARAM_INVALID);
47 auto* nodeContent = reinterpret_cast<NodeContent*>(content);
48 auto* uiNode = reinterpret_cast<UINode*>(child);
49 nodeContent->RemoveNode(uiNode);
50 return ERROR_CODE_NO_ERROR;
51 }
52
RegisterEvent(ArkUINodeContentHandle content,void * userData,void (* receiver)(ArkUINodeContentEvent * event))53 ArkUI_Int32 RegisterEvent(
54 ArkUINodeContentHandle content, void* userData, void (*receiver)(ArkUINodeContentEvent* event))
55 {
56 CHECK_NULL_RETURN(content, ERROR_CODE_PARAM_INVALID);
57 CHECK_NULL_RETURN(receiver, ERROR_CODE_PARAM_INVALID);
58 auto onAttach = [receiver, userData, content]() {
59 ArkUINodeContentEvent event { 0, userData, content };
60 receiver(&event);
61 };
62 auto onDetach = [receiver, userData, content]() {
63 ArkUINodeContentEvent event { 1, userData, content };
64 receiver(&event);
65 };
66 auto* nodeContent = reinterpret_cast<NodeContent*>(content);
67 nodeContent->SetAttachToMainTreeCallback(std::move(onAttach));
68 nodeContent->SetDetachFromMainTreeCallback(std::move(onDetach));
69 return ERROR_CODE_NO_ERROR;
70 }
71
SetUserData(ArkUINodeContentHandle content,void * userData)72 ArkUI_Int32 SetUserData(ArkUINodeContentHandle content, void* userData)
73 {
74 CHECK_NULL_RETURN(content, ERROR_CODE_PARAM_INVALID);
75 auto* nodeContent = reinterpret_cast<NodeContent*>(content);
76 nodeContent->SetUserData(userData);
77 return ERROR_CODE_NO_ERROR;
78 }
79
GetUserData(ArkUINodeContentHandle content)80 void* GetUserData(ArkUINodeContentHandle content)
81 {
82 CHECK_NULL_RETURN(content, nullptr);
83 auto* nodeContent = reinterpret_cast<NodeContent*>(content);
84 return nodeContent->GetUserData();
85 }
86 } // namespace
87
88 namespace NodeModifier {
GetNodeContentModifier()89 const ArkUINodeContentModifier* GetNodeContentModifier()
90 {
91 CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
92 static const ArkUINodeContentModifier modifier = {
93 .addChild = AddChild,
94 .insertChild = InsertChild,
95 .removeChild = RemoveChild,
96 .registerEvent = RegisterEvent,
97 .setUserData = SetUserData,
98 .getUserData = GetUserData,
99 };
100 CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
101 return &modifier;
102 }
103
GetCJUINodeContentModifier()104 const CJUINodeContentModifier* GetCJUINodeContentModifier()
105 {
106 CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
107 static const CJUINodeContentModifier modifier = {
108 .addChild = AddChild,
109 .insertChild = InsertChild,
110 .removeChild = RemoveChild,
111 .registerEvent = RegisterEvent,
112 .setUserData = SetUserData,
113 .getUserData = GetUserData,
114 };
115 CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
116 return &modifier;
117 }
118 } // namespace NodeModifier
119 } // namespace OHOS::Ace::NG
120