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 #ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_NAVDESTINATION_SCROLLABLE_PROCESSOR_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_NAVDESTINATION_SCROLLABLE_PROCESSOR_H
18 
19 #include <cstdint>
20 #include <optional>
21 #include <map>
22 
23 #include "bridge/declarative_frontend/jsview/js_scroller.h"
24 #include "core/components_ng/pattern/navrouter/navdestination_pattern.h"
25 #include "core/components_ng/pattern/navrouter/navdestination_scrollable_processor.h"
26 
27 namespace OHOS::Ace::Framework {
28 class JSNavDestinationScrollableProcessor : public NG::NavDestinationScrollableProcessor {
29     DECLARE_ACE_TYPE(JSNavDestinationScrollableProcessor, NG::NavDestinationScrollableProcessor)
30 public:
31     JSNavDestinationScrollableProcessor() = default;
32     ~JSNavDestinationScrollableProcessor() = default;
33 
34     void UpdateBindingRelation() override;
SetNodeId(int32_t id)35     void SetNodeId(int32_t id) override
36     {
37         nodeId_ = id;
38     }
SetNavDestinationPattern(WeakPtr<NG::NavDestinationPattern> pattern)39     void SetNavDestinationPattern(WeakPtr<NG::NavDestinationPattern> pattern) override
40     {
41         weakPattern_ = pattern;
42     }
43     void UnbindAllScrollers() override;
44 
45     void BindToScrollable(const JSCallbackInfo& info);
46     void BindToNestedScrollable(const JSCallbackInfo& info);
47 
48     void HandleOnTouchEvent(WeakPtr<JSScroller> jsScrollerWeak, const TouchEventInfo& info);
49     void HandleOnReachEvent(WeakPtr<JSScroller> jsScrollerWeak, bool isTopEvent);
50     void HandleOnScrollStartEvent(WeakPtr<JSScroller> jsScrollerWeak);
51     void HandleOnScrollStopEvent(WeakPtr<JSScroller> jsScrollerWeak);
52     void HandleOnDidScrollEvent(
53         WeakPtr<JSScroller> jsScrollerWeak, Dimension dimension, ScrollSource source, bool isAtTop, bool isAtBottom);
54 
55 private:
56     void CombineIncomingScrollers();
57     bool BuildNewBindingRelation();
58     bool RemoveUnneededBindingRelation();
59 
60     struct NestedScrollers {
61         NestedScrollers() = default;
NestedScrollersNestedScrollers62         NestedScrollers(WeakPtr<JSScroller> childScroller, std::optional<WeakPtr<JSScroller>> parentScroller)
63             : child(childScroller), parent(parentScroller) {}
64         WeakPtr<JSScroller> child;
65         std::optional<WeakPtr<JSScroller>> parent;
66 
67         bool operator< (const NestedScrollers& other) const
68         {
69             return child < other.child;
70         }
71     };
72     std::set<WeakPtr<JSScroller>> incommingScrollers_;
73     std::set<NestedScrollers> incommingNestedScrollers_;
74     bool needUpdateBindingRelation_ = false;
75 
76     struct ScrollInfo {
77         bool isTouching = false;
78         bool isScrolling = false;
79         bool isAtTop = false;
80         bool isAtBottom = false;
81         bool needUnbind = false;
82         std::optional<WeakPtr<JSScroller>> parentScroller;
83     };
84     std::map<WeakPtr<JSScroller>, ScrollInfo> scrollInfoMap_;
85     int32_t nodeId_ = 0;
86     WeakPtr<NG::NavDestinationPattern> weakPattern_;
87 };
88 
89 } // namespace OHOS::Ace::Framework
90 
91 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_NAVDESTINATION_SCROLLABLE_PROCESSOR_H
92