/* * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "frameworks/bridge/declarative_frontend/jsview/js_qrcode.h" #include "bridge/declarative_frontend/jsview/js_view_abstract.h" #include "bridge/declarative_frontend/jsview/models/qrcode_model_impl.h" #include "bridge/declarative_frontend/ark_theme/theme_apply/js_qrcode_theme.h" #include "bridge/declarative_frontend/view_stack_processor.h" #include "core/components/qrcode/qrcode_theme.h" #include "core/components_ng/base/view_abstract.h" #include "core/components_ng/pattern/qrcode/qrcode_model.h" #include "core/components_ng/pattern/qrcode/qrcode_model_ng.h" namespace OHOS::Ace { std::unique_ptr QRCodeModel::instance_ = nullptr; std::mutex QRCodeModel::mutex_; QRCodeModel* QRCodeModel::GetInstance() { if (!instance_) { std::lock_guard lock(mutex_); if (!instance_) { #ifdef NG_BUILD instance_.reset(new NG::QRCodeModelNG()); #else if (Container::IsCurrentUseNewPipeline()) { instance_.reset(new NG::QRCodeModelNG()); } else { instance_.reset(new Framework::QRCodeModelImpl()); } #endif } } return instance_.get(); } } // namespace OHOS::Ace namespace OHOS::Ace::Framework { void JSQRCode::Create(const std::string& value) { QRCodeModel::GetInstance()->Create(value); JSQRCodeTheme::ApplyTheme(); } void JSQRCode::SetQRCodeColor(const JSCallbackInfo& info) { if (info.Length() < 1) { return; } Color qrcodeColor; if (!ParseJsColor(info[0], qrcodeColor) && !JSQRCodeTheme::ObtainQRCodeColor(qrcodeColor)) { RefPtr qrcodeTheme = GetTheme(); CHECK_NULL_VOID(qrcodeTheme); qrcodeColor = qrcodeTheme->GetQrcodeColor(); } QRCodeModel::GetInstance()->SetQRCodeColor(qrcodeColor); } void JSQRCode::SetBackgroundColor(const JSCallbackInfo& info) { if (info.Length() < 1) { return; } Color backgroundColor; if (!ParseJsColor(info[0], backgroundColor) && !JSQRCodeTheme::ObtainBackgroundColor(backgroundColor)) { RefPtr qrcodeTheme = GetTheme(); CHECK_NULL_VOID(qrcodeTheme); backgroundColor = qrcodeTheme->GetBackgroundColor(); } QRCodeModel::GetInstance()->SetQRBackgroundColor(backgroundColor); } void JSQRCode::SetContentOpacity(const JSCallbackInfo& info) { double opacity = 1.0; if (!ParseJsDouble(info[0], opacity)) { opacity = 1.0; } if (LessNotEqual(opacity, 0.0) || GreatNotEqual(opacity, 1.0)) { opacity = 1.0; } QRCodeModel::GetInstance()->SetContentOpacity(opacity); } void JSQRCode::JSBind(BindingTarget globalObj) { JSClass::Declare("QRCode"); MethodOptions opt = MethodOptions::NONE; JSClass::StaticMethod("create", &JSQRCode::Create, opt); JSClass::StaticMethod("color", &JSQRCode::SetQRCodeColor, opt); JSClass::StaticMethod("backgroundColor", &JSQRCode::SetBackgroundColor, opt); JSClass::StaticMethod("contentOpacity", &JSQRCode::SetContentOpacity, opt); JSClass::StaticMethod("onAttach", &JSInteractableView::JsOnAttach); JSClass::StaticMethod("onAppear", &JSInteractableView::JsOnAppear); JSClass::StaticMethod("onDetach", &JSInteractableView::JsOnDetach); JSClass::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear); JSClass::StaticMethod("onClick", &JSInteractableView::JsOnClick); JSClass::StaticMethod("onTouch", &JSInteractableView::JsOnTouch); JSClass::StaticMethod("onHover", &JSInteractableView::JsOnHover); JSClass::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage); JSClass::Inherit(); JSClass::InheritAndBind(globalObj); } } // namespace OHOS::Ace::Framework