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("onAppear", &JSInteractableView::JsOnAppear);
57 JSClass<JSScrollBar>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
58 JSClass<JSScrollBar>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
59 JSClass<JSScrollBar>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
60 JSClass<JSScrollBar>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
61 JSClass<JSScrollBar>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
62 JSClass<JSScrollBar>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
63
64 JSClass<JSScrollBar>::InheritAndBind<JSContainerBase>(globalObj);
65 }
66
Create(const JSCallbackInfo & info)67 void JSScrollBar::Create(const JSCallbackInfo& info)
68 {
69 bool proxyFlag = false;
70 RefPtr<ScrollProxy> proxy;
71 int directionNum = -1;
72 int stateNum = -1;
73 bool infoflag = false;
74 if (info.Length() > 0 && info[0]->IsObject()) {
75 infoflag = true;
76 auto obj = JSRef<JSObject>::Cast(info[0]);
77 auto scrollerValue = obj->GetProperty("scroller");
78 if (scrollerValue->IsObject() && JSRef<JSObject>::Cast(scrollerValue)->Unwrap<JSScroller>()) {
79 auto jsScroller = JSRef<JSObject>::Cast(scrollerValue)->Unwrap<JSScroller>();
80 jsScroller->SetInstanceId(Container::CurrentId());
81 auto scrollBarProxy = jsScroller->GetScrollBarProxy();
82 proxyFlag = true;
83 proxy = ScrollBarModel::GetInstance()->GetScrollBarProxy(scrollBarProxy);
84 if (!proxy) {
85 jsScroller->SetScrollBarProxy(proxy);
86 }
87 }
88
89 auto directionValue = obj->GetProperty("direction");
90 if (directionValue->IsNumber()) {
91 directionNum = directionValue->ToNumber<int32_t>();
92 }
93
94 auto stateValue = obj->GetProperty("state");
95 if (stateValue->IsNumber()) {
96 stateNum = stateValue->ToNumber<int32_t>();
97 }
98 }
99
100 ScrollBarModel::GetInstance()->Create(proxy, infoflag, proxyFlag, directionNum, stateNum);
101 }
102 } // namespace OHOS::Ace::Framework
103