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