• 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 template<typename T>
29 struct LayoutConstraintT {
30     ScaleProperty scaleProperty = ScaleProperty::CreateScaleProperty();
31     SizeT<T> minSize { 0, 0 };
32     SizeT<T> maxSize { Infinity<T>(), Infinity<T>() };
33     SizeT<T> percentReference { 0, 0 };
34     OptionalSize<T> parentIdealSize;
35     OptionalSize<T> selfIdealSize;
36 
37     void ApplyAspectRatio(float ratio, const std::optional<CalcSize>& calcSize, bool greaterThanApiTen = false);
38 
39     void ApplyAspectRatioToParentIdealSize(bool useWidth, float ratio);
40 
41     void ApplyAspectRatioByMaxSize(float ratio, std::optional<bool> useDefinedWidth, bool greaterThanApiTen = false);
42 
43     void ApplyAspectRatioWithCalcSize(float ratio, bool useDefinedWidth);
44 
45     void ApplyAspectRatioWithoutCalcSize(float ratio, bool greaterThanApiTen = false);
46 
47     void Reset();
48 
49     void MinusPadding(const std::optional<T>& left, const std::optional<T>& right,
50         const std::optional<T>& top, const std::optional<T>& bottom);
51 
52     bool operator==(const LayoutConstraintT& layoutConstraint) const
53     {
54         return (scaleProperty == layoutConstraint.scaleProperty) && (minSize == layoutConstraint.minSize) &&
55                (maxSize == layoutConstraint.maxSize) && (percentReference == layoutConstraint.percentReference) &&
56                (parentIdealSize == layoutConstraint.parentIdealSize) &&
57                (selfIdealSize == layoutConstraint.selfIdealSize);
58     }
59 
60     bool operator!=(const LayoutConstraintT& layoutConstraint) const
61     {
62         return !(*this == layoutConstraint);
63     }
64 
EqualWithoutPercentWidthLayoutConstraintT65     bool EqualWithoutPercentWidth(const LayoutConstraintT& layoutConstraint) const
66     {
67         return (scaleProperty == layoutConstraint.scaleProperty) && (minSize == layoutConstraint.minSize) &&
68             (maxSize == layoutConstraint.maxSize) && (parentIdealSize == layoutConstraint.parentIdealSize) &&
69             (percentReference.Height() == layoutConstraint.percentReference.Height()) &&
70             (selfIdealSize == layoutConstraint.selfIdealSize);
71     }
72 
EqualWithoutPercentHeightLayoutConstraintT73     bool EqualWithoutPercentHeight(const LayoutConstraintT& layoutConstraint) const
74     {
75         return (scaleProperty == layoutConstraint.scaleProperty) && (minSize == layoutConstraint.minSize) &&
76             (maxSize == layoutConstraint.maxSize) && (parentIdealSize == layoutConstraint.parentIdealSize) &&
77             (percentReference.Width() == layoutConstraint.percentReference.Width()) &&
78             (selfIdealSize == layoutConstraint.selfIdealSize);
79     }
80 
UpdateSelfMarginSizeWithCheckLayoutConstraintT81     bool UpdateSelfMarginSizeWithCheck(const OptionalSize<T>& size)
82     {
83         if (selfIdealSize == size) {
84             return false;
85         }
86         return selfIdealSize.UpdateSizeWithCheck(size);
87     }
88 
UpdateIllegalSelfMarginSizeWithCheckLayoutConstraintT89     bool UpdateIllegalSelfMarginSizeWithCheck(const OptionalSize<T>& size)
90     {
91         if (selfIdealSize == size) {
92             return false;
93         }
94         return selfIdealSize.UpdateIllegalSizeWithCheck(size);
95     }
96 
UpdateIllegalSelfIdealSizeWithCheckLayoutConstraintT97     bool UpdateIllegalSelfIdealSizeWithCheck(const OptionalSize<T>& size)
98     {
99         if (selfIdealSize == size) {
100             return false;
101         }
102         return selfIdealSize.UpdateIllegalSizeWithCheck(size);
103     }
104 
UpdateParentIdealSizeWithCheckLayoutConstraintT105     bool UpdateParentIdealSizeWithCheck(const OptionalSize<T>&& size)
106     {
107         if (parentIdealSize == size) {
108             return false;
109         }
110         return parentIdealSize.UpdateSizeWithCheck(size);
111     }
112 
UpdateIllegalParentIdealSizeWithCheckLayoutConstraintT113     bool UpdateIllegalParentIdealSizeWithCheck(const OptionalSize<T>&& size)
114     {
115         if (parentIdealSize == size) {
116             return false;
117         }
118         return parentIdealSize.UpdateIllegalSizeWithCheck(size);
119     }
120 
UpdateMaxSizeWithCheckLayoutConstraintT121     bool UpdateMaxSizeWithCheck(const SizeT<T>& size)
122     {
123         if (maxSize == size) {
124             return false;
125         }
126         return maxSize.UpdateSizeWhenSmaller(size);
127     }
128 
UpdateMaxWidthWithCheckLayoutConstraintT129     bool UpdateMaxWidthWithCheck(const SizeT<T>& size)
130     {
131         if (maxSize == size) {
132             return false;
133         }
134         return maxSize.UpdateWidthWhenSmaller(size);
135     }
136 
UpdateMaxHeightWithCheckLayoutConstraintT137     bool UpdateMaxHeightWithCheck(const SizeT<T>& size)
138     {
139         if (maxSize == size) {
140             return false;
141         }
142         return maxSize.UpdateHeightWhenSmaller(size);
143     }
144 
UpdateMinSizeWithCheckLayoutConstraintT145     bool UpdateMinSizeWithCheck(const SizeT<T>& size)
146     {
147         if (minSize == size) {
148             return false;
149         }
150         return minSize.UpdateSizeWhenLarger(size);
151     }
152 
UpdatePercentReferenceLayoutConstraintT153     bool UpdatePercentReference(const SizeT<T>& size)
154     {
155         if (percentReference == size) {
156             return false;
157         }
158         percentReference.SetSizeT(size);
159         return true;
160     }
161 
162     std::string ToString() const;
163 
164     SizeF Constrain(const SizeF& size) const;
165 };
166 
167 using LayoutConstraintF = LayoutConstraintT<float>;
168 } // namespace OHOS::Ace::NG
169 
170 #include "layout_constraint.inl"
171 
172 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PROPERTIES_LAYOUT_CONSTRAINT_H
173