1 /*
2 * Copyright (c) 2021 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 "core/components/declaration/qrcode/qrcode_declaration.h"
17
18 #include "base/utils/string_utils.h"
19 #include "core/components/declaration/common/declaration_constants.h"
20 #include "frameworks/bridge/common/utils/utils.h"
21 #include "frameworks/core/components/qrcode/qrcode_theme.h"
22
23 namespace OHOS::Ace {
24
25 using namespace Framework;
26
InitSpecialized()27 void QrcodeDeclaration::InitSpecialized()
28 {
29 AddSpecializedAttribute(DeclarationConstants::DEFAULT_QRCODE_ATTR);
30 AddSpecializedStyle(DeclarationConstants::DEFAULT_QRCODE_STYLE);
31 }
32
InitializeStyle()33 void QrcodeDeclaration::InitializeStyle()
34 {
35 auto qrcodeTheme = GetTheme<QrcodeTheme>();
36 if (!qrcodeTheme) {
37 return;
38 }
39 SetQrcodeColor(qrcodeTheme->GetQrcodeColor());
40 SetBackgroundColor(qrcodeTheme->GetBackgroundColor());
41 SetQrcodeType(qrcodeTheme->GetQrcodeType());
42 SetQrcodeWidth(qrcodeTheme->GetQrcodeWidth());
43 SetQrcodeHeight(qrcodeTheme->GetQrcodeHeight());
44 }
45
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)46 bool QrcodeDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
47 {
48 static const LinearMapNode<void (*)(QrcodeDeclaration&, const std::string&)> qrcodeAttrOperators[] = {
49 { DOM_QRCODE_TYPE,
50 [](QrcodeDeclaration& declaration, const std::string& value) {
51 declaration.SetQrcodeType(ConvertStrToQrcodeType(value));
52 } },
53 { DOM_QRCODE_VALUE,
54 [](QrcodeDeclaration& declaration, const std::string& value) { declaration.SetValue(value); } },
55 };
56 auto operatorIter = BinarySearchFindIndex(qrcodeAttrOperators, ArraySize(qrcodeAttrOperators), attr.first.c_str());
57 if (operatorIter != -1) {
58 qrcodeAttrOperators[operatorIter].value(*this, attr.second);
59 return true;
60 }
61 return false;
62 }
63
SetSpecializedStyle(const std::pair<std::string,std::string> & style)64 bool QrcodeDeclaration::SetSpecializedStyle(const std::pair<std::string, std::string>& style)
65 {
66 const static LinearMapNode<void (*)(QrcodeDeclaration&, const std::string&)> qrcodeOperators[] = {
67 { DOM_QRCODE_BACKGROUND_COLOR,
68 [](QrcodeDeclaration& declaration, const std::string& value) {
69 declaration.SetBackgroundColor(declaration.ParseColor(value));
70 } },
71 { DOM_QRCODE_COLOR,
72 [](QrcodeDeclaration& declaration, const std::string& value) {
73 declaration.SetQrcodeColor(declaration.ParseColor(value));
74 } },
75 { DOM_QRCODE_HEIGHT,
76 [](QrcodeDeclaration& declaration, const std::string& value) {
77 declaration.SetQrcodeHeight(declaration.ParseDimension(value));
78 } },
79 { DOM_QRCODE_WIDTH,
80 [](QrcodeDeclaration& declaration, const std::string& value) {
81 declaration.SetQrcodeWidth(declaration.ParseDimension(value));
82 } },
83 };
84 auto operatorIter = BinarySearchFindIndex(qrcodeOperators, ArraySize(qrcodeOperators), style.first.c_str());
85 if (operatorIter != -1) {
86 qrcodeOperators[operatorIter].value(*this, style.second);
87 return true;
88 }
89 return false;
90 }
91
92 } // namespace OHOS::Ace
93