• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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/scrollable/nestable_scroll_container.h"
17 
18 #include "core/components_ng/pattern/refresh/refresh_pattern.h"
19 #include "core/components_ng/pattern/scrollable/scrollable_pattern.h"
20 
21 namespace OHOS::Ace::NG {
SearchParent()22 RefPtr<NestableScrollContainer> NestableScrollContainer::SearchParent()
23 {
24     auto host = GetHost();
25     CHECK_NULL_RETURN(host, nullptr);
26     for (auto parent = host->GetParent(); parent != nullptr; parent = parent->GetParent()) {
27         RefPtr<FrameNode> frameNode = AceType::DynamicCast<FrameNode>(parent);
28         if (!frameNode) {
29             continue;
30         }
31         auto pattern = frameNode->GetPattern<NestableScrollContainer>();
32         if (!pattern) {
33             continue;
34         }
35         if ((!AceApplicationInfo::GetInstance().GreatOrEqualTargetAPIVersion(PlatformVersion::VERSION_TWELVE) ||
36                 !isSearchRefresh_) &&
37             InstanceOf<RefreshPattern>(pattern)) {
38             continue;
39         }
40         if (pattern->GetAxis() != GetAxis()) {
41             continue;
42         }
43         return pattern;
44     }
45     return nullptr;
46 }
47 
UpdateNestedModeForChildren(const NestedScrollOptions & childNestedScroll)48 void NestableScrollContainer::UpdateNestedModeForChildren(const NestedScrollOptions& childNestedScroll)
49 {
50     if (!childNestedScroll_) {
51         childNestedScroll_ = std::make_unique<NestedScrollOptions>();
52     }
53     if (*childNestedScroll_ != childNestedScroll) {
54         *childNestedScroll_ = childNestedScroll;
55     }
56 }
57 
SetNestedScroll(const NestedScrollOptions & nestedScroll,bool isFixedNestedScrollMode)58 void NestableScrollContainer::SetNestedScroll(const NestedScrollOptions& nestedScroll, bool isFixedNestedScrollMode)
59 {
60     if (isFixedNestedScrollMode_ && !nestedScroll.NeedParent()) {
61         return;
62     }
63     if (!isFixedNestedScrollMode && AceType::InstanceOf<ScrollablePattern>(this)) {
64         if (nestedScroll.NeedParent()) {
65             isSearchRefresh_ = false;
66         } else {
67             isSearchRefresh_ = true;
68         }
69     }
70     SetIsFixedNestedScrollMode(isFixedNestedScrollMode);
71     auto parent = parent_.Upgrade();
72     if (parent && !nestedScroll.NeedParent() && nestedScroll_.NeedParent()) {
73         isNestedInterrupt_ = true;
74     }
75     nestedScroll_ = nestedScroll;
76 }
77 
SetParentScrollable()78 void NestableScrollContainer::SetParentScrollable()
79 {
80     parent_ = SearchParent();
81     auto parent = parent_.Upgrade();
82     CHECK_NULL_VOID(parent);
83     if (!isFixedNestedScrollMode_) {
84         auto && childNestedMode = parent->GetNestedModeForChildren();
85         auto selfNestedScrollMode = nestedScroll_;
86         if (childNestedMode) {
87             SetNestedScroll(*childNestedMode, true);
88         }
89         if (AceType::InstanceOf<RefreshPattern>(parent)) {
90             parent->SetNestedScroll(selfNestedScrollMode);
91         }
92     }
93 }
94 
OnScrollDragEndRecursive()95 void NestableScrollContainer::OnScrollDragEndRecursive()
96 {
97     auto parent = parent_.Upgrade();
98     if (parent && nestedScroll_.NeedParent()) {
99         parent->OnScrollDragEndRecursive();
100     }
101 }
102 } // namespace OHOS::Ace::NG
103