• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_POSITION_PARAM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_LAYOUT_POSITION_PARAM_H
18 
19 #include <functional>
20 #include <optional>
21 
22 #include "base/geometry/animatable_dimension.h"
23 #include "base/geometry/calc_dimension.h"
24 #include "base/geometry/dimension.h"
25 #include "core/common/resource/resource_object.h"
26 #include "core/components/common/layout/constants.h"
27 #include "core/pipeline/base/constants.h"
28 
29 namespace OHOS::Ace {
30 
31 constexpr int32_t HORIZONTAL_DIRECTION_RANGE = 3;
32 constexpr int32_t VERTICAL_DIRECTION_RANGE = 6;
33 constexpr int32_t HORIZONTAL_DIRECTION_START_INDEX = 7;
34 constexpr int32_t HORIZONTAL_DIRECTION_END_INDEX = 8;
35 
36 struct PositionParam {
37     std::pair<AnimatableDimension, bool> left = { AnimatableDimension(0.0, DimensionUnit::PX), false };
38     std::pair<AnimatableDimension, bool> right = { AnimatableDimension(0.0, DimensionUnit::PX), false };
39     std::pair<AnimatableDimension, bool> top = { AnimatableDimension(0.0, DimensionUnit::PX), false };
40     std::pair<AnimatableDimension, bool> bottom = { AnimatableDimension(0.0, DimensionUnit::PX), false };
41     std::pair<Dimension, Dimension> anchor = {0.0_px, 0.0_px};
42     PositionType type = PositionType::PTRELATIVE;
43 };
44 
45 struct EdgesParam {
46     std::optional<Dimension> top;
47     std::optional<Dimension> left;
48     std::optional<Dimension> bottom;
49     std::optional<Dimension> right;
50     std::optional<Dimension> start;
51     std::optional<Dimension> end;
52     struct resourceUpdater {
53         RefPtr<ResourceObject> resObj;
54         std::function<void(const RefPtr<ResourceObject>&, EdgesParam&)> updateFunc;
55     };
56     std::unordered_map<std::string, resourceUpdater> resMap_;
57     EdgesParam() = default;
58 
SetTopEdgesParam59     void SetTop(const CalcDimension& top)
60     {
61         this->top = top;
62     }
63 
SetLeftEdgesParam64     void SetLeft(const CalcDimension& left)
65     {
66         this->left = left;
67     }
68 
SetBottomEdgesParam69     void SetBottom(const CalcDimension& bottom)
70     {
71         this->bottom = bottom;
72     }
73 
SetRightEdgesParam74     void SetRight(const CalcDimension& right)
75     {
76         this->right = right;
77     }
78 
79     bool operator==(const EdgesParam& rhs) const
80     {
81         return ((this->top == rhs.top) && (this->left == rhs.left) && (this->bottom == rhs.bottom) &&
82                 (this->right == rhs.right) && (this->start == rhs.start) && (this->end == rhs.end));
83     }
84 
ToStringEdgesParam85     std::string ToString() const
86     {
87         std::string str;
88         str.append("top: [").append(top.has_value() ? top->ToString() : "NA").append("]");
89         str.append("left: [").append(left.has_value() ? left->ToString() : "NA").append("]");
90         str.append("right: [").append(right.has_value() ? right->ToString() : "NA").append("]");
91         str.append("bottom: [").append(bottom.has_value() ? bottom->ToString() : "NA").append("]");
92         return str;
93     }
94 
AddResourceEdgesParam95     void AddResource(
96         const std::string& key,
97         const RefPtr<ResourceObject>& resObj,
98         std::function<void(const RefPtr<ResourceObject>&, EdgesParam&)>&& updateFunc)
99     {
100         if (resObj == nullptr || !updateFunc) {
101             return;
102         }
103         resMap_[key] = {resObj, std::move(updateFunc)};
104     }
105 
ReloadResourcesEdgesParam106     void ReloadResources()
107     {
108         for (const auto& [key, resourceUpdater] : resMap_) {
109             resourceUpdater.updateFunc(resourceUpdater.resObj, *this);
110         }
111     }
112 };
113 
114 enum class AlignDirection {
115     LEFT = 0,
116     MIDDLE,
117     RIGHT,
118     TOP,
119     CENTER,
120     BOTTOM,
121     START,
122     END,
123 };
124 struct AlignRule {
125     std::string anchor;
126     union {
127         HorizontalAlign horizontal;
128         VerticalAlign vertical;
129     };
130 
131     bool operator==(const AlignRule& right) const
132     {
133         return ((this->anchor == right.anchor) && (this->vertical == right.vertical) &&
134                 (this->horizontal == right.horizontal));
135     }
136     bool operator!=(const AlignRule& right) const
137     {
138         return !operator==(right);
139     }
140 };
141 
142 enum class ChainStyle {
143     SPREAD,
144     SPREAD_INSIDE,
145     PACKED,
146 };
147 
148 enum class LineDirection {
149     VERTICAL,
150     HORIZONTAL,
151 };
152 
153 enum class BarrierDirection {
154     LEFT,
155     RIGHT,
156     TOP,
157     BOTTOM,
158     START,
159     END,
160 };
161 
162 struct GuidelineInfo {
163     std::string id;
164     LineDirection direction = LineDirection::VERTICAL;
165     std::optional<Dimension> start;
166     std::optional<Dimension> end;
167     struct resourceUpdater {
168         RefPtr<ResourceObject> resObj;
169         std::function<void(const RefPtr<ResourceObject>&, GuidelineInfo&)> updateFunc;
170     };
171     std::unordered_map<std::string, resourceUpdater> resMap_;
172 
173     bool operator==(const GuidelineInfo& right) const
174     {
175         return ((this->id == right.id) && (this->direction == right.direction) &&
176                 (this->start == right.start) && (this->end == right.end));
177     }
178 
AddResourceGuidelineInfo179     void AddResource(
180         const std::string& key,
181         const RefPtr<ResourceObject>& resObj,
182         std::function<void(const RefPtr<ResourceObject>&, GuidelineInfo&)>&& updateFunc)
183     {
184         if (resObj == nullptr || !updateFunc) {
185             return;
186         }
187         resMap_[key] = {resObj, std::move(updateFunc)};
188     }
189 
ReloadResourcesGuidelineInfo190     void ReloadResources()
191     {
192         for (const auto& [key, resourceUpdater] : resMap_) {
193             resourceUpdater.updateFunc(resourceUpdater.resObj, *this);
194         }
195     }
196 };
197 
198 struct ChainInfo {
199     std::optional<LineDirection> direction;
200     std::optional<ChainStyle> style;
201 
202     bool operator==(const ChainInfo& right) const
203     {
204         return ((this->direction == right.direction) && (this->style == right.style));
205     }
206 };
207 
208 struct BarrierInfo {
209     std::string id;
210     BarrierDirection direction = BarrierDirection::LEFT;
211     std::vector<std::string> referencedId;
212 
213     bool operator==(const BarrierInfo& right) const
214     {
215         return ((this->id == right.id) && (this->direction == right.direction) &&
216                 (this->referencedId == right.referencedId));
217     }
218 };
219 
220 #ifdef ACE_STATIC
221 struct LocalizedBarrierInfo : public BarrierInfo {
LocalizedBarrierInfoLocalizedBarrierInfo222     LocalizedBarrierInfo()
223     {
224         direction = BarrierDirection::START;
225     }
226 };
227 #endif
228 
229 } // namespace OHOS::Ace
230 
231 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_LAYOUT_POSITION_PARAM_H
232