• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/frame_node_modifier.h"
16 #include <cstdlib>
17 #include <vector>
18 
19 #include "base/error/error_code.h"
20 #include "base/memory/ace_type.h"
21 #include "base/utils/utils.h"
22 #include "core/components_ng/base/frame_node.h"
23 #include "core/components_ng/base/inspector.h"
24 #include "core/components_ng/base/ui_node.h"
25 #include "core/components_ng/pattern/custom_frame_node/custom_frame_node_pattern.h"
26 #include "core/interfaces/arkoala/arkoala_api.h"
27 #include "core/components_ng/base/view_abstract.h"
28 #include "core/components_ng/pattern/custom_frame_node/custom_frame_node.h"
29 #include "bridge/common/utils/engine_helper.h"
30 
31 namespace OHOS::Ace::NG {
32 enum class ExpandMode : uint32_t {
33     NOT_EXPAND = 0,
34     EXPAND,
35     LAZY_EXPAND,
36 };
37 
IsModifiable(ArkUINodeHandle node)38 ArkUI_Bool IsModifiable(ArkUINodeHandle node)
39 {
40     auto* currentNode = reinterpret_cast<UINode*>(node);
41     CHECK_NULL_RETURN(currentNode, false);
42     auto* frameNode = AceType::DynamicCast<UINode>(currentNode);
43     CHECK_NULL_RETURN(frameNode, false);
44     return frameNode->GetTag() == "CustomFrameNode";
45 }
46 
CreateFrameNode()47 ArkUINodeHandle CreateFrameNode()
48 {
49     auto nodeId = ElementRegister::GetInstance()->MakeUniqueId();
50     auto node = NG::CustomFrameNode::GetOrCreateCustomFrameNode(nodeId);
51     node->SetExclusiveEventForChild(true);
52     node->SetIsArkTsFrameNode(true);
53     return reinterpret_cast<ArkUINodeHandle>(OHOS::Ace::AceType::RawPtr(node));
54 }
55 
InvalidateInFrameNode(ArkUINodeHandle node)56 void InvalidateInFrameNode(ArkUINodeHandle node)
57 {
58     auto* frameNode = reinterpret_cast<FrameNode*>(node);
59     CHECK_NULL_VOID(frameNode);
60     CHECK_NULL_VOID(AceType::InstanceOf<CustomFrameNode>(frameNode));
61     auto pattern = frameNode->GetPattern<CustomFrameNodePattern>();
62     CHECK_NULL_VOID(pattern);
63     auto renderContext = frameNode->GetRenderContext();
64     CHECK_NULL_VOID(renderContext);
65     pattern->Invalidate();
66     renderContext->RequestNextFrame();
67 }
68 
GetParentNode(UINode * node)69 RefPtr<FrameNode> GetParentNode(UINode* node)
70 {
71     auto uiNode = AceType::Claim<UINode>(node);
72     auto parent = uiNode->GetParent();
73     while (parent != nullptr && !AceType::InstanceOf<FrameNode>(parent)) {
74         parent = parent->GetParent();
75     }
76     return (parent == nullptr || parent->GetTag() == V2::PAGE_ETS_TAG || parent->GetTag() == V2::STAGE_ETS_TAG)
77                ? nullptr : AceType::DynamicCast<FrameNode>(parent);
78 }
79 
AppendChildInFrameNode(ArkUINodeHandle node,ArkUINodeHandle child)80 ArkUI_Bool AppendChildInFrameNode(ArkUINodeHandle node, ArkUINodeHandle child)
81 {
82     auto* currentNode = reinterpret_cast<UINode*>(node);
83     CHECK_NULL_RETURN(currentNode, true);
84     auto* childNode = reinterpret_cast<UINode*>(child);
85     auto childRef = Referenced::Claim<UINode>(childNode);
86     CHECK_NULL_RETURN(childRef, true);
87     if (childRef->GetParent() != nullptr && childRef->GetParent() != currentNode) {
88         return false;
89     }
90     currentNode->AddChild(childRef);
91     currentNode->MarkNeedFrameFlushDirty(NG::PROPERTY_UPDATE_MEASURE);
92     return true;
93 }
94 
InsertChildAfterInFrameNode(ArkUINodeHandle node,ArkUINodeHandle child,ArkUINodeHandle sibling)95 ArkUI_Bool InsertChildAfterInFrameNode(ArkUINodeHandle node, ArkUINodeHandle child, ArkUINodeHandle sibling)
96 {
97     auto* currentNode = reinterpret_cast<UINode*>(node);
98     CHECK_NULL_RETURN(currentNode, true);
99     auto* childNode = reinterpret_cast<UINode*>(child);
100     CHECK_NULL_RETURN(childNode, true);
101     if (childNode->GetParent() != nullptr && childNode->GetParent() != currentNode) {
102         return false;
103     }
104     auto index = -1;
105     auto* siblingNode = reinterpret_cast<UINode*>(sibling);
106     index = currentNode->GetChildIndex(Referenced::Claim<UINode>(siblingNode));
107     currentNode->AddChild(Referenced::Claim<UINode>(childNode), index + 1);
108     currentNode->MarkNeedFrameFlushDirty(NG::PROPERTY_UPDATE_MEASURE);
109     return true;
110 }
111 
RemoveChildInFrameNode(ArkUINodeHandle node,ArkUINodeHandle child)112 void RemoveChildInFrameNode(ArkUINodeHandle node, ArkUINodeHandle child)
113 {
114     auto* currentNode = reinterpret_cast<UINode*>(node);
115     CHECK_NULL_VOID(currentNode);
116     auto* childNode = reinterpret_cast<UINode*>(child);
117     currentNode->RemoveChild(Referenced::Claim<UINode>(childNode));
118     currentNode->MarkNeedFrameFlushDirty(NG::PROPERTY_UPDATE_MEASURE);
119 }
120 
ClearChildrenInFrameNode(ArkUINodeHandle node)121 void ClearChildrenInFrameNode(ArkUINodeHandle node)
122 {
123     auto* currentNode = reinterpret_cast<FrameNode*>(node);
124     CHECK_NULL_VOID(currentNode);
125     currentNode->Clean();
126     currentNode->MarkNeedFrameFlushDirty(NG::PROPERTY_UPDATE_MEASURE);
127 }
128 
GetChildrenCount(ArkUINodeHandle node,ArkUI_Bool isExpanded)129 ArkUI_Uint32 GetChildrenCount(ArkUINodeHandle node, ArkUI_Bool isExpanded)
130 {
131     auto* currentNode = reinterpret_cast<FrameNode*>(node);
132     CHECK_NULL_RETURN(currentNode, 0);
133     auto* frameNode = AceType::DynamicCast<FrameNode>(currentNode);
134     CHECK_NULL_RETURN(frameNode, 0);
135     if (isExpanded) {
136         frameNode->GetAllChildrenWithBuild(false);
137     }
138     return isExpanded ? frameNode->GetAllChildrenWithBuild(false).size()
139                       : frameNode->GetTotalChildCountWithoutExpanded();
140 }
141 
GetChild(ArkUINodeHandle node,ArkUI_Int32 index,ArkUI_Uint32 expandMode)142 ArkUINodeHandle GetChild(ArkUINodeHandle node, ArkUI_Int32 index, ArkUI_Uint32 expandMode)
143 {
144     auto* currentNode = reinterpret_cast<FrameNode*>(node);
145     CHECK_NULL_RETURN(currentNode, nullptr);
146     auto* frameNode = AceType::DynamicCast<FrameNode>(currentNode);
147     CHECK_NULL_RETURN(frameNode, nullptr);
148     CHECK_NULL_RETURN(index >= 0, nullptr);
149     auto expandModeResult = static_cast<ExpandMode>(expandMode);
150     if (expandModeResult == ExpandMode::EXPAND) {
151         frameNode->GetAllChildrenWithBuild(false);
152     }
153     FrameNode* child = nullptr;
154     if (expandModeResult == ExpandMode::EXPAND || expandModeResult == ExpandMode::NOT_EXPAND) {
155         child = frameNode->GetFrameNodeChildByIndex(index, false, expandModeResult == ExpandMode::EXPAND);
156     } else if (expandModeResult == ExpandMode::LAZY_EXPAND) {
157         child = frameNode->GetFrameNodeChildByIndexWithoutBuild(index);
158         if (child == nullptr) {
159             return GetChild(node, index, static_cast<ArkUI_Uint32>(ExpandMode::EXPAND));
160         }
161     }
162     return reinterpret_cast<ArkUINodeHandle>(child);
163 }
164 
GetFirstChildIndexWithoutExpand(ArkUINodeHandle node,ArkUI_Uint32 * index)165 ArkUI_Int32 GetFirstChildIndexWithoutExpand(ArkUINodeHandle node, ArkUI_Uint32* index)
166 {
167     auto* currentNode = reinterpret_cast<FrameNode*>(node);
168     CHECK_NULL_RETURN(currentNode, ERROR_CODE_PARAM_INVALID);
169     auto* frameNode = AceType::DynamicCast<FrameNode>(currentNode);
170     CHECK_NULL_RETURN(frameNode, ERROR_CODE_PARAM_INVALID);
171     auto child = frameNode->GetFrameNodeChildByIndex(0, false, false);
172     CHECK_NULL_RETURN(child, ERROR_CODE_PARAM_INVALID);
173     auto* childNode = reinterpret_cast<FrameNode*>(child);
174     auto childRef = Referenced::Claim<FrameNode>(childNode);
175     *index = frameNode->GetFrameNodeIndex(childRef, true);
176     return ERROR_CODE_NO_ERROR;
177 }
178 
GetLastChildIndexWithoutExpand(ArkUINodeHandle node,ArkUI_Uint32 * index)179 ArkUI_Int32 GetLastChildIndexWithoutExpand(ArkUINodeHandle node, ArkUI_Uint32* index)
180 {
181     auto* currentNode = reinterpret_cast<FrameNode*>(node);
182     CHECK_NULL_RETURN(currentNode, ERROR_CODE_PARAM_INVALID);
183     auto* frameNode = AceType::DynamicCast<FrameNode>(currentNode);
184     CHECK_NULL_RETURN(frameNode, ERROR_CODE_PARAM_INVALID);
185     size_t size = static_cast<size_t>(frameNode->GetTotalChildCountWithoutExpanded());
186     CHECK_NULL_RETURN(size > 0, ERROR_CODE_PARAM_INVALID);
187     auto child = frameNode->GetFrameNodeChildByIndex(size - 1, false, false);
188     CHECK_NULL_RETURN(child, ERROR_CODE_PARAM_INVALID);
189     auto* childNode = reinterpret_cast<FrameNode*>(child);
190     auto childRef = Referenced::Claim<FrameNode>(childNode);
191     *index = frameNode->GetFrameNodeIndex(childRef, true);
192     return ERROR_CODE_NO_ERROR;
193 }
194 
GetFirst(ArkUINodeHandle node,ArkUI_Bool isExpanded)195 ArkUINodeHandle GetFirst(ArkUINodeHandle node, ArkUI_Bool isExpanded)
196 {
197     auto* currentNode = reinterpret_cast<FrameNode*>(node);
198     auto* frameNode = AceType::DynamicCast<FrameNode>(currentNode);
199     CHECK_NULL_RETURN(frameNode, nullptr);
200     if (isExpanded) {
201         frameNode->GetAllChildrenWithBuild(false);
202     }
203     auto child = frameNode->GetFrameNodeChildByIndex(0, false, isExpanded);
204     return reinterpret_cast<ArkUINodeHandle>(child);
205 }
206 
GetNextSibling(ArkUINodeHandle node,ArkUI_Bool isExpanded)207 ArkUINodeHandle GetNextSibling(ArkUINodeHandle node, ArkUI_Bool isExpanded)
208 {
209     auto* currentNode = reinterpret_cast<FrameNode*>(node);
210     CHECK_NULL_RETURN(currentNode, nullptr);
211     auto parent = GetParentNode(currentNode);
212     CHECK_NULL_RETURN(parent, nullptr);
213     auto index = -1;
214     if (isExpanded) {
215         parent->GetAllChildrenWithBuild(false);
216         index = parent->GetChildTrueIndex(Referenced::Claim<FrameNode>(currentNode));
217     } else {
218         index = parent->GetFrameNodeIndex(Referenced::Claim<FrameNode>(currentNode), false);
219     }
220     CHECK_NULL_RETURN(index > -1, nullptr);
221     auto sibling = parent->GetFrameNodeChildByIndex(index + 1, false, isExpanded);
222     return reinterpret_cast<ArkUINodeHandle>(sibling);
223 }
224 
GetPreviousSibling(ArkUINodeHandle node,ArkUI_Bool isExpanded)225 ArkUINodeHandle GetPreviousSibling(ArkUINodeHandle node, ArkUI_Bool isExpanded)
226 {
227     auto* currentNode = reinterpret_cast<FrameNode*>(node);
228     CHECK_NULL_RETURN(currentNode, nullptr);
229     auto parent = GetParentNode(currentNode);
230     CHECK_NULL_RETURN(parent, nullptr);
231     auto index = -1;
232     if (isExpanded) {
233         parent->GetAllChildrenWithBuild(false);
234         index = parent->GetChildTrueIndex(Referenced::Claim<FrameNode>(currentNode));
235     } else {
236         index = parent->GetFrameNodeIndex(Referenced::Claim<FrameNode>(currentNode), false);
237     }
238     CHECK_NULL_RETURN(index > 0, nullptr);
239     auto sibling = parent->GetFrameNodeChildByIndex(index - 1, false, isExpanded);
240     return reinterpret_cast<ArkUINodeHandle>(sibling);
241 }
242 
GetParent(ArkUINodeHandle node)243 ArkUINodeHandle GetParent(ArkUINodeHandle node)
244 {
245     auto* currentNode = reinterpret_cast<UINode*>(node);
246     CHECK_NULL_RETURN(currentNode, nullptr);
247     auto parent = GetParentNode(currentNode);
248     return reinterpret_cast<ArkUINodeHandle>(OHOS::Ace::AceType::RawPtr(parent));
249 }
250 
GetIdByNodePtr(ArkUINodeHandle node)251 ArkUI_Int32 GetIdByNodePtr(ArkUINodeHandle node)
252 {
253     auto* currentNode = reinterpret_cast<UINode*>(node);
254     CHECK_NULL_RETURN(currentNode, -1);
255     auto nodeId = currentNode->GetId();
256     return nodeId;
257 }
258 
PropertyUpdate(ArkUINodeHandle node)259 void PropertyUpdate(ArkUINodeHandle node)
260 {
261     auto* uiNode = reinterpret_cast<UINode*>(node);
262     if (uiNode) {
263         uiNode->MarkDirtyNode(PROPERTY_UPDATE_DIFF);
264     }
265 }
266 
GetLast(ArkUINodeHandle node,ArkUI_Bool isExpanded)267 ArkUINodeHandle GetLast(ArkUINodeHandle node, ArkUI_Bool isExpanded)
268 {
269     auto* currentNode = reinterpret_cast<FrameNode*>(node);
270     CHECK_NULL_RETURN(currentNode, nullptr);
271     auto* frameNode = AceType::DynamicCast<FrameNode>(currentNode);
272     CHECK_NULL_RETURN(frameNode, nullptr);
273     size_t size = isExpanded ? frameNode->GetAllChildrenWithBuild(false).size()
274                              : static_cast<size_t>(frameNode->GetTotalChildCountWithoutExpanded());
275     CHECK_NULL_RETURN(size > 0, nullptr);
276     auto child = frameNode->GetFrameNodeChildByIndex(size - 1, false, isExpanded);
277     return reinterpret_cast<ArkUINodeHandle>(child);
278 }
279 
GetPositionToParent(ArkUINodeHandle node,ArkUI_Float32 (* parentOffset)[2],ArkUI_Bool useVp)280 void GetPositionToParent(ArkUINodeHandle node, ArkUI_Float32 (*parentOffset)[2], ArkUI_Bool useVp)
281 {
282     auto* currentNode = reinterpret_cast<FrameNode*>(node);
283     CHECK_NULL_VOID(currentNode);
284     auto currFrameRect = currentNode->GetRectWithRender();
285     auto offset = currFrameRect.GetOffset();
286     if (useVp) {
287         (*parentOffset)[0] = PipelineBase::Px2VpWithCurrentDensity(offset.GetX());
288         (*parentOffset)[1] = PipelineBase::Px2VpWithCurrentDensity(offset.GetY());
289     } else {
290         (*parentOffset)[0] = offset.GetX();
291         (*parentOffset)[1] = offset.GetY();
292     }
293 }
294 
GetPositionToScreen(ArkUINodeHandle node,ArkUI_Float32 (* screenPosition)[2],ArkUI_Bool useVp)295 void GetPositionToScreen(ArkUINodeHandle node, ArkUI_Float32 (*screenPosition)[2], ArkUI_Bool useVp)
296 {
297     auto* currentNode = reinterpret_cast<FrameNode*>(node);
298     CHECK_NULL_VOID(currentNode);
299     auto offset = currentNode->GetPositionToScreen();
300     if (useVp) {
301         (*screenPosition)[0] = PipelineBase::Px2VpWithCurrentDensity(offset.GetX());
302         (*screenPosition)[1] = PipelineBase::Px2VpWithCurrentDensity(offset.GetY());
303     } else {
304         (*screenPosition)[0] = offset.GetX();
305         (*screenPosition)[1] = offset.GetY();
306     }
307 }
308 
GetPositionToWindow(ArkUINodeHandle node,ArkUI_Float32 (* windowOffset)[2],ArkUI_Bool useVp)309 void GetPositionToWindow(ArkUINodeHandle node, ArkUI_Float32 (*windowOffset)[2], ArkUI_Bool useVp)
310 {
311     auto* currentNode = reinterpret_cast<FrameNode*>(node);
312     CHECK_NULL_VOID(currentNode);
313     auto offset = currentNode->GetOffsetRelativeToWindow();
314     if (useVp) {
315         (*windowOffset)[0] = PipelineBase::Px2VpWithCurrentDensity(offset.GetX());
316         (*windowOffset)[1] = PipelineBase::Px2VpWithCurrentDensity(offset.GetY());
317     } else {
318         (*windowOffset)[0] = offset.GetX();
319         (*windowOffset)[1] = offset.GetY();
320     }
321 }
322 
GetPositionToParentWithTransform(ArkUINodeHandle node,ArkUI_Float32 (* parentPosition)[2],ArkUI_Bool useVp)323 void GetPositionToParentWithTransform(ArkUINodeHandle node, ArkUI_Float32 (*parentPosition)[2], ArkUI_Bool useVp)
324 {
325     auto* currentNode = reinterpret_cast<FrameNode*>(node);
326     CHECK_NULL_VOID(currentNode);
327     auto offset = currentNode->GetPositionToParentWithTransform();
328     if (useVp) {
329         (*parentPosition)[0] = PipelineBase::Px2VpWithCurrentDensity(offset.GetX());
330         (*parentPosition)[1] = PipelineBase::Px2VpWithCurrentDensity(offset.GetY());
331     } else {
332         (*parentPosition)[0] = offset.GetX();
333         (*parentPosition)[1] = offset.GetY();
334     }
335 }
336 
GetPositionToScreenWithTransform(ArkUINodeHandle node,ArkUI_Float32 (* screenPosition)[2],ArkUI_Bool useVp)337 void GetPositionToScreenWithTransform(ArkUINodeHandle node, ArkUI_Float32 (*screenPosition)[2], ArkUI_Bool useVp)
338 {
339     auto* currentNode = reinterpret_cast<FrameNode*>(node);
340     CHECK_NULL_VOID(currentNode);
341     auto offset = currentNode->GetPositionToScreenWithTransform();
342     if (useVp) {
343         (*screenPosition)[0] = PipelineBase::Px2VpWithCurrentDensity(offset.GetX());
344         (*screenPosition)[1] = PipelineBase::Px2VpWithCurrentDensity(offset.GetY());
345     } else {
346         (*screenPosition)[0] = offset.GetX();
347         (*screenPosition)[1] = offset.GetY();
348     }
349 }
350 
GetPositionToWindowWithTransform(ArkUINodeHandle node,ArkUI_Float32 (* windowPosition)[2],ArkUI_Bool useVp)351 void GetPositionToWindowWithTransform(ArkUINodeHandle node, ArkUI_Float32 (*windowPosition)[2], ArkUI_Bool useVp)
352 {
353     auto* currentNode = reinterpret_cast<FrameNode*>(node);
354     CHECK_NULL_VOID(currentNode);
355     auto offset = currentNode->GetPositionToWindowWithTransform();
356     if (useVp) {
357         (*windowPosition)[0] = PipelineBase::Px2VpWithCurrentDensity(offset.GetX());
358         (*windowPosition)[1] = PipelineBase::Px2VpWithCurrentDensity(offset.GetY());
359     } else {
360         (*windowPosition)[0] = offset.GetX();
361         (*windowPosition)[1] = offset.GetY();
362     }
363 }
364 
GetMeasuredSize(ArkUINodeHandle node)365 ArkUI_Float32* GetMeasuredSize(ArkUINodeHandle node)
366 {
367     auto* currentNode = reinterpret_cast<FrameNode*>(node);
368     CHECK_NULL_RETURN(currentNode, nullptr);
369     auto offset = currentNode->GetGeometryNode()->GetFrameSize();
370     ArkUI_Float32* ret = new ArkUI_Float32[2];
371     ret[0] = offset.Width();
372     ret[1] = offset.Height();
373     return ret;
374 }
375 
GetLayoutPosition(ArkUINodeHandle node)376 ArkUI_Float32* GetLayoutPosition(ArkUINodeHandle node)
377 {
378     auto* currentNode = reinterpret_cast<FrameNode*>(node);
379     CHECK_NULL_RETURN(currentNode, nullptr);
380     auto offset = currentNode->GetGeometryNode()->GetMarginFrameOffset();
381     ArkUI_Float32* ret = new ArkUI_Float32[2];
382     ret[0] = offset.GetX();
383     ret[1] = offset.GetY();
384     return ret;
385 }
386 
GetInspectorId(ArkUINodeHandle node)387 ArkUI_CharPtr GetInspectorId(ArkUINodeHandle node)
388 {
389     auto* currentNode = reinterpret_cast<FrameNode*>(node);
390     CHECK_NULL_RETURN(currentNode, "");
391     auto inspectorIdProp = currentNode->GetInspectorId();
392     if (inspectorIdProp.has_value()) {
393         static std::string inspectorId;
394         inspectorId = inspectorIdProp.value();
395         return inspectorId.c_str();
396     }
397 
398     return "";
399 }
400 
GetNodeType(ArkUINodeHandle node)401 ArkUI_CharPtr GetNodeType(ArkUINodeHandle node)
402 {
403     auto* currentNode = reinterpret_cast<FrameNode*>(node);
404     CHECK_NULL_RETURN(currentNode, "");
405     static std::string nodeType;
406     nodeType = currentNode->GetTag();
407     return nodeType.c_str();
408 }
409 
IsVisible(ArkUINodeHandle node)410 ArkUI_Bool IsVisible(ArkUINodeHandle node)
411 {
412     auto* currentNode = reinterpret_cast<FrameNode*>(node);
413     CHECK_NULL_RETURN(currentNode, false);
414     auto isVisible = currentNode->IsVisible();
415     auto parentNode = currentNode->GetParent();
416     while (isVisible && parentNode && AceType::InstanceOf<FrameNode>(*parentNode)) {
417         isVisible = isVisible && AceType::DynamicCast<FrameNode>(parentNode)->IsVisible();
418         parentNode = parentNode->GetParent();
419     }
420     return isVisible;
421 }
422 
IsAttached(ArkUINodeHandle node)423 ArkUI_Bool IsAttached(ArkUINodeHandle node)
424 {
425     auto* currentNode = reinterpret_cast<FrameNode*>(node);
426     CHECK_NULL_RETURN(currentNode, false);
427     return currentNode->IsOnMainTree();
428 }
429 
GetInspectorInfo(ArkUINodeHandle node)430 ArkUI_CharPtr GetInspectorInfo(ArkUINodeHandle node)
431 {
432     auto* currentNode = reinterpret_cast<FrameNode*>(node);
433     CHECK_NULL_RETURN(currentNode, "{}");
434     static std::string inspectorInfo;
435     inspectorInfo = NG::Inspector::GetInspectorOfNode(OHOS::Ace::AceType::Claim<FrameNode>(currentNode));
436     return inspectorInfo.c_str();
437 }
438 
GetFrameNodeById(ArkUI_Int32 nodeId)439 ArkUINodeHandle GetFrameNodeById(ArkUI_Int32 nodeId)
440 {
441     auto node = OHOS::Ace::ElementRegister::GetInstance()->GetNodeById(nodeId);
442     return reinterpret_cast<ArkUINodeHandle>(OHOS::Ace::AceType::RawPtr(node));
443 }
444 
GetFrameNodeByUniqueId(ArkUI_Int32 uniqueId)445 ArkUINodeHandle GetFrameNodeByUniqueId(ArkUI_Int32 uniqueId)
446 {
447     auto node = AceType::DynamicCast<NG::UINode>(OHOS::Ace::ElementRegister::GetInstance()->GetNodeById(uniqueId));
448     CHECK_NULL_RETURN(node, nullptr);
449     if (node->GetTag() == "root" || node->GetTag() == "stage" || node->GetTag() == "page") {
450         return nullptr;
451     }
452 
453     if (!AceType::InstanceOf<NG::FrameNode>(node)) {
454         auto parent = node->GetParent();
455         if (parent && parent->GetTag() == V2::COMMON_VIEW_ETS_TAG) {
456             node = parent;
457         } else {
458             node = node->GetFrameChildByIndexWithoutExpanded(0);
459         }
460     }
461 
462     return reinterpret_cast<ArkUINodeHandle>(OHOS::Ace::AceType::RawPtr(node));
463 }
464 
GetFrameNodeByKey(ArkUI_CharPtr key)465 ArkUINodeHandle GetFrameNodeByKey(ArkUI_CharPtr key)
466 {
467     auto node = NG::Inspector::GetFrameNodeByKey(key, true);
468     return reinterpret_cast<ArkUINodeHandle>(OHOS::Ace::AceType::RawPtr(node));
469 }
470 
GetAttachedFrameNodeById(ArkUI_CharPtr key)471 ArkUINodeHandle GetAttachedFrameNodeById(ArkUI_CharPtr key)
472 {
473     auto pipeline = NG::PipelineContext::GetCurrentContextSafely();
474     if (pipeline && !pipeline->CheckThreadSafe()) {
475         LOGF("GetAttachedNodeHandleById doesn't run on UI thread");
476         abort();
477     }
478     auto node = ElementRegister::GetInstance()->GetAttachedFrameNodeById(key);
479     CHECK_NULL_RETURN(node, nullptr);
480     return reinterpret_cast<ArkUINodeHandle>(OHOS::Ace::AceType::RawPtr(node));
481 }
482 
GetFirstUINode(ArkUINodeHandle node)483 ArkUINodeHandle GetFirstUINode(ArkUINodeHandle node)
484 {
485     auto* currentNode = reinterpret_cast<UINode*>(node);
486     CHECK_NULL_RETURN(currentNode, nullptr);
487     auto child = currentNode->GetFirstChild();
488     return reinterpret_cast<ArkUINodeHandle>(AceType::RawPtr(child));
489 }
490 
GetLayoutSize(ArkUINodeHandle node,ArkUI_Int32 * size)491 void GetLayoutSize(ArkUINodeHandle node, ArkUI_Int32* size)
492 {
493     auto* currentNode = reinterpret_cast<FrameNode*>(node);
494     CHECK_NULL_VOID(currentNode);
495     auto renderContext = currentNode->GetRenderContext();
496     CHECK_NULL_VOID(renderContext);
497     auto rectSize = renderContext->GetPaintRectWithoutTransform().GetSize();
498     size[0] = rectSize.Width();
499     size[1] = rectSize.Height();
500 }
501 
GetLayoutPositionWithoutMargin(ArkUINodeHandle node)502 ArkUI_Float32* GetLayoutPositionWithoutMargin(ArkUINodeHandle node)
503 {
504     auto* currentNode = reinterpret_cast<FrameNode*>(node);
505     CHECK_NULL_RETURN(currentNode, nullptr);
506     auto rect = currentNode->GetPaintRectWithTransform();
507     ArkUI_Float32* ret = new ArkUI_Float32[2];
508     ret[0] = rect.GetX();
509     ret[1] = rect.GetY();
510     return ret;
511 }
512 
SetSystemColorModeChangeEvent(ArkUINodeHandle node,void * userData,void * onColorModeChange)513 ArkUI_Int32 SetSystemColorModeChangeEvent(ArkUINodeHandle node, void* userData, void* onColorModeChange)
514 {
515     auto* frameNode = reinterpret_cast<FrameNode*>(node);
516     CHECK_NULL_RETURN(frameNode, 0);
517     auto onColorChange = [userData, onColorModeChange](int32_t colorMode) {
518         using FuncType = float (*)(int32_t, void*);
519         FuncType func = reinterpret_cast<FuncType>(onColorModeChange);
520         func(colorMode, userData);
521     };
522     ViewAbstract::SetSystemColorModeChangeEvent(frameNode, onColorChange);
523     return 0;
524 }
525 
ResetSystemColorModeChangeEvent(ArkUINodeHandle node)526 void ResetSystemColorModeChangeEvent(ArkUINodeHandle node)
527 {
528     auto* frameNode = reinterpret_cast<FrameNode*>(node);
529     CHECK_NULL_VOID(frameNode);
530     ViewAbstract::SetSystemColorModeChangeEvent(frameNode, nullptr);
531 }
532 
SetDrawCompleteEvent(ArkUINodeHandle node,void * userData,void (* onDraw)(void *))533 ArkUI_Int32 SetDrawCompleteEvent(ArkUINodeHandle node, void* userData, void (*onDraw)(void*))
534 {
535     auto* frameNode = reinterpret_cast<FrameNode*>(node);
536     CHECK_NULL_RETURN(frameNode, -1);
537     auto onDrawCallBack = [userData, onDraw]() {
538         if (!onDraw) {
539             return;
540         }
541         onDraw(userData);
542     };
543     ViewAbstract::SetDrawCompleteEvent(frameNode, std::move(onDrawCallBack));
544     return 0;
545 }
546 
ResetDrawCompleteEvent(ArkUINodeHandle node)547 ArkUI_Int32 ResetDrawCompleteEvent(ArkUINodeHandle node)
548 {
549     auto* frameNode = reinterpret_cast<FrameNode*>(node);
550     CHECK_NULL_RETURN(frameNode, -1);
551     ViewAbstract::SetDrawCompleteEvent(frameNode, nullptr);
552     return 0;
553 }
554 
SetLayoutEvent(ArkUINodeHandle node,void * userData,void (* onLayout)(void *))555 ArkUI_Int32 SetLayoutEvent(ArkUINodeHandle node, void* userData, void (*onLayout)(void*))
556 {
557     auto* frameNode = reinterpret_cast<FrameNode*>(node);
558     CHECK_NULL_RETURN(frameNode, -1);
559     auto onLayoutCallBack = [userData, onLayout]() {
560         if (!onLayout) {
561             return;
562         }
563         onLayout(userData);
564     };
565     ViewAbstract::SetLayoutEvent(frameNode, std::move(onLayoutCallBack));
566     return 0;
567 }
568 
ResetLayoutEvent(ArkUINodeHandle node)569 ArkUI_Int32 ResetLayoutEvent(ArkUINodeHandle node)
570 {
571     auto* frameNode = reinterpret_cast<FrameNode*>(node);
572     CHECK_NULL_RETURN(frameNode, -1);
573     ViewAbstract::SetLayoutEvent(frameNode, nullptr);
574     return 0;
575 }
576 
RequestFocus(ArkUINodeHandle node)577 ArkUI_Int32 RequestFocus(ArkUINodeHandle node)
578 {
579     auto* frameNode = reinterpret_cast<FrameNode*>(node);
580     CHECK_NULL_RETURN(frameNode, ARKUI_ERROR_CODE_FOCUS_NON_EXISTENT);
581     return static_cast<ArkUI_Int32>(ViewAbstract::RequestFocus(frameNode));
582 }
583 
ClearFocus(ArkUI_Int32 instanceId)584 void ClearFocus(ArkUI_Int32 instanceId)
585 {
586     ViewAbstract::ClearFocus(instanceId);
587 }
588 
FocusActivate(ArkUI_Int32 instanceId,bool isActive,bool isAutoInactive)589 void FocusActivate(ArkUI_Int32 instanceId, bool isActive, bool isAutoInactive)
590 {
591     ViewAbstract::FocusActivate(instanceId, isActive, isAutoInactive);
592 }
593 
SetAutoFocusTransfer(ArkUI_Int32 instanceId,bool isAutoFocusTransfer)594 void SetAutoFocusTransfer(ArkUI_Int32 instanceId, bool isAutoFocusTransfer)
595 {
596     ViewAbstract::SetAutoFocusTransfer(instanceId, isAutoFocusTransfer);
597 }
598 
SetCrossLanguageOptions(ArkUINodeHandle node,bool attributeSetting)599 ArkUI_Int32 SetCrossLanguageOptions(ArkUINodeHandle node, bool attributeSetting)
600 {
601     auto* currentNode = reinterpret_cast<UINode*>(node);
602     CHECK_NULL_RETURN(currentNode, ERROR_CODE_PARAM_INVALID);
603     static const std::vector<const char*> nodeTypeArray = {
604         OHOS::Ace::V2::SCROLL_ETS_TAG,
605     };
606     auto pos = std::find(nodeTypeArray.begin(), nodeTypeArray.end(), currentNode->GetTag());
607     if (pos == nodeTypeArray.end()) {
608         return ERROR_CODE_PARAM_INVALID;
609     }
610     currentNode->SetIsCrossLanguageAttributeSetting(attributeSetting);
611     return ERROR_CODE_NO_ERROR;
612 }
613 
GetCrossLanguageOptions(ArkUINodeHandle node)614 ArkUI_Bool GetCrossLanguageOptions(ArkUINodeHandle node)
615 {
616     auto* currentNode = reinterpret_cast<UINode*>(node);
617     CHECK_NULL_RETURN(currentNode, false);
618     return currentNode->isCrossLanguageAttributeSetting();
619 }
620 
CheckIfCanCrossLanguageAttributeSetting(ArkUINodeHandle node)621 ArkUI_Bool CheckIfCanCrossLanguageAttributeSetting(ArkUINodeHandle node)
622 {
623     auto* currentNode = reinterpret_cast<UINode*>(node);
624     CHECK_NULL_RETURN(currentNode, false);
625     return currentNode -> IsCNode() ? currentNode->isCrossLanguageAttributeSetting() : false;
626 }
627 
SetSystemFontStyleChangeEvent(ArkUINodeHandle node,void * userData,void * onFontStyleChange)628 ArkUI_Int32 SetSystemFontStyleChangeEvent(ArkUINodeHandle node, void* userData, void* onFontStyleChange)
629 {
630     auto* frameNode = reinterpret_cast<FrameNode*>(node);
631     CHECK_NULL_RETURN(frameNode, 0);
632     auto onFontChange = [userData, onFontStyleChange](float fontSize, float fontWeight) {
633         ArkUISystemFontStyleEvent fontStyle = new ArkUI_SystemFontStyleEvent();
634         fontStyle->fontSize = fontSize;
635         fontStyle->fontWeight = fontWeight;
636         using FuncType = float (*)(ArkUISystemFontStyleEvent, void*);
637         FuncType func = reinterpret_cast<FuncType>(onFontStyleChange);
638         func(fontStyle, userData);
639     };
640     ViewAbstract::SetSystemFontChangeEvent(frameNode, onFontChange);
641     return 0;
642 }
643 
ResetSystemFontStyleChangeEvent(ArkUINodeHandle node)644 void ResetSystemFontStyleChangeEvent(ArkUINodeHandle node)
645 {
646     auto* frameNode = reinterpret_cast<FrameNode*>(node);
647     CHECK_NULL_VOID(frameNode);
648     ViewAbstract::SetSystemFontChangeEvent(frameNode, nullptr);
649 }
650 
GetCustomPropertyCapiByKey(ArkUINodeHandle node,ArkUI_CharPtr key,char ** value,ArkUI_Uint32 * size)651 ArkUI_Uint32 GetCustomPropertyCapiByKey(ArkUINodeHandle node, ArkUI_CharPtr key, char** value, ArkUI_Uint32* size)
652 {
653     auto* frameNode = reinterpret_cast<FrameNode*>(node);
654     CHECK_NULL_RETURN(frameNode, 0);
655     std::string capiCustomProperty;
656     if (!frameNode->GetCapiCustomProperty(key, capiCustomProperty)) {
657         return 0;
658     }
659     *size = capiCustomProperty.size();
660     *value = new char[*size + 1];
661     capiCustomProperty.copy(*value, *size);
662     (*value)[*size] = '\0';
663     return 1;
664 }
665 
FreeCustomPropertyCharPtr(char * value,ArkUI_Uint32 size)666 void FreeCustomPropertyCharPtr(char* value, ArkUI_Uint32 size)
667 {
668     CHECK_NULL_VOID(value);
669     delete[] value;
670     value = nullptr;
671 }
672 
SetCustomPropertyModiferByKey(ArkUINodeHandle node,void * callback,void * getCallback)673 void SetCustomPropertyModiferByKey(ArkUINodeHandle node, void* callback, void* getCallback)
674 {
675     auto* frameNode = reinterpret_cast<FrameNode*>(node);
676     CHECK_NULL_VOID(frameNode);
677     std::function<bool()>* func = reinterpret_cast<std::function<bool()>*>(callback);
678     std::function<std::string(const std::string&)>* getFunc =
679         reinterpret_cast<std::function<std::string(const std::string&)>*>(getCallback);
680     frameNode->SetJSCustomProperty(*func, *getFunc);
681 }
682 
AddCustomProperty(ArkUINodeHandle node,ArkUI_CharPtr key,ArkUI_CharPtr value)683 void AddCustomProperty(ArkUINodeHandle node, ArkUI_CharPtr key, ArkUI_CharPtr value)
684 {
685     auto* uiNode = reinterpret_cast<UINode*>(node);
686     CHECK_NULL_VOID(uiNode);
687     auto pipeline = uiNode->GetContextRefPtr();
688     if (pipeline && !pipeline->CheckThreadSafe()) {
689         LOGW("AddCustomProperty doesn't run on UI thread");
690         return;
691     }
692     ViewAbstract::AddCustomProperty(uiNode, key, value);
693 }
694 
RemoveCustomProperty(ArkUINodeHandle node,ArkUI_CharPtr key)695 void RemoveCustomProperty(ArkUINodeHandle node, ArkUI_CharPtr key)
696 {
697     auto* uiNode = reinterpret_cast<UINode*>(node);
698     CHECK_NULL_VOID(uiNode);
699     auto pipeline = uiNode->GetContextRefPtr();
700     if (pipeline && !pipeline->CheckThreadSafe()) {
701         LOGW("RemoveCustomProperty doesn't run on UI thread");
702         return;
703     }
704     ViewAbstract::RemoveCustomProperty(uiNode, key);
705 }
706 
GetCurrentPageRootNode(ArkUINodeHandle node)707 ArkUINodeHandle GetCurrentPageRootNode(ArkUINodeHandle node)
708 {
709     auto uiNode = reinterpret_cast<UINode*>(node);
710     CHECK_NULL_RETURN(uiNode, nullptr);
711     auto rootNode = uiNode->GetCurrentPageRootNode();
712     return reinterpret_cast<ArkUINodeHandle>(OHOS::Ace::AceType::RawPtr(rootNode));
713 }
714 
GetNodeTag(ArkUINodeHandle node)715 ArkUI_Int32 GetNodeTag(ArkUINodeHandle node)
716 {
717     auto uiNode = reinterpret_cast<UINode*>(node);
718     CHECK_NULL_RETURN(uiNode, 0);
719     return uiNode->IsCNode();
720 }
721 
GetActiveChildrenInfo(ArkUINodeHandle handle,ArkUINodeHandle ** items,ArkUI_Int32 * size)722 void GetActiveChildrenInfo(ArkUINodeHandle handle, ArkUINodeHandle** items, ArkUI_Int32* size)
723 {
724     auto* frameNode = reinterpret_cast<FrameNode*>(handle);
725     CHECK_NULL_VOID(frameNode);
726     auto childList = frameNode->GetActiveChildren();
727     *size = childList.size();
728     *items = new ArkUINodeHandle[*size];
729     int32_t i = 0;
730     for (auto& child : childList) {
731         (*items)[i++] = reinterpret_cast<ArkUINodeHandle>(OHOS::Ace::AceType::RawPtr(child));
732     }
733 }
734 
SetKeyProcessingMode(ArkUI_Int32 instanceId,ArkUI_Int32 mode)735 void SetKeyProcessingMode(ArkUI_Int32 instanceId, ArkUI_Int32 mode)
736 {
737     auto container = Container::GetContainer(instanceId);
738     CHECK_NULL_VOID(container);
739     auto delegate = EngineHelper::GetDelegateByContainer(container);
740     CHECK_NULL_VOID(delegate);
741     delegate->SetKeyProcessingMode(mode);
742 }
743 
GetCustomProperty(ArkUINodeHandle node,ArkUI_CharPtr key,char ** value)744 void GetCustomProperty(ArkUINodeHandle node, ArkUI_CharPtr key, char** value)
745 {
746     auto* frameNode = reinterpret_cast<FrameNode*>(node);
747     CHECK_NULL_VOID(frameNode);
748     std::string capiCustomProperty;
749     if (frameNode->IsCNode()) {
750         frameNode->GetCapiCustomProperty(key, capiCustomProperty);
751     } else {
752         frameNode->GetJSCustomProperty(key, capiCustomProperty);
753     }
754     auto size = capiCustomProperty.size();
755     *value = new char[size + 1];
756     capiCustomProperty.copy(*value, size);
757     (*value)[size] = '\0';
758 }
759 
GetWindowInfoByNode(ArkUINodeHandle node,char ** name)760 ArkUI_Int32 GetWindowInfoByNode(ArkUINodeHandle node, char** name)
761 {
762     auto* frameNode = reinterpret_cast<FrameNode*>(node);
763     CHECK_NULL_RETURN(frameNode, OHOS::Ace::ERROR_CODE_PARAM_INVALID);
764     if (!frameNode->IsOnMainTree()) {
765         return OHOS::Ace::ERROR_CODE_NATIVE_IMPL_NODE_NOT_ON_MAIN_TREE;
766     }
767     auto context = frameNode->GetAttachedContext();
768     CHECK_NULL_RETURN(context, OHOS::Ace::ERROR_CODE_NATIVE_IMPL_NODE_NOT_ON_MAIN_TREE);
769     if (!context->CheckThreadSafe()) {
770         LOGF("GetWindowInfoByNode doesn't run on UI thread");
771         abort();
772     }
773     auto window = context->GetWindow();
774     CHECK_NULL_RETURN(window, OHOS::Ace::ERROR_CODE_NATIVE_IMPL_NODE_NOT_ON_MAIN_TREE);
775     std::string windowName = window->GetWindowName();
776     size_t nameSize = windowName.size();
777     *name = new char[nameSize + 1];
778     windowName.copy(*name, nameSize);
779     (*name)[nameSize] = '\0';
780     return OHOS::Ace::ERROR_CODE_NO_ERROR;
781 }
782 
783 namespace NodeModifier {
GetFrameNodeModifier()784 const ArkUIFrameNodeModifier* GetFrameNodeModifier()
785 {
786     static const ArkUIFrameNodeModifier modifier = { IsModifiable, CreateFrameNode, InvalidateInFrameNode,
787         AppendChildInFrameNode, InsertChildAfterInFrameNode, RemoveChildInFrameNode, ClearChildrenInFrameNode,
788         GetChildrenCount, GetChild, GetFirstChildIndexWithoutExpand, GetLastChildIndexWithoutExpand,
789         GetFirst, GetNextSibling, GetPreviousSibling, GetParent, GetIdByNodePtr,
790         PropertyUpdate, GetLast, GetPositionToParent, GetPositionToScreen, GetPositionToWindow,
791         GetPositionToParentWithTransform, GetPositionToScreenWithTransform, GetPositionToWindowWithTransform,
792         GetMeasuredSize, GetLayoutPosition, GetInspectorId, GetNodeType, IsVisible, IsAttached, GetInspectorInfo,
793         GetFrameNodeById, GetFrameNodeByUniqueId, GetFrameNodeByKey, GetAttachedFrameNodeById, GetFirstUINode,
794         GetLayoutSize, GetLayoutPositionWithoutMargin, SetSystemColorModeChangeEvent, ResetSystemColorModeChangeEvent,
795         SetSystemFontStyleChangeEvent, ResetSystemFontStyleChangeEvent, GetCustomPropertyCapiByKey,
796         SetCustomPropertyModiferByKey, AddCustomProperty, RemoveCustomProperty, FreeCustomPropertyCharPtr,
797         GetCurrentPageRootNode, GetNodeTag, GetActiveChildrenInfo, GetCustomProperty, SetDrawCompleteEvent,
798         ResetDrawCompleteEvent, SetLayoutEvent, ResetLayoutEvent, RequestFocus, ClearFocus, FocusActivate,
799         SetAutoFocusTransfer, GetWindowInfoByNode, SetCrossLanguageOptions, GetCrossLanguageOptions,
800         CheckIfCanCrossLanguageAttributeSetting, SetKeyProcessingMode };
801     return &modifier;
802 }
803 
GetCJUIFrameNodeModifier()804 const CJUIFrameNodeModifier* GetCJUIFrameNodeModifier()
805 {
806     static const CJUIFrameNodeModifier modifier = { IsModifiable, CreateFrameNode, InvalidateInFrameNode,
807         AppendChildInFrameNode, InsertChildAfterInFrameNode, RemoveChildInFrameNode, ClearChildrenInFrameNode,
808         GetChildrenCount, GetChild, GetFirst, GetNextSibling, GetPreviousSibling, GetParent, GetIdByNodePtr,
809         PropertyUpdate, GetLast, GetPositionToParent, GetPositionToScreen, GetPositionToWindow,
810         GetPositionToParentWithTransform, GetPositionToScreenWithTransform, GetPositionToWindowWithTransform,
811         GetMeasuredSize, GetLayoutPosition, GetInspectorId, GetNodeType, IsVisible, IsAttached, GetInspectorInfo,
812         GetFrameNodeById, GetFrameNodeByUniqueId, GetFrameNodeByKey, GetFirstUINode, GetLayoutSize,
813         GetLayoutPositionWithoutMargin };
814     return &modifier;
815 }
816 } // namespace NodeModifier
817 } // namespace OHOS::Ace::NG
818