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_line.h"
17
18 #include "bridge/declarative_frontend/jsview/models/line_model_impl.h"
19 #include "core/components/shape/shape_component.h"
20 #include "core/components_ng/pattern/shape/line_model.h"
21 #include "core/components_ng/pattern/shape/line_model_ng.h"
22
23 namespace OHOS::Ace {
24
25 std::unique_ptr<LineModel> LineModel::instance_ = nullptr;
26 std::mutex LineModel::mutex_;
27
GetInstance()28 LineModel* LineModel::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::LineModelNG());
35 #else
36 if (Container::IsCurrentUseNewPipeline()) {
37 instance_.reset(new NG::LineModelNG());
38 } else {
39 instance_.reset(new Framework::LineModelImpl());
40 }
41 #endif
42 }
43 }
44 return instance_.get();
45 }
46
47 } // namespace OHOS::Ace
48
49 namespace OHOS::Ace::Framework {
50
JSBind(BindingTarget globalObj)51 void JSLine::JSBind(BindingTarget globalObj)
52 {
53 JSClass<JSLine>::Declare("Line");
54 MethodOptions opt = MethodOptions::NONE;
55 JSClass<JSLine>::StaticMethod("create", &JSLine::Create, opt);
56
57 JSClass<JSLine>::StaticMethod("width", &JSShapeAbstract::JsWidth);
58 JSClass<JSLine>::StaticMethod("height", &JSShapeAbstract::JsHeight);
59 JSClass<JSLine>::StaticMethod("startPoint", &JSLine::SetStart);
60 JSClass<JSLine>::StaticMethod("endPoint", &JSLine::SetEnd);
61
62 JSClass<JSLine>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
63 JSClass<JSLine>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
64 JSClass<JSLine>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
65 JSClass<JSLine>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
66
67 JSClass<JSLine>::InheritAndBind<JSShapeAbstract>(globalObj);
68 }
69
Create(const JSCallbackInfo & info)70 void JSLine::Create(const JSCallbackInfo& info)
71 {
72 LineModel::GetInstance()->Create();
73 JSShapeAbstract::SetSize(info);
74 }
75
SetStart(const JSCallbackInfo & info)76 void JSLine::SetStart(const JSCallbackInfo& info)
77 {
78 if (info.Length() < 1 || !info[0]->IsArray()) {
79 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
80 return;
81 }
82 JSRef<JSArray> pointArray = JSRef<JSArray>::Cast(info[0]);
83 ShapePoint startPoint;
84 SetPoint(pointArray, startPoint);
85 LineModel::GetInstance()->StartPoint(startPoint);
86 }
87
SetEnd(const JSCallbackInfo & info)88 void JSLine::SetEnd(const JSCallbackInfo& info)
89 {
90 if (info.Length() < 1 || !info[0]->IsArray()) {
91 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
92 return;
93 }
94 JSRef<JSArray> pointArray = JSRef<JSArray>::Cast(info[0]);
95 ShapePoint endPoint;
96 SetPoint(pointArray, endPoint);
97 LineModel::GetInstance()->EndPoint(endPoint);
98 }
99
SetPoint(const JSRef<JSArray> & array,ShapePoint & point)100 void JSLine::SetPoint(const JSRef<JSArray>& array, ShapePoint& point)
101 {
102 if (array->Length() < 1) {
103 LOGE("The starting point is one");
104 return;
105 }
106 if (array->GetValueAt(0)->IsNumber()) {
107 point.first = Dimension(array->GetValueAt(0)->ToNumber<double>(), DimensionUnit::VP);
108 } else if (array->GetValueAt(0)->IsString()) {
109 if (Container::LessThanAPIVersion(PlatformVersion::VERSION_TEN)) {
110 point.first = StringUtils::StringToDimension(array->GetValueAt(0)->ToString(), true);
111 } else {
112 if (!StringUtils::StringToDimensionWithUnitNG(
113 array->GetValueAt(0)->ToString(), point.first, DimensionUnit::VP, 0.0)) {
114 // unit is invalid, use default value(0.0vp) instead.
115 point.first = 0.0_vp;
116 }
117 }
118 } else {
119 LOGE("Line point should be Number or String");
120 }
121 if (array->GetValueAt(1)->IsNumber()) {
122 point.second = Dimension(array->GetValueAt(1)->ToNumber<double>(), DimensionUnit::VP);
123 } else if (array->GetValueAt(1)->IsString()) {
124 if (Container::LessThanAPIVersion(PlatformVersion::VERSION_TEN)) {
125 point.second = StringUtils::StringToDimension(array->GetValueAt(1)->ToString(), true);
126 } else {
127 if (!StringUtils::StringToDimensionWithUnitNG(
128 array->GetValueAt(1)->ToString(), point.second, DimensionUnit::VP, 0.0)) {
129 // unit is invalid, use default value(0.0vp) instead.
130 point.second = 0.0_vp;
131 }
132 }
133 } else {
134 LOGE("Line point should be Number or String");
135 }
136 }
137
138 } // namespace OHOS::Ace::Framework