/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "bridge/declarative_frontend/jsview/js_scroll.h" #include "bridge/declarative_frontend/jsview/js_scroller.h" #include "bridge/declarative_frontend/jsview/js_view_common_def.h" #include "bridge/declarative_frontend/view_stack_processor.h" #include "core/components/scroll/scroll_component.h" namespace OHOS::Ace::Framework { namespace { const std::vector DISPLAY_MODE = {DisplayMode::OFF, DisplayMode::AUTO, DisplayMode::ON}; } void JSScroll::Create(const JSCallbackInfo& info) { RefPtr child; auto scrollComponent = AceType::MakeRefPtr(child); if (info.Length() > 0 && info[0]->IsObject()) { JSScroller* jsScroller = JSRef::Cast(info[0])->Unwrap(); if (jsScroller) { auto positionController = AceType::MakeRefPtr(); jsScroller->SetController(positionController); scrollComponent->SetScrollPositionController(positionController); // Init scroll bar proxy. auto proxy = jsScroller->GetScrollBarProxy(); if (!proxy) { proxy = AceType::MakeRefPtr(); jsScroller->SetScrollBarProxy(proxy); } scrollComponent->SetScrollBarProxy(proxy); } } else { auto positionController = AceType::MakeRefPtr(); scrollComponent->SetScrollPositionController(positionController); } // init scroll bar std::pair barColor; barColor.first = false; std::pair barWidth; barWidth.first = false; scrollComponent->InitScrollBar(GetTheme(), barColor, barWidth, EdgeEffect::NONE); ViewStackProcessor::GetInstance()->Push(scrollComponent); } void JSScroll::SetScrollable(int32_t value) { if (value >= 0 && value < 4) { auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); auto scrollComponent = AceType::DynamicCast(component); if (scrollComponent) { scrollComponent->SetAxisDirection((Axis)value); } } else { LOGE("invalid value for SetScrollable"); } } void JSScroll::OnScrollCallback(const JSCallbackInfo& args) { if (args[0]->IsFunction()) { auto onScroll = EventMarker( [execCtx = args.GetExecutionContext(), func = JSRef::Cast(args[0])](const BaseEventInfo* info) { JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx); auto eventInfo = TypeInfoHelper::DynamicCast(info); if (!eventInfo) { return; } auto params = ConvertToJSValues(eventInfo->GetScrollX(), eventInfo->GetScrollY()); func->Call(JSRef(), params.size(), params.data()); }); auto scrollComponent = AceType::DynamicCast(ViewStackProcessor::GetInstance()->GetMainComponent()); if (scrollComponent) { if (!scrollComponent->GetScrollPositionController()) { scrollComponent->SetScrollPositionController(AceType::MakeRefPtr()); } scrollComponent->SetOnScroll(onScroll); } } args.SetReturnValue(args.This()); } void JSScroll::OnScrollEdgeCallback(const JSCallbackInfo& args) { if (args[0]->IsFunction()) { auto onScroll = EventMarker( [execCtx = args.GetExecutionContext(), func = JSRef::Cast(args[0])](const BaseEventInfo* info) { JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx); auto eventInfo = TypeInfoHelper::DynamicCast(info); if (!eventInfo) { return; } auto param = ConvertToJSValue(static_cast(eventInfo->GetType())); func->Call(JSRef(), 1, ¶m); }); auto scrollComponent = AceType::DynamicCast(ViewStackProcessor::GetInstance()->GetMainComponent()); if (scrollComponent) { if (!scrollComponent->GetScrollPositionController()) { scrollComponent->SetScrollPositionController(AceType::MakeRefPtr()); } scrollComponent->SetOnScrollEdge(onScroll); } } args.SetReturnValue(args.This()); } void JSScroll::OnScrollEndCallback(const JSCallbackInfo& args) { if (args[0]->IsFunction()) { auto onScrollStop = EventMarker( [execCtx = args.GetExecutionContext(), func = JSRef::Cast(args[0])](const BaseEventInfo* info) { JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx); func->Call(JSRef(), 0, nullptr); }); auto scrollComponent = AceType::DynamicCast(ViewStackProcessor::GetInstance()->GetMainComponent()); if (scrollComponent) { if (!scrollComponent->GetScrollPositionController()) { scrollComponent->SetScrollPositionController(AceType::MakeRefPtr()); } scrollComponent->SetOnScrollEnd(onScrollStop); } } args.SetReturnValue(args.This()); } void JSScroll::JSBind(BindingTarget globalObj) { JSClass::Declare("Scroll"); MethodOptions opt = MethodOptions::NONE; JSClass::StaticMethod("create", &JSScroll::Create, opt); JSClass::StaticMethod("scrollable", &JSScroll::SetScrollable, opt); JSClass::StaticMethod("onScroll", &JSScroll::OnScrollCallback, opt); JSClass::StaticMethod("onScrollEdge", &JSScroll::OnScrollEdgeCallback, opt); JSClass::StaticMethod("onScrollEnd", &JSScroll::OnScrollEndCallback, opt); JSClass::StaticMethod("onClick", &JSInteractableView::JsOnClick); JSClass::StaticMethod("onTouch", &JSInteractableView::JsOnTouch); JSClass::StaticMethod("onHover", &JSInteractableView::JsOnHover); JSClass::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey); JSClass::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete); JSClass::StaticMethod("onAppear", &JSInteractableView::JsOnAppear); JSClass::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear); JSClass::StaticMethod("edgeEffect", &JSScroll::SetEdgeEffect, opt); JSClass::StaticMethod("scrollBar", &JSScroll::SetScrollBar, opt); JSClass::StaticMethod("scrollBarColor", &JSScroll::SetScrollBarColor, opt); JSClass::StaticMethod("scrollBarWidth", &JSScroll::SetScrollBarWidth, opt); JSClass::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage); JSClass::Inherit(); JSClass::Inherit(); JSClass::Bind<>(globalObj); } void JSScroll::SetScrollBar(int displayMode) { auto scrollComponent = AceType::DynamicCast(ViewStackProcessor::GetInstance()->GetMainComponent()); if (!scrollComponent) { return; } if (displayMode >= 0 && displayMode < static_cast(DISPLAY_MODE.size())) { scrollComponent->SetDisplayMode(DISPLAY_MODE[displayMode]); } } void JSScroll::SetScrollBarWidth(const std::string& scrollBarWidth) { auto scrollComponent = AceType::DynamicCast(ViewStackProcessor::GetInstance()->GetMainComponent()); if (!scrollComponent || scrollBarWidth.empty()) { return; } scrollComponent->SetScrollBarWidth(StringUtils::StringToDimension(scrollBarWidth)); } void JSScroll::SetScrollBarColor(const std::string& scrollBarColor) { auto scrollComponent = AceType::DynamicCast(ViewStackProcessor::GetInstance()->GetMainComponent()); if (!scrollComponent || scrollBarColor.empty()) { return; } scrollComponent->SetScrollBarColor(Color::FromString(scrollBarColor)); } void JSScroll::SetEdgeEffect(int edgeEffect) { auto scrollComponent = AceType::DynamicCast(ViewStackProcessor::GetInstance()->GetMainComponent()); if (!scrollComponent) { return; } RefPtr scrollEdgeEffect; if (edgeEffect == 0) { scrollEdgeEffect = AceType::MakeRefPtr(); } else if (edgeEffect == 1) { scrollEdgeEffect = AceType::MakeRefPtr(Color::GRAY); } else { scrollEdgeEffect = AceType::MakeRefPtr(EdgeEffect::NONE); } scrollComponent->SetScrollEffect(scrollEdgeEffect); } } // namespace OHOS::Ace::Framework