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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_QRCODE_QRCODE_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_QRCODE_QRCODE_THEME_H
18 
19 #include "core/common/ace_application_info.h"
20 #include "core/common/container.h"
21 #include "core/components/common/layout/constants.h"
22 #include "core/components/common/properties/color.h"
23 #include "core/components/theme/theme.h"
24 #include "core/components/theme/theme_constants.h"
25 #include "core/components/theme/theme_constants_defines.h"
26 #include "core/components/theme/theme_manager.h"
27 #include "core/pipeline/pipeline_base.h"
28 
29 namespace OHOS::Ace {
30 namespace {
31 constexpr double QRCODE_SIZE = 200.0;
32 constexpr Color QRCODE_DEFAULT_COLOR = Color(0xff000000);
33 constexpr Color QRCODE_DEFAULT_BACKGROUND_COLOR = Color(0xffffffff);
34 } // namespace
35 
36 class QrcodeTheme : public virtual Theme {
37     DECLARE_ACE_TYPE(QrcodeTheme, Theme);
38 
39 public:
40     class Builder {
41     public:
42         Builder() = default;
43         ~Builder() = default;
44 
Build(const RefPtr<ThemeConstants> & themeConstants)45         RefPtr<QrcodeTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
46         {
47             RefPtr<QrcodeTheme> theme = AceType::Claim(new QrcodeTheme());
48             if (!themeConstants) {
49                 return theme;
50             }
51             ParsePattern(themeConstants, theme);
52             return theme;
53         }
54 
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<QrcodeTheme> & theme)55         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<QrcodeTheme>& theme) const
56         {
57             RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_QRCODE);
58             if (!pattern) {
59                 return;
60             }
61             theme->backgroundColor_ = QRCODE_DEFAULT_BACKGROUND_COLOR;
62             if (Container::LessThanAPIVersion(PlatformVersion::VERSION_ELEVEN)) {
63                 theme->qrcodeColor_ = Color(0xff000000);
64                 theme->qrcodeType_ = QrcodeType::RECT;
65                 theme->qrcodeWidth_ = Dimension(QRCODE_SIZE, DimensionUnit::PX);
66                 theme->qrcodeHeight_ = Dimension(QRCODE_SIZE, DimensionUnit::PX);
67                 return;
68             }
69             theme->qrcodeColor_ = QRCODE_DEFAULT_COLOR;
70             theme->focusedColor_ = pattern->GetAttr<Color>("qrcode_focused_color", Color::RED);
71             theme->qrcodeType_ = QrcodeType(pattern->GetAttr<int>("qrcode_type", 0));
72             theme->qrcodeWidth_ = pattern->GetAttr<Dimension>("qrcode_size", 200.0_px);
73             theme->qrcodeHeight_ = pattern->GetAttr<Dimension>("qrcode_size", 200.0_px);
74         }
75     };
76 
77     ~QrcodeTheme() override = default;
78 
GetQrcodeColor()79     const Color& GetQrcodeColor() const
80     {
81         return qrcodeColor_;
82     }
83 
GetBackgroundColor()84     const Color& GetBackgroundColor() const
85     {
86         return backgroundColor_;
87     }
88 
GetFocusedColor()89     const Color& GetFocusedColor() const
90     {
91         return focusedColor_;
92     }
93 
GetQrcodeType()94     QrcodeType GetQrcodeType() const
95     {
96         return qrcodeType_;
97     }
98 
GetQrcodeWidth()99     const Dimension& GetQrcodeWidth() const
100     {
101         return qrcodeWidth_;
102     }
103 
GetQrcodeHeight()104     const Dimension& GetQrcodeHeight() const
105     {
106         return qrcodeHeight_;
107     }
108 
109 protected:
110     QrcodeTheme() = default;
111 
112 private:
113     Color qrcodeColor_;
114     Color backgroundColor_;
115     Color focusedColor_;
116     QrcodeType qrcodeType_ { QrcodeType::RECT };
117     Dimension qrcodeWidth_;
118     Dimension qrcodeHeight_;
119 };
120 
121 } // namespace OHOS::Ace
122 
123 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_QRCODE_QRCODE_THEME_H
124