1 /*
2 * Copyright (c) 2025 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 #include "core/components_ng/layout/layout_algorithm.h"
17
18 #include "ui/view/layout/layout_algorithm.h"
19
20 #include "core/components_ng/layout/layout_wrapper.h"
21 #include "core/components_ng/layout/layout_property.h"
22
23 namespace OHOS::Ace::NG {
24
LayoutAlgorithmWrapper(const RefPtr<LayoutAlgorithm> & layoutAlgorithmT,bool skipMeasure,bool skipLayout)25 LayoutAlgorithmWrapper::LayoutAlgorithmWrapper(
26 const RefPtr<LayoutAlgorithm>& layoutAlgorithmT, bool skipMeasure, bool skipLayout)
27 : layoutAlgorithm_(layoutAlgorithmT), skipMeasure_(skipMeasure), skipLayout_(skipLayout)
28 {}
29
30 LayoutAlgorithmWrapper::~LayoutAlgorithmWrapper() = default;
31
CreateLayoutAlgorithmWrapper(const RefPtr<Kit::LayoutAlgorithm> & layoutAlgorithm)32 RefPtr<LayoutAlgorithmWrapper> LayoutAlgorithmWrapper::CreateLayoutAlgorithmWrapper(
33 const RefPtr<Kit::LayoutAlgorithm>& layoutAlgorithm)
34 {
35 auto layoutAlgorithmWrapper = MakeRefPtr<LayoutAlgorithmWrapper>(nullptr);
36 layoutAlgorithmWrapper->SetAbsLayoutAlgorithm(layoutAlgorithm);
37 return layoutAlgorithmWrapper;
38 }
39
MeasureContent(const LayoutConstraintF & contentConstraint,LayoutWrapper * layoutWrapper)40 std::optional<SizeF> LayoutAlgorithmWrapper::MeasureContent(
41 const LayoutConstraintF& contentConstraint, LayoutWrapper* layoutWrapper)
42 {
43 if (!layoutAlgorithm_) {
44 if (absLayoutAlgorithm_) {
45 return absLayoutAlgorithm_->MeasureContent(contentConstraint);
46 }
47 return std::nullopt;
48 }
49 return layoutAlgorithm_->MeasureContent(contentConstraint, layoutWrapper);
50 }
51
GetLayoutConstraint(const std::optional<LayoutConstraintF> & contentConstraint)52 Kit::LayoutConstraintInfo GetLayoutConstraint(const std::optional<LayoutConstraintF>& contentConstraint)
53 {
54 Kit::LayoutConstraintInfo layoutConstraintInfo;
55 if (contentConstraint.has_value()) {
56 layoutConstraintInfo.minWidth = contentConstraint.value().minSize.Width();
57 layoutConstraintInfo.minHeight = contentConstraint.value().minSize.Height();
58 layoutConstraintInfo.maxWidth = contentConstraint.value().maxSize.Width();
59 layoutConstraintInfo.maxHeight = contentConstraint.value().maxSize.Height();
60 layoutConstraintInfo.percentReferWidth = contentConstraint.value().percentReference.Width();
61 layoutConstraintInfo.percentReferHeight = contentConstraint.value().percentReference.Height();
62 }
63 return layoutConstraintInfo;
64 }
65
Measure(LayoutWrapper * layoutWrapper)66 void LayoutAlgorithmWrapper::Measure(LayoutWrapper* layoutWrapper)
67 {
68 if (!layoutAlgorithm_) {
69 if (absLayoutAlgorithm_) {
70 auto layoutConstraint = layoutWrapper->GetLayoutProperty()->GetLayoutConstraint();
71 absLayoutAlgorithm_->Measure(GetLayoutConstraint(layoutConstraint));
72 }
73 return;
74 }
75 layoutAlgorithm_->Measure(layoutWrapper);
76 // automatically reset when layoutAlgorithm_ destruct each frame
77 layoutAlgorithm_->SetHasMeasured(true);
78 }
79
Layout(LayoutWrapper * layoutWrapper)80 void LayoutAlgorithmWrapper::Layout(LayoutWrapper* layoutWrapper)
81 {
82 if (!layoutAlgorithm_) {
83 if (absLayoutAlgorithm_) {
84 absLayoutAlgorithm_->Layout();
85 }
86 return;
87 }
88 layoutAlgorithm_->Layout(layoutWrapper);
89 }
90
SetAbsLayoutAlgorithm(const RefPtr<Kit::LayoutAlgorithm> & absLayoutAlgorithm)91 void LayoutAlgorithmWrapper::SetAbsLayoutAlgorithm(const RefPtr<Kit::LayoutAlgorithm>& absLayoutAlgorithm)
92 {
93 absLayoutAlgorithm_ = absLayoutAlgorithm;
94 }
95
96 } // namespace OHOS::Ace::NG
97