1 /*
2  * Copyright (c) 2021-2023 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 "frameworks/bridge/declarative_frontend/jsview/scroll_bar/js_scroll_bar.h"
17 
18 #include "bridge/declarative_frontend/jsview/js_scroller.h"
19 #include "bridge/declarative_frontend/jsview/models/scroll_bar_model_impl.h"
20 #include "core/common/container.h"
21 #include "core/components_ng/pattern/scroll_bar/scroll_bar_model_ng.h"
22 
23 #define AXIS_SIZE 3
24 
25 namespace OHOS::Ace {
26 std::unique_ptr<ScrollBarModel> ScrollBarModel::instance_ = nullptr;
27 std::mutex ScrollBarModel::mutex_;
28 
GetInstance()29 ScrollBarModel* ScrollBarModel::GetInstance()
30 {
31     if (!instance_) {
32         std::lock_guard<std::mutex> lock(mutex_);
33         if (!instance_) {
34 #ifdef NG_BUILD
35             instance_.reset(new NG::ScrollBarModelNG());
36 #else
37             if (Container::IsCurrentUseNewPipeline()) {
38                 instance_.reset(new NG::ScrollBarModelNG());
39             } else {
40                 instance_.reset(new Framework::ScrollBarModelImpl());
41             }
42 #endif
43         }
44     }
45     return instance_.get();
46 }
47 }
48 
49 namespace OHOS::Ace::Framework {
JSBind(BindingTarget globalObj)50 void JSScrollBar::JSBind(BindingTarget globalObj)
51 {
52     JSClass<JSScrollBar>::Declare("ScrollBar");
53     MethodOptions opt = MethodOptions::NONE;
54     JSClass<JSScrollBar>::StaticMethod("create", &JSScrollBar::Create, opt);
55 
56     JSClass<JSScrollBar>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
57     JSClass<JSScrollBar>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
58     JSClass<JSScrollBar>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
59     JSClass<JSScrollBar>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
60     JSClass<JSScrollBar>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
61     JSClass<JSScrollBar>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
62     JSClass<JSScrollBar>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
63     JSClass<JSScrollBar>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
64     JSClass<JSScrollBar>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
65     JSClass<JSScrollBar>::StaticMethod("enableNestedScroll", &JSScrollBar::JsSetEnableNestedScroll);
66 
67     JSClass<JSScrollBar>::InheritAndBind<JSContainerBase>(globalObj);
68 }
69 
Create(const JSCallbackInfo & info)70 void JSScrollBar::Create(const JSCallbackInfo& info)
71 {
72     bool proxyFlag = false;
73     RefPtr<ScrollProxy> proxy;
74     int directionNum = -1;
75     int stateNum = -1;
76     bool infoflag = false;
77     if (info.Length() > 0 && info[0]->IsObject()) {
78         infoflag = true;
79         auto obj = JSRef<JSObject>::Cast(info[0]);
80         auto scrollerValue = obj->GetProperty("scroller");
81         if (scrollerValue->IsObject() && JSRef<JSObject>::Cast(scrollerValue)->Unwrap<JSScroller>()) {
82             auto jsScroller = JSRef<JSObject>::Cast(scrollerValue)->Unwrap<JSScroller>();
83             jsScroller->SetInstanceId(Container::CurrentId());
84             auto scrollBarProxy = jsScroller->GetScrollBarProxy();
85             proxyFlag = true;
86             proxy = ScrollBarModel::GetInstance()->GetScrollBarProxy(scrollBarProxy);
87             jsScroller->SetScrollBarProxy(proxy);
88         }
89 
90         auto directionValue = obj->GetProperty("direction");
91         if (directionValue->IsNumber()) {
92             directionNum = directionValue->ToNumber<int32_t>();
93         }
94 
95         auto stateValue = obj->GetProperty("state");
96         if (stateValue->IsNumber()) {
97             stateNum = stateValue->ToNumber<int32_t>();
98         }
99     }
100 
101     ScrollBarModel::GetInstance()->Create(proxy, infoflag, proxyFlag, directionNum, stateNum);
102 }
103 
JsSetEnableNestedScroll(const JSCallbackInfo & args)104 void JSScrollBar::JsSetEnableNestedScroll(const JSCallbackInfo& args)
105 {
106     if (args.Length() < 1 || !args[0]->IsBoolean()) {
107         ScrollBarModel::GetInstance()->SetEnableNestedScroll(false);
108         return;
109     }
110 
111     ScrollBarModel::GetInstance()->SetEnableNestedScroll(args[0]->ToBoolean());
112 }
113 
114 } // namespace OHOS::Ace::Framework
115