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
ParseEffectEdge(const JSRef<JSVal> & jsValue)48 EffectEdge JSScrollable::ParseEffectEdge(const JSRef<JSVal>& jsValue)
49 {
50 auto effectEdge = static_cast<int32_t>(EffectEdge::ALL);
51 if (jsValue->IsObject()) {
52 auto paramObject = JSRef<JSObject>::Cast(jsValue);
53 JSRef<JSVal> effectEdgedParam = paramObject->GetProperty("effectEdge");
54 if (effectEdgedParam->IsNull() || effectEdgedParam->IsUndefined() ||
55 !JSViewAbstract::ParseJsInt32(effectEdgedParam, effectEdge) ||
56 effectEdge < static_cast<int32_t>(EffectEdge::START) ||
57 effectEdge > static_cast<int32_t>(EffectEdge::END)) {
58 effectEdge = static_cast<int32_t>(EffectEdge::ALL);
59 }
60 }
61 return static_cast<EffectEdge>(effectEdge);
62 }
63
ParseDisplayMode(const JSCallbackInfo & info,DisplayMode defaultValue)64 DisplayMode JSScrollable::ParseDisplayMode(const JSCallbackInfo& info, DisplayMode defaultValue)
65 {
66 if (info.Length() < 1) {
67 return defaultValue;
68 }
69 auto displayMode = static_cast<int32_t>(defaultValue);
70 if (!info[0]->IsUndefined() && info[0]->IsNumber()) {
71 JSViewAbstract::ParseJsInt32(info[0], displayMode);
72 if (displayMode < 0 || displayMode >= static_cast<int32_t>(DISPLAY_MODE.size())) {
73 displayMode = static_cast<int32_t>(defaultValue);
74 }
75 }
76 return static_cast<DisplayMode>(displayMode);
77 }
78
ParseBarColor(const JSCallbackInfo & info)79 std::string JSScrollable::ParseBarColor(const JSCallbackInfo& info)
80 {
81 auto pipelineContext = PipelineContext::GetCurrentContext();
82 CHECK_NULL_RETURN(pipelineContext, "");
83 auto theme = pipelineContext->GetTheme<ScrollBarTheme>();
84 CHECK_NULL_RETURN(theme, "");
85 Color color(theme->GetForegroundColor());
86 JSViewAbstract::ParseJsColor(info[0], color);
87 return color.ColorToString();
88 }
89
ParseBarWidth(const JSCallbackInfo & info)90 std::string JSScrollable::ParseBarWidth(const JSCallbackInfo& info)
91 {
92 if (info.Length() < 1) {
93 return "";
94 }
95
96 CalcDimension scrollBarWidth;
97 if (!JSViewAbstract::ParseJsDimensionVp(info[0], scrollBarWidth) || info[0]->IsNull() || info[0]->IsUndefined() ||
98 (info[0]->IsString() && info[0]->ToString().empty()) || LessNotEqual(scrollBarWidth.Value(), 0.0) ||
99 scrollBarWidth.Unit() == DimensionUnit::PERCENT) {
100 auto pipelineContext = PipelineContext::GetCurrentContext();
101 CHECK_NULL_RETURN(pipelineContext, "");
102 auto theme = pipelineContext->GetTheme<ScrollBarTheme>();
103 CHECK_NULL_RETURN(theme, "");
104 scrollBarWidth = theme->GetNormalWidth();
105 }
106 return scrollBarWidth.ToString();
107 }
108
JsClip(const JSCallbackInfo & info)109 void JSScrollable::JsClip(const JSCallbackInfo& info)
110 {
111 if (info[0]->IsUndefined()) {
112 ViewAbstractModel::GetInstance()->SetClipEdge(true);
113 return;
114 }
115 if (info[0]->IsObject()) {
116 JSShapeAbstract* clipShape = JSRef<JSObject>::Cast(info[0])->Unwrap<JSShapeAbstract>();
117 if (clipShape == nullptr) {
118 return;
119 }
120 ViewAbstractModel::GetInstance()->SetClipShape(clipShape->GetBasicShape());
121 } else if (info[0]->IsBoolean()) {
122 ViewAbstractModel::GetInstance()->SetClipEdge(info[0]->ToBoolean());
123 }
124 }
125 } // namespace OHOS::Ace::Framework
126