1 /*
2 * Copyright (c) 2024-2025 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
16 #include "interfaces/inner_api/ace_kit/src/view/frame_node_impl.h"
17
18 #include "base/geometry/ng/rect_t.h"
19 #include "ui/base/ace_type.h"
20 #include "ui/base/referenced.h"
21 #include "ui/base/utils/utils.h"
22 #include "ui/properties/ng/measure_property.h"
23 #include "ui/view/frame_node.h"
24 #include "ui/view/pattern.h"
25 #include "ui/view/ui_context.h"
26
27 #include "core/components_ng/base/view_abstract.h"
28 #include "core/components_ng/property/calc_length.h"
29 #include "core/components_ng/property/layout_constraint.h"
30 #include "core/components_ng/property/property.h"
31 #include "core/interfaces/arkoala/arkoala_api.h"
32 #include "core/pipeline_ng/pipeline_context.h"
33
34 namespace OHOS::Ace::Kit {
GetFrameNode(ArkUINodeHandle node)35 RefPtr<FrameNode> FrameNode::GetFrameNode(ArkUINodeHandle node)
36 {
37 auto* frameNode = reinterpret_cast<NG::FrameNode*>(node);
38 CHECK_NULL_RETURN(frameNode, nullptr);
39 auto kitNode = frameNode->GetKitNode();
40 if (!kitNode) {
41 kitNode = MakeRefPtr<FrameNodeImpl>(Claim(frameNode));
42 }
43 return kitNode;
44 }
45
FrameNodeImpl(const RefPtr<AceNode> & node,const RefPtr<Pattern> & pattern)46 FrameNodeImpl::FrameNodeImpl(const RefPtr<AceNode>& node, const RefPtr<Pattern>& pattern)
47 : frameNode_(AceType::RawPtr(node)), pattern_(pattern)
48 {
49 // Need temporary save strong pointer to AceNode in the FrameNodeImpl.
50 // That guaranty that AceNode will not be destroyed since there is strong pointer to it.
51 // Need to keep this temporary strong pointer until ownership of AceNode will be moved ace_engine
52 // with ViewStackProcessor::Push() call or FrameNodeImpl::AddChild() call or other calls like this.
53 // Call FrameNodeImpl::MoveOwnershipAndGetAceNode() when you need put AceNode to ace_engine.
54 PushAceNode(node);
55 }
56
FrameNodeImpl(const RefPtr<AceNode> & node)57 FrameNodeImpl::FrameNodeImpl(const RefPtr<AceNode>& node) : frameNode_(AceType::RawPtr(node))
58 {
59 // The same reason of this call as above
60 PushAceNode(node);
61 }
62
63 // This constructor should be called for AceNode that already owned by ace_engine
64 // so PushAceNode() is not called here.
FrameNodeImpl(AceNode * node)65 FrameNodeImpl::FrameNodeImpl(AceNode* node) : frameNode_(node) {}
66
~FrameNodeImpl()67 FrameNodeImpl::~FrameNodeImpl()
68 {
69 Reset();
70 }
71
Reset()72 void FrameNodeImpl::Reset()
73 {
74 frameNode_ = nullptr;
75 nodeTempRef_.Reset();
76 pattern_.Reset();
77 }
78
InitializePatternAndContext()79 void FrameNodeImpl::InitializePatternAndContext()
80 {
81 CHECK_NULL_VOID(frameNode_);
82 frameNode_->InitializePatternAndContext();
83 CHECK_NULL_VOID(pattern_);
84 property_ = pattern_->CreateProperty();
85 CHECK_NULL_VOID(property_);
86 property_->SetHost(WeakClaim(this));
87 }
88
PushAceNode(const RefPtr<AceNode> & node)89 void FrameNodeImpl::PushAceNode(const RefPtr<AceNode>& node)
90 {
91 nodeTempRef_ = node;
92 }
93
PopAceNode()94 RefPtr<AceNode> FrameNodeImpl::PopAceNode()
95 {
96 RefPtr<AceNode> node;
97 node.Swap(nodeTempRef_);
98 return node;
99 }
100
MoveOwnershipAndGetAceNode()101 RefPtr<AceNode> FrameNodeImpl::MoveOwnershipAndGetAceNode()
102 {
103 auto aceNode = PopAceNode();
104 if (aceNode) {
105 aceNode->SetKitNode(AceType::Claim(this));
106 } else {
107 aceNode = GetAceNode();
108 }
109 return aceNode;
110 }
111
GetAceNode() const112 RefPtr<AceNode> FrameNodeImpl::GetAceNode() const
113 {
114 return AceType::Claim(frameNode_);
115 }
116
GetAceNodePtr()117 AceNode* FrameNodeImpl::GetAceNodePtr()
118 {
119 return frameNode_;
120 }
121
Measure(const Kit::LayoutConstraintInfo & parentContraint)122 void FrameNodeImpl::Measure(const Kit::LayoutConstraintInfo& parentContraint)
123 {
124 CHECK_NULL_VOID(frameNode_);
125 std::optional<NG::LayoutConstraintF> constraint = std::make_optional<NG::LayoutConstraintF>();
126 //minWidth
127 constraint->minSize.SetWidth(parentContraint.minWidth);
128 //minHeight
129 constraint->minSize.SetHeight(parentContraint.minHeight);
130 //maxWidth
131 constraint->maxSize.SetWidth(parentContraint.maxWidth);
132 //maxHeight
133 constraint->maxSize.SetHeight(parentContraint.maxHeight);
134 //minWidth == maxWidth
135 if (parentContraint.minWidth == parentContraint.maxWidth) {
136 constraint->selfIdealSize.SetWidth(parentContraint.minWidth);
137 }
138 //minHeight == maxHeight
139 if (parentContraint.minHeight == parentContraint.maxHeight) {
140 constraint->selfIdealSize.SetHeight(parentContraint.minHeight);
141 }
142 //percentReferenceWidth
143 constraint->percentReference.SetWidth(parentContraint.percentReferWidth);
144 //percentReferenceHeight
145 constraint->percentReference.SetHeight(parentContraint.percentReferHeight);
146 frameNode_->SetActive(true);
147 frameNode_->Measure(constraint);
148 }
149
Layout()150 void FrameNodeImpl::Layout()
151 {
152 CHECK_NULL_VOID(frameNode_);
153 frameNode_->Layout();
154 }
155
GetLayoutProperty()156 RefPtr<Ace::NG::LayoutProperty> FrameNodeImpl::GetLayoutProperty()
157 {
158 CHECK_NULL_RETURN(frameNode_, nullptr);
159 return frameNode_->GetLayoutProperty();
160 }
161
GetPattern()162 RefPtr<Pattern> FrameNodeImpl::GetPattern()
163 {
164 return pattern_;
165 }
166
GetProperty()167 RefPtr<Property> FrameNodeImpl::GetProperty()
168 {
169 return property_;
170 }
171
GetLayoutWrapper()172 NG::LayoutWrapper* FrameNodeImpl::GetLayoutWrapper()
173 {
174 CHECK_NULL_RETURN(frameNode_, nullptr);
175 return frameNode_;
176 }
177
MeasureChildren()178 void FrameNodeImpl::MeasureChildren()
179 {
180 CHECK_NULL_VOID(frameNode_);
181 auto layoutConstraint = frameNode_->GetLayoutProperty()->CreateChildConstraint();
182 for (auto& child : frameNode_->GetAllChildrenWithBuild()) {
183 child->Measure(layoutConstraint);
184 }
185 }
186
LayoutChildren()187 void FrameNodeImpl::LayoutChildren()
188 {
189 CHECK_NULL_VOID(frameNode_);
190 for (auto& child : frameNode_->GetAllChildrenWithBuild()) {
191 child->Layout();
192 }
193 }
194
GetUIContext() const195 RefPtr<UIContext> FrameNodeImpl::GetUIContext() const
196 {
197 CHECK_NULL_RETURN(frameNode_, nullptr);
198 auto* pipeline = frameNode_->GetContextWithCheck();
199 CHECK_NULL_RETURN(pipeline, nullptr);
200 return pipeline->GetUIContext();
201 }
202
GetHandle()203 NodeHandle FrameNodeImpl::GetHandle()
204 {
205 return reinterpret_cast<NodeHandle>(frameNode_);
206 }
207
AddChild(const RefPtr<FrameNode> & child)208 void FrameNodeImpl::AddChild(const RefPtr<FrameNode>& child)
209 {
210 CHECK_NULL_VOID(frameNode_);
211 auto childNode = AceType::DynamicCast<FrameNodeImpl>(child);
212 CHECK_NULL_VOID(childNode);
213 auto childNodePtr = childNode->MoveOwnershipAndGetAceNode();
214 CHECK_NULL_VOID(childNodePtr);
215 frameNode_->AddChild(childNodePtr);
216 }
217
GetChildren()218 std::list<RefPtr<FrameNode>> FrameNodeImpl::GetChildren()
219 {
220 std::list<RefPtr<FrameNode>> children;
221 CHECK_NULL_RETURN(frameNode_, children);
222 for (auto& child : frameNode_->GetAllChildrenWithBuild(false)) {
223 auto node = child->GetHostNode();
224 if (!node) {
225 continue;
226 }
227 auto kitNode = node->GetKitNode();
228 if (!kitNode) {
229 kitNode = MakeRefPtr<FrameNodeImpl>(AceType::RawPtr(node));
230 node->SetKitNode(kitNode);
231 }
232 children.emplace_back(kitNode);
233 }
234 return children;
235 }
236
MarkDirtyNode(NG::PropertyChangeFlag flag)237 void FrameNodeImpl::MarkDirtyNode(NG::PropertyChangeFlag flag)
238 {
239 CHECK_NULL_VOID(frameNode_);
240 frameNode_->MarkDirtyNode(flag);
241 }
242
RemoveChild(const RefPtr<FrameNode> & child)243 void FrameNodeImpl::RemoveChild(const RefPtr<FrameNode> &child)
244 {
245 CHECK_NULL_VOID(frameNode_);
246 auto childNode = AceType::DynamicCast<FrameNodeImpl>(child);
247 auto* childNodePtr = childNode->GetAceNodePtr();
248 CHECK_NULL_VOID(childNodePtr);
249 frameNode_->RemoveChild(AceType::Claim(childNodePtr));
250 }
251
GetTag() const252 std::string FrameNodeImpl::GetTag() const
253 {
254 CHECK_NULL_RETURN(frameNode_, "");
255 return frameNode_->GetTag();
256 }
257
GetId() const258 int32_t FrameNodeImpl::GetId() const
259 {
260 CHECK_NULL_RETURN(frameNode_, -1);
261 return frameNode_->GetId();
262 }
263
SetAICallerHelper(const std::shared_ptr<AICallerHelper> & aiCallerHelper)264 void FrameNodeImpl::SetAICallerHelper(const std::shared_ptr<AICallerHelper>& aiCallerHelper)
265 {
266 CHECK_NULL_VOID(frameNode_);
267 frameNode_->SetAICallerHelper(aiCallerHelper);
268 }
269
SetMeasureCallback(const std::function<void (RefPtr<FrameNode>)> & measureCallback)270 void FrameNodeImpl::SetMeasureCallback(const std::function<void(RefPtr<FrameNode>)>& measureCallback)
271 {
272 CHECK_NULL_VOID(frameNode_);
273 auto onMeasureCallback = [weakNode = WeakClaim(frameNode_), measureCallback](int32_t nodeId) {
274 auto frameNode = weakNode.Upgrade();
275 CHECK_NULL_VOID(frameNode);
276 RefPtr<FrameNode> node = frameNode->GetKitNode();
277 if (!node) {
278 node = AceType::MakeRefPtr<FrameNodeImpl>(frameNode);
279 }
280 measureCallback(node);
281 };
282 frameNode_->SetMeasureCallback(std::move(onMeasureCallback));
283 }
284
GetMeasureWidth()285 int32_t FrameNodeImpl::GetMeasureWidth()
286 {
287 CHECK_NULL_RETURN(frameNode_, 0);
288 return frameNode_->GetGeometryNode()->GetFrameSize().Width();
289 }
290
GetMeasureHeight()291 int32_t FrameNodeImpl::GetMeasureHeight()
292 {
293 CHECK_NULL_RETURN(frameNode_, 0);
294 return frameNode_->GetGeometryNode()->GetFrameSize().Height();
295 }
296
GetParentHandle()297 NodeHandle FrameNodeImpl::GetParentHandle()
298 {
299 CHECK_NULL_RETURN(frameNode_, nullptr);
300 auto* frameNode = reinterpret_cast<AceNode*>(frameNode_);
301 CHECK_NULL_RETURN(frameNode, nullptr);
302 auto parent = frameNode->GetParent();
303 while (parent != nullptr && !AceType::InstanceOf<NG::FrameNode>(parent)) {
304 parent = parent->GetParent();
305 }
306 CHECK_NULL_RETURN(parent, nullptr);
307 return reinterpret_cast<NodeHandle>(OHOS::Ace::AceType::RawPtr(parent));
308 }
309
SetOnNodeDestroyCallback(const std::function<void (RefPtr<FrameNode>)> & destroyCallback)310 void FrameNodeImpl::SetOnNodeDestroyCallback(const std::function<void(RefPtr<FrameNode>)>& destroyCallback)
311 {
312 CHECK_NULL_VOID(frameNode_);
313 auto onDestroyCallback = [weakNode = WeakClaim(frameNode_), destroyCallback](int32_t nodeId) {
314 auto frameNode = weakNode.Upgrade();
315 CHECK_NULL_VOID(frameNode);
316 RefPtr<FrameNode> node = frameNode->GetKitNode();
317 if (!node) {
318 node = AceType::MakeRefPtr<FrameNodeImpl>(frameNode);
319 }
320 destroyCallback(node);
321 };
322 frameNode_->SetOnNodeDestroyCallback(std::move(onDestroyCallback));
323 }
324
SetConfigurationUpdateCallback(const std::function<void (const ConfigurationChange & configurationChange)> && callback)325 void FrameNodeImpl::SetConfigurationUpdateCallback(
326 const std::function<void(const ConfigurationChange& configurationChange)>&& callback)
327 {
328 frameNode_->SetConfigurationModeUpdateCallback(std::move(callback));
329 }
330
AddExtraCustomProperty(const std::string & key,void * extraData)331 void FrameNodeImpl::AddExtraCustomProperty(const std::string& key, void* extraData)
332 {
333 CHECK_NULL_VOID(frameNode_);
334 frameNode_->AddExtraCustomProperty(key, extraData);
335 }
336
GetExtraCustomProperty(const std::string & key) const337 void* FrameNodeImpl::GetExtraCustomProperty(const std::string& key) const
338 {
339 CHECK_NULL_RETURN(frameNode_, nullptr);
340 return frameNode_->GetExtraCustomProperty(key);
341 }
342
SetClipEdge(bool isClip)343 void FrameNodeImpl::SetClipEdge(bool isClip)
344 {
345 CHECK_NULL_VOID(frameNode_);
346 NG::ViewAbstract::SetClipEdge(frameNode_, isClip);
347 }
348
SetPadding(const NG::PaddingPropertyT<NG::CalcLength> & value)349 void FrameNodeImpl::SetPadding(const NG::PaddingPropertyT<NG::CalcLength>& value)
350 {
351 CHECK_NULL_VOID(frameNode_);
352 NG::ViewAbstract::SetPadding(frameNode_, value);
353 }
354
SetSafeAreaPadding(const NG::CalcLength & value)355 void FrameNodeImpl::SetSafeAreaPadding(const NG::CalcLength& value)
356 {
357 CHECK_NULL_VOID(frameNode_);
358 NG::ViewAbstract::SetSafeAreaPadding(frameNode_, value);
359 }
360
ResetSafeAreaPadding()361 void FrameNodeImpl::ResetSafeAreaPadding()
362 {
363 CHECK_NULL_VOID(frameNode_);
364 NG::ViewAbstract::ResetSafeAreaPadding();
365 }
366
SetLinearGradient(const NG::Gradient & gradient)367 void FrameNodeImpl::SetLinearGradient(const NG::Gradient& gradient)
368 {
369 CHECK_NULL_VOID(frameNode_);
370 NG::ViewAbstract::SetLinearGradient(frameNode_, gradient);
371 }
372
SetLinearGradientBlur(const NG::LinearGradientBlurPara & blurPara)373 void FrameNodeImpl::SetLinearGradientBlur(const NG::LinearGradientBlurPara& blurPara)
374 {
375 CHECK_NULL_VOID(frameNode_);
376 NG::ViewAbstract::SetLinearGradientBlur(frameNode_, blurPara);
377 }
378
SetCompositingFilter(const OHOS::Rosen::Filter * compositingFilter)379 void FrameNodeImpl::SetCompositingFilter(const OHOS::Rosen::Filter* compositingFilter)
380 {
381 CHECK_NULL_VOID(frameNode_);
382 NG::ViewAbstract::SetCompositingFilter(frameNode_, compositingFilter);
383 }
384
ResetCompositingFilter()385 void FrameNodeImpl::ResetCompositingFilter()
386 {
387 CHECK_NULL_VOID(frameNode_);
388 NG::ViewAbstract::SetCompositingFilter(frameNode_, nullptr);
389 }
390
NeedAvoidContainerModal()391 bool FrameNodeImpl::NeedAvoidContainerModal()
392 {
393 CHECK_NULL_RETURN(frameNode_, false);
394 auto pipeline = frameNode_->GetContext();
395 CHECK_NULL_RETURN(pipeline, false);
396 auto avoidInfoMgr = pipeline->GetAvoidInfoManager();
397 CHECK_NULL_RETURN(avoidInfoMgr, false);
398 return avoidInfoMgr->NeedAvoidContainerModal();
399 }
400
GetContainerModalTitleHeight()401 int32_t FrameNodeImpl::GetContainerModalTitleHeight()
402 {
403 CHECK_NULL_RETURN(frameNode_, 0);
404 auto pipeline = frameNode_->GetContext();
405 CHECK_NULL_RETURN(pipeline, 0);
406 auto avoidInfoMgr = pipeline->GetAvoidInfoManager();
407 CHECK_NULL_RETURN(avoidInfoMgr, 0);
408 return avoidInfoMgr->GetContainerModalTitleHeight();
409 }
410
GetContainerModalButtonsOffset()411 NG::OffsetF FrameNodeImpl::GetContainerModalButtonsOffset()
412 {
413 NG::OffsetF offset = NG::OffsetF(0.0, 0.0);
414 CHECK_NULL_RETURN(frameNode_, offset);
415 auto pipeline = frameNode_->GetContext();
416 CHECK_NULL_RETURN(pipeline, offset);
417 auto avoidInfoMgr = pipeline->GetAvoidInfoManager();
418 CHECK_NULL_RETURN(avoidInfoMgr, offset);
419 Ace::NG::RectF containerModal;
420 Ace::NG::RectF buttonsRect;
421 auto isSuccess = avoidInfoMgr->GetContainerModalButtonsRect(containerModal, buttonsRect);
422 if (!isSuccess) {
423 return offset;
424 }
425 return buttonsRect.GetOffset();
426 }
427
GetContainerModalButtonsSize()428 NG::SizeF FrameNodeImpl::GetContainerModalButtonsSize()
429 {
430 NG::SizeF buttonsSize = NG::SizeF(0.0, 0.0);
431 CHECK_NULL_RETURN(frameNode_, buttonsSize);
432 auto pipeline = frameNode_->GetContext();
433 CHECK_NULL_RETURN(pipeline, buttonsSize);
434 auto avoidInfoMgr = pipeline->GetAvoidInfoManager();
435 CHECK_NULL_RETURN(avoidInfoMgr, buttonsSize);
436 Ace::NG::RectF containerModal;
437 Ace::NG::RectF buttonsRect;
438 auto isSuccess = avoidInfoMgr->GetContainerModalButtonsRect(containerModal, buttonsRect);
439 if (!isSuccess) {
440 return buttonsSize;
441 }
442 return NG::SizeF(buttonsRect.Width(), buttonsRect.Height());
443 }
444
GetParentGlobalOffsetDuringLayout()445 NG::OffsetF FrameNodeImpl::GetParentGlobalOffsetDuringLayout()
446 {
447 NG::OffsetF offset {};
448 CHECK_NULL_RETURN(frameNode_, offset);
449 offset = frameNode_->GetParentGlobalOffsetDuringLayout();
450 return offset;
451 }
452 } // namespace OHOS::Ace::Kit
453