1 /*
2 * Copyright (c) 2021-2022 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_progress.h"
17
18 #include "bridge/declarative_frontend/jsview/js_interactable_view.h"
19 #include "bridge/declarative_frontend/jsview/models/progress_model_impl.h"
20 #include "core/components/common/properties/color.h"
21 #include "core/components/progress/progress_theme.h"
22 #include "core/components_ng/pattern/progress/progress_model.h"
23 #include "core/components_ng/pattern/progress/progress_model_ng.h"
24
25 namespace OHOS::Ace {
26
27 std::unique_ptr<ProgressModel> ProgressModel::instance_ = nullptr;
28
GetInstance()29 ProgressModel* ProgressModel::GetInstance()
30 {
31 if (!instance_) {
32 #ifdef NG_BUILD
33 instance_.reset(new NG::ProgressModelNG());
34 #else
35 if (Container::IsCurrentUseNewPipeline()) {
36 instance_.reset(new NG::ProgressModelNG());
37 } else {
38 instance_.reset(new Framework::ProgressModelImpl());
39 }
40 #endif
41 }
42 return instance_.get();
43 }
44
45 } // namespace OHOS::Ace
46
47 namespace OHOS::Ace::Framework {
48
Create(const JSCallbackInfo & info)49 void JSProgress::Create(const JSCallbackInfo& info)
50 {
51 if (info.Length() != 1 || !info[0]->IsObject()) {
52 LOGE("create progress fail beacase the param is invalid");
53 return;
54 }
55 auto paramObject = JSRef<JSObject>::Cast(info[0]);
56
57 auto value = 0;
58 auto jsValue = paramObject->GetProperty("value");
59 if (jsValue->IsNumber()) {
60 value = jsValue->ToNumber<double>();
61 } else {
62 LOGE("create progress fail because the value is not number");
63 }
64
65 auto total = 100;
66 auto jsTotal = paramObject->GetProperty("total");
67 if (jsTotal->IsNumber() && jsTotal->ToNumber<int>() > 0) {
68 total = jsTotal->ToNumber<int>();
69 } else {
70 LOGE("create progress fail because the total is not value or total is less than zero");
71 }
72
73 if (value > total) {
74 LOGE("value is lager than total , set value euqals total");
75 value = total;
76 } else if (value < 0) {
77 LOGE("value is s less than zero, set value euqals zero");
78 value = 0;
79 }
80
81 auto progressType = ProgressType::LINEAR;
82 auto jsStyle = paramObject->GetProperty("type");
83 if (jsStyle->IsNull() || jsStyle->IsUndefined()) {
84 jsStyle = paramObject->GetProperty("style");
85 }
86
87 auto progressStyle = static_cast<ProgressStyle>(jsStyle->ToNumber<int32_t>());
88 if (progressStyle == ProgressStyle::Eclipse) {
89 progressType = ProgressType::MOON;
90 } else if (progressStyle == ProgressStyle::Ring) {
91 progressType = ProgressType::RING;
92 } else if (progressStyle == ProgressStyle::ScaleRing) {
93 progressType = ProgressType::SCALE;
94 } else if (progressStyle == ProgressStyle::Capsule) {
95 progressType = ProgressType::CAPSULE;
96 }
97
98 ProgressModel::GetInstance()->Create(0.0, value, 0.0, total, static_cast<NG::ProgressType>(progressType));
99 }
100
JSBind(BindingTarget globalObj)101 void JSProgress::JSBind(BindingTarget globalObj)
102 {
103 JSClass<JSProgress>::Declare("Progress");
104 MethodOptions opt = MethodOptions::NONE;
105
106 JSClass<JSProgress>::StaticMethod("create", &JSProgress::Create, opt);
107 JSClass<JSProgress>::StaticMethod("value", &JSProgress::SetValue, opt);
108 JSClass<JSProgress>::StaticMethod("color", &JSProgress::SetColor, opt);
109 JSClass<JSProgress>::StaticMethod("circularStyle", &JSProgress::SetCircularStyle, opt);
110 JSClass<JSProgress>::StaticMethod("cricularStyle", &JSProgress::SetCircularStyle, opt);
111 JSClass<JSProgress>::StaticMethod("style", &JSProgress::SetCircularStyle, opt);
112 JSClass<JSProgress>::StaticMethod("backgroundColor", &JSProgress::JsBackgroundColor, opt);
113 JSClass<JSProgress>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
114 JSClass<JSProgress>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
115 JSClass<JSProgress>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
116 JSClass<JSProgress>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
117 JSClass<JSProgress>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
118 JSClass<JSProgress>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
119 JSClass<JSProgress>::Inherit<JSViewAbstract>();
120 JSClass<JSProgress>::Bind(globalObj);
121 }
122
SetValue(double value)123 void JSProgress::SetValue(double value)
124 {
125 if (std::isnan(value)) {
126 return;
127 }
128
129 if (value < 0) {
130 LOGE("value is leses than zero , set value euqals zero");
131 value = 0;
132 }
133
134 ProgressModel::GetInstance()->SetValue(value);
135 }
136
SetColor(const JSCallbackInfo & info)137 void JSProgress::SetColor(const JSCallbackInfo& info)
138 {
139 Color colorVal;
140 if (!ParseJsColor(info[0], colorVal)) {
141 return;
142 }
143
144 ProgressModel::GetInstance()->SetColor(colorVal);
145 }
146
SetCircularStyle(const JSCallbackInfo & info)147 void JSProgress::SetCircularStyle(const JSCallbackInfo& info)
148 {
149 if (info.Length() < 1) {
150 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
151 return;
152 }
153
154 auto paramObject = JSRef<JSObject>::Cast(info[0]);
155 RefPtr<ProgressTheme> theme = GetTheme<ProgressTheme>();
156
157 Dimension strokeWidthDimension;
158 auto jsStrokeWidth = paramObject->GetProperty("strokeWidth");
159 if (!ParseJsDimensionVp(jsStrokeWidth, strokeWidthDimension)) {
160 LOGI("circular Style error. now use default strokeWidth");
161 strokeWidthDimension = theme->GetTrackThickness();
162 }
163
164 if (strokeWidthDimension.Value() <= 0.0 || strokeWidthDimension.Unit() == DimensionUnit::PERCENT) {
165 strokeWidthDimension = theme->GetTrackThickness();
166 }
167
168 ProgressModel::GetInstance()->SetStrokeWidth(strokeWidthDimension);
169
170 auto jsScaleCount = paramObject->GetProperty("scaleCount");
171 auto scaleCount = jsScaleCount->IsNumber() ? jsScaleCount->ToNumber<int32_t>() : theme->GetScaleNumber();
172 if (scaleCount > 0.0) {
173 ProgressModel::GetInstance()->SetScaleCount(scaleCount);
174 } else {
175 ProgressModel::GetInstance()->SetScaleCount(theme->GetScaleNumber());
176 }
177
178 Dimension scaleWidthDimension;
179 auto jsScaleWidth = paramObject->GetProperty("scaleWidth");
180 if (!ParseJsDimensionVp(jsScaleWidth, scaleWidthDimension)) {
181 LOGI("circular Style error. now use default scaleWidth");
182 scaleWidthDimension = theme->GetScaleWidth();
183 }
184
185 if ((scaleWidthDimension.Value() <= 0.0) || (scaleWidthDimension.Value() > strokeWidthDimension.Value()) ||
186 scaleWidthDimension.Unit() == DimensionUnit::PERCENT) {
187 scaleWidthDimension = theme->GetScaleWidth();
188 }
189
190 ProgressModel::GetInstance()->SetScaleWidth(scaleWidthDimension);
191 }
192
JsBackgroundColor(const JSCallbackInfo & info)193 void JSProgress::JsBackgroundColor(const JSCallbackInfo& info)
194 {
195 if (info.Length() < 1) {
196 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
197 return;
198 }
199
200 Color colorVal;
201 if (!ParseJsColor(info[0], colorVal)) {
202 return;
203 }
204
205 ProgressModel::GetInstance()->SetBackgroundColor(colorVal);
206 }
207
208 } // namespace OHOS::Ace::Framework
209