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