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