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         return;
80     }
81     JSRef<JSArray> pointArray = JSRef<JSArray>::Cast(info[0]);
82     ShapePoint startPoint;
83     SetPoint(pointArray, startPoint);
84     LineModel::GetInstance()->StartPoint(startPoint);
85 }
86 
SetEnd(const JSCallbackInfo & info)87 void JSLine::SetEnd(const JSCallbackInfo& info)
88 {
89     if (info.Length() < 1 || !info[0]->IsArray()) {
90         return;
91     }
92     JSRef<JSArray> pointArray = JSRef<JSArray>::Cast(info[0]);
93     ShapePoint endPoint;
94     SetPoint(pointArray, endPoint);
95     LineModel::GetInstance()->EndPoint(endPoint);
96 }
97 
SetPoint(const JSRef<JSArray> & array,ShapePoint & point)98 void JSLine::SetPoint(const JSRef<JSArray>& array, ShapePoint& point)
99 {
100     if (array->Length() < 1) {
101         return;
102     }
103 
104     auto parseJsDimension = [](const JSRef<JSVal>& jsValue, Dimension &val) {
105         if (jsValue->IsNumber()) {
106             val = Dimension(jsValue->ToNumber<double>(), DimensionUnit::VP);
107         } else if (jsValue->IsString()) {
108             if (Container::LessThanAPIVersion(PlatformVersion::VERSION_TEN)) {
109                 val = StringUtils::StringToDimension(jsValue->ToString(), true);
110             } else if (!StringUtils::StringToDimensionWithUnitNG(jsValue->ToString(), val, DimensionUnit::VP, 0.0)) {
111                 // unit is invalid, use default value(0.0vp) instead.
112                 val = 0.0_vp;
113             }
114         } else if (jsValue->IsObject()) {
115             CalcDimension value;
116             ParseJsDimensionVpNG(jsValue, value);
117             if (!StringUtils::StringToDimensionWithUnitNG(value.ToString(), val, DimensionUnit::VP, 0.0)) {
118                 // unit is invalid, use default value(0.0vp) instead.
119                 val = 0.0_vp;
120             }
121         }
122     };
123 
124     parseJsDimension(array->GetValueAt(0), point.first);
125     parseJsDimension(array->GetValueAt(1), point.second);
126 }
127 
128 } // namespace OHOS::Ace::Framework
129