• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_counter.h"
17 
18 #include "frameworks/bridge/common/utils/utils.h"
19 #include "frameworks/bridge/declarative_frontend/engine/bindings.h"
20 #include "frameworks/bridge/declarative_frontend/engine/functions/js_click_function.h"
21 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
22 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
23 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
24 #include "frameworks/core/components/counter/counter_theme.h"
25 
26 namespace OHOS::Ace::Framework {
27 namespace {
28 
29 constexpr Dimension COUNTER_DEFAULT_HEIGHT = 32.0_vp;
30 constexpr Dimension COUNTER_DEFAULT_WIDTH = 100.0_vp;
31 constexpr Dimension COUNTER_DEFAULT_CONTROLWIDTH = 32.0_vp;
32 constexpr Dimension COUNTER_DEFAULT_RADIUS = 4.0_vp;
33 
34 } // namespace
35 
JSBind(BindingTarget globalObj)36 void JSCounter::JSBind(BindingTarget globalObj)
37 {
38     JSClass<JSCounter>::Declare("Counter");
39     JSClass<JSCounter>::StaticMethod("create", &JSCounter::Create, MethodOptions::NONE);
40     JSClass<JSCounter>::StaticMethod("onInc", &JSCounter::JsOnInc);
41     JSClass<JSCounter>::StaticMethod("onDec", &JSCounter::JsOnDec);
42     JSClass<JSCounter>::StaticMethod("height", &JSCounter::JSHeight);
43     JSClass<JSCounter>::StaticMethod("width", &JSCounter::JSWidth);
44     JSClass<JSCounter>::StaticMethod("size", &JSCounter::SetSize);
45     JSClass<JSCounter>::StaticMethod("controlWidth", &JSCounter::JSControlwidth);
46     JSClass<JSCounter>::StaticMethod("state", &JSCounter::JSStateChange);
47     JSClass<JSCounter>::StaticMethod("backgroundColor", &JSCounter::JsBackgroundColor);
48     JSClass<JSCounter>::Inherit<JSContainerBase>();
49     JSClass<JSCounter>::Bind(globalObj);
50 }
51 
JsOnInc(const JSCallbackInfo & args)52 void JSCounter::JsOnInc(const JSCallbackInfo& args)
53 {
54     if (!JSViewBindEvent(&CounterComponent::SetOnInc, args)) {
55         LOGW("Failed to bind event");
56     }
57     args.ReturnSelf();
58 }
59 
JsOnDec(const JSCallbackInfo & args)60 void JSCounter::JsOnDec(const JSCallbackInfo& args)
61 {
62     if (!JSViewBindEvent(&CounterComponent::SetOnDec, args)) {
63         LOGW("Failed to bind event");
64     }
65     args.ReturnSelf();
66 }
67 
JSHeight(const JSCallbackInfo & args)68 void JSCounter::JSHeight(const JSCallbackInfo& args)
69 {
70     if (args.Length() < 1) {
71         LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
72         return;
73     }
74 
75     Dimension value;
76     if (!ConvertFromJSValue(args[0], value)) {
77         LOGE("args can not set height");
78         return;
79     }
80 
81     if (LessNotEqual(value.Value(), 0.0)) {
82         return;
83     }
84 
85     JSViewSetProperty(&CounterComponent::SetHeight, value);
86     args.ReturnSelf();
87 }
88 
JSWidth(const JSCallbackInfo & args)89 void JSCounter::JSWidth(const JSCallbackInfo& args)
90 {
91     if (args.Length() < 1) {
92         LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
93         return;
94     }
95 
96     Dimension value;
97     if (!ConvertFromJSValue(args[0], value)) {
98         LOGE("args can not set width");
99         return;
100     }
101 
102     if (LessNotEqual(value.Value(), 0.0)) {
103         return;
104     }
105 
106     JSViewSetProperty(&CounterComponent::SetWidth, value);
107     args.ReturnSelf();
108 }
109 
SetSize(const JSCallbackInfo & args)110 void JSCounter::SetSize(const JSCallbackInfo& args)
111 {
112     if (args.Length() >= 1 && args[0]->IsObject()) {
113         JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
114 
115         Dimension height;
116         if (ConvertFromJSValue(obj->GetProperty("height"), height) && height.IsValid()) {
117             if (GreatOrEqual(height.Value(), 0.0)) {
118                 JSViewSetProperty(&CounterComponent::SetHeight, height);
119             }
120         }
121 
122         Dimension width;
123         if (ConvertFromJSValue(obj->GetProperty("width"), width) && width.IsValid()) {
124             if (GreatOrEqual(width.Value(), 0.0)) {
125                 JSViewSetProperty(&CounterComponent::SetWidth, width);
126             }
127         }
128     }
129     args.ReturnSelf();
130 }
131 
JSControlwidth(const JSCallbackInfo & args)132 void JSCounter::JSControlwidth(const JSCallbackInfo& args)
133 {
134     if (args.Length() < 1) {
135         LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
136         return;
137     }
138 
139     Dimension value;
140     if (!ConvertFromJSValue(args[0], value)) {
141         LOGE("args can not set controlwidth");
142         return;
143     }
144 
145     JSViewSetProperty(&CounterComponent::SetControlWidth, value);
146     args.ReturnSelf();
147 }
148 
JSStateChange(bool state)149 void JSCounter::JSStateChange(bool state)
150 {
151     JSViewSetProperty(&CounterComponent::SetState, state);
152 }
153 
JsBackgroundColor(const JSCallbackInfo & args)154 void JSCounter::JsBackgroundColor(const JSCallbackInfo& args)
155 {
156     if (args.Length() < 1) {
157         LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
158         return;
159     }
160 
161     if (!args[0]->IsString() && !args[0]->IsNumber()) {
162         LOGE("arg is not a string or number.");
163         return;
164     }
165 
166     Color color;
167     if (!ConvertFromJSValue(args[0], color)) {
168         LOGE("args can not set backgroundColor");
169         return;
170     }
171     JSViewSetProperty(&CounterComponent::SetBackgroundColor, color);
172     args.ReturnSelf();
173 }
174 
Create()175 void JSCounter::Create()
176 {
177     std::list<RefPtr<Component>> children;
178     RefPtr<OHOS::Ace::CounterComponent> component = AceType::MakeRefPtr<CounterComponent>(children);
179     ViewStackProcessor::GetInstance()->Push(component);
180 
181     component->SetHeight(COUNTER_DEFAULT_HEIGHT);
182     component->SetWidth(COUNTER_DEFAULT_WIDTH);
183     component->SetControlWidth(COUNTER_DEFAULT_CONTROLWIDTH);
184     component->SetControlRadius(COUNTER_DEFAULT_RADIUS);
185 }
186 
187 } // namespace OHOS::Ace::Framework
188