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_COUNTER_COUNTER_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_COUNTER_COUNTER_THEME_H
18 
19 #include "core/components/common/properties/color.h"
20 #include "core/components/common/properties/edge.h"
21 #include "core/components/common/properties/radius.h"
22 #include "core/components/common/properties/text_style.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_ng/property/border_property.h"
27 
28 namespace OHOS::Ace {
29 namespace {
30 constexpr double BUTTON_ALPHA_DISABLED = 0.4;
31 } // namespace
32 class CounterTheme : public virtual Theme {
33     DECLARE_ACE_TYPE(CounterTheme, Theme);
34 
35 public:
36     class Builder {
37     public:
38         Builder() = default;
39         ~Builder() = default;
40 
Build(const RefPtr<ThemeConstants> & themeConstants)41         RefPtr<CounterTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
42         {
43             RefPtr<CounterTheme> theme = AceType::MakeRefPtr<CounterTheme>();
44             if (!themeConstants) {
45                 return theme;
46             }
47 
48             // init theme from global data
49             ParsePattern(themeConstants, theme);
50             return theme;
51         }
52 
ParsePattern(const RefPtr<ThemeConstants> & themeConstants,const RefPtr<CounterTheme> & theme)53         void ParsePattern(const RefPtr<ThemeConstants>& themeConstants, const RefPtr<CounterTheme>& theme) const
54         {
55             RefPtr<ThemeStyle> counterPattern = themeConstants->GetPatternByName(THEME_PATTERN_COUNTER);
56             if (!counterPattern) {
57                 LOGW("find pattern of counter fail");
58                 return;
59             }
60             theme->alphaDisabled_ = counterPattern->GetAttr<double>("button_alpha_disabled", BUTTON_ALPHA_DISABLED);
61             theme->contentTextStyle_.SetFontSize(counterPattern->GetAttr<Dimension>("title_font_size", 15.0_vp));
62             theme->contentTextStyle_.SetTextColor(counterPattern->GetAttr<Color>("title_font_color",
63                 Color(0xff191919)));
64             theme->backgroundColor_ = counterPattern->GetAttr<Color>("title_background_color", Color::WHITE);
65         }
66     };
67 
68     ~CounterTheme() override = default;
69 
GetContentTextStyle()70     const TextStyle& GetContentTextStyle() const
71     {
72         return contentTextStyle_;
73     }
74 
GetAlphaDisabled()75     double GetAlphaDisabled() const
76     {
77         return alphaDisabled_;
78     }
79 
GetBackGroundColor()80     const Color& GetBackGroundColor() const
81     {
82         return backgroundColor_;
83     }
84 
GetHeight()85     const Dimension& GetHeight() const
86     {
87         return height_;
88     }
89 
GetWidth()90     const Dimension& GetWidth() const
91     {
92         return width_;
93     }
94 
GetControlWidth()95     const Dimension& GetControlWidth() const
96     {
97         return controlWidth_;
98     }
99 
GetContentWidth()100     const Dimension& GetContentWidth() const
101     {
102         return contentWidth_;
103     }
104 
GetBorderRadius()105     const NG::BorderRadiusProperty& GetBorderRadius() const
106     {
107         return borderRadius_;
108     }
109 
GetBorderWidth()110     const NG::BorderWidthProperty& GetBorderWidth() const
111     {
112         return borderWidth_;
113     }
114 
GetContentBorderWidth()115     const NG::BorderWidthProperty& GetContentBorderWidth() const
116     {
117         return contentBorderWidth_;
118     }
119 
GetBorderColor()120     const NG::BorderColorProperty& GetBorderColor() const
121     {
122         return borderColor_;
123     }
124 
GetBorderStyle()125     const NG::BorderStyleProperty& GetBorderStyle() const
126     {
127         return borderStyle_;
128     }
129 
130 private:
131     TextStyle contentTextStyle_;
132     Color backgroundColor_ = Color(0xff191919);
133     Dimension height_ = 32.0_vp;
134     Dimension width_ = 100.0_vp;
135     Dimension controlWidth_ = 32.0_vp;
136     Dimension contentWidth_ = 36.0_vp;
137     double alphaDisabled_ = 0.4;
138     NG::BorderRadiusProperty borderRadius_ = { 4.0_vp, 4.0_vp, 4.0_vp, 4.0_vp };
139     NG::BorderWidthProperty borderWidth_ = { 1.0_vp, 1.0_vp, 1.0_vp, 1.0_vp };
140     NG::BorderWidthProperty contentBorderWidth_ = { 0.0_vp, 1.0_vp, 0.0_vp, 1.0_vp };
141     NG::BorderColorProperty borderColor_ = { Color::GRAY, Color::GRAY, Color::GRAY, Color::GRAY };
142     NG::BorderStyleProperty borderStyle_ = { BorderStyle::SOLID, BorderStyle::SOLID, BorderStyle::SOLID,
143         BorderStyle::SOLID };
144 };
145 
146 } // namespace OHOS::Ace
147 
148 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_COUNTER_COUNTER_THEME_H
149