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 "core/components/shape/shape_component.h"
19 #include "frameworks/base/memory/referenced.h"
20 #include "frameworks/bridge/declarative_frontend/engine/functions/js_function.h"
21 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
22
23 namespace OHOS::Ace::Framework {
24
JSBind(BindingTarget globalObj)25 void JSLine::JSBind(BindingTarget globalObj)
26 {
27 JSClass<JSLine>::Declare("Line");
28 MethodOptions opt = MethodOptions::NONE;
29 JSClass<JSLine>::StaticMethod("create", &JSLine::Create, opt);
30
31 JSClass<JSLine>::StaticMethod("width", &JSShapeAbstract::JsWidth);
32 JSClass<JSLine>::StaticMethod("height", &JSShapeAbstract::JsHeight);
33 JSClass<JSLine>::StaticMethod("startPoint", &JSLine::SetStart);
34 JSClass<JSLine>::StaticMethod("endPoint", &JSLine::SetEnd);
35
36 JSClass<JSLine>::Inherit<JSShapeAbstract>();
37 JSClass<JSLine>::Bind<>(globalObj);
38 }
39
Create(const JSCallbackInfo & info)40 void JSLine::Create(const JSCallbackInfo& info)
41 {
42 RefPtr<ShapeComponent> lineComponent = AceType::MakeRefPtr<OHOS::Ace::ShapeComponent>(ShapeType::LINE);
43 lineComponent->SetStroke(Color::BLACK);
44 ViewStackProcessor::GetInstance()->Push(lineComponent);
45 JSShapeAbstract::SetSize(info);
46 }
47
SetStart(const JSCallbackInfo & info)48 void JSLine::SetStart(const JSCallbackInfo& info)
49 {
50 if (info.Length() < 1) {
51 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
52 return;
53 }
54 auto line = AceType::DynamicCast<ShapeComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
55 if (!line) {
56 LOGE("startPointComponent is null.");
57 return;
58 }
59 JSRef<JSArray> pointArray = JSRef<JSArray>::Cast(info[0]);
60 ShapePoint startPoint;
61 SetPoint(pointArray, startPoint);
62 line->SetStart(startPoint);
63 }
64
SetEnd(const JSCallbackInfo & info)65 void JSLine::SetEnd(const JSCallbackInfo& info)
66 {
67 if (info.Length() < 1) {
68 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
69 return;
70 }
71 auto line = AceType::DynamicCast<ShapeComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
72 if (!line) {
73 LOGE("startPointComponent is null.");
74 return;
75 }
76 JSRef<JSArray> pointArray = JSRef<JSArray>::Cast(info[0]);
77 ShapePoint endPoint;
78 SetPoint(pointArray, endPoint);
79 line->SetEnd(endPoint);
80 }
81
SetPoint(const JSRef<JSArray> & array,ShapePoint & point)82 void JSLine::SetPoint(const JSRef<JSArray>& array, ShapePoint& point)
83 {
84 if (array->Length() < 1) {
85 LOGE("The starting point is one");
86 return;
87 } else {
88 if (array->GetValueAt(0)->IsNumber()) {
89 point.first = Dimension(array->GetValueAt(0)->ToNumber<double>(), DimensionUnit::VP);
90 } else if (array->GetValueAt(0)->IsString()) {
91 point.first = StringUtils::StringToDimension(array->GetValueAt(0)->ToString(), true);
92 } else {
93 LOGE("Line point should be Number or String");
94 }
95 if (array->GetValueAt(1)->IsNumber()) {
96 point.second = Dimension(array->GetValueAt(1)->ToNumber<double>(), DimensionUnit::VP);
97 } else if (array->GetValueAt(0)->IsString()) {
98 point.second = StringUtils::StringToDimension(array->GetValueAt(1)->ToString(), true);
99 } else {
100 LOGE("Line point should be Number or String");
101 }
102 }
103 }
104
105 } // namespace OHOS::Ace::Framework