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_circle.h"
17 #include <memory>
18 
19 #include "bridge/declarative_frontend/jsview/models/circle_model_impl.h"
20 #include "core/common/container.h"
21 #include "core/components_ng/pattern/shape/circle_model.h"
22 #include "core/components_ng/pattern/shape/circle_model_ng.h"
23 
24 namespace OHOS::Ace {
GetInstance()25 CircleModel* CircleModel::GetInstance()
26 {
27 #ifdef NG_BUILD
28     static NG::CircleModelNG instance;
29     return &instance;
30 #else
31     if (Container::IsCurrentUseNewPipeline()) {
32         static NG::CircleModelNG instance;
33         return &instance;
34     } else {
35         static Framework::CircleModelImpl instance;
36         return &instance;
37     }
38 #endif
39 }
40 } // namespace OHOS::Ace
41 
42 namespace OHOS::Ace::Framework {
43 
Create(const JSCallbackInfo& info)44 void JSCircle::Create(const JSCallbackInfo& info)
45 {
46     CircleModel::GetInstance()->Create();
47     JSShapeAbstract::SetSize(info);
48 }
49 
ConstructorCallback(const JSCallbackInfo& info)50 void JSCircle::ConstructorCallback(const JSCallbackInfo& info)
51 {
52     auto circle = AceType::MakeRefPtr<Circle>();
53     if (info.Length() == 1 && info[0]->IsObject()) {
54         JSRef<JSObject> params = JSRef<JSObject>::Cast(info[0]);
55         JSRef<JSVal> width = params->GetProperty("width");
56         CalcDimension dimWidth;
57         JSRef<JSVal> height = params->GetProperty("height");
58         CalcDimension dimHeight;
59         if (Container::LessThanAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
60             if (ParseJsDimensionVp(width, dimWidth)) {
61                 circle->SetWidth(dimWidth);
62             }
63             if (ParseJsDimensionVp(height, dimHeight)) {
64                 circle->SetHeight(dimHeight);
65             }
66         } else {
67             if (ParseJsDimensionVpNG(width, dimWidth) && dimWidth.IsValid()) {
68                 circle->SetWidth(dimWidth);
69             }
70             if (ParseJsDimensionVpNG(height, dimHeight) && dimHeight.IsValid()) {
71                 circle->SetHeight(dimHeight);
72             }
73         }
74     }
75     auto jsCircle = AceType::MakeRefPtr<JSCircle>();
76     jsCircle->SetBasicShape(circle);
77     jsCircle->IncRefCount();
78     info.SetReturnValue(AceType::RawPtr(jsCircle));
79 }
80 
DestructorCallback(JSCircle* jsCircle)81 void JSCircle::DestructorCallback(JSCircle* jsCircle)
82 {
83     if (jsCircle != nullptr) {
84         jsCircle->DecRefCount();
85     }
86 }
87 
JSBind(BindingTarget globalObj)88 void JSCircle::JSBind(BindingTarget globalObj)
89 {
90     JSClass<JSCircle>::Declare("Circle");
91     MethodOptions opt = MethodOptions::NONE;
92     JSClass<JSCircle>::StaticMethod("create", &JSCircle::Create, opt);
93 
94     JSClass<JSCircle>::CustomMethod("width", &JSShapeAbstract::ObjectWidth);
95     JSClass<JSCircle>::CustomMethod("height", &JSShapeAbstract::ObjectHeight);
96     JSClass<JSCircle>::CustomMethod("size", &JSShapeAbstract::ObjectSize);
97     JSClass<JSCircle>::CustomMethod("offset", &JSShapeAbstract::ObjectOffset);
98     JSClass<JSCircle>::CustomMethod("fill", &JSShapeAbstract::ObjectFill);
99     JSClass<JSCircle>::CustomMethod("position", &JSShapeAbstract::ObjectPosition);
100 
101     JSClass<JSCircle>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
102     JSClass<JSCircle>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
103     JSClass<JSCircle>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
104     JSClass<JSCircle>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
105     JSClass<JSCircle>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
106     JSClass<JSCircle>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
107 
108     JSClass<JSCircle>::InheritAndBind<JSShapeAbstract>(
109         globalObj, JSCircle::ConstructorCallback, JSCircle::DestructorCallback);
110 }
111 
112 } // namespace OHOS::Ace::Framework
113