• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_scrollbar_bridge.h"
17 
18 #include "base/log/log.h"
19 #include "base/utils/string_utils.h"
20 #include "base/utils/utils.h"
21 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_scrollbar_bridge.h"
22 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
23 #include "bridge/declarative_frontend/jsview/scroll_bar/js_scroll_bar.h"
24 #include "core/components_ng/pattern/scroll_bar/scroll_bar_model_ng.h"
25 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
26 
27 namespace OHOS::Ace::NG {
SetScrollBarEnableNestedScroll(ArkUIRuntimeCallInfo * runtimeCallInfo)28 ArkUINativeModuleValue ScrollBarBridge::SetScrollBarEnableNestedScroll(ArkUIRuntimeCallInfo* runtimeCallInfo)
29 {
30     EcmaVM* vm = runtimeCallInfo->GetVM();
31     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
32     Local<JSValueRef> nativeNodeArg = runtimeCallInfo->GetCallArgRef(0);
33     Local<JSValueRef> enablePagingArg = runtimeCallInfo->GetCallArgRef(1);
34     CHECK_NULL_RETURN(nativeNodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
35     auto nativeNode = nodePtr(nativeNodeArg->ToNativePointer(vm)->Value());
36     if (enablePagingArg->IsBoolean()) {
37         bool enablePaging = enablePagingArg->ToBoolean(vm)->Value();
38         GetArkUINodeModifiers()->getScrollBarModifier()->setScrollBarEnableNestedScroll(nativeNode, enablePaging);
39     }
40     return panda::JSValueRef::Undefined(vm);
41 }
42 
ResetScrollBarEnableNestedScroll(ArkUIRuntimeCallInfo * runtimeCallInfo)43 ArkUINativeModuleValue ScrollBarBridge::ResetScrollBarEnableNestedScroll(ArkUIRuntimeCallInfo* runtimeCallInfo)
44 {
45     EcmaVM* vm = runtimeCallInfo->GetVM();
46     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
47     Local<JSValueRef> nativeNodeArg = runtimeCallInfo->GetCallArgRef(0);
48     CHECK_NULL_RETURN(nativeNodeArg->IsNativePointer(vm), panda::JSValueRef::Undefined(vm));
49     auto nativeNode = nodePtr(nativeNodeArg->ToNativePointer(vm)->Value());
50     GetArkUINodeModifiers()->getScrollBarModifier()->resetScrollBarEnableNestedScroll(nativeNode);
51     return panda::JSValueRef::Undefined(vm);
52 }
53 
SetScrollBarScrollBarColor(ArkUIRuntimeCallInfo * runtimeCallInfo)54 ArkUINativeModuleValue ScrollBarBridge::SetScrollBarScrollBarColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
55 {
56     EcmaVM* vm = runtimeCallInfo->GetVM();
57     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
58     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
59     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
60     if (!firstArg->IsNativePointer(vm)) {
61         return panda::JSValueRef::Undefined(vm);
62     }
63     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
64     Color color;
65     if (SystemProperties::ConfigChangePerform()) {
66         RefPtr<ResourceObject> resObj;
67         if (!ArkTSUtils::ParseColorMetricsToColor(vm, secondArg, color, resObj)) {
68             GetArkUINodeModifiers()->getScrollBarModifier()->resetScrollBarScrollBarColor(nativeNode);
69             GetArkUINodeModifiers()->getScrollBarModifier()->createScrollBarScrollBarColorWithResourceObj(
70                 nativeNode, nullptr);
71         } else {
72             auto nodeInfo = ArkTSUtils::MakeNativeNodeInfo(nativeNode);
73             ArkTSUtils::CompleteResourceObjectFromColor(resObj, color, true, nodeInfo);
74             GetArkUINodeModifiers()->getScrollBarModifier()->setScrollBarScrollBarColor(nativeNode, color.GetValue());
75             GetArkUINodeModifiers()->getScrollBarModifier()->createScrollBarScrollBarColorWithResourceObj(
76                 nativeNode, AceType::RawPtr(resObj));
77         }
78     } else {
79         if (!ParseColorMetricsToColor(vm, secondArg, color)) {
80             GetArkUINodeModifiers()->getScrollBarModifier()->resetScrollBarScrollBarColor(nativeNode);
81         } else {
82             GetArkUINodeModifiers()->getScrollBarModifier()->setScrollBarScrollBarColor(nativeNode, color.GetValue());
83         }
84     }
85     return panda::JSValueRef::Undefined(vm);
86 }
87 
ResetScrollBarScrollBarColor(ArkUIRuntimeCallInfo * runtimeCallInfo)88 ArkUINativeModuleValue ScrollBarBridge::ResetScrollBarScrollBarColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
89 {
90     EcmaVM* vm = runtimeCallInfo->GetVM();
91     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
92     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
93     if (!firstArg->IsNativePointer(vm)) {
94         return panda::JSValueRef::Undefined(vm);
95     }
96     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
97 
98     GetArkUINodeModifiers()->getScrollBarModifier()->resetScrollBarScrollBarColor(nativeNode);
99     return panda::JSValueRef::Undefined(vm);
100 }
101 
ParseColorMetricsToColor(const EcmaVM * vm,const Local<JSValueRef> & jsValue,Color & result)102 bool ScrollBarBridge::ParseColorMetricsToColor(const EcmaVM* vm, const Local<JSValueRef>& jsValue, Color& result)
103 {
104     if (!jsValue->IsObject(vm)) {
105         return false;
106     }
107     auto obj = jsValue->ToObject(vm);
108     auto toNumericProp = obj->Get(vm, "toNumeric");
109     if (toNumericProp->IsFunction(vm)) {
110         panda::Local<panda::FunctionRef> func = toNumericProp;
111         auto colorVal = func->Call(vm, obj, nullptr, 0);
112         result.SetValue(colorVal->Uint32Value(vm));
113         return true;
114     }
115     return false;
116 }
117 } // namespace OHOS::Ace::NG