1 /*
2 * Copyright (c) 2024 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 #include "core/components_ng/pattern/swiper_indicator/indicator_common/indicator_controller.h"
17
18 #include "core/components_ng/pattern/swiper_indicator/indicator_common/indicator_pattern.h"
19
20 namespace OHOS::Ace::NG {
ShowPrevious() const21 void IndicatorController::ShowPrevious() const
22 {
23 if (indicatorPattern_.Upgrade()) {
24 indicatorPattern_.Upgrade()->ShowPrevious();
25 }
26 }
27
ShowNext() const28 void IndicatorController::ShowNext() const
29 {
30 if (indicatorPattern_.Upgrade()) {
31 indicatorPattern_.Upgrade()->ShowNext();
32 }
33 }
34
ChangeIndex(int32_t index,bool useAnimation) const35 void IndicatorController::ChangeIndex(int32_t index, bool useAnimation) const
36 {
37 if (indicatorPattern_.Upgrade()) {
38 indicatorPattern_.Upgrade()->ChangeIndex(index, useAnimation);
39 }
40 }
41
ResetJSIndicatorController()42 void IndicatorController::ResetJSIndicatorController()
43 {
44 if (resetFunc_) {
45 resetFunc_();
46 resetFunc_ = nullptr;
47 }
48 }
49
SetJSIndicatorController(std::function<void ()> resetFunc)50 void IndicatorController::SetJSIndicatorController(std::function<void()> resetFunc)
51 {
52 resetFunc_ = resetFunc;
53 }
54
SetIndicatorPattern(const RefPtr<IndicatorPattern> & indicatorPattern)55 void IndicatorController::SetIndicatorPattern(const RefPtr<IndicatorPattern>& indicatorPattern)
56 {
57 indicatorPattern_ = indicatorPattern;
58 }
59
SetSwiperNode(const RefPtr<FrameNode> & swiperNode)60 void IndicatorController::SetSwiperNode(const RefPtr<FrameNode>& swiperNode)
61 {
62 swiperNode_ = swiperNode;
63 if (indicatorPattern_.Upgrade() && swiperNode) {
64 auto indicatorNode = indicatorPattern_.Upgrade()->GetHost();
65 CHECK_NULL_VOID(indicatorNode);
66 auto pattern = swiperNode->GetPattern<SwiperPattern>();
67 CHECK_NULL_VOID(pattern);
68 auto lastIndicatorNode = pattern->GetIndicatorNode();
69 pattern->SetIndicatorNode(indicatorNode);
70 if (lastIndicatorNode && lastIndicatorNode != indicatorNode) {
71 auto lastIndicatorPattern = lastIndicatorNode->GetPattern<IndicatorPattern>();
72 CHECK_NULL_VOID(lastIndicatorPattern);
73 lastIndicatorPattern->ResetSwiperNode();
74 }
75 }
76 }
77
GetSwiperNode() const78 const WeakPtr<FrameNode>& IndicatorController::GetSwiperNode() const
79 {
80 return swiperNode_;
81 }
82
ResetIndicatorControllor(const RefPtr<IndicatorController> & controller,const RefPtr<FrameNode> & indicatorNode)83 void IndicatorController::ResetIndicatorControllor(
84 const RefPtr<IndicatorController>& controller, const RefPtr<FrameNode>& indicatorNode)
85 {
86 CHECK_NULL_VOID(indicatorNode);
87 auto indicatorPattern = indicatorNode->GetPattern<IndicatorPattern>();
88 CHECK_NULL_VOID(indicatorPattern);
89 auto lastSwiperNode = indicatorPattern->GetBindSwiperNode();
90 controller->SetSwiperNode(lastSwiperNode);
91 indicatorPattern->ResetJSIndicatorController();
92 }
93
UpdateIndicatorNode()94 void IndicatorController::UpdateIndicatorNode()
95 {
96 auto indicatorPattern = indicatorPattern_.Upgrade();
97 CHECK_NULL_VOID(indicatorPattern);
98 auto dotIndicatorModifier = indicatorPattern->GetDotIndicatorModifier();
99 if (dotIndicatorModifier) {
100 dotIndicatorModifier->StopAnimation();
101 }
102 auto indicatorNode = indicatorPattern->GetHost();
103 CHECK_NULL_VOID(indicatorNode);
104 indicatorNode->MarkDirtyNode(NG::PROPERTY_UPDATE_MEASURE);
105 }
106
GetIndicatorNode()107 RefPtr<FrameNode> IndicatorController::GetIndicatorNode()
108 {
109 auto indicatorPattern = indicatorPattern_.Upgrade();
110 CHECK_NULL_RETURN(indicatorPattern, nullptr);
111 return indicatorPattern->GetHost();
112 }
113 } // namespace OHOS::Ace::NG
114