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_path.h"
17
18 #include "core/components/shape/shape_component.h"
19 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
20
21 namespace OHOS::Ace::Framework {
22
Create(const JSCallbackInfo & info)23 void JSPath::Create(const JSCallbackInfo& info)
24 {
25 RefPtr<Component> component = AceType::MakeRefPtr<OHOS::Ace::ShapeComponent>(ShapeType::PATH);
26 ViewStackProcessor::GetInstance()->Push(component);
27 JSShapeAbstract::SetSize(info);
28 if (info.Length() > 0 && info[0]->IsObject()) {
29 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
30 JSRef<JSVal> commands = obj->GetProperty("commands");
31 if (commands->IsString()) {
32 SetCommands(commands->ToString());
33 }
34 }
35 }
36
SetCommands(const std::string & commands)37 void JSPath::SetCommands(const std::string& commands)
38 {
39 auto stack = ViewStackProcessor::GetInstance();
40 auto component = AceType::DynamicCast<OHOS::Ace::ShapeComponent>(stack->GetMainComponent());
41 if (component) {
42 AnimationOption option = stack->GetImplicitAnimationOption();
43 component->SetPathCmd(commands, option);
44 }
45 }
46
ObjectCommands(const JSCallbackInfo & info)47 void JSPath::ObjectCommands(const JSCallbackInfo& info)
48 {
49 info.ReturnSelf();
50 if (info.Length() > 0 && info[0]->IsString()) {
51 auto path = AceType::DynamicCast<Path>(basicShape_);
52 if (path) {
53 path->SetValue(info[0]->ToString());
54 }
55 }
56 }
57
ConstructorCallback(const JSCallbackInfo & info)58 void JSPath::ConstructorCallback(const JSCallbackInfo& info)
59 {
60 auto jsPath = AceType::MakeRefPtr<JSPath>();
61 auto path = AceType::MakeRefPtr<Path>();
62 if (info.Length() > 0 && info[0]->IsObject()) {
63 JSRef<JSObject> params = JSRef<JSObject>::Cast(info[0]);
64 JSRef<JSVal> commands = params->GetProperty("commands");
65 if (commands->IsString()) {
66 path->SetValue(commands->ToString());
67 }
68 }
69 jsPath->SetBasicShape(path);
70 jsPath->IncRefCount();
71 info.SetReturnValue(AceType::RawPtr(jsPath));
72 }
73
DestructorCallback(JSPath * jsPath)74 void JSPath::DestructorCallback(JSPath* jsPath)
75 {
76 if (jsPath != nullptr) {
77 jsPath->DecRefCount();
78 }
79 }
80
JSBind(BindingTarget globalObj)81 void JSPath::JSBind(BindingTarget globalObj)
82 {
83 JSClass<JSPath>::Declare("Path");
84 JSClass<JSPath>::StaticMethod("create", &JSPath::Create);
85 JSClass<JSPath>::StaticMethod("commands", &JSPath::SetCommands);
86
87 JSClass<JSPath>::CustomMethod("commands", &JSPath::ObjectCommands);
88 JSClass<JSPath>::CustomMethod("offset", &JSShapeAbstract::ObjectOffset);
89 JSClass<JSPath>::CustomMethod("fill", &JSShapeAbstract::ObjectFill);
90
91 JSClass<JSPath>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
92 JSClass<JSPath>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
93 JSClass<JSPath>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
94 JSClass<JSPath>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
95 JSClass<JSPath>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
96 JSClass<JSPath>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
97
98 JSClass<JSPath>::Inherit<JSShapeAbstract>();
99 JSClass<JSPath>::Bind(globalObj, JSPath::ConstructorCallback, JSPath::DestructorCallback);
100 }
101
102 } // namespace OHOS::Ace::Framework
103