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 {
GetInstance()24 LineModel* LineModel::GetInstance()
25 {
26 #ifdef NG_BUILD
27 static NG::LineModelNG instance;
28 return &instance;
29 #else
30 if (Container::IsCurrentUseNewPipeline()) {
31 static NG::LineModelNG instance;
32 return &instance;
33 } else {
34 static Framework::LineModelImpl instance;
35 return &instance;
36 }
37 #endif
38 }
39 } // namespace OHOS::Ace
40
41 namespace OHOS::Ace::Framework {
42
JSBind(BindingTarget globalObj)43 void JSLine::JSBind(BindingTarget globalObj)
44 {
45 JSClass<JSLine>::Declare("Line");
46 MethodOptions opt = MethodOptions::NONE;
47 JSClass<JSLine>::StaticMethod("create", &JSLine::Create, opt);
48
49 JSClass<JSLine>::StaticMethod("width", &JSShapeAbstract::JsWidth);
50 JSClass<JSLine>::StaticMethod("height", &JSShapeAbstract::JsHeight);
51 JSClass<JSLine>::StaticMethod("startPoint", &JSLine::SetStart);
52 JSClass<JSLine>::StaticMethod("endPoint", &JSLine::SetEnd);
53
54 JSClass<JSLine>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
55 JSClass<JSLine>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
56 JSClass<JSLine>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
57 JSClass<JSLine>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
58
59 JSClass<JSLine>::InheritAndBind<JSShapeAbstract>(globalObj);
60 }
61
Create(const JSCallbackInfo & info)62 void JSLine::Create(const JSCallbackInfo& info)
63 {
64 LineModel::GetInstance()->Create();
65 JSShapeAbstract::SetSize(info);
66 }
67
SetStart(const JSCallbackInfo & info)68 void JSLine::SetStart(const JSCallbackInfo& info)
69 {
70 if (info.Length() < 1 || !info[0]->IsArray()) {
71 return;
72 }
73 UnRegisterResource("LineStartPoint");
74 JSRef<JSArray> pointArray = JSRef<JSArray>::Cast(info[0]);
75 ShapePoint startPoint;
76 RefPtr<ResourceObject> pointResObjFirst;
77 RefPtr<ResourceObject> pointResObjSecond;
78 SetPoint(pointArray, startPoint, pointResObjFirst, pointResObjSecond);
79 if (SystemProperties::ConfigChangePerform() && (pointResObjFirst || pointResObjSecond)) {
80 std::vector<RefPtr<ResourceObject>> resObjArray = { pointResObjFirst, pointResObjSecond };
81 LineModel::GetInstance()->StartPoint(startPoint, resObjArray);
82 }
83 LineModel::GetInstance()->StartPoint(startPoint);
84 }
85
SetEnd(const JSCallbackInfo & info)86 void JSLine::SetEnd(const JSCallbackInfo& info)
87 {
88 if (info.Length() < 1 || !info[0]->IsArray()) {
89 return;
90 }
91 UnRegisterResource("LineEndPoint");
92 JSRef<JSArray> pointArray = JSRef<JSArray>::Cast(info[0]);
93 ShapePoint endPoint;
94 RefPtr<ResourceObject> pointResObjFirst;
95 RefPtr<ResourceObject> pointResObjSecond;
96 SetPoint(pointArray, endPoint, pointResObjFirst, pointResObjSecond);
97 if (SystemProperties::ConfigChangePerform() && (pointResObjFirst || pointResObjSecond)) {
98 std::vector<RefPtr<ResourceObject>> resObjArray = { pointResObjFirst, pointResObjSecond };
99 LineModel::GetInstance()->EndPoint(endPoint, resObjArray);
100 }
101 LineModel::GetInstance()->EndPoint(endPoint);
102 }
103
SetPoint(const JSRef<JSArray> & array,ShapePoint & point,RefPtr<ResourceObject> & pointResObjFirst,RefPtr<ResourceObject> & pointResObjSecond)104 void JSLine::SetPoint(const JSRef<JSArray>& array, ShapePoint& point, RefPtr<ResourceObject>& pointResObjFirst,
105 RefPtr<ResourceObject>& pointResObjSecond)
106 {
107 if (array->Length() < 1) {
108 return;
109 }
110
111 auto parseJsDimension = [](const JSRef<JSVal>& jsValue, Dimension &val, RefPtr<ResourceObject>& resObj) {
112 if (jsValue->IsNumber()) {
113 val = Dimension(jsValue->ToNumber<double>(), DimensionUnit::VP);
114 } else if (jsValue->IsString()) {
115 if (Container::LessThanAPIVersion(PlatformVersion::VERSION_TEN)) {
116 val = StringUtils::StringToDimension(jsValue->ToString(), true);
117 } else if (!StringUtils::StringToDimensionWithUnitNG(jsValue->ToString(), val, DimensionUnit::VP, 0.0)) {
118 // unit is invalid, use default value(0.0vp) instead.
119 val = 0.0_vp;
120 }
121 } else if (jsValue->IsObject()) {
122 CalcDimension value;
123 ParseJsDimensionVpNG(jsValue, value, resObj);
124 if (!StringUtils::StringToDimensionWithUnitNG(value.ToString(), val, DimensionUnit::VP, 0.0)) {
125 // unit is invalid, use default value(0.0vp) instead.
126 val = 0.0_vp;
127 }
128 }
129 };
130
131 parseJsDimension(array->GetValueAt(0), point.first, pointResObjFirst);
132 parseJsDimension(array->GetValueAt(1), point.second, pointResObjSecond);
133 }
134
135 } // namespace OHOS::Ace::Framework
136