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 "frameworks/bridge/declarative_frontend/jsview/scroll_bar/js_scroll_bar.h"
17
18 #include "bridge/declarative_frontend/jsview/js_scroller.h"
19 #include "core/components/scroll_bar/scroll_bar_component.h"
20 #include "core/components_ng/pattern/scroll_bar/scroll_bar_pattern.h"
21 #include "core/components_ng/pattern/scroll_bar/scroll_bar_view.h"
22 #include "core/components_ng/pattern/scroll_bar/proxy/scroll_bar_proxy.h"
23 #include "frameworks/bridge/common/utils/utils.h"
24 #include "frameworks/bridge/declarative_frontend/engine/bindings.h"
25 #include "frameworks/bridge/declarative_frontend/engine/functions/js_click_function.h"
26 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
27 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
28
29 namespace OHOS::Ace::Framework {
30 namespace {
31
32 const std::vector<DisplayMode> DISPLAY_MODE = { DisplayMode::OFF, DisplayMode::AUTO, DisplayMode::ON };
33 const std::vector<Axis> AXIS = { Axis::VERTICAL, Axis::HORIZONTAL, Axis::NONE };
34
NGSetScrollControllerAndProxy(RefPtr<NG::ScrollBarPattern> & pattern,const JSCallbackInfo & info)35 void NGSetScrollControllerAndProxy(RefPtr<NG::ScrollBarPattern>& pattern, const JSCallbackInfo& info)
36 {
37 CHECK_NULL_VOID(pattern);
38 if (info.Length() > 0 && info[0]->IsObject()) {
39 auto obj = JSRef<JSObject>::Cast(info[0]);
40 // Parse scroller.
41 auto scrollerValue = obj->GetProperty("scroller");
42 if (scrollerValue->IsObject() && JSRef<JSObject>::Cast(scrollerValue)->Unwrap<JSScroller>()) {
43 auto jsScroller = JSRef<JSObject>::Cast(scrollerValue)->Unwrap<JSScroller>();
44 auto proxy = AceType::DynamicCast<NG::ScrollBarProxy>(jsScroller->GetScrollBarProxy());
45 if (!proxy) {
46 proxy = AceType::MakeRefPtr<NG::ScrollBarProxy>();
47 jsScroller->SetScrollBarProxy(proxy);
48 }
49 proxy->RegisterScrollBar(pattern);
50 pattern->SetScrollBarProxy(proxy);
51 }
52 // Parse direction.
53 auto directionValue = obj->GetProperty("direction");
54 if (directionValue->IsNumber()) {
55 NG::ScrollBarView::SetAxis(AXIS[directionValue->ToNumber<int32_t>()]);
56 }
57 // Parse state.
58 auto stateValue = obj->GetProperty("state");
59 if (stateValue->IsNumber()) {
60 NG::ScrollBarView::SetDisplayMode(stateValue->ToNumber<int32_t>());
61 }
62 }
63 }
64
65 } // namespace
66
JSBind(BindingTarget globalObj)67 void JSScrollBar::JSBind(BindingTarget globalObj)
68 {
69 JSClass<JSScrollBar>::Declare("ScrollBar");
70 MethodOptions opt = MethodOptions::NONE;
71 JSClass<JSScrollBar>::StaticMethod("create", &JSScrollBar::Create, opt);
72
73 JSClass<JSScrollBar>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
74 JSClass<JSScrollBar>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
75 JSClass<JSScrollBar>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
76 JSClass<JSScrollBar>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
77 JSClass<JSScrollBar>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
78 JSClass<JSScrollBar>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
79 JSClass<JSScrollBar>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
80
81 JSClass<JSScrollBar>::Inherit<JSContainerBase>();
82 JSClass<JSScrollBar>::Inherit<JSViewAbstract>();
83 JSClass<JSScrollBar>::Bind<>(globalObj);
84 }
85
Create(const JSCallbackInfo & info)86 void JSScrollBar::Create(const JSCallbackInfo& info)
87 {
88 if (Container::IsCurrentUseNewPipeline()) {
89 auto frameNode = NG::ScrollBarView::Create();
90 auto pattern = AceType::DynamicCast<NG::ScrollBarPattern>(frameNode->GetPattern());
91 NGSetScrollControllerAndProxy(pattern, info);
92 return;
93 }
94
95 RefPtr<Component> child;
96 auto scrollBarComponent = AceType::MakeRefPtr<OHOS::Ace::ScrollBarComponent>(child);
97 if (info.Length() > 0 && info[0]->IsObject()) {
98 auto obj = JSRef<JSObject>::Cast(info[0]);
99 // Parse scroller.
100 auto scrollerValue = obj->GetProperty("scroller");
101 if (scrollerValue->IsObject() && JSRef<JSObject>::Cast(scrollerValue)->Unwrap<JSScroller>()) {
102 auto jsScroller = JSRef<JSObject>::Cast(scrollerValue)->Unwrap<JSScroller>();
103 auto proxy = AceType::DynamicCast<ScrollBarProxy>(jsScroller->GetScrollBarProxy());
104 if (!proxy) {
105 proxy = AceType::MakeRefPtr<ScrollBarProxy>();
106 jsScroller->SetScrollBarProxy(proxy);
107 }
108 scrollBarComponent->SetScrollBarProxy(proxy);
109 }
110
111 // Parse direction.
112 auto directionValue = obj->GetProperty("direction");
113 if (directionValue->IsNumber()) {
114 scrollBarComponent->SetAxis(AXIS[directionValue->ToNumber<int32_t>()]);
115 }
116
117 // Parse state.
118 auto stateValue = obj->GetProperty("state");
119 if (stateValue->IsNumber()) {
120 scrollBarComponent->SetDisplayMode(DISPLAY_MODE[stateValue->ToNumber<int32_t>()]);
121 }
122 }
123 ViewStackProcessor::GetInstance()->ClaimElementId(scrollBarComponent);
124 ViewStackProcessor::GetInstance()->Push(scrollBarComponent);
125 // Push DisplayComponent to enable opacity animation.
126 ViewStackProcessor::GetInstance()->GetDisplayComponent();
127 }
128
129 } // namespace OHOS::Ace::Framework
130