1 /*
2 * Copyright (c) 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 "bridge/declarative_frontend/jsview/js_scrollable.h"
17
18 #include "base/utils/utils.h"
19 #include "bridge/declarative_frontend/jsview/js_shape_abstract.h"
20 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
21 #include "core/components_ng/base/view_stack_model.h"
22
23 namespace OHOS::Ace::Framework {
24 namespace {
25 const int32_t EDGE_EFFECT_PARAM_COUNT = 2;
26 const std::vector<DisplayMode> DISPLAY_MODE = { DisplayMode::OFF, DisplayMode::AUTO, DisplayMode::ON };
27 } // namespace
28
ParseEdgeEffect(const JSCallbackInfo & info,EdgeEffect defaultValue)29 EdgeEffect JSScrollable::ParseEdgeEffect(const JSCallbackInfo& info, EdgeEffect defaultValue)
30 {
31 if (info.Length() < 1) {
32 return defaultValue;
33 }
34 auto edgeEffect = static_cast<int32_t>(defaultValue);
35 if (info[0]->IsNull() || info[0]->IsUndefined() || !JSViewAbstract::ParseJsInt32(info[0], edgeEffect) ||
36 edgeEffect < static_cast<int32_t>(EdgeEffect::SPRING) || edgeEffect > static_cast<int32_t>(EdgeEffect::NONE)) {
37 edgeEffect = static_cast<int32_t>(defaultValue);
38 }
39 return static_cast<EdgeEffect>(edgeEffect);
40 }
41
ParseAlwaysEnable(const JSCallbackInfo & info,bool defaultValue)42 bool JSScrollable::ParseAlwaysEnable(const JSCallbackInfo& info, bool defaultValue)
43 {
44 auto alwaysEnabled = defaultValue;
45 if (info.Length() == EDGE_EFFECT_PARAM_COUNT) {
46 auto paramObject = JSRef<JSObject>::Cast(info[1]);
47 if (!(info[1]->IsNull() || info[1]->IsUndefined())) {
48 JSRef<JSVal> alwaysEnabledParam = paramObject->GetProperty("alwaysEnabled");
49 alwaysEnabled = alwaysEnabledParam->IsBoolean() ? alwaysEnabledParam->ToBoolean() : false;
50 }
51 }
52 return alwaysEnabled;
53 }
54
ParseDisplayMode(const JSCallbackInfo & info,DisplayMode defaultValue)55 DisplayMode JSScrollable::ParseDisplayMode(const JSCallbackInfo& info, DisplayMode defaultValue)
56 {
57 if (info.Length() < 1) {
58 return defaultValue;
59 }
60 auto displayMode = static_cast<int32_t>(defaultValue);
61 if (!info[0]->IsUndefined() && info[0]->IsNumber()) {
62 JSViewAbstract::ParseJsInt32(info[0], displayMode);
63 if (displayMode < 0 || displayMode >= static_cast<int32_t>(DISPLAY_MODE.size())) {
64 displayMode = static_cast<int32_t>(defaultValue);
65 }
66 }
67 return static_cast<DisplayMode>(displayMode);
68 }
69
ParseBarColor(const std::string & color)70 std::string JSScrollable::ParseBarColor(const std::string& color)
71 {
72 if (!color.empty() && color != "undefined") {
73 return color;
74 }
75 auto pipelineContext = PipelineContext::GetCurrentContext();
76 CHECK_NULL_RETURN(pipelineContext, "");
77 auto theme = pipelineContext->GetTheme<ScrollBarTheme>();
78 CHECK_NULL_RETURN(theme, "");
79 Color defaultColor(theme->GetForegroundColor());
80 return defaultColor.ColorToString();
81 }
82
ParseBarWidth(const JSCallbackInfo & info)83 std::string JSScrollable::ParseBarWidth(const JSCallbackInfo& info)
84 {
85 if (info.Length() < 1) {
86 return "";
87 }
88
89 CalcDimension scrollBarWidth;
90 if (!JSViewAbstract::ParseJsDimensionVp(info[0], scrollBarWidth) || info[0]->IsNull() || info[0]->IsUndefined() ||
91 (info[0]->IsString() && info[0]->ToString().empty()) || LessNotEqual(scrollBarWidth.Value(), 0.0) ||
92 scrollBarWidth.Unit() == DimensionUnit::PERCENT) {
93 auto pipelineContext = PipelineContext::GetCurrentContext();
94 CHECK_NULL_RETURN(pipelineContext, "");
95 auto theme = pipelineContext->GetTheme<ScrollBarTheme>();
96 CHECK_NULL_RETURN(theme, "");
97 scrollBarWidth = theme->GetNormalWidth();
98 }
99 return scrollBarWidth.ToString();
100 }
101
JsClip(const JSCallbackInfo & info)102 void JSScrollable::JsClip(const JSCallbackInfo& info)
103 {
104 if (info[0]->IsUndefined()) {
105 ViewAbstractModel::GetInstance()->SetClipEdge(true);
106 return;
107 }
108 if (info[0]->IsObject()) {
109 JSShapeAbstract* clipShape = JSRef<JSObject>::Cast(info[0])->Unwrap<JSShapeAbstract>();
110 if (clipShape == nullptr) {
111 return;
112 }
113 ViewAbstractModel::GetInstance()->SetClipShape(clipShape->GetBasicShape());
114 } else if (info[0]->IsBoolean()) {
115 ViewAbstractModel::GetInstance()->SetClipEdge(info[0]->ToBoolean());
116 }
117 }
118 } // namespace OHOS::Ace::Framework
119