• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
25 
26 std::unique_ptr<CircleModel> CircleModel::instance_ = nullptr;
27 
GetInstance()28 CircleModel* CircleModel::GetInstance()
29 {
30     if (!instance_) {
31 #ifdef NG_BUILD
32         instance_.reset(new NG::CircleModelNG());
33 #else
34         if (Container::IsCurrentUseNewPipeline()) {
35             instance_.reset(new NG::CircleModelNG());
36         } else {
37             instance_.reset(new Framework::CircleModelImpl());
38         }
39 #endif
40     }
41     return instance_.get();
42 }
43 
44 } // namespace OHOS::Ace
45 
46 namespace OHOS::Ace::Framework {
47 
Create(const JSCallbackInfo & info)48 void JSCircle::Create(const JSCallbackInfo& info)
49 {
50     CircleModel::GetInstance()->Create();
51     JSShapeAbstract::SetSize(info);
52 }
53 
ConstructorCallback(const JSCallbackInfo & info)54 void JSCircle::ConstructorCallback(const JSCallbackInfo& info)
55 {
56     if (info.Length() < 1) {
57         LOGE("The arg is wrong, it is supposed to have at least 1 argument");
58         return;
59     }
60     if (!info[0]->IsObject()) {
61         LOGE("The arg is not Object");
62         return;
63     }
64     auto circle = AceType::MakeRefPtr<Circle>();
65     JSRef<JSObject> params = JSRef<JSObject>::Cast(info[0]);
66     JSRef<JSVal> width = params->GetProperty("width");
67     Dimension dimWidth;
68     if (ParseJsDimensionVp(width, dimWidth)) {
69         circle->SetWidth(dimWidth);
70     }
71     JSRef<JSVal> height = params->GetProperty("height");
72     Dimension dimHeight;
73     if (ParseJsDimensionVp(height, dimHeight)) {
74         circle->SetHeight(dimHeight);
75     }
76 
77     auto jsCircle = AceType::MakeRefPtr<JSCircle>();
78     jsCircle->SetBasicShape(circle);
79     jsCircle->IncRefCount();
80     info.SetReturnValue(AceType::RawPtr(jsCircle));
81 }
82 
DestructorCallback(JSCircle * jsCircle)83 void JSCircle::DestructorCallback(JSCircle* jsCircle)
84 {
85     if (jsCircle != nullptr) {
86         jsCircle->DecRefCount();
87     }
88 }
89 
JSBind(BindingTarget globalObj)90 void JSCircle::JSBind(BindingTarget globalObj)
91 {
92     JSClass<JSCircle>::Declare("Circle");
93     MethodOptions opt = MethodOptions::NONE;
94     JSClass<JSCircle>::StaticMethod("create", &JSCircle::Create, opt);
95 
96     JSClass<JSCircle>::CustomMethod("width", &JSShapeAbstract::ObjectWidth);
97     JSClass<JSCircle>::CustomMethod("height", &JSShapeAbstract::ObjectHeight);
98     JSClass<JSCircle>::CustomMethod("size", &JSShapeAbstract::ObjectSize);
99     JSClass<JSCircle>::CustomMethod("offset", &JSShapeAbstract::ObjectOffset);
100     JSClass<JSCircle>::CustomMethod("fill", &JSShapeAbstract::ObjectFill);
101 
102     JSClass<JSCircle>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
103     JSClass<JSCircle>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
104     JSClass<JSCircle>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
105     JSClass<JSCircle>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
106     JSClass<JSCircle>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
107     JSClass<JSCircle>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
108 
109     JSClass<JSCircle>::Inherit<JSShapeAbstract>();
110     JSClass<JSCircle>::Bind(globalObj, JSCircle::ConstructorCallback, JSCircle::DestructorCallback);
111 }
112 
113 } // namespace OHOS::Ace::Framework
114