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
GetInstance()27 LineModel* LineModel::GetInstance()
28 {
29 if (!instance_) {
30 #ifdef NG_BUILD
31 instance_.reset(new NG::LineModelNG());
32 #else
33 if (Container::IsCurrentUseNewPipeline()) {
34 instance_.reset(new NG::LineModelNG());
35 } else {
36 instance_.reset(new Framework::LineModelImpl());
37 }
38 #endif
39 }
40 return instance_.get();
41 }
42
43 } // namespace OHOS::Ace
44
45 namespace OHOS::Ace::Framework {
46
JSBind(BindingTarget globalObj)47 void JSLine::JSBind(BindingTarget globalObj)
48 {
49 JSClass<JSLine>::Declare("Line");
50 MethodOptions opt = MethodOptions::NONE;
51 JSClass<JSLine>::StaticMethod("create", &JSLine::Create, opt);
52
53 JSClass<JSLine>::StaticMethod("width", &JSShapeAbstract::JsWidth);
54 JSClass<JSLine>::StaticMethod("height", &JSShapeAbstract::JsHeight);
55 JSClass<JSLine>::StaticMethod("startPoint", &JSLine::SetStart);
56 JSClass<JSLine>::StaticMethod("endPoint", &JSLine::SetEnd);
57
58 JSClass<JSLine>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
59 JSClass<JSLine>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
60 JSClass<JSLine>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
61 JSClass<JSLine>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
62
63 JSClass<JSLine>::Inherit<JSShapeAbstract>();
64 JSClass<JSLine>::Bind<>(globalObj);
65 }
66
Create(const JSCallbackInfo & info)67 void JSLine::Create(const JSCallbackInfo& info)
68 {
69 LineModel::GetInstance()->Create();
70 JSShapeAbstract::SetSize(info);
71 }
72
SetStart(const JSCallbackInfo & info)73 void JSLine::SetStart(const JSCallbackInfo& info)
74 {
75 if (info.Length() < 1) {
76 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
77 return;
78 }
79 JSRef<JSArray> pointArray = JSRef<JSArray>::Cast(info[0]);
80 ShapePoint startPoint;
81 SetPoint(pointArray, startPoint);
82 LineModel::GetInstance()->StartPoint(startPoint);
83 }
84
SetEnd(const JSCallbackInfo & info)85 void JSLine::SetEnd(const JSCallbackInfo& info)
86 {
87 if (info.Length() < 1) {
88 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
89 return;
90 }
91 JSRef<JSArray> pointArray = JSRef<JSArray>::Cast(info[0]);
92 ShapePoint endPoint;
93 SetPoint(pointArray, endPoint);
94 LineModel::GetInstance()->EndPoint(endPoint);
95 }
96
SetPoint(const JSRef<JSArray> & array,ShapePoint & point)97 void JSLine::SetPoint(const JSRef<JSArray>& array, ShapePoint& point)
98 {
99 if (array->Length() < 1) {
100 LOGE("The starting point is one");
101 return;
102 } else {
103 if (array->GetValueAt(0)->IsNumber()) {
104 point.first = Dimension(array->GetValueAt(0)->ToNumber<double>(), DimensionUnit::VP);
105 } else if (array->GetValueAt(0)->IsString()) {
106 point.first = StringUtils::StringToDimension(array->GetValueAt(0)->ToString(), true);
107 } else {
108 LOGE("Line point should be Number or String");
109 }
110 if (array->GetValueAt(1)->IsNumber()) {
111 point.second = Dimension(array->GetValueAt(1)->ToNumber<double>(), DimensionUnit::VP);
112 } else if (array->GetValueAt(0)->IsString()) {
113 point.second = StringUtils::StringToDimension(array->GetValueAt(1)->ToString(), true);
114 } else {
115 LOGE("Line point should be Number or String");
116 }
117 }
118 }
119
120 } // namespace OHOS::Ace::Framework