• 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 
DefaultSupportDrag()57     virtual bool DefaultSupportDrag()
58     {
59         return false;
60     }
61 
GetContextParam()62     virtual std::optional<RenderContext::ContextParam> GetContextParam() const
63     {
64         return std::nullopt;
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 
GetOverridePaintRect()107     virtual std::optional<RectF> GetOverridePaintRect() const
108     {
109         return std::nullopt;
110     }
111 
CreateEventHub()112     virtual RefPtr<EventHub> CreateEventHub()
113     {
114         return MakeRefPtr<EventHub>();
115     }
116 
OnContextAttached()117     virtual void OnContextAttached() {}
118 
OnModifyDone()119     virtual void OnModifyDone()
120     {
121         auto frameNode = frameNode_.Upgrade();
122         auto children = frameNode->GetChildren();
123         if (children.empty()) {
124             return;
125         }
126         auto renderContext = frameNode->GetRenderContext();
127         if (!renderContext->HasForegroundColor() && !renderContext->HasForegroundColorStrategy()) {
128             return;
129         }
130         std::list<RefPtr<FrameNode>> childrenList {};
131         std::queue<RefPtr<FrameNode>> queue {};
132         queue.emplace(frameNode);
133         RefPtr<FrameNode> parentNode;
134         while (!queue.empty()) {
135             parentNode = queue.front();
136             queue.pop();
137             auto childs = parentNode->GetChildren();
138             if (childs.empty()) {
139                 continue;
140             }
141             for (auto child : childs) {
142                 if (!AceType::InstanceOf<NG::FrameNode>(child)) {
143                     continue;
144                 }
145                 auto childFrameNode = AceType::DynamicCast<FrameNode>(child);
146                 auto childRenderContext = childFrameNode->GetRenderContext();
147                 if (childRenderContext->HasForegroundColorFlag() && childRenderContext->GetForegroundColorFlagValue()) {
148                     continue;
149                 }
150                 queue.emplace(childFrameNode);
151                 childrenList.emplace_back(childFrameNode);
152             }
153         }
154         bool isForegroundColor = renderContext->HasForegroundColor();
155         for (auto child : childrenList) {
156             auto childRenderContext = child->GetRenderContext();
157             if (!childRenderContext->HasForegroundColor() && !childRenderContext->HasForegroundColorStrategy()) {
158                 if (isForegroundColor) {
159                     childRenderContext->UpdateForegroundColor(renderContext->GetForegroundColorValue());
160                     childRenderContext->ResetForegroundColorStrategy();
161                     childRenderContext->UpdateForegroundColorFlag(false);
162                 } else {
163                     childRenderContext->UpdateForegroundColorStrategy(renderContext->GetForegroundColorStrategyValue());
164                     childRenderContext->ResetForegroundColor();
165                     childRenderContext->UpdateForegroundColorFlag(false);
166                 }
167             } else {
168                 if (!childRenderContext->HasForegroundColorFlag()) {
169                     continue;
170                 }
171                 if (childRenderContext->GetForegroundColorFlagValue()) {
172                     continue;
173                 }
174                 if (isForegroundColor) {
175                     childRenderContext->UpdateForegroundColor(renderContext->GetForegroundColorValue());
176                     childRenderContext->ResetForegroundColorStrategy();
177                     childRenderContext->UpdateForegroundColorFlag(false);
178                 } else {
179                     childRenderContext->UpdateForegroundColorStrategy(renderContext->GetForegroundColorStrategyValue());
180                     childRenderContext->ResetForegroundColor();
181                     childRenderContext->UpdateForegroundColorFlag(false);
182                 }
183             }
184         }
185     }
186 
OnMountToParentDone()187     virtual void OnMountToParentDone() {}
188 
IsRootPattern()189     virtual bool IsRootPattern() const
190     {
191         return false;
192     }
193 
IsMeasureBoundary()194     virtual bool IsMeasureBoundary() const
195     {
196         return false;
197     }
198 
IsRenderBoundary()199     virtual bool IsRenderBoundary() const
200     {
201         return true;
202     }
203 
204     // TODO: for temp use, need to delete this.
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> &,bool,bool)205     virtual bool OnDirtyLayoutWrapperSwap(
206         const RefPtr<LayoutWrapper>& /*dirty*/, bool /*skipMeasure*/, bool /*skipLayout*/)
207     {
208         return false;
209     }
210 
211     // Called on main thread to check if need rerender of the content.
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> &,const DirtySwapConfig &)212     virtual bool OnDirtyLayoutWrapperSwap(
213         const RefPtr<LayoutWrapper>& /*dirty*/, const DirtySwapConfig& /*changeConfig*/)
214     {
215         return false;
216     }
217 
UsResRegion()218     virtual bool UsResRegion()
219     {
220         return true;
221     }
222 
GetHostFrameSize()223     std::optional<SizeF> GetHostFrameSize() const
224     {
225         auto frameNode = frameNode_.Upgrade();
226         if (!frameNode) {
227             return std::nullopt;
228         }
229         return frameNode->GetGeometryNode()->GetMarginFrameSize();
230     }
231 
GetHostFrameOffset()232     std::optional<OffsetF> GetHostFrameOffset() const
233     {
234         auto frameNode = frameNode_.Upgrade();
235         if (!frameNode) {
236             return std::nullopt;
237         }
238         return frameNode->GetGeometryNode()->GetFrameOffset();
239     }
240 
GetHostFrameGlobalOffset()241     std::optional<OffsetF> GetHostFrameGlobalOffset() const
242     {
243         auto frameNode = frameNode_.Upgrade();
244         if (!frameNode) {
245             return std::nullopt;
246         }
247         return frameNode->GetGeometryNode()->GetFrameOffset() + frameNode->GetGeometryNode()->GetParentGlobalOffset();
248     }
249 
GetHostContentSize()250     std::optional<SizeF> GetHostContentSize() const
251     {
252         auto frameNode = frameNode_.Upgrade();
253         if (!frameNode) {
254             return std::nullopt;
255         }
256         const auto& content = frameNode->GetGeometryNode()->GetContent();
257         if (!content) {
258             return std::nullopt;
259         }
260         return content->GetRect().GetSize();
261     }
262 
GetHost()263     RefPtr<FrameNode> GetHost() const
264     {
265         return frameNode_.Upgrade();
266     }
267 
DumpInfo()268     virtual void DumpInfo() {}
269 
270     template<typename T>
GetLayoutProperty()271     RefPtr<T> GetLayoutProperty() const
272     {
273         auto host = GetHost();
274         CHECK_NULL_RETURN(host, nullptr);
275         return DynamicCast<T>(host->GetLayoutProperty<T>());
276     }
277 
278     template<typename T>
GetPaintProperty()279     RefPtr<T> GetPaintProperty() const
280     {
281         auto host = GetHost();
282         CHECK_NULL_RETURN(host, nullptr);
283         return DynamicCast<T>(host->GetPaintProperty<T>());
284     }
285 
286     template<typename T>
GetEventHub()287     RefPtr<T> GetEventHub() const
288     {
289         auto host = GetHost();
290         CHECK_NULL_RETURN(host, nullptr);
291         return DynamicCast<T>(host->GetEventHub<T>());
292     }
293 
294     // Called after frameNode RebuildRenderContextTree.
OnRebuildFrame()295     virtual void OnRebuildFrame() {}
296     // Called before frameNode CreateLayoutWrapper.
BeforeCreateLayoutWrapper()297     virtual void BeforeCreateLayoutWrapper() {}
298     // Called before frameNode CreatePaintWrapper.
BeforeCreatePaintWrapper()299     virtual void BeforeCreatePaintWrapper() {}
300 
GetFocusPattern()301     virtual FocusPattern GetFocusPattern() const
302     {
303         return { FocusType::DISABLE, false, FocusStyleType::NONE };
304     }
305 
GetScopeFocusAlgorithm()306     virtual ScopeFocusAlgorithm GetScopeFocusAlgorithm()
307     {
308         return ScopeFocusAlgorithm();
309     }
310 
ScrollToNode(const RefPtr<FrameNode> & focusFrameNode)311     virtual bool ScrollToNode(const RefPtr<FrameNode>& focusFrameNode)
312     {
313         return false;
314     }
315 
GetFocusNodeIndex(const RefPtr<FocusHub> & focusNode)316     virtual int32_t GetFocusNodeIndex(const RefPtr<FocusHub>& focusNode)
317     {
318         return -1;
319     }
320 
ScrollToFocusNodeIndex(int32_t index)321     virtual void ScrollToFocusNodeIndex(int32_t index) {}
322 
323     // out of viewport or visible is none or gone.
OnInActive()324     virtual void OnInActive() {}
OnActive()325     virtual void OnActive() {}
326 
327     // called by window life cycle.
OnWindowShow()328     virtual void OnWindowShow() {}
OnWindowHide()329     virtual void OnWindowHide() {}
OnWindowFocused()330     virtual void OnWindowFocused() {}
OnWindowUnfocused()331     virtual void OnWindowUnfocused() {}
OnWindowSizeChanged(int32_t width,int32_t height,WindowSizeChangeReason type)332     virtual void OnWindowSizeChanged(int32_t width, int32_t height, WindowSizeChangeReason type) {}
OnNotifyMemoryLevel(int32_t level)333     virtual void OnNotifyMemoryLevel(int32_t level) {}
334 
335     // get XTS inspector value
ToJsonValue(std::unique_ptr<JsonValue> & json)336     virtual void ToJsonValue(std::unique_ptr<JsonValue>& json) const {}
337 
FromJson(const std::unique_ptr<JsonValue> & json)338     virtual void FromJson(const std::unique_ptr<JsonValue>& json) {}
339 
OnAreaChangedInner()340     virtual void OnAreaChangedInner() {}
OnVisibleChange(bool isVisible)341     virtual void OnVisibleChange(bool isVisible) {}
ProvideRestoreInfo()342     virtual std::string ProvideRestoreInfo()
343     {
344         return "";
345     }
346 
OnRestoreInfo(const std::string & restoreInfo)347     virtual void OnRestoreInfo(const std::string& restoreInfo) {}
348 
IsNeedAdjustByAspectRatio()349     virtual bool IsNeedAdjustByAspectRatio()
350     {
351         auto host = GetHost();
352         CHECK_NULL_RETURN(host, false);
353         auto layoutProperty = host->GetLayoutProperty();
354         CHECK_NULL_RETURN(host, false);
355         return layoutProperty->HasAspectRatio();
356     }
357 
OnTouchTestHit(SourceType hitTestType)358     virtual void OnTouchTestHit(SourceType hitTestType) {}
359 
GetDragRecordSize()360     virtual int32_t GetDragRecordSize()
361     {
362         return -1;
363     }
364 
OnLanguageConfigurationUpdate()365     virtual void OnLanguageConfigurationUpdate() {}
OnColorConfigurationUpdate()366     virtual void OnColorConfigurationUpdate() {}
367 protected:
OnAttachToFrameNode()368     virtual void OnAttachToFrameNode() {}
OnDetachFromFrameNode(FrameNode * frameNode)369     virtual void OnDetachFromFrameNode(FrameNode* frameNode) {}
370 
371     WeakPtr<FrameNode> frameNode_;
372 private:
373     ACE_DISALLOW_COPY_AND_MOVE(Pattern);
374 };
375 } // namespace OHOS::Ace::NG
376 
377 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_PATTERN_H
378