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_qrcode.h"
17
18 #include "base/log/ace_trace.h"
19 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
20 #include "bridge/declarative_frontend/jsview/models/qrcode_model_impl.h"
21 #include "bridge/declarative_frontend/view_stack_processor.h"
22 #include "core/components_ng/base/view_abstract.h"
23 #include "core/components_ng/pattern/qrcode/qrcode_model.h"
24 #include "core/components_ng/pattern/qrcode/qrcode_model_ng.h"
25
26 namespace OHOS::Ace {
27
28 std::unique_ptr<QRCodeModel> QRCodeModel::instance_ = nullptr;
29
GetInstance()30 QRCodeModel* QRCodeModel::GetInstance()
31 {
32 if (!instance_) {
33 #ifdef NG_BUILD
34 instance_.reset(new NG::ImageModelNG());
35 #else
36 if (Container::IsCurrentUseNewPipeline()) {
37 instance_.reset(new NG::QRCodeModelNG());
38 } else {
39 instance_.reset(new Framework::QRCodeModelImpl());
40 }
41 #endif
42 }
43 return instance_.get();
44 }
45
46 } // namespace OHOS::Ace
47
48 namespace OHOS::Ace::Framework {
49
Create(const std::string & value)50 void JSQRCode::Create(const std::string& value)
51 {
52 QRCodeModel::GetInstance()->Create(value);
53 }
54
SetQRCodeColor(const JSCallbackInfo & info)55 void JSQRCode::SetQRCodeColor(const JSCallbackInfo& info)
56 {
57 if (info.Length() < 1) {
58 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
59 return;
60 }
61 Color qrcodeColor;
62 if (!ParseJsColor(info[0], qrcodeColor)) {
63 return;
64 }
65 QRCodeModel::GetInstance()->SetQRCodeColor(qrcodeColor);
66 }
67
SetBackgroundColor(const JSCallbackInfo & info)68 void JSQRCode::SetBackgroundColor(const JSCallbackInfo& info)
69 {
70 if (info.Length() < 1) {
71 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
72 return;
73 }
74 Color backgroundColor;
75 if (!ParseJsColor(info[0], backgroundColor)) {
76 return;
77 }
78
79 QRCodeModel::GetInstance()->SetQRBackgroundColor(backgroundColor);
80 }
81
JSBind(BindingTarget globalObj)82 void JSQRCode::JSBind(BindingTarget globalObj)
83 {
84 JSClass<JSQRCode>::Declare("QRCode");
85 MethodOptions opt = MethodOptions::NONE;
86 JSClass<JSQRCode>::StaticMethod("create", &JSQRCode::Create, opt);
87 JSClass<JSQRCode>::StaticMethod("color", &JSQRCode::SetQRCodeColor, opt);
88 JSClass<JSQRCode>::StaticMethod("backgroundColor", &JSQRCode::SetBackgroundColor, opt);
89 JSClass<JSQRCode>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
90 JSClass<JSQRCode>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
91 JSClass<JSQRCode>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
92 JSClass<JSQRCode>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
93 JSClass<JSQRCode>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
94 JSClass<JSQRCode>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
95 JSClass<JSQRCode>::Inherit<JSInteractableView>();
96 JSClass<JSQRCode>::Inherit<JSViewAbstract>();
97 JSClass<JSQRCode>::Bind<>(globalObj);
98 }
99
100 } // namespace OHOS::Ace::Framework
101