/* * 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_polygon.h" #include "bridge/declarative_frontend/jsview/models/polygon_model_impl.h" #include "core/common/container.h" #include "core/components_ng/pattern/shape/polygon_model.h" #include "core/components_ng/pattern/shape/polygon_model_ng.h" namespace OHOS::Ace { std::unique_ptr PolygonModel::instance_ = nullptr; std::mutex PolygonModel::mutex_; PolygonModel* PolygonModel::GetInstance() { if (!instance_) { std::lock_guard lock(mutex_); if (!instance_) { #ifdef NG_BUILD instance_.reset(new NG::PolygonModelNG()); #else if (Container::IsCurrentUseNewPipeline()) { instance_.reset(new NG::PolygonModelNG()); } else { instance_.reset(new Framework::PolygonModelImpl()); } #endif } } return instance_.get(); } } // namespace OHOS::Ace namespace OHOS::Ace::Framework { void JSPolygon::Create(const JSCallbackInfo& info) { PolygonModel::GetInstance()->Create(true); JSShapeAbstract::SetSize(info); } void JSPolygon::JSBind(BindingTarget globalObj) { JSClass::Declare("Polygon"); MethodOptions opt = MethodOptions::NONE; JSClass::StaticMethod("create", &JSPolygon::Create, opt); JSClass::StaticMethod("width", &JSShapeAbstract::JsWidth); JSClass::StaticMethod("height", &JSShapeAbstract::JsHeight); JSClass::StaticMethod("points", &JSPolygon::JsPoints); JSClass::StaticMethod("onTouch", &JSInteractableView::JsOnTouch); JSClass::StaticMethod("onHover", &JSInteractableView::JsOnHover); JSClass::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey); JSClass::StaticMethod("onClick", &JSInteractableView::JsOnClick); JSClass::InheritAndBind(globalObj); } void JSPolygon::JsPoints(const JSCallbackInfo& info) { ShapePoints shapePoints; PolygonModel::GetInstance()->SetPoints(shapePoints); if (info.Length() < 1 || !info[0]->IsArray()) { return; } ShapePoint shapePoint; JSRef pointsArray = JSRef::Cast(info[0]); if (pointsArray->Length() < 2) { return; } for (size_t i = 0; i < pointsArray->Length(); i++) { JSRef val = pointsArray->GetValueAt(i); if (!val->IsArray()) { continue; } JSRef pointArray = JSRef::Cast(val); if (pointArray->GetValueAt(0)->IsNumber()) { shapePoint.first = Dimension(pointArray->GetValueAt(0)->ToNumber(), DimensionUnit::VP); } else if (pointArray->GetValueAt(0)->IsString()) { shapePoint.first = StringUtils::StringToDimension(pointArray->GetValueAt(0)->ToString(), true); } else { return; } if (pointArray->GetValueAt(1)->IsNumber()) { shapePoint.second = Dimension(pointArray->GetValueAt(1)->ToNumber(), DimensionUnit::VP); } else if (pointArray->GetValueAt(1)->IsString()) { shapePoint.second = StringUtils::StringToDimension(pointArray->GetValueAt(1)->ToString(), true); } else { return; } shapePoints.push_back(shapePoint); } PolygonModel::GetInstance()->SetPoints(shapePoints); } } // namespace OHOS::Ace::Framework