1 /*
2  * Copyright (c) 2021-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_BASE_LAYOUT_LAYOUT_PARAM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_LAYOUT_LAYOUT_PARAM_H
18 
19 #include "base/geometry/size.h"
20 
21 namespace OHOS::Ace {
22 
23 class LayoutParam {
24 public:
25     LayoutParam() = default;
26     ~LayoutParam() = default;
27 
LayoutParam(Size max,Size min)28     LayoutParam(Size max, Size min)
29     {
30         SetMaxSize(max);
31         SetMinSize(min);
32     }
33 
GetMaxSize()34     const Size& GetMaxSize() const
35     {
36         return maxSize_;
37     }
38 
SetMaxSize(const Size & size)39     void SetMaxSize(const Size& size)
40     {
41         maxSize_ = size;
42     }
43 
GetMinSize()44     const Size& GetMinSize() const
45     {
46         return minSize_;
47     }
48 
SetMinSize(const Size & size)49     void SetMinSize(const Size& size)
50     {
51         minSize_ = size;
52     }
53 
SetFixedSize(const Size & size)54     void SetFixedSize(const Size& size)
55     {
56         minSize_ = size;
57         maxSize_ = size;
58     }
59 
SetHasUsedConstraints(bool flag)60     void SetHasUsedConstraints(bool flag)
61     {
62         hasUsedConstraints_ = flag;
63     }
64 
HasUsedConstraints()65     bool HasUsedConstraints() const
66     {
67         return hasUsedConstraints_;
68     }
69 
IsValid()70     bool IsValid() const
71     {
72         return maxSize_.IsValid();
73     }
74 
IsTight()75     bool IsTight() const
76     {
77         return (minSize_.Width() >= maxSize_.Width()) && (minSize_.Height() >= maxSize_.Height());
78     }
79 
IsWidthValid()80     bool IsWidthValid() const
81     {
82         return (maxSize_.Width() >= minSize_.Width()) && (maxSize_.Width() > 0.0);
83     }
84 
IsHeightValid()85     bool IsHeightValid() const
86     {
87         return (maxSize_.Height() >= minSize_.Height()) && (maxSize_.Height() > 0.0);
88     }
89 
Constrain(Size size)90     Size Constrain(Size size) const
91     {
92         Size constrainSize;
93         if (size.Width() < 0) {
94             constrainSize.SetWidth(size.Width());
95         } else {
96             constrainSize.SetWidth(std::clamp(size.Width(), minSize_.Width(), maxSize_.Width()));
97         }
98         if (size.Height() < 0) {
99             constrainSize.SetHeight(size.Height());
100         } else {
101             constrainSize.SetHeight(std::clamp(size.Height(), minSize_.Height(), maxSize_.Height()));
102         }
103         return constrainSize;
104     }
105 
Enforce(const LayoutParam & layoutParam)106     LayoutParam Enforce(const LayoutParam& layoutParam) const
107     {
108         Size min = layoutParam.Constrain(minSize_);
109         Size max = layoutParam.Constrain(maxSize_);
110         return LayoutParam(max, min);
111     }
112 
113     bool operator==(const LayoutParam& layoutParam) const
114     {
115         return (minSize_ == layoutParam.minSize_) && (maxSize_ == layoutParam.maxSize_);
116     }
117 
118     bool operator!=(const LayoutParam& layoutParam) const
119     {
120         return !operator==(layoutParam);
121     }
122 
ToString()123     std::string ToString() const
124     {
125         return std::string(minSize_.ToString())
126             .append(" - ")
127             .append(maxSize_.ToString());
128     }
129 
SetMinWidth(double value)130     void SetMinWidth(double value)
131     {
132         minSize_.SetWidth(value);
133     }
134 
SetMaxWidth(double value)135     void SetMaxWidth(double value)
136     {
137         maxSize_.SetWidth(value);
138     }
139 
140 private:
141     Size minSize_ { 0.0f, 0.0f };
142     Size maxSize_ { Size::INFINITE_SIZE, Size::INFINITE_SIZE };
143     bool hasUsedConstraints_ = false;
144 };
145 
146 enum class MeasureType {
147     PARENT,       // return the max layout params from parent
148     CONTENT,      // return the current component size
149     DEFAULT       // user hasn't set the component size
150 };
151 
152 } // namespace OHOS::Ace
153 
154 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_LAYOUT_LAYOUT_PARAM_H
155