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