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 "frameworks/bridge/declarative_frontend/jsview/js_gauge.h"
17
18 #include "bridge/declarative_frontend/view_stack_processor.h"
19 #include "core/components/chart/chart_component.h"
20 #include "core/components/progress/progress_component.h"
21 #include "frameworks/bridge/declarative_frontend/jsview/js_interactable_view.h"
22
23 namespace OHOS::Ace::Framework {
24
JSBind(BindingTarget globalObj)25 void JSGauge::JSBind(BindingTarget globalObj)
26 {
27 JSClass<JSGauge>::Declare("Gauge");
28 JSClass<JSGauge>::StaticMethod("create", &JSGauge::Create);
29
30 JSClass<JSGauge>::StaticMethod("value", &JSGauge::SetValue);
31 JSClass<JSGauge>::StaticMethod("startAngle", &JSGauge::SetStartAngle);
32 JSClass<JSGauge>::StaticMethod("endAngle", &JSGauge::SetEndAngle);
33 JSClass<JSGauge>::StaticMethod("colors", &JSGauge::SetColors);
34 JSClass<JSGauge>::StaticMethod("strokeWidth", &JSGauge::SetStrokeWidth);
35 JSClass<JSGauge>::StaticMethod("labelConfig", &JSGauge::SetLableConfig);
36 JSClass<JSGauge>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
37 JSClass<JSGauge>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
38 JSClass<JSGauge>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
39 JSClass<JSGauge>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
40 JSClass<JSGauge>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
41 JSClass<JSGauge>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
42
43 JSClass<JSGauge>::Inherit<JSViewAbstract>();
44 JSClass<JSGauge>::Bind(globalObj);
45 }
46
Create(const JSCallbackInfo & info)47 void JSGauge::Create(const JSCallbackInfo& info)
48 {
49 if (info.Length() < 1 && !info[0]->IsObject()) {
50 LOGE("gauge create error, info is non-valid");
51 return;
52 }
53
54 auto paramObject = JSRef<JSObject>::Cast(info[0]);
55 auto value = paramObject->GetProperty("value");
56 auto min = paramObject->GetProperty("min");
57 auto max = paramObject->GetProperty("max");
58
59 double gaugeMin = min->IsNumber() ? min->ToNumber<double>() : 0;
60 double gaugeMax = max->IsNumber() ? max->ToNumber<double>() : 100;
61 double gaugeValue = value->IsNumber() ? value->ToNumber<double>() : 0;
62 auto progressChild =
63 AceType::MakeRefPtr<ProgressComponent>(gaugeMin, gaugeValue, gaugeMin, gaugeMax, ProgressType::GAUGE);
64 progressChild->SetIndicatorFlag(true);
65 progressChild->SetInspectorTag("Gauge");
66 ViewStackProcessor::GetInstance()->Push(progressChild);
67 RefPtr<ProgressTheme> progressTheme = GetTheme<ProgressTheme>();
68 progressChild->InitStyle(progressTheme);
69 }
70
SetValue(const JSCallbackInfo & info)71 void JSGauge::SetValue(const JSCallbackInfo& info)
72 {
73 if (info.Length() < 1 && !info[0]->IsNumber()) {
74 LOGE("JSGauge::SetValue::The info is wrong, it is supposed to have atleast 1 arguments");
75 return;
76 }
77 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
78 auto gaugeComponent = AceType::DynamicCast<ProgressComponent>(component);
79 if (!gaugeComponent) {
80 return;
81 }
82 gaugeComponent->SetValue(info[0]->ToNumber<double>());
83 }
84
SetStartAngle(const JSCallbackInfo & info)85 void JSGauge::SetStartAngle(const JSCallbackInfo& info)
86 {
87 if (info.Length() < 1 && !info[0]->IsNumber()) {
88 LOGE("JSGauge::SetStartAngle::The info is wrong, it is supposed to have atleast 1 arguments");
89 return;
90 }
91 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
92 auto gaugeComponent = AceType::DynamicCast<ProgressComponent>(component);
93 if (!gaugeComponent) {
94 return;
95 }
96 gaugeComponent->GetTrack()->GetTrackInfo()->SetStartDegree(info[0]->ToNumber<double>());
97 }
98
SetEndAngle(const JSCallbackInfo & info)99 void JSGauge::SetEndAngle(const JSCallbackInfo& info)
100 {
101 if (info.Length() < 1 && !info[0]->IsNumber()) {
102 LOGE("JSGauge::SetEndAngle::The info is wrong, it is supposed to have atleast 1 arguments");
103 return;
104 }
105 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
106 auto gaugeComponent = AceType::DynamicCast<ProgressComponent>(component);
107 if (!gaugeComponent) {
108 return;
109 }
110 gaugeComponent->GetTrack()->GetTrackInfo()->SetSweepDegree(info[0]->ToNumber<double>());
111 }
112
SetColors(const JSCallbackInfo & info)113 void JSGauge::SetColors(const JSCallbackInfo& info)
114 {
115 if (info.Length() < 1 || !info[0]->IsArray()) {
116 LOGE("The number of argument is less than 1, or the argument is not array.");
117 return;
118 }
119 std::vector<Color> colors;
120 std::vector<double> values;
121 auto jsColor = JSRef<JSArray>::Cast(info[0]);
122 for (size_t i = 0; i < jsColor->Length(); ++i) {
123 JSRef<JSVal> jsValue = jsColor->GetValueAt(i);
124 if (!jsValue->IsArray()) {
125 return;
126 }
127 JSRef<JSArray> tempColors = jsColor->GetValueAt(i);
128 double value = tempColors->GetValueAt(1)->ToNumber<double>();
129 Color selectedColor;
130 if (!ParseJsColor(tempColors->GetValueAt(0), selectedColor)) {
131 return;
132 }
133 colors.push_back(selectedColor);
134 values.push_back(value);
135 }
136 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
137 auto gaugeComponent = AceType::DynamicCast<ProgressComponent>(component);
138 if (!gaugeComponent) {
139 return;
140 }
141 gaugeComponent->SetSectionsStyle(colors, values);
142 }
143
SetStrokeWidth(const JSCallbackInfo & info)144 void JSGauge::SetStrokeWidth(const JSCallbackInfo& info)
145 {
146 if (info.Length() < 1) {
147 LOGE(" JSGauge::SetStrokeWidth::The info is wrong, it is supposed to have atleast 1 arguments");
148 return;
149 }
150 Dimension strokeWidth;
151 if (!ParseJsDimensionVp(info[0], strokeWidth)) {
152 return;
153 }
154 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
155 auto gaugeComponent = AceType::DynamicCast<ProgressComponent>(component);
156 if (!gaugeComponent) {
157 return;
158 }
159 gaugeComponent->SetTrackThickness(strokeWidth);
160 }
161
SetLableConfig(const JSCallbackInfo & info)162 void JSGauge::SetLableConfig(const JSCallbackInfo& info)
163 {
164 if (info.Length() < 1 && !info[0]->IsObject()) {
165 LOGE("JSGauge::SetLableTextConfig::The info is wrong, it is supposed to have atleast 1 arguments");
166 return;
167 }
168 auto paramObject = JSRef<JSObject>::Cast(info[0]);
169 auto lableText = paramObject->GetProperty("text");
170 auto lableColor = paramObject->GetProperty("color");
171 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
172 auto gaugeComponent = AceType::DynamicCast<ProgressComponent>(component);
173
174 if (lableText->IsString()) {
175 gaugeComponent->SetLableMarkedText(lableText->ToString());
176 }
177 Color currentColor;
178 if (ParseJsColor(lableColor, currentColor)) {
179 gaugeComponent->SetMarkedTextColor(currentColor);
180 }
181 }
182
183 } // namespace OHOS::Ace::Framework
184