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
117 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)118 static float CalcIndicatrOffSetX(const std::optional<Dimension>& left, const std::optional<Dimension>& right,
119 float swiperPaddingLeft, float swiperPaddingRight,
120 float borderWidthLeft, float borderWidthRight,
121 float swiperWidth, float indicatorWidth, const Axis& direction)
122 {
123 float offsetX = 0.0f;
124 if (left.has_value() && !NearZero(left.value().Value())) {
125 auto leftValue = GetValidEdgeLength(swiperWidth, indicatorWidth, left.value());
126 offsetX = leftValue + swiperPaddingLeft + borderWidthLeft;
127 } else if (right.has_value() && !NearZero(right.value().Value())) {
128 auto rightValue = GetValidEdgeLength(swiperWidth, indicatorWidth, right.value());
129 offsetX = swiperWidth - indicatorWidth - rightValue - swiperPaddingRight - borderWidthRight;
130 } else {
131 if (direction == Axis::HORIZONTAL) {
132 offsetX = (swiperWidth - swiperPaddingRight - borderWidthRight +
133 swiperPaddingLeft + borderWidthLeft - indicatorWidth) * 0.5f;
134 } else {
135 offsetX = swiperWidth - indicatorWidth - swiperPaddingRight - borderWidthRight;
136 }
137 }
138 return offsetX;
139 }
140
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)141 static float CalcIndicatrOffsetY(const std::optional<Dimension>& top, const std::optional<Dimension>& bottom,
142 float swiperPaddingTop, float swiperPaddingBottom,
143 float borderWidthTop, float borderWidthBottom,
144 float swiperHeight, float indicatorHeight, const Axis& direction)
145 {
146 float offsetY = 0.0f;
147 if (top.has_value() && !NearZero(top.value().Value())) {
148 auto topValue = GetValidEdgeLength(swiperHeight, indicatorHeight, top.value());
149 offsetY = topValue + swiperPaddingTop + borderWidthTop;
150 } else if (bottom.has_value() && !NearZero(bottom.value().Value())) {
151 auto bottomValue = GetValidEdgeLength(swiperHeight, indicatorHeight, bottom.value());
152 offsetY = swiperHeight - indicatorHeight - bottomValue - swiperPaddingBottom - borderWidthBottom;
153 } else {
154 if (direction == Axis::HORIZONTAL) {
155 offsetY = swiperHeight - indicatorHeight - swiperPaddingBottom - borderWidthBottom;
156 } else {
157 offsetY = (swiperHeight - swiperPaddingBottom - borderWidthBottom +
158 swiperPaddingTop + borderWidthTop - indicatorHeight) * 0.5f;
159 }
160 }
161 return offsetY;
162 }
163 };
164 } // namespace OHOS::Ace::NG
165 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_INDICATOR_SWIPER_INDICATOR_UTILS_H
166