1 /*
2 * Copyright (c) 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 #include "core/components_ng/pattern/swiper/swiper_pattern.h"
17
18 #include "base/log/log_wrapper.h"
19 #include "core/components/common/layout/constants.h"
20
21 namespace OHOS::Ace::NG {
22
OnAttachToFrameNodeMultiThread()23 void SwiperPattern::OnAttachToFrameNodeMultiThread()
24 {
25 auto host = GetHost();
26 CHECK_NULL_VOID(host);
27 auto renderContext = host->GetRenderContext();
28 CHECK_NULL_VOID(renderContext);
29 renderContext->SetClipToFrame(true);
30 renderContext->SetClipToBounds(true);
31 }
32
OnDetachFromFrameNodeMultiThread(FrameNode * node)33 void SwiperPattern::OnDetachFromFrameNodeMultiThread(FrameNode* node)
34 {
35 }
36
OnAttachToMainTreeMultiThread()37 void SwiperPattern::OnAttachToMainTreeMultiThread()
38 {
39 do {
40 auto host = GetHost();
41 CHECK_NULL_BREAK(host);
42 auto pipeline = host->GetContext();
43 CHECK_NULL_BREAK(pipeline);
44 auto renderContext = host->GetRenderContext();
45 CHECK_NULL_BREAK(renderContext);
46 auto indicatorTheme = pipeline->GetTheme<SwiperIndicatorTheme>();
47 CHECK_NULL_BREAK(indicatorTheme);
48 renderContext->UpdateClipEdge(indicatorTheme->GetClipEdge());
49 InitSurfaceChangedCallback();
50 } while (false);
51 if (!isInit_) {
52 SetOnHiddenChangeForParent();
53 }
54 }
55
OnDetachFromMainTreeMultiThread()56 void SwiperPattern::OnDetachFromMainTreeMultiThread()
57 {
58 RemoveOnHiddenChange();
59 auto node = GetHost();
60 CHECK_NULL_VOID(node);
61 auto pipeline = node->GetContext();
62 CHECK_NULL_VOID(pipeline);
63 if (HasSurfaceChangedCallback()) {
64 pipeline->UnregisterSurfaceChangedCallback(surfaceChangedCallbackId_.value_or(-1));
65 }
66 pipeline->RemoveWindowStateChangedCallback(node->GetId());
67 }
68
ChangeIndexMultiThread(int32_t index,bool useAnimation)69 void SwiperPattern::ChangeIndexMultiThread(int32_t index, bool useAnimation)
70 {
71 auto host = GetHost();
72 CHECK_NULL_VOID(host);
73 auto changeTask = [weakPattern = WeakClaim(this), index, useAnimation]() {
74 auto pattern = weakPattern.Upgrade();
75 CHECK_NULL_VOID(pattern);
76 pattern->ChangeIndex(index, useAnimation);
77 };
78 host->PostAfterAttachMainTreeTask(std::move(changeTask));
79 }
80
ChangeIndexMultiThread(int32_t index,SwiperAnimationMode mode)81 void SwiperPattern::ChangeIndexMultiThread(int32_t index, SwiperAnimationMode mode)
82 {
83 auto host = GetHost();
84 CHECK_NULL_VOID(host);
85 auto changeTask = [weakPattern = WeakClaim(this), index, mode]() {
86 auto pattern = weakPattern.Upgrade();
87 CHECK_NULL_VOID(pattern);
88 pattern->ChangeIndex(index, mode);
89 };
90 host->PostAfterAttachMainTreeTask(std::move(changeTask));
91 }
92
SetCachedCountMultiThread(int32_t cachedCount)93 void SwiperPattern::SetCachedCountMultiThread(int32_t cachedCount)
94 {
95 auto host = GetHost();
96 CHECK_NULL_VOID(host);
97 auto setTask = [weakPattern = WeakClaim(this), cachedCount]() {
98 auto pattern = weakPattern.Upgrade();
99 CHECK_NULL_VOID(pattern);
100 pattern->SetCachedCount(cachedCount);
101 };
102 host->PostAfterAttachMainTreeTask(std::move(setTask));
103 }
104 } // namespace OHOS::Ace::NG
105