1 /*
2  * Copyright (c) 2021-2022 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_path.h"
17 
18 #include "bridge/declarative_frontend/jsview/models/path_model_impl.h"
19 #include "core/common/container.h"
20 #include "core/components/shape/shape_component.h"
21 #include "core/components_ng/pattern/shape/path_model.h"
22 #include "core/components_ng/pattern/shape/path_model_ng.h"
23 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
24 
25 namespace OHOS::Ace {
26 
27 std::unique_ptr<PathModel> PathModel::instance_ = nullptr;
28 std::mutex PathModel::mutex_;
29 
GetInstance()30 PathModel* PathModel::GetInstance()
31 {
32     if (!instance_) {
33         std::lock_guard<std::mutex> lock(mutex_);
34         if (!instance_) {
35 #ifdef NG_BUILD
36             instance_.reset(new NG::PathModelNG());
37 #else
38             if (Container::IsCurrentUseNewPipeline()) {
39                 instance_.reset(new NG::PathModelNG());
40             } else {
41                 instance_.reset(new Framework::PathModelImpl());
42             }
43 #endif
44         }
45     }
46     return instance_.get();
47 }
48 
49 } // namespace OHOS::Ace
50 
51 namespace OHOS::Ace::Framework {
52 
Create(const JSCallbackInfo & info)53 void JSPath::Create(const JSCallbackInfo& info)
54 {
55     PathModel::GetInstance()->Create();
56     JSShapeAbstract::SetSize(info);
57     if (info.Length() > 0 && info[0]->IsObject()) {
58         JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
59         JSRef<JSVal> commands = obj->GetProperty("commands");
60         if (commands->IsString()) {
61             SetCommands(commands->ToString());
62         }
63     }
64 }
65 
SetCommands(const std::string & commands)66 void JSPath::SetCommands(const std::string& commands)
67 {
68     PathModel::GetInstance()->SetCommands(commands);
69 }
70 
ObjectCommands(const JSCallbackInfo & info)71 void JSPath::ObjectCommands(const JSCallbackInfo& info)
72 {
73     info.ReturnSelf();
74     if (info.Length() > 0 && info[0]->IsString()) {
75         auto path = AceType::DynamicCast<Path>(basicShape_);
76         if (path) {
77             path->SetValue(info[0]->ToString());
78         }
79     }
80 }
81 
ConstructorCallback(const JSCallbackInfo & info)82 void JSPath::ConstructorCallback(const JSCallbackInfo& info)
83 {
84     auto jsPath = AceType::MakeRefPtr<JSPath>();
85     auto path = AceType::MakeRefPtr<Path>();
86     if (info.Length() > 0 && info[0]->IsObject()) {
87         JSRef<JSObject> params = JSRef<JSObject>::Cast(info[0]);
88         JSRef<JSVal> commands = params->GetProperty("commands");
89         if (commands->IsString()) {
90             path->SetValue(commands->ToString());
91         }
92     }
93     jsPath->SetBasicShape(path);
94     jsPath->IncRefCount();
95     info.SetReturnValue(AceType::RawPtr(jsPath));
96 }
97 
DestructorCallback(JSPath * jsPath)98 void JSPath::DestructorCallback(JSPath* jsPath)
99 {
100     if (jsPath != nullptr) {
101         jsPath->DecRefCount();
102     }
103 }
104 
JSBind(BindingTarget globalObj)105 void JSPath::JSBind(BindingTarget globalObj)
106 {
107     JSClass<JSPath>::Declare("Path");
108     JSClass<JSPath>::StaticMethod("create", &JSPath::Create);
109     JSClass<JSPath>::StaticMethod("commands", &JSPath::SetCommands);
110 
111     JSClass<JSPath>::CustomMethod("commands", &JSPath::ObjectCommands);
112     JSClass<JSPath>::CustomMethod("offset", &JSShapeAbstract::ObjectOffset);
113     JSClass<JSPath>::CustomMethod("fill", &JSShapeAbstract::ObjectFill);
114     JSClass<JSPath>::CustomMethod("position", &JSShapeAbstract::ObjectPosition);
115 
116     JSClass<JSPath>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
117     JSClass<JSPath>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
118     JSClass<JSPath>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
119     JSClass<JSPath>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
120     JSClass<JSPath>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
121     JSClass<JSPath>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
122 
123     JSClass<JSPath>::InheritAndBind<JSShapeAbstract>(
124         globalObj, JSPath::ConstructorCallback, JSPath::DestructorCallback);
125 }
126 
127 } // namespace OHOS::Ace::Framework
128