1 /*
2  * Copyright (c) 2023 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_PATTERNS_SWIPER_INDICATOR_SWIPER_INDICATOR_UTILS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_INDICATOR_SWIPER_INDICATOR_UTILS_H
18 
19 #include <optional>
20 
21 #include "core/components/declaration/swiper/swiper_declaration.h"
22 #include "core/components_ng/pattern/swiper/swiper_layout_property.h"
23 #include "core/components_ng/pattern/swiper_indicator/indicator_common/swiper_indicator_layout_property.h"
24 
25 namespace OHOS::Ace::NG {
26 static SwiperIndicatorType swiperIndicatorType = SwiperIndicatorType::DOT;
27 
GET_PADDING_PROPERTY_VALUE_PX(const std::optional<CalcLength> & value)28 inline float GET_PADDING_PROPERTY_VALUE_PX(const std::optional<CalcLength>& value)
29 {
30     return value.value_or(CalcLength(0.0_vp)).GetDimension().ConvertToPx();
31 }
32 
GET_BORDER_PROPERTY_VALUE_PX(const std::optional<Dimension> & value)33 inline float GET_BORDER_PROPERTY_VALUE_PX(const std::optional<Dimension>& value)
34 {
35     return value.value_or(Dimension(0.0, DimensionUnit::PX)).ConvertToPx();
36 }
37 
38 class SwiperIndicatorUtils {
39 public:
40     SwiperIndicatorUtils() = delete;
41     ~SwiperIndicatorUtils() = delete;
42 
GetSwiperIndicatorType()43     static SwiperIndicatorType GetSwiperIndicatorType()
44     {
45         return swiperIndicatorType;
46     }
47 
SetSwiperIndicatorType(SwiperIndicatorType indicatorType)48     static void SetSwiperIndicatorType(SwiperIndicatorType indicatorType)
49     {
50         swiperIndicatorType = indicatorType;
51     }
52 
CalcIndicatrFrameOffSet(const RefPtr<SwiperLayoutProperty> & swiperLayoutProperty,const RefPtr<SwiperIndicatorLayoutProperty> & indicatorLayoutProperty,float indicatorWidth,float indicatorHeight)53     static const OffsetF CalcIndicatrFrameOffSet(const RefPtr<SwiperLayoutProperty>& swiperLayoutProperty,
54                                                 const RefPtr<SwiperIndicatorLayoutProperty>& indicatorLayoutProperty,
55                                                 float indicatorWidth, float indicatorHeight)
56     {
57         auto direction = Axis::HORIZONTAL;
58         float swiperPaddingLeft = 0.0f;
59         float swiperPaddingRight = 0.0f;
60         float swiperPaddingTop = 0.0f;
61         float swiperPaddingBottom = 0.0f;
62         float borderWidthLeft = 0.0f;
63         float borderWidthRight = 0.0f;
64         float borderWidthTop = 0.0f;
65         float borderWidthBottom = 0.0f;
66         if (swiperLayoutProperty) {
67             direction = swiperLayoutProperty->GetDirectionValue(Axis::HORIZONTAL);
68             const auto& swiperPaddingProperty = swiperLayoutProperty->GetPaddingProperty();
69             const auto& borderWidthProperty = swiperLayoutProperty->GetBorderWidthProperty();
70             if (swiperPaddingProperty != nullptr) {
71                 swiperPaddingLeft = GET_PADDING_PROPERTY_VALUE_PX(swiperPaddingProperty->left);
72                 swiperPaddingRight = GET_PADDING_PROPERTY_VALUE_PX(swiperPaddingProperty->right);
73                 swiperPaddingTop = GET_PADDING_PROPERTY_VALUE_PX(swiperPaddingProperty->top);
74                 swiperPaddingBottom = GET_PADDING_PROPERTY_VALUE_PX(swiperPaddingProperty->bottom);
75             }
76             if (borderWidthProperty != nullptr) {
77                 borderWidthLeft = GET_BORDER_PROPERTY_VALUE_PX(borderWidthProperty->leftDimen);
78                 borderWidthRight = GET_BORDER_PROPERTY_VALUE_PX(borderWidthProperty->rightDimen);
79                 borderWidthTop = GET_BORDER_PROPERTY_VALUE_PX(borderWidthProperty->topDimen);
80                 borderWidthBottom = GET_BORDER_PROPERTY_VALUE_PX(borderWidthProperty->bottomDimen);
81             }
82         }
83 
84         CHECK_NULL_RETURN(indicatorLayoutProperty, OffsetF(0.0f, 0.0f));
85         auto left = indicatorLayoutProperty->GetLeft();
86         auto right = indicatorLayoutProperty->GetRight();
87         auto top = indicatorLayoutProperty->GetTop();
88         auto bottom = indicatorLayoutProperty->GetBottom();
89         const auto& layoutConstraint = indicatorLayoutProperty->GetLayoutConstraint();
90         float swiperWidth = 0.0f;
91         float swiperHeight = 0.0f;
92         if (layoutConstraint.has_value()) {
93             swiperWidth = layoutConstraint->parentIdealSize.Width().value_or(0.0f);
94             swiperHeight = layoutConstraint->parentIdealSize.Height().value_or(0.0f);
95         }
96 
97         return OffsetF {CalcIndicatrOffSetX(left, right, swiperPaddingLeft, swiperPaddingRight,
98                                             borderWidthLeft, borderWidthRight,
99                                             swiperWidth, indicatorWidth, direction),
100                         CalcIndicatrOffsetY(top, bottom, swiperPaddingTop, swiperPaddingBottom,
101                                             borderWidthTop, borderWidthBottom,
102                                             swiperHeight, indicatorHeight, direction)};
103     }
104 
GetValidEdgeLength(float swiperLength,float indicatorLength,const Dimension & edge)105     static float GetValidEdgeLength(float swiperLength, float indicatorLength, const Dimension& edge)
106     {
107         float edgeLength = edge.Unit() == DimensionUnit::PERCENT ? swiperLength * edge.Value() : edge.ConvertToPx();
108         if (!NearZero(edgeLength) && edgeLength > (swiperLength - indicatorLength)) {
109             edgeLength = swiperLength - indicatorLength;
110         }
111         if (edgeLength < 0.0) {
112             edgeLength = 0.0;
113         }
114         return edgeLength;
115     }
116 
GetLoopIndex(int32_t index,int32_t totalCount)117     static int32_t GetLoopIndex(int32_t index, int32_t totalCount)
118     {
119         if (totalCount <= 0) {
120             return index;
121         }
122 
123         auto loopIndex = index;
124         while (loopIndex < 0) {
125             loopIndex = loopIndex + totalCount;
126         }
127         loopIndex %= totalCount;
128         return loopIndex;
129     }
130 
CalcLoopCount(int32_t index,int32_t totalCount)131     static int32_t CalcLoopCount(int32_t index, int32_t totalCount)
132     {
133         if (totalCount <= 0) {
134             return 0;
135         }
136 
137         if (index < 0) {
138             index++;
139         }
140 
141         return std::abs(index / totalCount);
142     }
143 
144 private:
CalcIndicatrOffSetX(const std::optional<Dimension> & left,const std::optional<Dimension> & right,float swiperPaddingLeft,float swiperPaddingRight,float borderWidthLeft,float borderWidthRight,float swiperWidth,float indicatorWidth,const Axis & direction)145     static float CalcIndicatrOffSetX(const std::optional<Dimension>& left, const std::optional<Dimension>& right,
146                                     float swiperPaddingLeft, float swiperPaddingRight,
147                                     float borderWidthLeft, float borderWidthRight,
148                                     float swiperWidth, float indicatorWidth, const Axis& direction)
149     {
150         float offsetX = 0.0f;
151         if (left.has_value() && GreatOrEqual(left.value().Value(), 0)) {
152             auto leftValue = GetValidEdgeLength(swiperWidth, indicatorWidth, left.value());
153             offsetX = leftValue + swiperPaddingLeft + borderWidthLeft;
154         } else if (right.has_value() && GreatOrEqual(right.value().Value(), 0)) {
155             auto rightValue = GetValidEdgeLength(swiperWidth, indicatorWidth, right.value());
156             offsetX = swiperWidth - indicatorWidth - rightValue - swiperPaddingRight - borderWidthRight;
157         } else {
158             if (direction == Axis::HORIZONTAL) {
159                 offsetX = (swiperWidth - swiperPaddingRight - borderWidthRight +
160                     swiperPaddingLeft + borderWidthLeft - indicatorWidth) * 0.5f;
161             } else {
162                 offsetX = swiperWidth - indicatorWidth - swiperPaddingRight - borderWidthRight;
163             }
164         }
165         return offsetX;
166     }
167 
CalcIndicatrOffsetY(const std::optional<Dimension> & top,const std::optional<Dimension> & bottom,float swiperPaddingTop,float swiperPaddingBottom,float borderWidthTop,float borderWidthBottom,float swiperHeight,float indicatorHeight,const Axis & direction)168     static float CalcIndicatrOffsetY(const std::optional<Dimension>& top, const std::optional<Dimension>& bottom,
169                                     float swiperPaddingTop, float swiperPaddingBottom,
170                                     float borderWidthTop, float borderWidthBottom,
171                                     float swiperHeight, float indicatorHeight, const Axis& direction)
172     {
173         float offsetY = 0.0f;
174         if (top.has_value() && GreatOrEqual(top.value().Value(), 0)) {
175             auto topValue = GetValidEdgeLength(swiperHeight, indicatorHeight, top.value());
176             offsetY = topValue + swiperPaddingTop + borderWidthTop;
177         } else if (bottom.has_value() && GreatOrEqual(bottom.value().Value(), 0)) {
178             auto bottomValue = GetValidEdgeLength(swiperHeight, indicatorHeight, bottom.value());
179             offsetY = swiperHeight - indicatorHeight - bottomValue - swiperPaddingBottom - borderWidthBottom;
180         } else {
181             if (direction == Axis::HORIZONTAL) {
182                 offsetY = swiperHeight - indicatorHeight - swiperPaddingBottom - borderWidthBottom;
183             } else {
184                 offsetY = (swiperHeight - swiperPaddingBottom - borderWidthBottom +
185                     swiperPaddingTop + borderWidthTop - indicatorHeight) * 0.5f;
186             }
187         }
188         return offsetY;
189     }
190 };
191 } // namespace OHOS::Ace::NG
192 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_INDICATOR_SWIPER_INDICATOR_UTILS_H
193