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