• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_SCROLLER_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_SCROLLER_H
18 
19 #include "base/memory/referenced.h"
20 #include "bridge/declarative_frontend/engine/bindings.h"
21 #include "core/components/scroll/scroll_controller_base.h"
22 #include "core/components/scroll_bar/scroll_proxy.h"
23 
24 namespace OHOS::Ace::Framework {
25 
26 class JSScroller : public Referenced {
27 public:
28     JSScroller() = default;
29     ~JSScroller() override = default;
30 
31     static void JSBind(BindingTarget globalObj);
32     static void Constructor(const JSCallbackInfo& args);
33     static void Destructor(JSScroller* scroller);
34 
35     void ScrollTo(const JSCallbackInfo& args);
36     void ScrollEdge(const JSCallbackInfo& args);
37     void Fling(const JSCallbackInfo& args);
38     void ScrollPage(const JSCallbackInfo& args);
39     void CurrentOffset(const JSCallbackInfo& args);
40     void ScrollToIndex(const JSCallbackInfo& args);
41     void ScrollBy(const JSCallbackInfo& args);
42     void IsAtEnd(const JSCallbackInfo& args);
43     void GetItemRect(const JSCallbackInfo& args);
44 
GetController()45     const WeakPtr<ScrollControllerBase>& GetController() const
46     {
47         return controllerWeak_;
48     }
49 
SetController(const RefPtr<ScrollControllerBase> & controller)50     void SetController(const RefPtr<ScrollControllerBase>& controller)
51     {
52         auto oldController = controllerWeak_.Upgrade();
53         if (oldController) {
54             ScrollerObserver observer;
55             oldController->SetObserver(observer);
56         }
57         if (controller) {
58             controller->SetObserver(observer_);
59         }
60         controllerWeak_ = controller;
61     }
62 
SetScrollBarProxy(const RefPtr<ScrollProxy> & proxy)63     void SetScrollBarProxy(const RefPtr<ScrollProxy>& proxy)
64     {
65         scrollBarProxyWeak_ = proxy;
66     }
67 
GetScrollBarProxy()68     RefPtr<ScrollProxy> GetScrollBarProxy()
69     {
70         return scrollBarProxyWeak_.Upgrade();
71     }
72 
73     JSRef<JSObject> CreateRectangle(const Rect& info);
74 
SetInstanceId(int32_t instanceId)75     void SetInstanceId(int32_t instanceId)
76     {
77         instanceId_ = instanceId;
78     }
79 
GetInstanceId()80     int32_t GetInstanceId()
81     {
82         return instanceId_;
83     }
84 
SetObserver(const ScrollerObserver & observer)85     void SetObserver(const ScrollerObserver& observer)
86     {
87         observer_ = observer;
88         auto controller = controllerWeak_.Upgrade();
89         if (controller) {
90             controller->SetObserver(observer);
91         }
92     }
93 
94 private:
95     bool ParseCurveParams(RefPtr<Curve>& curve, const JSRef<JSVal>& jsValue);
96 
97     WeakPtr<ScrollControllerBase> controllerWeak_;
98     WeakPtr<ScrollProxy> scrollBarProxyWeak_;
99 
100     ACE_DISALLOW_COPY_AND_MOVE(JSScroller);
101 
102     int32_t instanceId_ = INSTANCE_ID_UNDEFINED;
103 
104     ScrollerObserver observer_;
105 };
106 
107 } // namespace OHOS::Ace::Framework
108 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_SCROLLER_H
109