• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_RENDER_NODE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_RENDER_NODE_H
18 
19 #include <list>
20 
21 #include "base/geometry/dimension.h"
22 #include "base/geometry/rect.h"
23 #include "base/geometry/matrix4.h"
24 #include "base/memory/ace_type.h"
25 #include "base/utils/macros.h"
26 #include "base/utils/system_properties.h"
27 #include "core/accessibility/accessibility_manager.h"
28 #include "core/animation/animatable_properties.h"
29 #include "core/animation/keyframe_animation.h"
30 #include "core/animation/property_animatable.h"
31 #include "core/common/draw_delegate.h"
32 #include "core/components/common/layout/align_declaration.h"
33 #include "core/components/common/layout/constants.h"
34 #include "core/components/common/layout/layout_param.h"
35 #include "core/components/common/properties/motion_path_option.h"
36 #include "core/components/common/properties/state_attributes.h"
37 #include "core/components/common/properties/text_style.h"
38 #include "core/components_v2/extensions/events/event_extensions.h"
39 #include "core/components_v2/inspector/inspector_node.h"
40 #include "core/event/axis_event.h"
41 #include "core/event/touch_event.h"
42 #include "core/gestures/drag_recognizer.h"
43 #include "core/pipeline/base/render_context.h"
44 #include "core/pipeline/base/render_layer.h"
45 #include "core/pipeline/pipeline_context.h"
46 
47 namespace OHOS::Ace {
48 
49 extern const Dimension FOCUS_BOUNDARY;
50 
51 class Component;
52 
53 // If no insertion location is specified, new child will be added to the end of children list by default.
54 constexpr int32_t DEFAULT_RENDER_NODE_SLOT = -1;
55 constexpr int32_t PRESS_DURATION = 100;
56 constexpr int32_t HOVER_DURATION = 250;
57 constexpr uint32_t FIND_MAX_COUNT = 64;
58 
59 using HoverAndPressCallback = std::function<void(const Color&)>;
60 using Rosen::RSNode;
61 
62 // RenderNode is the base class for different render backend, represent a render unit for render pipeline.
63 class ACE_EXPORT RenderNode : public PropertyAnimatable, public AnimatableProperties, public virtual AceType {
64     DECLARE_ACE_TYPE(RenderNode, PropertyAnimatable, AceType);
65 
66 public:
67     using OpacityCallback = std::function<void(uint8_t)>;
68     using SlipFactorSetting = std::function<void(double)>;
69     ~RenderNode() override = default;
70 
71     static void MarkTreeRender(const RefPtr<RenderNode>& root, bool& meetHole, bool needFlush);
72 
73     static void MarkWholeRender(const WeakPtr<RenderNode>& nodeWeak, bool needFlush);
74 
SetZIndex(int32_t zIndex)75     void SetZIndex(int32_t zIndex)
76     {
77         zIndex_ = zIndex;
78     }
79 
GetZIndex()80     int32_t GetZIndex() const
81     {
82         return zIndex_;
83     }
84 
SetIsPercentSize(bool isPercentSize)85     void SetIsPercentSize(bool isPercentSize)
86     {
87         isPercentSize_ = isPercentSize;
88     }
89 
GetIsPercentSize()90     bool GetIsPercentSize() const
91     {
92         return isPercentSize_;
93     }
94 
95     void AddChild(const RefPtr<RenderNode>& child, int32_t slot = DEFAULT_RENDER_NODE_SLOT);
96 
97     void RemoveChild(const RefPtr<RenderNode>& child);
98 
99     void MovePosition(int32_t slot);
100 
101     void ClearChildren();
102 
103     virtual void MoveWhenOutOfViewPort(bool hasEffect);
104 
105     bool IsPointInBox(const TouchEvent& point);
106     void Attach(const WeakPtr<PipelineContext>& context);
107 
108     // unmount from render tree
Unmount()109     void Unmount()
110     {
111         RefPtr<RenderNode> parent = parent_.Upgrade();
112         if (parent) {
113             parent->MarkNeedLayout();
114             parent->RemoveChild(AceType::Claim(this));
115         }
116     }
117 
118     // Update node with attr, style, event, method and so on.
119     // This method will call Update virtual function.
120     void UpdateAll(const RefPtr<Component>& component);
121 
AddToScene()122     virtual void AddToScene() {}
123 
124     virtual void Update(const RefPtr<Component>& component) = 0;
125 
126     // Called when page context attached, subclass can initialize object which needs page context.
OnAttachContext()127     virtual void OnAttachContext() {}
128 
FinishRender(const std::unique_ptr<DrawDelegate> & delegate,const Rect & dirty)129     virtual void FinishRender(const std::unique_ptr<DrawDelegate>& delegate, const Rect& dirty) {}
130 
131     virtual void UpdateTouchRect();
132 
133     void SetTouchRectList(std::vector<Rect>& touchRectList);
134     bool CompareTouchRect(const Rect& parentTouchRect, const Rect& childTouchRect);
135     void CompareTouchRectList(std::vector<Rect>& touchRectList,
136         const std::vector<Rect>& childTouchRectList, const std::vector<Rect>& parentTouchRectList);
137 
NeedLayout()138     bool NeedLayout() const
139     {
140         return needLayout_;
141     }
142 
SetNeedLayout(bool needLayout)143     void SetNeedLayout(bool needLayout)
144     {
145         needLayout_ = needLayout;
146     }
147 
148     void MarkNeedLayout(bool selfOnly = false, bool forceParent = false);
149 
150     /**
151      * \brief check in mark need layout progress if parent need layout again.
152      * \return true if need layout agagin.
153      */
CheckIfNeedLayoutAgain()154     virtual bool CheckIfNeedLayoutAgain()
155     {
156         return true;
157     }
158 
159     void MarkNeedPredictLayout();
160 
161     void OnLayout();
162 
163     // deadline : The remaining time until the next vsync. (unit: microsecond)
OnPredictLayout(int64_t deadline)164     virtual void OnPredictLayout(int64_t deadline) {}
165 
GetChildViewPort()166     virtual Size GetChildViewPort()
167     {
168         return viewPort_;
169     }
170 
171     // Called by parent to perform layout.
Layout(const LayoutParam & layoutParam)172     void Layout(const LayoutParam& layoutParam)
173     {
174         auto pipeline = context_.Upgrade();
175         if (!pipeline) {
176             LOGE("pipeline is null when layout");
177             return;
178         }
179 
180         bool dipScaleChange = !NearEqual(pipeline->GetDipScale(), dipScale_);
181         dipScale_ = pipeline->GetDipScale();
182         if (dipScaleChange || layoutParam_ != layoutParam) {
183             layoutParam_ = layoutParam;
184             layoutParamChanged_ = true;
185             SetNeedLayout(true);
186         }
187 
188         if (onChangeCallback_) {
189             onChangeCallback_();
190         }
191         OnLayout();
192     }
193 
194     // Called by parent to update layout param without PerformLayout.
SetLayoutParam(const LayoutParam & layoutParam)195     void SetLayoutParam(const LayoutParam& layoutParam)
196     {
197         if (layoutParam_ != layoutParam) {
198             layoutParam_ = layoutParam;
199             layoutParamChanged_ = true;
200             MarkNeedLayout();
201         }
202     }
203 
GetLayoutParam()204     const LayoutParam& GetLayoutParam() const
205     {
206         return layoutParam_;
207     }
208 
209     // Each subclass should override this function for actual layout operation.
210     virtual void PerformLayout() = 0;
211 
GetPosition()212     Offset GetPosition() const
213     {
214         return paintRect_.GetOffset();
215     }
216 
217     void SetPosition(const Offset& offset);
218 
SetAbsolutePosition(const Offset & offset)219     void SetAbsolutePosition(const Offset& offset)
220     {
221         SetPositionInternal(offset);
222     }
223 
GetLayoutSize()224     Size GetLayoutSize() const
225     {
226         return paintRect_.GetSize();
227     }
228 
229     Rect GetRectWithShadow() const;
230 
SetShadow(const Shadow & shadow)231     void SetShadow(const Shadow& shadow)
232     {
233         shadow_ = shadow;
234         hasShadow_ = true;
235     }
236 
237     void SetLayoutSize(const Size& size);
238 
GetRectBasedWindowTopLeft()239     Rect GetRectBasedWindowTopLeft()
240     {
241         return Rect(GetGlobalOffset(), paintRect_.GetSize());
242     }
243 
GetTouchRect()244     virtual const Rect& GetTouchRect()
245     {
246         if (needUpdateTouchRect_) {
247             needUpdateTouchRect_ = false;
248             UpdateTouchRect();
249         }
250         return touchRect_;
251     }
252 
GetTouchRectList()253     virtual const std::vector<Rect>& GetTouchRectList()
254     {
255         if (needUpdateTouchRect_) {
256             needUpdateTouchRect_ = false;
257             touchRectList_.clear();
258             UpdateTouchRect();
259         }
260         return touchRectList_;
261     }
262 
ChangeTouchRectList(std::vector<Rect> & touchRectList)263     void ChangeTouchRectList(std::vector<Rect>& touchRectList)
264     {
265         touchRectList_ = touchRectList;
266     }
267 
InTouchRectList(const Point & parentLocalPoint,const std::vector<Rect> & touchRectList)268     bool InTouchRectList(const Point& parentLocalPoint, const std::vector<Rect>& touchRectList) const
269     {
270         for (auto& rect : touchRectList) {
271             if (rect.IsInRegion(parentLocalPoint)) {
272                 return true;
273             }
274         }
275         return false;
276     }
277 
IsUseOnly()278     virtual bool IsUseOnly()
279     {
280         return false;
281     }
282 
IsNotSiblingAddRecognizerToResult()283     virtual bool IsNotSiblingAddRecognizerToResult()
284     {
285         return true;
286     }
287 
GetTransformPoint(const Point & point)288     virtual Point GetTransformPoint(const Point& point)
289     {
290         return point;
291     }
292 
GetTransformRect(const Rect & rect)293     virtual Rect GetTransformRect(const Rect& rect)
294     {
295         return rect;
296     }
297 
298     const Rect& GetPaintRect() const;
299 
300     Rect GetTransitionPaintRect() const;
301 
302     Offset GetTransitionGlobalOffset() const;
303 
304     void SetPaintRect(const Rect& rect);
305 
SetTouchRect(const Rect & rect)306     void SetTouchRect(const Rect& rect)
307     {
308         touchRect_ = rect;
309         needUpdateTouchRect_ = false;
310     }
311 
MarkNeedUpdateTouchRect(bool needUpdateTouchRect)312     void MarkNeedUpdateTouchRect(bool needUpdateTouchRect)
313     {
314         needUpdateTouchRect_ = needUpdateTouchRect;
315     }
316 
OnChildAdded(const RefPtr<RenderNode> & child)317     virtual void OnChildAdded(const RefPtr<RenderNode>& child)
318     {
319         if (slipFactorSetting_) {
320             child->SetSlipFactorSetting(slipFactorSetting_);
321         }
322     }
323 
OnChildRemoved(const RefPtr<RenderNode> & child)324     virtual void OnChildRemoved(const RefPtr<RenderNode>& child) {}
325 
OnRemove()326     virtual void OnRemove() {}
327 
GetAccessibilityText()328     const std::string& GetAccessibilityText() const
329     {
330         return accessibilityText_;
331     }
332 
SetAccessibilityText(const std::string & accessibilityText)333     void SetAccessibilityText(const std::string& accessibilityText)
334     {
335         accessibilityText_ = accessibilityText;
336     }
337 
338     virtual void DumpTree(int32_t depth);
339 
340     virtual void DumpTree(int32_t depth, std::vector<std::string>& info);
341 
342     virtual void Dump();
343 
344     enum class BridgeType { NONE, ROSEN, FLUTTER };
345 
GetBridgeType()346     virtual BridgeType GetBridgeType() const
347     {
348         return BridgeType::NONE;
349     }
350 
SetNeedRender(bool needRender)351     void SetNeedRender(bool needRender)
352     {
353         needRender_ = needRender;
354     }
355 
356     void MarkNeedRender(bool overlay = false);
357 
NeedRender()358     bool NeedRender() const
359     {
360         return needRender_;
361     }
362 
363     void SetDepth(int32_t depth);
364 
GetDepth()365     int32_t GetDepth() const
366     {
367         return depth_;
368     }
369 
GetPositionType()370     PositionType GetPositionType() const
371     {
372         return positionParam_.type;
373     }
374 
GetLeft()375     virtual const Dimension& GetLeft() const
376     {
377         return positionParam_.left.first;
378     }
379 
GetRight()380     virtual const Dimension& GetRight() const
381     {
382         return positionParam_.right.first;
383     }
384 
GetTop()385     virtual const Dimension& GetTop() const
386     {
387         return positionParam_.top.first;
388     }
389 
GetBottom()390     virtual const Dimension& GetBottom() const
391     {
392         return positionParam_.bottom.first;
393     }
394 
GetAnchorX()395     const Dimension& GetAnchorX() const
396     {
397         return positionParam_.anchor.first;
398     }
399 
GetAnchorY()400     const Dimension& GetAnchorY() const
401     {
402         return positionParam_.anchor.second;
403     }
404 
HasLeft()405     virtual bool HasLeft() const
406     {
407         return positionParam_.left.second;
408     }
409 
HasRight()410     virtual bool HasRight() const
411     {
412         return positionParam_.right.second;
413     }
414 
HasTop()415     virtual bool HasTop() const
416     {
417         return positionParam_.top.second;
418     }
419 
HasBottom()420     virtual bool HasBottom() const
421     {
422         return positionParam_.bottom.second;
423     }
424 
SetLeft(const Dimension & left)425     virtual void SetLeft(const Dimension& left) // add for animation
426     {
427         if (positionParam_.left.first != left) {
428             positionParam_.left = std::make_pair(left, true);
429             MarkNeedLayout();
430         }
431     }
432 
SetTop(const Dimension & top)433     virtual void SetTop(const Dimension& top) // add for animation
434     {
435         if (positionParam_.top.first != top) {
436             positionParam_.top = std::make_pair(top, true);
437             MarkNeedLayout();
438         }
439     }
440 
SetRight(const Dimension & right)441     virtual void SetRight(const Dimension& right) // add for animation
442     {
443         if (positionParam_.right.first != right) {
444             positionParam_.top = std::make_pair(right, true);
445             MarkNeedLayout();
446         }
447     }
448 
SetBottom(const Dimension & bottom)449     virtual void SetBottom(const Dimension& bottom) // add for animation
450     {
451         if (positionParam_.bottom.first != bottom) {
452             positionParam_.bottom = std::make_pair(bottom, true);
453             MarkNeedLayout();
454         }
455     }
456 
GetParent()457     WeakPtr<RenderNode> GetParent() const
458     {
459         return parent_;
460     }
461 
GetContext()462     WeakPtr<PipelineContext> GetContext() const
463     {
464         return context_;
465     }
466 
GetRenderLayer()467     virtual RenderLayer GetRenderLayer()
468     {
469         return nullptr;
470     }
471 
472     void SetVisible(bool visible, bool inRecursion = false)
473     {
474         if (visible_ != visible) {
475             visible_ = visible;
476             AddDirtyRenderBoundaryNode();
477             OnVisibleChanged();
478             if (!inRecursion && SystemProperties::GetRosenBackendEnabled()) {
479                 MarkParentNeedRender();
480             }
481         }
482         for (auto& child : children_) {
483             child->SetVisible(visible, true);
484         }
485     }
486 
GetVisible()487     virtual bool GetVisible() const
488     {
489         return visible_;
490     }
491 
492     virtual void SetHidden(bool hidden, bool inRecursion = false)
493     {
494         if (hidden_ != hidden) {
495             hidden_ = hidden;
496             AddDirtyRenderBoundaryNode();
497             OnHiddenChanged(hidden);
498             if (!inRecursion && SystemProperties::GetRosenBackendEnabled()) {
499                 MarkParentNeedRender();
500             }
501             if (hidden_) {
502                 disableTouchEvent_ = true;
503             } else {
504                 disableTouchEvent_ = false;
505             }
506         }
507         for (auto& child : children_) {
508             child->SetHidden(hidden, true);
509         }
510     }
511 
SetSelfHidden(bool hidden)512     void SetSelfHidden(bool hidden)
513     {
514         if (hidden_ != hidden) {
515             hidden_ = hidden;
516             AddDirtyRenderBoundaryNode();
517             OnHiddenChanged(hidden);
518             if (SystemProperties::GetRosenBackendEnabled()) {
519                 MarkParentNeedRender();
520             }
521         }
522     }
523 
GetHidden()524     bool GetHidden() const
525     {
526         return hidden_;
527     }
528 
IsTakenBoundary()529     bool IsTakenBoundary() const
530     {
531         return takeBoundary_;
532     }
533 
IsRepaintBoundary()534     virtual bool IsRepaintBoundary() const
535     {
536         return IsHeadRenderNode();
537     }
538 
GetChildren()539     virtual const std::list<RefPtr<RenderNode>>& GetChildren() const
540     {
541         return children_;
542     }
543 
544     virtual void NotifyPaintFinish();
545 
546     virtual void RenderWithContext(RenderContext& context, const Offset& offset);
547     virtual void Paint(RenderContext& context, const Offset& offset);
548     virtual void PaintChild(const RefPtr<RenderNode>& child, RenderContext& context, const Offset& offset);
549 
OnPaintFinish()550     virtual void OnPaintFinish() {}
551 
552     virtual bool TouchTest(const Point& globalPoint, const Point& parentLocalPoint, const TouchRestrict& touchRestrict,
553         TouchTestResult& result);
554 
555     virtual void MouseTest(const Point& globalPoint, const Point& parentLocalPoint, MouseTestResult& result);
556 
557     virtual bool MouseHoverTest(const Point& parentLocalPoint);
558 
559     virtual bool MouseDetect(const Point& globalPoint, const Point& parentLocalPoint, MouseHoverTestList& result,
560         WeakPtr<RenderNode>& hoverNode);
561 
562     virtual bool AxisDetect(const Point& globalPoint, const Point& parentLocalPoint, WeakPtr<RenderNode>& axisNode,
563         const AxisDirection direction);
564 
HandleMouseHoverEvent(const MouseState mouseState)565     virtual void HandleMouseHoverEvent(const MouseState mouseState) {}
566 
HandleMouseEvent(const MouseEvent & event)567     virtual bool HandleMouseEvent(const MouseEvent& event)
568     {
569         return false;
570     }
571 
HandleAxisEvent(const AxisEvent & event)572     virtual void HandleAxisEvent(const AxisEvent& event) {}
573 
574     virtual bool RotationMatchTest(const RefPtr<RenderNode>& requestRenderNode);
575 
576     virtual bool RotationTest(const RotationEvent& event);
577 
578     virtual bool RotationTestForward(const RotationEvent& event);
579 
580     virtual double GetBaselineDistance(TextBaseline textBaseline);
581 
582     virtual Size GetContentSize();
583 
584     virtual bool ScrollPageByChild(Offset& delta, int32_t source);
585 
586     // Change render nodes' status
ChangeStatus(RenderStatus renderStatus)587     void ChangeStatus(RenderStatus renderStatus)
588     {
589         // Myself status should be changed and function achieved by derived class which is component
590         OnStatusChanged(renderStatus);
591 
592         // Deep traversal
593         for (auto& child : children_) {
594             child->ChangeStatus(renderStatus);
595         }
596     }
597 
598     virtual void OnStatusStyleChanged(VisualState state);
599 
600     Offset GetOffsetFromOrigin(const Offset& offset) const;
601 
602     virtual Offset GetGlobalOffset() const;
603 
604     virtual Offset GetGlobalOffsetExternal() const;
605 
606     // Whether |rect| is in the paint rect of render tree recursively.
607     bool IsVisible(const Rect& rect, bool totally = false) const;
608 
SetOnChangeCallback(std::function<void ()> && onChangeCallback)609     void SetOnChangeCallback(std::function<void()>&& onChangeCallback)
610     {
611         onChangeCallback_ = std::move(onChangeCallback);
612     }
613 
SetDisableTouchEvent(bool disableTouchEvent)614     void SetDisableTouchEvent(bool disableTouchEvent)
615     {
616         disableTouchEvent_ = disableTouchEvent;
617     }
618 
GetDisableTouchEvent()619     bool GetDisableTouchEvent() const
620     {
621         return disableTouchEvent_;
622     }
623 
IsChildrenTouchEnable()624     virtual bool IsChildrenTouchEnable()
625     {
626         return true;
627     }
628 
SetTextDirection(TextDirection textDirection)629     void SetTextDirection(TextDirection textDirection)
630     {
631         textDirection_ = textDirection;
632     }
633 
GetTextDirection()634     TextDirection GetTextDirection() const
635     {
636         return textDirection_;
637     }
638 
639     // Transfer any other dimension unit to logical px.
640     // NOTE: context_ MUST be initialized before call this method.
641     double NormalizeToPx(Dimension dimension) const;
642 
643     // Mainly use this function to convert Percent to Px. Do not call this function in Update().
644     double NormalizePercentToPx(const Dimension& dimension, bool isVertical, bool referSelf = false) const;
645 
646     // for accessibility
SetAccessibilityNode(const WeakPtr<AccessibilityNode> & accessibilityNode)647     void SetAccessibilityNode(const WeakPtr<AccessibilityNode>& accessibilityNode)
648     {
649         accessibilityNode_ = accessibilityNode;
650     }
651 
GetAccessibilityNode()652     const WeakPtr<AccessibilityNode>& GetAccessibilityNode() const
653     {
654         return accessibilityNode_;
655     }
656 
GetAccessibilityNodeId()657     int32_t GetAccessibilityNodeId() const
658     {
659         auto accessibilityNode = accessibilityNode_.Upgrade();
660         if (accessibilityNode) {
661             return accessibilityNode->GetNodeId();
662         }
663         return 0;
664     }
665 
ClearAccessibilityRect()666     void ClearAccessibilityRect()
667     {
668         auto node = accessibilityNode_.Upgrade();
669         if (node) {
670             node->ClearRect();
671         }
672         for (auto& child : children_) {
673             child->ClearAccessibilityRect();
674         }
675     }
676     void SetAccessibilityRect(const Rect& rect);
677 
SetNeedUpdateAccessibility(bool needUpdate)678     void SetNeedUpdateAccessibility(bool needUpdate)
679     {
680         needUpdateAccessibility_ = needUpdate;
681         for (auto& child : children_) {
682             child->SetNeedUpdateAccessibility(needUpdate);
683         }
684     }
685 
SetAccessibilityVisible(bool visible)686     void SetAccessibilityVisible(bool visible)
687     {
688         auto node = accessibilityNode_.Upgrade();
689         if (node) {
690             node->SetVisible(visible);
691         }
692         for (auto& child : children_) {
693             child->SetAccessibilityVisible(visible);
694         }
695     }
696 
697     RefPtr<RenderNode> GetLastChild() const;
698 
699     RefPtr<RenderNode> GetFirstChild() const;
700 
GetOffsetToStage()701     Offset GetOffsetToStage() const
702     {
703         auto offset = GetGlobalOffset();
704         auto context = GetContext().Upgrade();
705         if (context) {
706             offset = offset - context->GetStageRect().GetOffset();
707         }
708         return offset;
709     }
710 
711     Offset GetOffsetToPage() const;
712 
GetFlexWeight()713     double GetFlexWeight() const
714     {
715         return flexWeight_;
716     }
717 
GetDisplayIndex()718     int32_t GetDisplayIndex() const
719     {
720         return displayIndex_;
721     }
722 
723     OpacityCallback GetOpacityCallback(int32_t domId);
724 
725     virtual bool SupportOpacity();
726 
727     void GetDomOpacityCallbacks(int32_t domId, std::list<OpacityCallback>& result);
728 
729     int32_t GetNodeId() const;
730 
731     uint8_t GetOpacity() const;
732 
733     virtual void UpdateOpacity(uint8_t opacity);
734 
InterceptTouchEvent()735     bool InterceptTouchEvent() const
736     {
737         return interceptTouchEvent_;
738     }
739 
SetInterceptTouchEvent(bool interceptTouchEvent)740     void SetInterceptTouchEvent(bool interceptTouchEvent)
741     {
742         interceptTouchEvent_ = interceptTouchEvent;
743     }
744 
DispatchCancelPressAnimation()745     void DispatchCancelPressAnimation()
746     {
747         OnCancelPressAnimation();
748         for (const auto& child : children_) {
749             child->DispatchCancelPressAnimation();
750         }
751     }
752 
CheckAxisNode()753     virtual WeakPtr<RenderNode> CheckAxisNode()
754     {
755         return nullptr;
756     }
CheckHoverNode()757     virtual WeakPtr<RenderNode> CheckHoverNode()
758     {
759         return nullptr;
760     }
MouseHoverEnterTest()761     virtual void MouseHoverEnterTest() {}
MouseHoverExitTest()762     virtual void MouseHoverExitTest() {}
AnimateMouseHoverEnter()763     virtual void AnimateMouseHoverEnter() {}
AnimateMouseHoverExit()764     virtual void AnimateMouseHoverExit() {}
OnCancelPressAnimation()765     virtual void OnCancelPressAnimation() {}
OnMouseHoverEnterAnimation()766     virtual void OnMouseHoverEnterAnimation() {}
OnMouseHoverExitAnimation()767     virtual void OnMouseHoverExitAnimation() {}
OnMouseClickDownAnimation()768     virtual void OnMouseClickDownAnimation() {}
OnMouseClickUpAnimation()769     virtual void OnMouseClickUpAnimation() {}
StopMouseHoverAnimation()770     virtual void StopMouseHoverAnimation() {}
IsAxisScrollable(AxisDirection direction)771     virtual bool IsAxisScrollable(AxisDirection direction)
772     {
773         return false;
774     }
775 
OnVisibleChanged()776     virtual void OnVisibleChanged() {}
777 
778     void CreateMouseAnimation(RefPtr<KeyframeAnimation<Color>>& animation, const Color& from, const Color& to);
SetHoverAndPressCallback(const HoverAndPressCallback & callback)779     void SetHoverAndPressCallback(const HoverAndPressCallback& callback)
780     {
781         hoveAndPressCallback_ = callback;
782     }
783 
GetEventEffectColor()784     Color GetEventEffectColor() const
785     {
786         return eventEffectColor_;
787     }
788 
789     void UpdateWindowBlurRRect(bool clear = false);
790 
791     void WindowBlurTest();
792 
GetWindowBlurRRect()793     virtual RRect GetWindowBlurRRect() const
794     {
795         return RRect::MakeRRect(Rect(Offset::Zero(), GetLayoutSize()), Radius(0.0));
796     }
797 
798     RRect GetGlobalWindowBlurRRect(std::vector<RRect>& coords) const;
799 
MarkNeedWindowBlur(bool flag)800     void MarkNeedWindowBlur(bool flag)
801     {
802         if (needWindowBlur_ != flag) {
803             needWindowBlur_ = flag;
804             if (!needWindowBlur_) {
805                 UpdateWindowBlurProgress(0.0f);
806                 SetWindowBlurStyle(WindowBlurStyle::STYLE_BACKGROUND_SMALL_LIGHT);
807             }
808             OnWindowBlurChanged();
809         }
810     }
811 
NeedWindowBlur()812     bool NeedWindowBlur() const
813     {
814         return needWindowBlur_;
815     }
816 
UpdateWindowBlurProgress(float progress)817     void UpdateWindowBlurProgress(float progress)
818     {
819         windowBlurProgress_ = progress;
820     }
821 
GetWindowBlurProgress()822     float GetWindowBlurProgress() const
823     {
824         return windowBlurProgress_;
825     }
826 
SetWindowBlurStyle(WindowBlurStyle style)827     void SetWindowBlurStyle(WindowBlurStyle style)
828     {
829         windowBlurStyle_ = style;
830     }
831 
GetWindowBlurStyle()832     WindowBlurStyle GetWindowBlurStyle() const
833     {
834         return windowBlurStyle_;
835     }
836 
SetSlipFactorSetting(const SlipFactorSetting & slipFactorSetting)837     void SetSlipFactorSetting(const SlipFactorSetting& slipFactorSetting)
838     {
839         slipFactorSetting_ = slipFactorSetting;
840     }
841 
IsInfiniteLayout()842     bool IsInfiniteLayout() const
843     {
844         return GetLayoutSize().Width() > INT32_MAX || GetLayoutSize().Height() > INT32_MAX;
845     }
846 
IsIgnored()847     bool IsIgnored() const
848     {
849         return isIgnored_;
850     }
851 
SetIsIgnored(bool ignore)852     void SetIsIgnored(bool ignore)
853     {
854         isIgnored_ = ignore;
855     }
856 
SetGlobalPoint(const Point & point)857     void SetGlobalPoint(const Point& point)
858     {
859         globalPoint_ = point;
860     }
861 
GetGlobalPoint()862     const Point& GetGlobalPoint()
863     {
864         return globalPoint_;
865     }
866 
SetCoordinatePoint(const Point & point)867     void SetCoordinatePoint(const Point& point)
868     {
869         coordinatePoint_ = point;
870     }
871 
GetCoordinatePoint()872     const Point& GetCoordinatePoint()
873     {
874         return coordinatePoint_;
875     }
876 
IsTouchable()877     bool IsTouchable() const
878     {
879         return touchable_;
880     }
881 
IsDisabled()882     bool IsDisabled() const
883     {
884         return disabled_;
885     }
886 
OnAppShow()887     virtual void OnAppShow()
888     {
889         isAppOnShow_ = true;
890         for (const auto& child : children_) {
891             child->OnAppShow();
892         }
893     }
894 
OnAppHide()895     virtual void OnAppHide()
896     {
897         isAppOnShow_ = false;
898         for (const auto& child : children_) {
899             child->OnAppHide();
900         }
901     }
902 
IsAppShow()903     bool IsAppShow()
904     {
905         return isAppOnShow_;
906     }
907 
908     template<typename T>
GetTheme()909     RefPtr<T> GetTheme() const
910     {
911         auto context = context_.Upgrade();
912         if (!context) {
913             return nullptr;
914         }
915         auto themeManager = context->GetThemeManager();
916         if (!themeManager) {
917             return nullptr;
918         }
919         return themeManager->GetTheme<T>();
920     }
921 
922     virtual bool HasEffectiveTransform() const;
923 
OnTransition(TransitionType type,int32_t id)924     virtual void OnTransition(TransitionType type, int32_t id) {}
925 
926     bool IsDisappearing();
927     virtual bool HasDisappearingTransition(int32_t nodeId);
928     void NotifyTransition(TransitionType type, int32_t nodeId);
SetPendingAppearingTransition()929     virtual void SetPendingAppearingTransition() {}
930 
931     Rect GetDirtyRect() const;
932     std::function<void(const DragUpdateInfo&)> onDomDragEnter_ = nullptr;
933     std::function<void(const DragUpdateInfo&)> onDomDragOver_ = nullptr;
934     std::function<void(const DragUpdateInfo&)> onDomDragLeave_ = nullptr;
935     std::function<void(const DragEndInfo&)> onDomDragDrop_ = nullptr;
936     virtual bool GetAlignDeclarationOffset(AlignDeclarationPtr alignDeclarationPtr, Offset& offset) const;
937 
938     // Each subclass override this to return touch target object which is used to receive touch event.
939     // For convenience, it is recommended to return directly to the corresponding gesture recognizer.
940     // Sees gestures directory.
941     // Uses coordinateOffset for recognizer to calculate the local location of the touch point.
942     // Uses touchRestrict for restrict gesture recognition in some sense.
OnTouchTestHit(const Offset & coordinateOffset,const TouchRestrict & touchRestrict,TouchTestResult & result)943     virtual void OnTouchTestHit(
944         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result)
945     {}
OnPreDraw()946     virtual void OnPreDraw() {}
947 
948     template<typename T>
FindChildNodeOfClass(const Point & globalPoint,const Point & parentLocalPoint)949     RefPtr<T> FindChildNodeOfClass(const Point& globalPoint, const Point& parentLocalPoint)
950     {
951         Point transformPoint = GetTransformPoint(parentLocalPoint);
952         if (!InTouchRectList(transformPoint, GetTouchRectList())) {
953             return nullptr;
954         }
955 
956         // Calculates the local point location in this node.
957         const auto localPoint = transformPoint - GetPaintRect().GetOffset();
958         const auto& children = GetChildren();
959         for (auto iter = children.rbegin(); iter != children.rend(); ++iter) {
960             auto& child = *iter;
961             if (!child->GetVisible()) {
962                 continue;
963             }
964 
965             if (child->InterceptTouchEvent()) {
966                 continue;
967             }
968 
969             auto target = child->FindChildNodeOfClass<T>(globalPoint, localPoint);
970             if (target) {
971                 return target;
972             }
973         }
974 
975         for (auto& rect : GetTouchRectList()) {
976             if (touchable_ && rect.IsInRegion(transformPoint)) {
977                 RefPtr<RenderNode> renderNode = AceType::Claim<RenderNode>(this);
978                 if (AceType::InstanceOf<T>(renderNode)) {
979                     return AceType::DynamicCast<T>(renderNode);
980                 }
981             }
982         }
983         return nullptr;
984     }
985 
986     template<typename T>
FindTargetRenderNode(const RefPtr<PipelineContext> context,const GestureEvent & info)987     RefPtr<T> FindTargetRenderNode(const RefPtr<PipelineContext> context, const GestureEvent& info)
988     {
989         if (!context) {
990             return nullptr;
991         }
992 
993         auto pageRenderNode = context->GetLastPageRender();
994         if (!pageRenderNode) {
995             return nullptr;
996         }
997 
998         return pageRenderNode->FindChildNodeOfClass<T>(info.GetGlobalPoint(), info.GetGlobalPoint());
999     }
1000 
1001     template<class T>
FindChildOfClass(const RefPtr<RenderNode> & parent)1002     RefPtr<T> FindChildOfClass(const RefPtr<RenderNode>& parent)
1003     {
1004         // BFS to find child in tree.
1005         uint32_t findCount = 0;
1006         auto searchQueue = parent->GetChildren(); // copy children to a queue
1007         while (++findCount <= FIND_MAX_COUNT && !searchQueue.empty()) {
1008             const auto child = searchQueue.front();
1009             searchQueue.pop_front();
1010             if (!child) {
1011                 continue;
1012             }
1013             if (AceType::InstanceOf<T>(child)) {
1014                 return AceType::DynamicCast<T>(child);
1015             }
1016             searchQueue.insert(searchQueue.end(), child->GetChildren().begin(), child->GetChildren().end());
1017         }
1018         return RefPtr<T>();
1019     }
1020 
1021     void SaveExplicitAnimationOption(const AnimationOption& option);
1022 
1023     const AnimationOption& GetExplicitAnimationOption() const;
1024 
1025     void ClearExplicitAnimationOption();
1026 
1027     void ClearDisappearingNode(RefPtr<RenderNode> child);
1028 
1029     void CreateLayoutTransition();
1030 
1031     void CreatePathAnimation();
1032 
IsExclusiveEventForChild()1033     bool IsExclusiveEventForChild() const
1034     {
1035         return exclusiveEventForChild_;
1036     }
SetExclusiveEventForChild(bool exclusiveEventForChild)1037     void SetExclusiveEventForChild(bool exclusiveEventForChild)
1038     {
1039         exclusiveEventForChild_ = exclusiveEventForChild;
1040     }
1041 
1042     void MarkUpdateType(const RefPtr<Component>& component);
1043 
GetComponent()1044     virtual RefPtr<Component> GetComponent()
1045     {
1046         return nullptr;
1047     }
1048 
1049     virtual void NotifySizeTransition(const AnimationOption& option, Size fromSize, Size toSize, int32_t nodeId);
1050     void CreateGeometryTransitionFrom(const RefPtr<RenderNode>& targetNode, AnimationOption& sharedOption);
1051     void CreateGeometryTransitionTo(const RefPtr<RenderNode>& targetNode, AnimationOption& sharedOption);
1052     void SetIsPaintGeometryTransition(bool isPaintGeometryTransition);
1053     void SetPaintOutOfParent(bool isPaintOutOfParent);
1054     bool IsPaintOutOfParent();
1055     void UpdatePosition();
1056 
SetHasSubWindow(bool hasSubWindow)1057     void SetHasSubWindow(bool hasSubWindow)
1058     {
1059         hasSubWindow_ = hasSubWindow;
1060     }
1061 
GetHasSubWindow()1062     bool GetHasSubWindow() const
1063     {
1064         return hasSubWindow_;
1065     }
1066 
1067     // mark JSview boundary, create/destroy RSNode if need
1068     void SyncRSNodeBoundary(bool isHead, bool isTail);
1069     void SyncRSNode(std::shared_ptr<RSNode> rsNode);
GetRSNode()1070     const std::shared_ptr<RSNode>& GetRSNode() const
1071     {
1072         return rsNode_;
1073     }
1074     // sync geometry properties to ROSEN backend
1075     virtual void SyncGeometryProperties();
1076 
IsResponseRegion()1077     bool IsResponseRegion() const
1078     {
1079         return isResponseRegion_;
1080     }
1081 
1082     double GetPxValue(double standard, const Dimension& value);
1083 
GetResponseRegionList()1084     const std::vector<Rect>& GetResponseRegionList() const
1085     {
1086         return responseRegionList_;
1087     }
1088 
GetInspectorNode()1089     const WeakPtr<V2::InspectorNode>& GetInspectorNode() const
1090     {
1091         return inspector_;
1092     }
1093 
SetInspectorNode(const RefPtr<V2::InspectorNode> & inspectorNode)1094     void SetInspectorNode(const RefPtr<V2::InspectorNode>& inspectorNode)
1095     {
1096         inspector_ = inspectorNode;
1097     }
1098 
SetNeedClip(bool needClip)1099     virtual void SetNeedClip(bool needClip)
1100     {
1101         needClip_ = needClip;
1102     }
1103 
GetNeedClip()1104     bool GetNeedClip()
1105     {
1106         return needClip_;
1107     }
1108 
1109     RefPtr<RenderNode> FindDropChild(const Point& globalPoint, const Point& parentLocalPoint);
1110     static constexpr size_t DEFAULT_INDEX = -1;
1111 
ProvideRestoreInfo()1112     virtual std::string ProvideRestoreInfo()
1113     {
1114         return "";
1115     }
1116 
SetRestoreInfo(const std::string & restoreInfo)1117     void SetRestoreInfo(const std::string& restoreInfo)
1118     {
1119         restoreInfo_ = restoreInfo;
1120     }
1121 
GetRestoreInfo()1122     const std::string& GetRestoreInfo() const
1123     {
1124         return restoreInfo_;
1125     }
1126 
1127 protected:
1128     explicit RenderNode(bool takeBoundary = false);
1129     virtual void ClearRenderObject();
1130 
OnMouseTestHit(const Offset & coordinateOffset,MouseTestResult & result)1131     virtual void OnMouseTestHit(const Offset& coordinateOffset, MouseTestResult& result) {}
OnMouseHoverEnterTest()1132     virtual void OnMouseHoverEnterTest() {}
OnMouseHoverExitTest()1133     virtual void OnMouseHoverExitTest() {}
1134 
1135     void PrepareLayout();
1136 
SetParent(const WeakPtr<RenderNode> & parent)1137     void SetParent(const WeakPtr<RenderNode>& parent)
1138     {
1139         parent_ = parent;
1140     }
1141 
1142     void TakeBoundary(bool taken = true)
1143     {
1144         takeBoundary_ = taken;
1145     }
1146 
IsLayoutParamChanged()1147     bool IsLayoutParamChanged() const
1148     {
1149         return layoutParamChanged_;
1150     }
1151 
OnGlobalPositionChanged()1152     virtual void OnGlobalPositionChanged()
1153     {
1154         MarkNeedSyncGeometryProperties();
1155         if (IsTailRenderNode()) {
1156             return;
1157         }
1158         for (const auto& child : children_) {
1159             if (child) {
1160                 child->OnGlobalPositionChanged();
1161             }
1162         }
1163     };
OnPositionChanged()1164     virtual void OnPositionChanged() {};
OnSizeChanged()1165     virtual void OnSizeChanged() {};
OnRenderFinish(RenderContext & context)1166     virtual void OnRenderFinish(RenderContext& context) {};
OnStatusChanged(RenderStatus renderStatus)1167     virtual void OnStatusChanged(RenderStatus renderStatus) {};
OnHiddenChanged(bool hidden)1168     virtual void OnHiddenChanged(bool hidden) {};
OnWindowBlurChanged()1169     virtual void OnWindowBlurChanged() {};
1170     virtual bool MarkNeedRenderSpecial();
1171 
1172     double GetHighestChildBaseline(TextBaseline baseline);
1173     double GetFirstChildBaseline(TextBaseline baseline);
1174     Size GetLargestChildContentSize();
1175     void UpdateAccessibilityPosition();
1176     void CheckIfNeedUpdateTouchRect();
1177 
GetThemeManager()1178     RefPtr<ThemeManager> GetThemeManager() const
1179     {
1180         auto context = context_.Upgrade();
1181         if (!context) {
1182             return nullptr;
1183         }
1184         return context->GetThemeManager();
1185     }
1186 
GetUpdateType()1187     uint32_t GetUpdateType()
1188     {
1189         return updateType_;
1190     }
1191 
1192     virtual std::shared_ptr<RSNode> CreateRSNode() const;
OnRSTransition(TransitionType type)1193     virtual void OnRSTransition(TransitionType type) {};
1194     // JSview boundary, all nodes in [head, tail] share the same RSNode
IsHeadRenderNode()1195     bool IsHeadRenderNode() const
1196     {
1197 #ifdef ENABLE_ROSEN_BACKEND
1198         return SystemProperties::GetRosenBackendEnabled() ? isHeadRenderNode_ : false;
1199 #else
1200         return false;
1201 #endif
1202     }
IsTailRenderNode()1203     bool IsTailRenderNode() const
1204     {
1205         return isTailRenderNode_;
1206     }
1207     Offset GetPaintOffset() const;
HasGeometryProperties()1208     virtual bool HasGeometryProperties() const
1209     {
1210         return IsTailRenderNode();
1211     }
1212     void MarkNeedSyncGeometryProperties();
1213 
1214     bool hasSubWindow_ = false;
1215     bool needClip_ = false;
1216     WeakPtr<PipelineContext> context_;
1217     Size viewPort_;
1218     Point globalPoint_;
1219     Point coordinatePoint_;
1220     WeakPtr<V2::InspectorNode> inspector_;
1221     WeakPtr<AccessibilityNode> accessibilityNode_;
1222 
1223     Rect touchRect_;                  // Self touch rect
1224     std::vector<Rect> touchRectList_; // Self and all children touch rect
1225     std::vector<DimensionRect> responseRegion_;
1226     std::vector<Rect> responseRegionList_;
1227     PositionParam positionParam_;
1228     uint8_t opacity_ = 255;
1229     Shadow shadow_;
1230 
1231     float windowBlurProgress_ = 0.0f;
1232     WindowBlurStyle windowBlurStyle_ = WindowBlurStyle::STYLE_BACKGROUND_SMALL_LIGHT;
1233     bool touchable_ = true;
1234     bool interceptTouchEvent_ = false;
1235     bool needWindowBlur_ = false;
1236     bool needUpdateAccessibility_ = true;
1237     bool disabled_ = false;
1238     bool isResponseRegion_ = false;
1239     HoverAnimationType hoverAnimationType_ = HoverAnimationType::AUTO;
1240     int32_t minPlatformVersion_ = 0;
1241 
1242     MouseState mouseState_ = MouseState::NONE;
1243     SlipFactorSetting slipFactorSetting_;
1244 
1245     HoverAndPressCallback hoveAndPressCallback_;
1246 
1247     // hover or press color
1248     Color eventEffectColor_ = Color::TRANSPARENT;
1249     std::function<void(const std::string&)> onLayoutReady_;
1250 
1251     bool isAppOnShow_ = true;
1252     AnimationOption nonStrictOption_; // clear after transition done
1253     MotionPathOption motionPathOption_;
1254     RefPtr<V2::EventExtensions> eventExtensions_;
1255 
1256     // Compute multiSelect zone
1257     Rect ComputeSelectedZone(const Offset& startOffset, const Offset& endOffset);
1258 
1259 private:
AddDirtyRenderBoundaryNode()1260     void AddDirtyRenderBoundaryNode()
1261     {
1262         if (visible_ && !hidden_ && IsRepaintBoundary()) {
1263             auto pipelineContext = context_.Upgrade();
1264             if (pipelineContext == nullptr) {
1265                 return;
1266             }
1267             pipelineContext->AddDirtyRenderNode(AceType::Claim(this));
1268         }
1269     }
1270 
1271     void SetPositionInternal(const Offset& offset);
1272     bool InLayoutTransition() const;
1273     // Sync view hierarchy to RSNode
1274     void RSNodeAddChild(const RefPtr<RenderNode>& child);
1275     void MarkParentNeedRender() const;
1276 
1277     std::list<RefPtr<RenderNode>> hoverChildren_;
1278     std::list<RefPtr<RenderNode>> children_;
1279     std::string accessibilityText_;
1280     LayoutParam layoutParam_;
1281     Rect paintRect_;
1282     WeakPtr<RenderNode> parent_;
1283     int32_t depth_ = 0;
1284     bool needRender_ = false;
1285     bool needLayout_ = false;
1286     bool visible_ = true;
1287     bool takeBoundary_ = false;
1288     bool layoutParamChanged_ = false;
1289     bool pendingDispatchLayoutReady_ = false;
1290     bool disableTouchEvent_ = false;
1291     bool needUpdateTouchRect_ = false;
1292     bool hasShadow_ = false;
1293 
1294     double flexWeight_ = 0.0;
1295     int32_t displayIndex_ = 1;
1296 
1297     double dipScale_ = 0.0;
1298 
1299     TextDirection textDirection_ { TextDirection::LTR };
1300     Offset selfOffset_ { 0, 0 };
1301 
1302     bool hidden_ = false;
1303     bool isIgnored_ = false;
1304     std::function<void()> onChangeCallback_;
1305     std::list<RefPtr<RenderNode>> disappearingNodes_;
1306     AnimatableDimension paintX_;
1307     AnimatableDimension paintY_;
1308     AnimatableDimension paintW_;
1309     AnimatableDimension paintH_;
1310     Size transitionPaintRectSize_;
1311     Rect nonStrictPaintRect_;
1312     bool isFirstSizeAssign_ = true;
1313     bool isFirstPositionAssign_ = true;
1314 
1315     // for container, this flag controls only the last child in touch area is consuming event.
1316     bool exclusiveEventForChild_ = false;
1317     int32_t zIndex_ = 0;
1318     bool isPercentSize_ = false;
1319     uint32_t updateType_ = 0;
1320 
1321     std::shared_ptr<RSNode> rsNode_ = nullptr;
1322     bool isHeadRenderNode_ = false;
1323     bool isTailRenderNode_ = false;
1324 
1325     bool isPaintGeometryTransition_ = false;
1326     bool isPaintOutOfParent_ = false;
1327 
1328     std::string restoreInfo_;
1329 
1330     ACE_DISALLOW_COPY_AND_MOVE(RenderNode);
1331 };
1332 
1333 } // namespace OHOS::Ace
1334 
1335 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_RENDER_NODE_H
1336