1 /*
2  * Copyright (c) 2021 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/pipeline/base/related_node.h"
17 
18 #include "core/pipeline/base/render_node.h"
19 
20 namespace OHOS::Ace {
21 namespace {
22 
23 constexpr int32_t MAX_DEEP_SEARCH = 10;
24 
25 }
26 
InitRelatedParent(const WeakPtr<RenderNode> & weakParent)27 void RelatedChild::InitRelatedParent(const WeakPtr<RenderNode>& weakParent)
28 {
29     int32_t searchDeep = 0;
30     auto parent = weakParent.Upgrade();
31     while (searchDeep++ < MAX_DEEP_SEARCH && parent) {
32         auto relatedContainer = AceType::DynamicCast<RelatedContainer>(parent);
33         if (relatedContainer) {
34             relatedParent_ = AceType::WeakClaim(AceType::RawPtr(relatedContainer));
35             return;
36         } else {
37             parent = parent->GetParent().Upgrade();
38         }
39     }
40 }
41 
RelatedEventStart()42 void RelatedChild::RelatedEventStart()
43 {
44     if (!IsRelatedEventEnable()) {
45         return;
46     }
47     auto parent = relatedParent_.Upgrade();
48     parent->OnRelatedStart();
49 }
50 
RelatedScrollEventPrepare(const Offset & delta)51 bool RelatedChild::RelatedScrollEventPrepare(const Offset& delta)
52 {
53     if (!IsRelatedEventEnable()) {
54         return false;
55     }
56     auto parent = relatedParent_.Upgrade();
57     Offset consumed;
58     parent->OnRelatedPreScroll(delta, consumed);
59     if (!NearZero(consumed.GetY())) {
60         return true;
61     }
62     return false;
63 }
64 
RelatedScrollEventDoing(const Offset & delta)65 bool RelatedChild::RelatedScrollEventDoing(const Offset& delta)
66 {
67     if (!IsRelatedEventEnable()) {
68         return false;
69     }
70     auto parent = relatedParent_.Upgrade();
71     Offset consumed;
72     parent->OnRelatedScroll(delta, consumed);
73     if (!NearZero(consumed.GetY())) {
74         return true;
75     }
76     return false;
77 }
78 
RelatedEventEnd()79 void RelatedChild::RelatedEventEnd()
80 {
81     if (!IsRelatedEventEnable()) {
82         return;
83     }
84     auto parent = relatedParent_.Upgrade();
85     parent->OnRelatedEnd();
86 }
87 
88 } // namespace OHOS::Ace
89