• 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_PROPERTIES_LAYOUT_CONSTRAINT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PROPERTIES_LAYOUT_CONSTRAINT_H
18 
19 #include <algorithm>
20 #include <optional>
21 #include <string>
22 
23 #include "base/geometry/ng/size_t.h"
24 #include "core/components_ng/property/calc_length.h"
25 #include "core/components_ng/property/measure_property.h"
26 
27 namespace OHOS::Ace::NG {
28 enum class ReferenceEdge {
29     START,
30     END,
31 };
32 struct ViewPosReference {
33     float viewPosStart;
34     float viewPosEnd;
35     float referencePos;
36     ReferenceEdge referenceEdge;
37     Axis axis;
38 
39     bool operator==(const ViewPosReference &other) const
40     {
41         return NearEqual(viewPosStart, other.viewPosStart) &&
42                NearEqual(viewPosEnd, other.viewPosEnd) &&
43                NearEqual(referencePos, other.referencePos) &&
44                referenceEdge == other.referenceEdge &&
45                axis == other.axis;
46     }
47 };
48 template<typename T>
49 struct LayoutConstraintT {
50     ScaleProperty scaleProperty = ScaleProperty::CreateScaleProperty();
51     SizeT<T> minSize { 0, 0 };
52     SizeT<T> maxSize { Infinity<T>(), Infinity<T>() };
53     SizeT<T> percentReference { 0, 0 };
54     OptionalSize<T> parentIdealSize;
55     OptionalSize<T> selfIdealSize;
56     std::optional<ViewPosReference> viewPosRef;
57 
CompareWithInfinityCheckLayoutConstraintT58     static bool CompareWithInfinityCheck(const OptionalSize<float>& first, const OptionalSize<float>& second)
59     {
60         if (first.Width().has_value() ^ second.Width().has_value()) {
61             return false;
62         }
63         auto widthBothInf = GreaterOrEqualToInfinity(first.Width().value_or(0.0f)) &&
64                             GreaterOrEqualToInfinity(second.Width().value_or(0.0f));
65         if (!widthBothInf && !NearEqual(first.Width().value_or(0), second.Width().value_or(0))) {
66             return false;
67         }
68         if (first.Height().has_value() ^ second.Height().has_value()) {
69             return false;
70         }
71         auto heightBothInf = GreaterOrEqualToInfinity(first.Height().value_or(0.0f)) &&
72                             GreaterOrEqualToInfinity(second.Height().value_or(0.0f));
73         if (!heightBothInf && !NearEqual(first.Height().value_or(0), second.Height().value_or(0))) {
74             return false;
75         }
76         return true;
77     }
78 
CompareWithInfinityCheckLayoutConstraintT79     static bool CompareWithInfinityCheck(const SizeT<float>& first, const SizeT<float>& second)
80     {
81         auto widthBothInf = GreaterOrEqualToInfinity(first.Width()) && GreaterOrEqualToInfinity(second.Width());
82         auto heightBothInf = GreaterOrEqualToInfinity(first.Height()) && GreaterOrEqualToInfinity(second.Height());
83         if (widthBothInf && heightBothInf) {
84             return true;
85         }
86         return NearEqual(first.Width(), second.Width()) && NearEqual(first.Height(), second.Height());
87     }
88 
CompareWithInfinityCheckLayoutConstraintT89     static bool CompareWithInfinityCheck(float first, float second)
90     {
91         auto bothInf = GreaterOrEqualToInfinity(first) && GreaterOrEqualToInfinity(second);
92         if (bothInf) {
93             return true;
94         }
95         return NearEqual(first, second);
96     }
97 
98     void ApplyAspectRatio(float ratio, const std::optional<CalcSize>& calcSize, bool greaterThanApiTen = false);
99 
100     void ApplyAspectRatioToParentIdealSize(bool useWidth, float ratio);
101 
102     void ApplyAspectRatioByMaxSize(float ratio, std::optional<bool> useDefinedWidth, bool greaterThanApiTen = false);
103 
104     void ApplyAspectRatioWithCalcSize(float ratio, bool useDefinedWidth);
105 
106     void ApplyAspectRatioWithoutCalcSize(float ratio, bool greaterThanApiTen = false);
107 
108     void Reset();
109 
110     void MinusPadding(const std::optional<T>& left, const std::optional<T>& right, const std::optional<T>& top,
111         const std::optional<T>& bottom);
112 
113     void MinusPaddingToNonNegativeSize(const std::optional<T>& left, const std::optional<T>& right,
114         const std::optional<T>& top, const std::optional<T>& bottom);
115 
116     bool operator==(const LayoutConstraintT& layoutConstraint) const
117     {
118         return (scaleProperty == layoutConstraint.scaleProperty) && (minSize == layoutConstraint.minSize) &&
119                (maxSize == layoutConstraint.maxSize) && (percentReference == layoutConstraint.percentReference) &&
120                (parentIdealSize == layoutConstraint.parentIdealSize) &&
121                (selfIdealSize == layoutConstraint.selfIdealSize) && (viewPosRef == layoutConstraint.viewPosRef);
122     }
123 
124     bool operator!=(const LayoutConstraintT& layoutConstraint) const
125     {
126         return !(*this == layoutConstraint);
127     }
128 
EqualWithoutPercentWidthLayoutConstraintT129     bool EqualWithoutPercentWidth(const LayoutConstraintT& layoutConstraint) const
130     {
131         return (scaleProperty == layoutConstraint.scaleProperty) &&
132                CompareWithInfinityCheck(minSize, layoutConstraint.minSize) &&
133                CompareWithInfinityCheck(maxSize, layoutConstraint.maxSize) &&
134                CompareWithInfinityCheck(parentIdealSize, layoutConstraint.parentIdealSize) &&
135                CompareWithInfinityCheck(percentReference.Height(), layoutConstraint.percentReference.Height()) &&
136                CompareWithInfinityCheck(selfIdealSize, layoutConstraint.selfIdealSize) &&
137                (viewPosRef == layoutConstraint.viewPosRef);
138     }
139 
EqualWithoutPercentHeightLayoutConstraintT140     bool EqualWithoutPercentHeight(const LayoutConstraintT& layoutConstraint) const
141     {
142         return (scaleProperty == layoutConstraint.scaleProperty) &&
143                CompareWithInfinityCheck(minSize, layoutConstraint.minSize) &&
144                CompareWithInfinityCheck(maxSize, layoutConstraint.maxSize) &&
145                CompareWithInfinityCheck(parentIdealSize, layoutConstraint.parentIdealSize) &&
146                CompareWithInfinityCheck(percentReference.Width(), layoutConstraint.percentReference.Width()) &&
147                CompareWithInfinityCheck(selfIdealSize, layoutConstraint.selfIdealSize) &&
148                (viewPosRef == layoutConstraint.viewPosRef);
149     }
150 
UpdateSelfMarginSizeWithCheckLayoutConstraintT151     bool UpdateSelfMarginSizeWithCheck(const OptionalSize<T>& size)
152     {
153         if (selfIdealSize == size) {
154             return false;
155         }
156         return selfIdealSize.UpdateSizeWithCheck(size);
157     }
158 
UpdateIllegalSelfMarginSizeWithCheckLayoutConstraintT159     bool UpdateIllegalSelfMarginSizeWithCheck(const OptionalSize<T>& size)
160     {
161         if (selfIdealSize == size) {
162             return false;
163         }
164         return selfIdealSize.UpdateIllegalSizeWithCheck(size);
165     }
166 
UpdateIllegalSelfIdealSizeWithCheckLayoutConstraintT167     bool UpdateIllegalSelfIdealSizeWithCheck(const OptionalSize<T>& size)
168     {
169         if (selfIdealSize == size) {
170             return false;
171         }
172         return selfIdealSize.UpdateIllegalSizeWithCheck(size);
173     }
174 
UpdateParentIdealSizeWithCheckLayoutConstraintT175     bool UpdateParentIdealSizeWithCheck(const OptionalSize<T>&& size)
176     {
177         if (parentIdealSize == size) {
178             return false;
179         }
180         return parentIdealSize.UpdateSizeWithCheck(size);
181     }
182 
UpdateIllegalParentIdealSizeWithCheckLayoutConstraintT183     bool UpdateIllegalParentIdealSizeWithCheck(const OptionalSize<T>&& size)
184     {
185         if (parentIdealSize == size) {
186             return false;
187         }
188         return parentIdealSize.UpdateIllegalSizeWithCheck(size);
189     }
190 
UpdateMaxSizeWithCheckLayoutConstraintT191     bool UpdateMaxSizeWithCheck(const SizeT<T>& size)
192     {
193         if (maxSize == size) {
194             return false;
195         }
196         return maxSize.UpdateSizeWhenSmaller(size);
197     }
198 
UpdateMaxWidthWithCheckLayoutConstraintT199     bool UpdateMaxWidthWithCheck(const SizeT<T>& size)
200     {
201         if (maxSize == size) {
202             return false;
203         }
204         return maxSize.UpdateWidthWhenSmaller(size);
205     }
206 
UpdateMaxHeightWithCheckLayoutConstraintT207     bool UpdateMaxHeightWithCheck(const SizeT<T>& size)
208     {
209         if (maxSize == size) {
210             return false;
211         }
212         return maxSize.UpdateHeightWhenSmaller(size);
213     }
214 
UpdateMinSizeWithCheckLayoutConstraintT215     bool UpdateMinSizeWithCheck(const SizeT<T>& size)
216     {
217         if (minSize == size) {
218             return false;
219         }
220         return minSize.UpdateSizeWhenLarger(size);
221     }
222 
UpdatePercentReferenceLayoutConstraintT223     bool UpdatePercentReference(const SizeT<T>& size)
224     {
225         if (percentReference == size) {
226             return false;
227         }
228         percentReference.SetSizeT(size);
229         return true;
230     }
231 
232     std::string ToString() const;
233 
234     SizeF Constrain(const SizeF& size) const;
235 };
236 
237 using LayoutConstraintF = LayoutConstraintT<float>;
238 } // namespace OHOS::Ace::NG
239 
240 #include "layout_constraint.inl"
241 
242 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PROPERTIES_LAYOUT_CONSTRAINT_H
243