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/qrcode/qrcode_theme.h"
23 #include "core/components_ng/base/view_abstract.h"
24 #include "core/components_ng/pattern/qrcode/qrcode_model.h"
25 #include "core/components_ng/pattern/qrcode/qrcode_model_ng.h"
26
27 namespace OHOS::Ace {
28
29 std::unique_ptr<QRCodeModel> QRCodeModel::instance_ = nullptr;
30 std::mutex QRCodeModel::mutex_;
31
GetInstance()32 QRCodeModel* QRCodeModel::GetInstance()
33 {
34 if (!instance_) {
35 std::lock_guard<std::mutex> lock(mutex_);
36 if (!instance_) {
37 #ifdef NG_BUILD
38 instance_.reset(new NG::QRCodeModelNG());
39 #else
40 if (Container::IsCurrentUseNewPipeline()) {
41 instance_.reset(new NG::QRCodeModelNG());
42 } else {
43 instance_.reset(new Framework::QRCodeModelImpl());
44 }
45 #endif
46 }
47 }
48 return instance_.get();
49 }
50
51 } // namespace OHOS::Ace
52
53 namespace OHOS::Ace::Framework {
54
Create(const std::string & value)55 void JSQRCode::Create(const std::string& value)
56 {
57 QRCodeModel::GetInstance()->Create(value);
58 }
59
SetQRCodeColor(const JSCallbackInfo & info)60 void JSQRCode::SetQRCodeColor(const JSCallbackInfo& info)
61 {
62 if (info.Length() < 1) {
63 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
64 return;
65 }
66 Color qrcodeColor;
67 if (!ParseJsColor(info[0], qrcodeColor)) {
68 RefPtr<QrcodeTheme> qrcodeTheme = GetTheme<QrcodeTheme>();
69 CHECK_NULL_VOID_NOLOG(qrcodeTheme);
70 qrcodeColor = qrcodeTheme->GetQrcodeColor();
71 }
72 QRCodeModel::GetInstance()->SetQRCodeColor(qrcodeColor);
73 }
74
SetBackgroundColor(const JSCallbackInfo & info)75 void JSQRCode::SetBackgroundColor(const JSCallbackInfo& info)
76 {
77 if (info.Length() < 1) {
78 LOGE("The argv is wrong, it is supposed to have at least 1 argument");
79 return;
80 }
81 Color backgroundColor;
82 if (!ParseJsColor(info[0], backgroundColor)) {
83 RefPtr<QrcodeTheme> qrcodeTheme = GetTheme<QrcodeTheme>();
84 CHECK_NULL_VOID_NOLOG(qrcodeTheme);
85 backgroundColor = qrcodeTheme->GetBackgroundColor();
86 }
87
88 QRCodeModel::GetInstance()->SetQRBackgroundColor(backgroundColor);
89 }
90
SetContentOpacity(const JSCallbackInfo & info)91 void JSQRCode::SetContentOpacity(const JSCallbackInfo& info)
92 {
93 double opacity = 1.0;
94 if (!ParseJsDouble(info[0], opacity)) {
95 opacity = 1.0;
96 }
97 if (LessNotEqual(opacity, 0.0) || GreatNotEqual(opacity, 1.0)) {
98 opacity = 1.0;
99 }
100 QRCodeModel::GetInstance()->SetContentOpacity(opacity);
101 }
102
JSBind(BindingTarget globalObj)103 void JSQRCode::JSBind(BindingTarget globalObj)
104 {
105 JSClass<JSQRCode>::Declare("QRCode");
106 MethodOptions opt = MethodOptions::NONE;
107 JSClass<JSQRCode>::StaticMethod("create", &JSQRCode::Create, opt);
108 JSClass<JSQRCode>::StaticMethod("color", &JSQRCode::SetQRCodeColor, opt);
109 JSClass<JSQRCode>::StaticMethod("backgroundColor", &JSQRCode::SetBackgroundColor, opt);
110 JSClass<JSQRCode>::StaticMethod("contentOpacity", &JSQRCode::SetContentOpacity, opt);
111 JSClass<JSQRCode>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
112 JSClass<JSQRCode>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
113 JSClass<JSQRCode>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
114 JSClass<JSQRCode>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
115 JSClass<JSQRCode>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
116 JSClass<JSQRCode>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
117 JSClass<JSQRCode>::Inherit<JSInteractableView>();
118 JSClass<JSQRCode>::InheritAndBind<JSViewAbstract>(globalObj);
119 }
120
121 } // namespace OHOS::Ace::Framework
122