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/ark_theme/theme_apply/js_qrcode_theme.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 JSQRCodeTheme::ApplyTheme();
59 }
60
SetQRCodeColor(const JSCallbackInfo & info)61 void JSQRCode::SetQRCodeColor(const JSCallbackInfo& info)
62 {
63 if (info.Length() < 1) {
64 return;
65 }
66 Color qrcodeColor;
67 if (!ParseJsColor(info[0], qrcodeColor) && !JSQRCodeTheme::ObtainQRCodeColor(qrcodeColor)) {
68 RefPtr<QrcodeTheme> qrcodeTheme = GetTheme<QrcodeTheme>();
69 CHECK_NULL_VOID(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 return;
79 }
80 Color backgroundColor;
81 if (!ParseJsColor(info[0], backgroundColor) && !JSQRCodeTheme::ObtainBackgroundColor(backgroundColor)) {
82 RefPtr<QrcodeTheme> qrcodeTheme = GetTheme<QrcodeTheme>();
83 CHECK_NULL_VOID(qrcodeTheme);
84 backgroundColor = qrcodeTheme->GetBackgroundColor();
85 }
86
87 QRCodeModel::GetInstance()->SetQRBackgroundColor(backgroundColor);
88 }
89
SetContentOpacity(const JSCallbackInfo & info)90 void JSQRCode::SetContentOpacity(const JSCallbackInfo& info)
91 {
92 double opacity = 1.0;
93 if (!ParseJsDouble(info[0], opacity)) {
94 opacity = 1.0;
95 }
96 if (LessNotEqual(opacity, 0.0) || GreatNotEqual(opacity, 1.0)) {
97 opacity = 1.0;
98 }
99 QRCodeModel::GetInstance()->SetContentOpacity(opacity);
100 }
101
JSBind(BindingTarget globalObj)102 void JSQRCode::JSBind(BindingTarget globalObj)
103 {
104 JSClass<JSQRCode>::Declare("QRCode");
105 MethodOptions opt = MethodOptions::NONE;
106 JSClass<JSQRCode>::StaticMethod("create", &JSQRCode::Create, opt);
107 JSClass<JSQRCode>::StaticMethod("color", &JSQRCode::SetQRCodeColor, opt);
108 JSClass<JSQRCode>::StaticMethod("backgroundColor", &JSQRCode::SetBackgroundColor, opt);
109 JSClass<JSQRCode>::StaticMethod("contentOpacity", &JSQRCode::SetContentOpacity, opt);
110 JSClass<JSQRCode>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
111 JSClass<JSQRCode>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
112 JSClass<JSQRCode>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
113 JSClass<JSQRCode>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
114 JSClass<JSQRCode>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
115 JSClass<JSQRCode>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
116 JSClass<JSQRCode>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
117 JSClass<JSQRCode>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
118 JSClass<JSQRCode>::Inherit<JSInteractableView>();
119 JSClass<JSQRCode>::InheritAndBind<JSViewAbstract>(globalObj);
120 }
121
122 } // namespace OHOS::Ace::Framework
123