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