• 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_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 JSCallbackInfo & info)55 void JSQRCode::Create(const JSCallbackInfo& info)
56 {
57     if (info.Length() < 1) {
58         return;
59     }
60     std::string value;
61     RefPtr<ResourceObject> resObj;
62     if (info[0]->IsNumber()) {
63         auto num = info[0]->ToNumber<int32_t>();
64         value = std::to_string(num);
65     } else if (info[0]->IsNull()) {
66         value = "null";
67     } else if (info[0]->IsUndefined()) {
68         value = "undefined";
69     } else {
70         ParseJsString(info[0], value, resObj);
71     }
72     QRCodeModel::GetInstance()->Create(value);
73     JSQRCodeTheme::ApplyTheme();
74     if (SystemProperties::ConfigChangePerform() && resObj) {
75         QRCodeModel::GetInstance()->CreateWithResourceObj(QRCodeResourceType::CREATE, resObj);
76     }
77 }
78 
SetQRCodeColor(const JSCallbackInfo & info)79 void JSQRCode::SetQRCodeColor(const JSCallbackInfo& info)
80 {
81     if (info.Length() < 1) {
82         return;
83     }
84     Color qrcodeColor;
85     RefPtr<ResourceObject> resObj;
86     bool state = ParseJsColor(info[0], qrcodeColor, resObj);
87     if (SystemProperties::ConfigChangePerform()) {
88         QRCodeModel::GetInstance()->CreateWithResourceObj(QRCodeResourceType::COLOR, resObj);
89     }
90 
91     if (!state && !JSQRCodeTheme::ObtainQRCodeColor(qrcodeColor)) {
92         RefPtr<QrcodeTheme> qrcodeTheme = GetTheme<QrcodeTheme>();
93         CHECK_NULL_VOID(qrcodeTheme);
94         qrcodeColor = qrcodeTheme->GetQrcodeColor();
95     }
96     QRCodeModel::GetInstance()->SetQRCodeColor(qrcodeColor);
97 }
98 
SetBackgroundColor(const JSCallbackInfo & info)99 void JSQRCode::SetBackgroundColor(const JSCallbackInfo& info)
100 {
101     if (info.Length() < 1) {
102         return;
103     }
104     Color backgroundColor;
105     RefPtr<ResourceObject> resObj;
106     bool state = ParseJsColor(info[0], backgroundColor, resObj);
107     if (SystemProperties::ConfigChangePerform()) {
108         QRCodeModel::GetInstance()->CreateWithResourceObj(QRCodeResourceType::BACKGROUND_COLOR, resObj);
109     }
110 
111     if (!state && !JSQRCodeTheme::ObtainBackgroundColor(backgroundColor)) {
112         RefPtr<QrcodeTheme> qrcodeTheme = GetTheme<QrcodeTheme>();
113         CHECK_NULL_VOID(qrcodeTheme);
114         backgroundColor = qrcodeTheme->GetBackgroundColor();
115     }
116 
117     QRCodeModel::GetInstance()->SetQRBackgroundColor(backgroundColor);
118 }
119 
SetContentOpacity(const JSCallbackInfo & info)120 void JSQRCode::SetContentOpacity(const JSCallbackInfo& info)
121 {
122     double opacity = 1.0;
123 
124     RefPtr<ResourceObject> resObj;
125     bool state = ParseJsDouble(info[0], opacity, resObj);
126     if (SystemProperties::ConfigChangePerform()) {
127         QRCodeModel::GetInstance()->CreateWithResourceObj(QRCodeResourceType::CONTENT_OPACITY, resObj);
128     }
129     if (!state) {
130         opacity = 1.0;
131     }
132     if (LessNotEqual(opacity, 0.0) || GreatNotEqual(opacity, 1.0)) {
133         opacity = 1.0;
134     }
135     QRCodeModel::GetInstance()->SetContentOpacity(opacity);
136 }
137 
JSBind(BindingTarget globalObj)138 void JSQRCode::JSBind(BindingTarget globalObj)
139 {
140     JSClass<JSQRCode>::Declare("QRCode");
141     MethodOptions opt = MethodOptions::NONE;
142     JSClass<JSQRCode>::StaticMethod("create", &JSQRCode::Create, opt);
143     JSClass<JSQRCode>::StaticMethod("color", &JSQRCode::SetQRCodeColor, opt);
144     JSClass<JSQRCode>::StaticMethod("backgroundColor", &JSQRCode::SetBackgroundColor, opt);
145     JSClass<JSQRCode>::StaticMethod("contentOpacity", &JSQRCode::SetContentOpacity, opt);
146     JSClass<JSQRCode>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
147     JSClass<JSQRCode>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
148     JSClass<JSQRCode>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
149     JSClass<JSQRCode>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
150     JSClass<JSQRCode>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
151     JSClass<JSQRCode>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
152     JSClass<JSQRCode>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
153     JSClass<JSQRCode>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
154     JSClass<JSQRCode>::Inherit<JSInteractableView>();
155     JSClass<JSQRCode>::InheritAndBind<JSViewAbstract>(globalObj);
156 }
157 
158 } // namespace OHOS::Ace::Framework
159