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