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_RATING_RATING_THEME_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RATING_RATING_THEME_H
18 
19 #include "base/resource/internal_resource.h"
20 #include "core/components/common/properties/color.h"
21 #include "core/components/theme/theme.h"
22 #include "core/components/theme/theme_constants.h"
23 #include "core/components/theme/theme_constants_defines.h"
24 
25 namespace OHOS::Ace {
26 namespace {
27 constexpr Dimension BORDER_RADIUS = 4.0_vp;
28 constexpr Color STAR_PRESS_COLOR = Color(0x19182431);
29 constexpr Color STAR_HOVER_COLOR = Color(0x0c182431);
30 } // namespace
31 /**
32  * RatingTheme defines color and styles of RatingComponent. RatingTheme should be built
33  * using RatingTheme::Builder.
34  */
35 class RatingTheme : public virtual Theme {
36     DECLARE_ACE_TYPE(RatingTheme, Theme);
37 
38 public:
39     class Builder {
40     public:
41         Builder() = default;
42         ~Builder() = default;
43 
Build(const RefPtr<ThemeConstants> & themeConstants)44         RefPtr<RatingTheme> Build(const RefPtr<ThemeConstants>& themeConstants) const
45         {
46             RefPtr<RatingTheme> theme = AceType::Claim(new RatingTheme());
47             if (!themeConstants) {
48                 return theme;
49             }
50             theme->foregroundResourceId_ = themeConstants->GetResourceId(THEME_RATING_RESOURCE_ID_BIG_ON);
51             theme->secondaryResourceId_ = themeConstants->GetResourceId(THEME_RATING_RESOURCE_ID_BIG_HALF);
52             theme->backgroundResourceId_ = themeConstants->GetResourceId(THEME_RATING_RESOURCE_ID_BIG_OFF);
53             theme->foregroundMiniResourceId_ = themeConstants->GetResourceId(THEME_RATING_RESOURCE_ID_MINI_ON);
54             theme->secondaryMiniResourceId_ = themeConstants->GetResourceId(THEME_RATING_RESOURCE_ID_MINI_HALF);
55             theme->backgroundMiniResourceId_ = themeConstants->GetResourceId(THEME_RATING_RESOURCE_ID_MINI_OFF);
56 
57             RefPtr<ThemeStyle> pattern = themeConstants->GetPatternByName(THEME_PATTERN_RATING);
58             if (pattern) {
59                 theme->starNum_ = static_cast<int32_t>(pattern->GetAttr<double>("rating_start_num", 0.0));
60                 theme->ratingScore_ = pattern->GetAttr<double>("rating_score", 0.0);
61                 theme->ratingMiniScore_ = pattern->GetAttr<double>("rating_mini_score", 0.0);
62                 theme->stepSize_ = pattern->GetAttr<double>("rating_step_size", 0.0);
63                 theme->paddingVertical_ = pattern->GetAttr<Dimension>("rating_padding_vertical", 0.0_vp);
64                 theme->ratingWidth_ = pattern->GetAttr<Dimension>("rating_big_width", 0.0_vp);
65                 theme->ratingHeight_ = pattern->GetAttr<Dimension>("rating_big_height", 0.0_vp);
66                 theme->ratingMiniWidth_ = pattern->GetAttr<Dimension>("rating_mini_width", 0.0_vp);
67                 theme->ratingMiniHeight_ = pattern->GetAttr<Dimension>("rating_mini_height", 0.0_vp);
68                 theme->designedStarAspectRatio_ = pattern->GetAttr<double>("rating_designed_start_aspect_ratio", 0.0);
69                 theme->focusBorderWidth_ = pattern->GetAttr<Dimension>("rating_focus_border_width", 0.0_vp);
70                 theme->hoverColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_HOVERED, STAR_HOVER_COLOR);
71                 theme->pressColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_PRESSED, STAR_PRESS_COLOR);
72                 theme->starColorActive_ = pattern->GetAttr<Color>("icon_color_active", Color::RED);
73                 theme->starColorInactive_ = pattern->GetAttr<Color>("icon_color_inactive", Color::GRAY);
74                 theme->borderRadius_ = pattern->GetAttr<Dimension>("hover_radius_size", BORDER_RADIUS);
75                 theme->hoverAnimationDuration_ = pattern->GetAttr<double>("hover_animation_duration", 0.0);
76                 theme->pressAnimationDuration_ = pattern->GetAttr<double>("press_animation_duration", 0.0);
77             } else {
78                 LOGW("find pattern of rating fail");
79             }
80             return theme;
81         }
82     };
83 
84     ~RatingTheme() override = default;
85 
GetStarNum()86     int32_t GetStarNum() const
87     {
88         return starNum_;
89     }
90 
GetRatingWidth()91     const Dimension& GetRatingWidth() const
92     {
93         return ratingWidth_;
94     }
95 
GetRatingHeight()96     const Dimension& GetRatingHeight() const
97     {
98         return ratingHeight_;
99     }
100 
GetRatingMiniWidth()101     const Dimension& GetRatingMiniWidth() const
102     {
103         return ratingMiniWidth_;
104     }
105 
GetRatingMiniHeight()106     const Dimension& GetRatingMiniHeight() const
107     {
108         return ratingMiniHeight_;
109     }
110 
GetPaddingVertical()111     const Dimension& GetPaddingVertical() const
112     {
113         return paddingVertical_;
114     }
115 
GetStepSize()116     double GetStepSize() const
117     {
118         return stepSize_;
119     }
120 
GetRatingScore()121     double GetRatingScore() const
122     {
123         return ratingScore_;
124     }
125 
GetRatingMiniScore()126     double GetRatingMiniScore() const
127     {
128         return ratingMiniScore_;
129     }
130 
GetForegroundResourceId()131     const InternalResource::ResourceId& GetForegroundResourceId() const
132     {
133         return foregroundResourceId_;
134     }
135 
GetSecondaryResourceId()136     const InternalResource::ResourceId& GetSecondaryResourceId() const
137     {
138         return secondaryResourceId_;
139     }
140 
GetBackgroundResourceId()141     const InternalResource::ResourceId& GetBackgroundResourceId() const
142     {
143         return backgroundResourceId_;
144     }
145 
GetForegroundMiniResourceId()146     const InternalResource::ResourceId& GetForegroundMiniResourceId() const
147     {
148         return foregroundMiniResourceId_;
149     }
150 
GetSecondaryMiniResourceId()151     const InternalResource::ResourceId& GetSecondaryMiniResourceId() const
152     {
153         return secondaryMiniResourceId_;
154     }
155 
GetBackgroundMiniResourceId()156     const InternalResource::ResourceId& GetBackgroundMiniResourceId() const
157     {
158         return backgroundMiniResourceId_;
159     }
160 
GetDesignedStarAspectRatio()161     double GetDesignedStarAspectRatio() const
162     {
163         return designedStarAspectRatio_;
164     }
165 
GetFocusBorderWidth()166     const Dimension& GetFocusBorderWidth() const
167     {
168         return focusBorderWidth_;
169     }
170 
GetFocusBorderRadius()171     const Dimension& GetFocusBorderRadius() const
172     {
173         return borderRadius_;
174     }
175 
GetHoverColor()176     const Color& GetHoverColor() const
177     {
178         return hoverColor_;
179     }
180 
GetPressColor()181     const Color& GetPressColor() const
182     {
183         return pressColor_;
184     }
185 
GetStarColorActive()186     const Color& GetStarColorActive() const
187     {
188         return starColorActive_;
189     }
190 
GetStarColorInactive()191     const Color& GetStarColorInactive() const
192     {
193         return starColorInactive_;
194     }
195 
GetHoverAnimationDuration()196     double GetHoverAnimationDuration() const
197     {
198         return hoverAnimationDuration_;
199     }
200 
GetPressAnimationDuration()201     double GetPressAnimationDuration() const
202     {
203         return pressAnimationDuration_;
204     }
205 
206 protected:
207     RatingTheme() = default;
208 
209 private:
210     int32_t starNum_ = 0;
211     Dimension ratingWidth_;
212     Dimension ratingHeight_;
213     Dimension ratingMiniWidth_;
214     Dimension ratingMiniHeight_;
215     Dimension paddingVertical_;
216     double stepSize_ = 0.0;
217     double ratingScore_ = 0.0;
218     double ratingMiniScore_ = 0.0;
219     double designedStarAspectRatio_ = 1.0;
220     double hoverAnimationDuration_ = 0.0;
221     double pressAnimationDuration_ = 0.0;
222     InternalResource::ResourceId foregroundResourceId_ = InternalResource::ResourceId::RATE_STAR_BIG_ON_SVG;
223     InternalResource::ResourceId secondaryResourceId_ = InternalResource::ResourceId::RATE_STAR_BIG_OFF_SVG;
224     InternalResource::ResourceId backgroundResourceId_ = InternalResource::ResourceId::RATE_STAR_BIG_OFF_SVG;
225     InternalResource::ResourceId foregroundMiniResourceId_ = InternalResource::ResourceId::RATE_STAR_SMALL_ON_SVG;
226     InternalResource::ResourceId secondaryMiniResourceId_ = InternalResource::ResourceId::RATE_STAR_SMALL_ON_SVG;
227     InternalResource::ResourceId backgroundMiniResourceId_ = InternalResource::ResourceId::RATE_STAR_SMALL_OFF_SVG;
228 
229     // properties for phone platform
230     Color hoverColor_;
231     Color pressColor_;
232     Color starColorActive_;
233     Color starColorInactive_;
234     Dimension focusBorderWidth_;
235     Dimension borderRadius_;
236 };
237 
238 } // namespace OHOS::Ace
239 
240 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RATING_RATING_THEME_H
241