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_LAYOUTS_LAYOUT_ALGORITHM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_LAYOUTS_LAYOUT_ALGORITHM_H
18 
19 #include <optional>
20 
21 #include "base/memory/ace_type.h"
22 #include "base/thread/cancelable_callback.h"
23 #include "base/utils/macros.h"
24 #include "base/utils/noncopyable.h"
25 #include "core/components_ng/property/layout_constraint.h"
26 #include "core/pipeline_ng/ui_task_scheduler.h"
27 
28 namespace OHOS::Ace::NG {
29 class LayoutWrapper;
30 
31 class ACE_EXPORT LayoutAlgorithm : public virtual AceType {
32     DECLARE_ACE_TYPE(LayoutAlgorithm, AceType);
33 
34 public:
35     LayoutAlgorithm() = default;
36     ~LayoutAlgorithm() override = default;
37 
Reset()38     void Reset()
39     {
40         OnReset();
41     }
42 
MeasureContent(const LayoutConstraintF &,LayoutWrapper *)43     virtual std::optional<SizeF> MeasureContent(
44         const LayoutConstraintF& /*contentConstraint*/, LayoutWrapper* /*layoutWrapper*/)
45     {
46         return std::nullopt;
47     }
48 
Measure(LayoutWrapper * layoutWrapper)49     virtual void Measure(LayoutWrapper* layoutWrapper) {}
50 
Layout(LayoutWrapper * layoutWrapper)51     virtual void Layout(LayoutWrapper* layoutWrapper) {}
52 
SkipMeasure()53     virtual bool SkipMeasure()
54     {
55         return false;
56     }
57 
SkipLayout()58     virtual bool SkipLayout()
59     {
60         return false;
61     }
62 
CanRunOnWhichThread()63     virtual TaskThread CanRunOnWhichThread()
64     {
65         return MAIN_TASK;
66     }
67 
68 protected:
OnReset()69     virtual void OnReset() {}
70 
71     ACE_DISALLOW_COPY_AND_MOVE(LayoutAlgorithm);
72 };
73 
74 class ACE_EXPORT LayoutAlgorithmWrapper : public LayoutAlgorithm {
75     DECLARE_ACE_TYPE(LayoutAlgorithmWrapper, LayoutAlgorithm);
76 
77 public:
78     explicit LayoutAlgorithmWrapper(
79         const RefPtr<LayoutAlgorithm>& layoutAlgorithmT, bool skipMeasure = false, bool skipLayout = false)
80         : layoutAlgorithm_(layoutAlgorithmT), skipMeasure_(skipMeasure), skipLayout_(skipLayout)
81     {}
82     ~LayoutAlgorithmWrapper() override = default;
83 
OnReset()84     void OnReset() override
85     {
86         layoutAlgorithm_.Reset();
87         skipMeasure_ = false;
88         skipLayout_ = false;
89     }
90 
MeasureContent(const LayoutConstraintF & contentConstraint,LayoutWrapper * layoutWrapper)91     std::optional<SizeF> MeasureContent(
92         const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper) override
93     {
94         if (!layoutAlgorithm_) {
95             return std::nullopt;
96         }
97         return layoutAlgorithm_->MeasureContent(contentConstraint, layoutWrapper);
98     }
99 
Measure(LayoutWrapper * layoutWrapper)100     void Measure(LayoutWrapper* layoutWrapper) override
101     {
102         if (!layoutAlgorithm_) {
103             return;
104         }
105         layoutAlgorithm_->Measure(layoutWrapper);
106     }
107 
Layout(LayoutWrapper * layoutWrapper)108     void Layout(LayoutWrapper* layoutWrapper) override
109     {
110         if (!layoutAlgorithm_) {
111             return;
112         }
113         layoutAlgorithm_->Layout(layoutWrapper);
114     }
115 
SetSkipMeasure()116     void SetSkipMeasure()
117     {
118         skipMeasure_ = true;
119     }
120 
SetSkipLayout()121     void SetSkipLayout()
122     {
123         skipLayout_ = true;
124     }
125 
SetNeedMeasure()126     void SetNeedMeasure()
127     {
128         skipMeasure_ = false;
129     }
130 
SetNeedLayout()131     void SetNeedLayout()
132     {
133         skipLayout_ = false;
134     }
135 
SkipMeasure()136     bool SkipMeasure() override
137     {
138         return skipMeasure_;
139     }
140 
SkipLayout()141     bool SkipLayout() override
142     {
143         return skipLayout_;
144     }
145 
GetLayoutAlgorithm()146     const RefPtr<LayoutAlgorithm>& GetLayoutAlgorithm() const
147     {
148         return layoutAlgorithm_;
149     }
150 
SetLayoutAlgorithm(RefPtr<LayoutAlgorithm> algorithm)151     void SetLayoutAlgorithm(RefPtr<LayoutAlgorithm> algorithm)
152     {
153         layoutAlgorithm_ = std::move(algorithm);
154     }
155 
IsExpire()156     bool IsExpire() const
157     {
158         return frameId != UITaskScheduler::GetFrameId();
159     }
160 
SetPercentHeight(bool value)161     void SetPercentHeight(bool value)
162     {
163         percentHeight_ = value;
164     }
165 
SetPercentWidth(bool value)166     void SetPercentWidth(bool value)
167     {
168         percentWidth_ = value;
169     }
170 
GetPercentHeight()171     bool GetPercentHeight() const
172     {
173         return percentHeight_;
174     }
175 
GetPercentWidth()176     bool GetPercentWidth() const
177     {
178         return percentHeight_;
179     }
180 
181 private:
182     RefPtr<LayoutAlgorithm> layoutAlgorithm_;
183 
184     bool skipMeasure_ = false;
185     bool skipLayout_ = false;
186     bool percentHeight_ = false;
187     bool percentWidth_ = false;
188     uint64_t frameId = UITaskScheduler::GetFrameId();
189 
190     ACE_DISALLOW_COPY_AND_MOVE(LayoutAlgorithmWrapper);
191 };
192 } // namespace OHOS::Ace::NG
193 
194 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_LAYOUTS_LAYOUT_ALGORITHM_H
195