• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_PATTERN_H
18 
19 #include <optional>
20 
21 #include "base/geometry/ng/rect_t.h"
22 #include "base/memory/ace_type.h"
23 #include "base/memory/referenced.h"
24 #include "base/utils/noncopyable.h"
25 #include "core/components_ng/base/frame_node.h"
26 #include "core/components_ng/event/event_hub.h"
27 #include "core/components_ng/layout/layout_property.h"
28 #include "core/components_ng/property/property.h"
29 #include "core/components_ng/render/node_paint_method.h"
30 #include "core/components_ng/render/paint_property.h"
31 
32 namespace OHOS::Ace::NG {
33 struct DirtySwapConfig {
34     bool frameSizeChange = false;
35     bool frameOffsetChange = false;
36     bool contentSizeChange = false;
37     bool contentOffsetChange = false;
38     bool skipMeasure = false;
39     bool skipLayout = false;
40 };
41 
42 // Pattern is the base class for different measure, layout and paint behavior.
43 class Pattern : public virtual AceType {
44     DECLARE_ACE_TYPE(Pattern, AceType);
45 
46 public:
47     Pattern() = default;
48     ~Pattern() override = default;
49 
50     // atomic node is like button, image, custom node and so on.
51     // In ets UI compiler, the atomic node does not Add Pop function, only have Create function.
IsAtomicNode()52     virtual bool IsAtomicNode() const
53     {
54         return true;
55     }
56 
GetSurfaceNodeName()57     virtual std::optional<std::string> GetSurfaceNodeName() const
58     {
59         return std::nullopt;
60     }
61 
UseExternalRSNode()62     virtual bool UseExternalRSNode() const
63     {
64         return false;
65     }
66 
DetachFromFrameNode(FrameNode * frameNode)67     void DetachFromFrameNode(FrameNode* frameNode)
68     {
69         OnDetachFromFrameNode(frameNode);
70         frameNode_.Reset();
71     }
72 
AttachToFrameNode(const WeakPtr<FrameNode> & frameNode)73     void AttachToFrameNode(const WeakPtr<FrameNode>& frameNode)
74     {
75         if (frameNode_ == frameNode) {
76             return;
77         }
78         frameNode_ = frameNode;
79         OnAttachToFrameNode();
80     }
81 
CreateAccessibilityProperty()82     virtual RefPtr<AccessibilityProperty> CreateAccessibilityProperty()
83     {
84         return MakeRefPtr<AccessibilityProperty>();
85     }
86 
CreatePaintProperty()87     virtual RefPtr<PaintProperty> CreatePaintProperty()
88     {
89         return MakeRefPtr<PaintProperty>();
90     }
91 
CreateLayoutProperty()92     virtual RefPtr<LayoutProperty> CreateLayoutProperty()
93     {
94         return MakeRefPtr<LayoutProperty>();
95     }
96 
CreateLayoutAlgorithm()97     virtual RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm()
98     {
99         return MakeRefPtr<BoxLayoutAlgorithm>();
100     }
101 
CreateNodePaintMethod()102     virtual RefPtr<NodePaintMethod> CreateNodePaintMethod()
103     {
104         return nullptr;
105     }
106 
NeedOverridePaintRect()107     virtual bool NeedOverridePaintRect()
108     {
109         return false;
110     }
111 
GetOverridePaintRect()112     virtual std::optional<RectF> GetOverridePaintRect() const
113     {
114         return std::nullopt;
115     }
116 
CreateEventHub()117     virtual RefPtr<EventHub> CreateEventHub()
118     {
119         return MakeRefPtr<EventHub>();
120     }
121 
OnContextAttached()122     virtual void OnContextAttached() {}
123 
OnModifyDone()124     virtual void OnModifyDone() {}
125 
OnMountToParentDone()126     virtual void OnMountToParentDone() {}
127 
IsRootPattern()128     virtual bool IsRootPattern() const
129     {
130         return false;
131     }
132 
IsMeasureBoundary()133     virtual bool IsMeasureBoundary() const
134     {
135         return false;
136     }
137 
IsRenderBoundary()138     virtual bool IsRenderBoundary() const
139     {
140         return true;
141     }
142 
143     // TODO: for temp use, need to delete this.
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> &,bool,bool)144     virtual bool OnDirtyLayoutWrapperSwap(
145         const RefPtr<LayoutWrapper>& /*dirty*/, bool /*skipMeasure*/, bool /*skipLayout*/)
146     {
147         return false;
148     }
149 
150     // Called on main thread to check if need rerender of the content.
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> &,const DirtySwapConfig &)151     virtual bool OnDirtyLayoutWrapperSwap(
152         const RefPtr<LayoutWrapper>& /*dirty*/, const DirtySwapConfig& /*changeConfig*/)
153     {
154         return false;
155     }
156 
UsResRegion()157     virtual bool UsResRegion()
158     {
159         return true;
160     }
161 
GetHostFrameSize()162     std::optional<SizeF> GetHostFrameSize() const
163     {
164         auto frameNode = frameNode_.Upgrade();
165         if (!frameNode) {
166             return std::nullopt;
167         }
168         return frameNode->GetGeometryNode()->GetMarginFrameSize();
169     }
170 
GetHostFrameOffset()171     std::optional<OffsetF> GetHostFrameOffset() const
172     {
173         auto frameNode = frameNode_.Upgrade();
174         if (!frameNode) {
175             return std::nullopt;
176         }
177         return frameNode->GetGeometryNode()->GetFrameOffset();
178     }
179 
GetHostFrameGlobalOffset()180     std::optional<OffsetF> GetHostFrameGlobalOffset() const
181     {
182         auto frameNode = frameNode_.Upgrade();
183         if (!frameNode) {
184             return std::nullopt;
185         }
186         return frameNode->GetGeometryNode()->GetFrameOffset() + frameNode->GetGeometryNode()->GetParentGlobalOffset();
187     }
188 
GetHostContentSize()189     std::optional<SizeF> GetHostContentSize() const
190     {
191         auto frameNode = frameNode_.Upgrade();
192         if (!frameNode) {
193             return std::nullopt;
194         }
195         const auto& content = frameNode->GetGeometryNode()->GetContent();
196         if (!content) {
197             return std::nullopt;
198         }
199         return content->GetRect().GetSize();
200     }
201 
GetHost()202     RefPtr<FrameNode> GetHost() const
203     {
204         return frameNode_.Upgrade();
205     }
206 
DumpInfo()207     virtual void DumpInfo() {}
208 
209     template<typename T>
GetLayoutProperty()210     RefPtr<T> GetLayoutProperty() const
211     {
212         auto host = GetHost();
213         CHECK_NULL_RETURN(host, nullptr);
214         return DynamicCast<T>(host->GetLayoutProperty<T>());
215     }
216 
217     template<typename T>
GetPaintProperty()218     RefPtr<T> GetPaintProperty() const
219     {
220         auto host = GetHost();
221         CHECK_NULL_RETURN(host, nullptr);
222         return DynamicCast<T>(host->GetPaintProperty<T>());
223     }
224 
225     template<typename T>
GetEventHub()226     RefPtr<T> GetEventHub() const
227     {
228         auto host = GetHost();
229         CHECK_NULL_RETURN(host, nullptr);
230         return DynamicCast<T>(host->GetEventHub<T>());
231     }
232 
233     // Called after frameNode RebuildRenderContextTree.
OnRebuildFrame()234     virtual void OnRebuildFrame() {}
235     // Called before frameNode CreateLayoutWrapper.
BeforeCreateLayoutWrapper()236     virtual void BeforeCreateLayoutWrapper() {}
237     // Called before frameNode CreatePaintWrapper.
BeforeCreatePaintWrapper()238     virtual void BeforeCreatePaintWrapper() {}
239 
GetFocusPattern()240     virtual FocusPattern GetFocusPattern() const
241     {
242         return { FocusType::DISABLE, false, FocusStyleType::NONE };
243     }
244 
GetScopeFocusAlgorithm()245     virtual ScopeFocusAlgorithm GetScopeFocusAlgorithm()
246     {
247         return ScopeFocusAlgorithm();
248     }
249 
250     // out of viewport or visible is none or gone.
OnInActive()251     virtual void OnInActive() {}
OnActive()252     virtual void OnActive() {}
253 
254     // called by window life cycle.
OnWindowShow()255     virtual void OnWindowShow() {}
OnWindowHide()256     virtual void OnWindowHide() {}
OnWindowFocused()257     virtual void OnWindowFocused() {}
OnWindowUnfocused()258     virtual void OnWindowUnfocused() {}
OnNotifyMemoryLevel(int32_t level)259     virtual void OnNotifyMemoryLevel(int32_t level) {}
260 
261     // get XTS inspector value
ToJsonValue(std::unique_ptr<JsonValue> & json)262     virtual void ToJsonValue(std::unique_ptr<JsonValue>& json) const {}
263 
OnAreaChangedInner()264     virtual void OnAreaChangedInner() {}
OnVisibleChange(bool isVisible)265     virtual void OnVisibleChange(bool isVisible) {}
266 
267 protected:
OnAttachToFrameNode()268     virtual void OnAttachToFrameNode() {}
OnDetachFromFrameNode(FrameNode * frameNode)269     virtual void OnDetachFromFrameNode(FrameNode* frameNode) {}
270 
271 private:
272     WeakPtr<FrameNode> frameNode_;
273 
274     ACE_DISALLOW_COPY_AND_MOVE(Pattern);
275 };
276 } // namespace OHOS::Ace::NG
277 
278 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_PATTERN_H
279