/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "frameworks/bridge/declarative_frontend/jsview/js_counter.h" #include "bridge/declarative_frontend/jsview/models/counter_model_impl.h" #include "core/components_ng/pattern/counter/counter_model_ng.h" #include "frameworks/bridge/common/utils/utils.h" #include "frameworks/bridge/declarative_frontend/engine/bindings.h" #include "frameworks/bridge/declarative_frontend/engine/functions/js_click_function.h" #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h" #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h" #include "frameworks/core/components/counter/counter_theme.h" namespace OHOS::Ace { std::unique_ptr CounterModel::instance_ = nullptr; CounterModel* CounterModel::GetInstance() { if (!instance_) { #ifdef NG_BUILD instance_.reset(new NG::CounterModelNG()); #else if (Container::IsCurrentUseNewPipeline()) { instance_.reset(new NG::CounterModelNG()); } else { instance_.reset(new Framework::CounterModelImpl()); } #endif } return instance_.get(); } } // namespace OHOS::Ace namespace OHOS::Ace::Framework { void JSCounter::JSBind(BindingTarget globalObj) { JSClass::Declare("Counter"); JSClass::StaticMethod("create", &JSCounter::Create, MethodOptions::NONE); JSClass::StaticMethod("onInc", &JSCounter::JsOnInc); JSClass::StaticMethod("onDec", &JSCounter::JsOnDec); JSClass::StaticMethod("height", &JSCounter::JSHeight); JSClass::StaticMethod("width", &JSCounter::JSWidth); JSClass::StaticMethod("size", &JSCounter::SetSize); JSClass::StaticMethod("controlWidth", &JSCounter::JSControlWidth); JSClass::StaticMethod("state", &JSCounter::JSStateChange); JSClass::StaticMethod("backgroundColor", &JSCounter::JsBackgroundColor); JSClass::Inherit(); JSClass::Bind(globalObj); } void JSCounter::JsOnInc(const JSCallbackInfo& args) { if (args.Length() < 1) { LOGW("Must contain at least 1 argument"); return; } if (!args[0]->IsFunction()) { LOGW("Argument is not a function object"); return; } CounterModel::GetInstance()->SetOnInc( JsEventCallback(args.GetExecutionContext(), JSRef::Cast(args[0]))); args.ReturnSelf(); } void JSCounter::JsOnDec(const JSCallbackInfo& args) { if (args.Length() < 1) { LOGW("Must contain at least 1 argument"); return; } if (!args[0]->IsFunction()) { LOGW("Argument is not a function object"); return; } CounterModel::GetInstance()->SetOnDec( JsEventCallback(args.GetExecutionContext(), JSRef::Cast(args[0]))); args.ReturnSelf(); } void JSCounter::JSHeight(const JSCallbackInfo& args) { if (args.Length() < 1) { LOGE("The arg is wrong, it is supposed to have at least 1 arguments"); return; } Dimension value; if (!ConvertFromJSValue(args[0], value)) { LOGE("args can not set height"); return; } if (LessNotEqual(value.Value(), 0.0)) { return; } CounterModel::GetInstance()->SetHeight(value); args.ReturnSelf(); } void JSCounter::JSWidth(const JSCallbackInfo& args) { if (args.Length() < 1) { LOGE("The arg is wrong, it is supposed to have at least 1 arguments"); return; } Dimension value; if (!ConvertFromJSValue(args[0], value)) { LOGE("args can not set width"); return; } if (LessNotEqual(value.Value(), 0.0)) { return; } CounterModel::GetInstance()->SetWidth(value); args.ReturnSelf(); } void JSCounter::SetSize(const JSCallbackInfo& args) { if (args.Length() >= 1 && args[0]->IsObject()) { JSRef obj = JSRef::Cast(args[0]); Dimension height; if (ConvertFromJSValue(obj->GetProperty("height"), height) && height.IsValid()) { if (GreatOrEqual(height.Value(), 0.0)) { CounterModel::GetInstance()->SetWidth(height); } } Dimension width; if (ConvertFromJSValue(obj->GetProperty("width"), width) && width.IsValid()) { if (GreatOrEqual(width.Value(), 0.0)) { CounterModel::GetInstance()->SetWidth(width); } } } args.ReturnSelf(); } void JSCounter::JSControlWidth(const JSCallbackInfo& args) { if (args.Length() < 1) { LOGE("The arg is wrong, it is supposed to have at least 1 arguments"); return; } Dimension value; if (!ConvertFromJSValue(args[0], value)) { LOGE("args can not set control width"); return; } CounterModel::GetInstance()->SetControlWidth(value); args.ReturnSelf(); } void JSCounter::JSStateChange(bool state) { CounterModel::GetInstance()->SetStateChange(state); } void JSCounter::JsBackgroundColor(const JSCallbackInfo& args) { if (args.Length() < 1) { LOGE("The arg is wrong, it is supposed to have at least 1 arguments"); return; } Color color; if (!ParseJsColor(args[0], color)) { LOGE("args can not set backgroundColor"); return; } CounterModel::GetInstance()->SetBackgroundColor(color); args.ReturnSelf(); } void JSCounter::Create() { CounterModel::GetInstance()->Create(); } } // namespace OHOS::Ace::Framework