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