1 /*
2  * Copyright (c) 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_NG_PATTERNS_ROOT_ROOT_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_ROOT_ROOT_PATTERN_H
18 
19 #include "interfaces/inner_api/ace/arkui_rect.h"
20 
21 #include "base/utils/noncopyable.h"
22 #include "core/components_ng/base/frame_node.h"
23 #include "core/components_ng/base/ui_node.h"
24 #include "core/components_ng/pattern/pattern.h"
25 #include "core/components_ng/pattern/root/root_layout_algorithm.h"
26 #include "core/components_v2/inspector/inspector_constants.h"
27 #include "core/pipeline_ng/pipeline_context.h"
28 
29 namespace OHOS::Ace::NG {
30 // RootPattern is the base class for root render node.
31 class ACE_EXPORT RootPattern : public Pattern {
32     DECLARE_ACE_TYPE(RootPattern, Pattern);
33 
34 public:
35     RootPattern() = default;
36     ~RootPattern() override = default;
37 
IsRootPattern()38     bool IsRootPattern() const override
39     {
40         return true;
41     }
42 
IsMeasureBoundary()43     bool IsMeasureBoundary() const override
44     {
45         return true;
46     }
47 
IsAtomicNode()48     bool IsAtomicNode() const override
49     {
50         return false;
51     }
52 
CreateLayoutAlgorithm()53     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
54     {
55         return MakeRefPtr<RootLayoutAlgorithm>();
56     }
57 
OnAttachToFrameNode()58     void OnAttachToFrameNode() override
59     {
60         auto host = GetHost();
61         CHECK_NULL_VOID(host);
62         host->GetLayoutProperty()->UpdateAlignment(Alignment::TOP_LEFT);
63         LOGI("root node OnAttachToFrameNode, id:%{public}d", host->GetId());
64     }
65 
OnDetachFromFrameNode(FrameNode * frameNode)66     void OnDetachFromFrameNode(FrameNode* frameNode) override
67     {
68         LOGI("root node OnDetachFromFrameNode, id:%{public}d", frameNode->GetId());
69     }
70 
GetFocusPattern()71     FocusPattern GetFocusPattern() const override
72     {
73         return { FocusType::SCOPE, true };
74     }
75 
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> &,const DirtySwapConfig &)76     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& /*unused*/, const DirtySwapConfig& /*unused*/) override
77     {
78         auto host = GetHost();
79         CHECK_NULL_RETURN(host, false);
80         auto children = host->GetChildren();
81         auto pipeline = PipelineContext::GetCurrentContext();
82         CHECK_NULL_RETURN(pipeline, false);
83         auto iter = std::find_if(children.begin(), children.end(),
84             [](const RefPtr<UINode>& node) { return node->GetTag() == V2::STAGE_ETS_TAG; });
85         if (iter == children.end() || (*iter) == children.back()) {
86             if (!pipeline->GetOverlayNodePositions().empty()) {
87                 pipeline->TriggerOverlayNodePositionsUpdateCallback({});
88             }
89             pipeline->SetOverlayNodePositions({});
90             return false;
91         }
92         std::vector<Ace::RectF> positions;
93         for (++iter; iter != children.end(); iter++) {
94             auto frameNode = AceType::DynamicCast<FrameNode>(*iter);
95             if (!frameNode) {
96                 continue;
97             }
98             auto frameRect = frameNode->GetGeometryNode()->GetFrameRect();
99             auto item = Ace::RectF(frameRect.GetX(), frameRect.GetY(), frameRect.Width(), frameRect.Height());
100             positions.emplace_back(item);
101         }
102         auto funcEqual = [](const Ace::RectF& newRect, const Ace::RectF& rect) {
103             return NearEqual(newRect.GetX(), rect.GetX()) && NearEqual(newRect.GetY(), rect.GetY()) &&
104                    NearEqual(newRect.Width(), rect.Width()) && NearEqual(newRect.Height(), rect.Height());
105         };
106         auto oldPositions = pipeline->GetOverlayNodePositions();
107         if (std::equal(positions.begin(), positions.end(), oldPositions.begin(), oldPositions.end(), funcEqual)) {
108             return false;
109         }
110         pipeline->TriggerOverlayNodePositionsUpdateCallback(positions);
111         pipeline->SetOverlayNodePositions(positions);
112         return false;
113     }
114 
115 private:
116     ACE_DISALLOW_COPY_AND_MOVE(RootPattern);
117 };
118 } // namespace OHOS::Ace::NG
119 
120 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_ROOT_ROOT_PATTERN_H
121