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_polygon.h"
17
18 #include "bridge/declarative_frontend/jsview/models/polygon_model_impl.h"
19 #include "core/common/container.h"
20 #include "core/components_ng/pattern/shape/polygon_model.h"
21 #include "core/components_ng/pattern/shape/polygon_model_ng.h"
22
23 namespace OHOS::Ace {
24
25 std::unique_ptr<PolygonModel> PolygonModel::instance_ = nullptr;
26 std::mutex PolygonModel::mutex_;
27
GetInstance()28 PolygonModel* PolygonModel::GetInstance()
29 {
30 if (!instance_) {
31 std::lock_guard<std::mutex> lock(mutex_);
32 if (!instance_) {
33 #ifdef NG_BUILD
34 instance_.reset(new NG::PolygonModelNG());
35 #else
36 if (Container::IsCurrentUseNewPipeline()) {
37 instance_.reset(new NG::PolygonModelNG());
38 } else {
39 instance_.reset(new Framework::PolygonModelImpl());
40 }
41 #endif
42 }
43 }
44 return instance_.get();
45 }
46
47 } // namespace OHOS::Ace
48
49 namespace OHOS::Ace::Framework {
50
Create(const JSCallbackInfo & info)51 void JSPolygon::Create(const JSCallbackInfo& info)
52 {
53 PolygonModel::GetInstance()->Create(true);
54 JSShapeAbstract::SetSize(info);
55 }
56
JSBind(BindingTarget globalObj)57 void JSPolygon::JSBind(BindingTarget globalObj)
58 {
59 JSClass<JSPolygon>::Declare("Polygon");
60 MethodOptions opt = MethodOptions::NONE;
61 JSClass<JSPolygon>::StaticMethod("create", &JSPolygon::Create, opt);
62
63 JSClass<JSPolygon>::StaticMethod("width", &JSShapeAbstract::JsWidth);
64 JSClass<JSPolygon>::StaticMethod("height", &JSShapeAbstract::JsHeight);
65 JSClass<JSPolygon>::StaticMethod("points", &JSPolygon::JsPoints);
66
67 JSClass<JSPolygon>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
68 JSClass<JSPolygon>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
69 JSClass<JSPolygon>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
70 JSClass<JSPolygon>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
71
72 JSClass<JSPolygon>::InheritAndBind<JSShapeAbstract>(globalObj);
73 }
74
JsPoints(const JSCallbackInfo & info)75 void JSPolygon::JsPoints(const JSCallbackInfo& info)
76 {
77 ShapePoints shapePoints;
78 PolygonModel::GetInstance()->SetPoints(shapePoints);
79 if (info.Length() < 1 || !info[0]->IsArray()) {
80 return;
81 }
82 ShapePoint shapePoint;
83 JSRef<JSArray> pointsArray = JSRef<JSArray>::Cast(info[0]);
84 if (pointsArray->Length() < 2) {
85 return;
86 }
87 for (size_t i = 0; i < pointsArray->Length(); i++) {
88 JSRef<JSVal> val = pointsArray->GetValueAt(i);
89 if (!val->IsArray()) {
90 continue;
91 }
92 JSRef<JSArray> pointArray = JSRef<JSArray>::Cast(val);
93 if (pointArray->GetValueAt(0)->IsNumber()) {
94 shapePoint.first = Dimension(pointArray->GetValueAt(0)->ToNumber<double>(), DimensionUnit::VP);
95 } else if (pointArray->GetValueAt(0)->IsString()) {
96 shapePoint.first = StringUtils::StringToDimension(pointArray->GetValueAt(0)->ToString(), true);
97 } else {
98 return;
99 }
100 if (pointArray->GetValueAt(1)->IsNumber()) {
101 shapePoint.second = Dimension(pointArray->GetValueAt(1)->ToNumber<double>(), DimensionUnit::VP);
102 } else if (pointArray->GetValueAt(1)->IsString()) {
103 shapePoint.second = StringUtils::StringToDimension(pointArray->GetValueAt(1)->ToString(), true);
104 } else {
105 return;
106 }
107 shapePoints.push_back(shapePoint);
108 }
109 PolygonModel::GetInstance()->SetPoints(shapePoints);
110 }
111
112 } // namespace OHOS::Ace::Framework
113