• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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->starNum_ = themeConstants->GetInt(THEME_RATING_STAR_NUM);
51             theme->ratingScore_ = themeConstants->GetDouble(THEME_RATING_SCORE);
52             theme->ratingMiniScore_ = themeConstants->GetDouble(THEME_RATING_MINI_SCORE);
53             theme->stepSize_ = themeConstants->GetDouble(THEME_RATING_STEPSIZE);
54             theme->paddingVertical_ = themeConstants->GetDimension(THEME_RATING_PADDING_VERTICAL);
55             theme->ratingWidth_ = themeConstants->GetDimension(THEME_RATING_BIG_WIDTH);
56             theme->ratingHeight_ = themeConstants->GetDimension(THEME_RATING_BIG_HEIGHT);
57             theme->ratingMiniWidth_ = themeConstants->GetDimension(THEME_RATING_MINI_WIDTH);
58             theme->ratingMiniHeight_ = themeConstants->GetDimension(THEME_RATING_MINI_HEIGHT);
59             theme->foregroundResourceId_ = themeConstants->GetResourceId(THEME_RATING_RESOURCE_ID_BIG_ON);
60             theme->secondaryResourceId_ = themeConstants->GetResourceId(THEME_RATING_RESOURCE_ID_BIG_HALF);
61             theme->backgroundResourceId_ = themeConstants->GetResourceId(THEME_RATING_RESOURCE_ID_BIG_OFF);
62             theme->foregroundMiniResourceId_ = themeConstants->GetResourceId(THEME_RATING_RESOURCE_ID_MINI_ON);
63             theme->secondaryMiniResourceId_ = themeConstants->GetResourceId(THEME_RATING_RESOURCE_ID_MINI_HALF);
64             theme->backgroundMiniResourceId_ = themeConstants->GetResourceId(THEME_RATING_RESOURCE_ID_MINI_OFF);
65             theme->designedStarAspectRatio_ = themeConstants->GetDouble(THEME_RATING_DESIGNED_STAR_ASPECT_RATIO);
66             theme->focusBorderWidth_ = themeConstants->GetDimension(THEME_RATING_FOCUS_BORDER_WIDTH);
67             theme->hoverColor_ = themeConstants->GetColor(THEME_RATING_HOVER_COLOR);
68             theme->starColorActive_ = themeConstants->GetColor(THEME_RATING_STAR_COLOR_ACTIVE);
69             theme->starColorInactive_ = themeConstants->GetColor(THEME_RATING_STAR_COLOR_INACTIVE);
70             theme->borderRadius_ = themeConstants->GetDimension(THEME_RATING_FOCUS_BORDER_RADIUS);
71             auto themeStyle = themeConstants->GetThemeStyle();
72             if (!themeStyle) {
73                 return theme;
74             }
75             auto pattern = themeStyle->GetAttr<RefPtr<ThemeStyle>>(THEME_PATTERN_RATING, nullptr);
76             if (pattern) {
77                 theme->hoverColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_HOVERED, STAR_HOVER_COLOR);
78                 theme->pressColor_ = pattern->GetAttr<Color>(PATTERN_BG_COLOR_PRESSED, STAR_PRESS_COLOR);
79                 theme->starColorActive_ = pattern->GetAttr<Color>("icon_color_active", Color::RED);
80                 theme->starColorInactive_ = pattern->GetAttr<Color>("icon_color_inactive", Color::GRAY);
81                 theme->borderRadius_ = pattern->GetAttr<Dimension>("hover_radius_size", BORDER_RADIUS);
82                 theme->hoverAnimationDuration_ = pattern->GetAttr<double>("hover_animation_duration", 0.0);
83                 theme->pressAnimationDuration_ = pattern->GetAttr<double>("press_animation_duration", 0.0);
84             } else {
85                 LOGW("find pattern of rating fail");
86             }
87             return theme;
88         }
89     };
90 
91     ~RatingTheme() override = default;
92 
GetStarNum()93     int32_t GetStarNum() const
94     {
95         return starNum_;
96     }
97 
GetRatingWidth()98     const Dimension& GetRatingWidth() const
99     {
100         return ratingWidth_;
101     }
102 
GetRatingHeight()103     const Dimension& GetRatingHeight() const
104     {
105         return ratingHeight_;
106     }
107 
GetRatingMiniWidth()108     const Dimension& GetRatingMiniWidth() const
109     {
110         return ratingMiniWidth_;
111     }
112 
GetRatingMiniHeight()113     const Dimension& GetRatingMiniHeight() const
114     {
115         return ratingMiniHeight_;
116     }
117 
GetPaddingVertical()118     const Dimension& GetPaddingVertical() const
119     {
120         return paddingVertical_;
121     }
122 
GetStepSize()123     double GetStepSize() const
124     {
125         return stepSize_;
126     }
127 
GetRatingScore()128     double GetRatingScore() const
129     {
130         return ratingScore_;
131     }
132 
GetRatingMiniScore()133     double GetRatingMiniScore() const
134     {
135         return ratingMiniScore_;
136     }
137 
GetForegroundResourceId()138     const InternalResource::ResourceId& GetForegroundResourceId() const
139     {
140         return foregroundResourceId_;
141     }
142 
GetSecondaryResourceId()143     const InternalResource::ResourceId& GetSecondaryResourceId() const
144     {
145         return secondaryResourceId_;
146     }
147 
GetBackgroundResourceId()148     const InternalResource::ResourceId& GetBackgroundResourceId() const
149     {
150         return backgroundResourceId_;
151     }
152 
GetForegroundMiniResourceId()153     const InternalResource::ResourceId& GetForegroundMiniResourceId() const
154     {
155         return foregroundMiniResourceId_;
156     }
157 
GetSecondaryMiniResourceId()158     const InternalResource::ResourceId& GetSecondaryMiniResourceId() const
159     {
160         return secondaryMiniResourceId_;
161     }
162 
GetBackgroundMiniResourceId()163     const InternalResource::ResourceId& GetBackgroundMiniResourceId() const
164     {
165         return backgroundMiniResourceId_;
166     }
167 
GetDesignedStarAspectRatio()168     double GetDesignedStarAspectRatio() const
169     {
170         return designedStarAspectRatio_;
171     }
172 
GetFocusBorderWidth()173     const Dimension& GetFocusBorderWidth() const
174     {
175         return focusBorderWidth_;
176     }
177 
GetFocusBorderRadius()178     const Dimension& GetFocusBorderRadius() const
179     {
180         return borderRadius_;
181     }
182 
GetHoverColor()183     const Color& GetHoverColor() const
184     {
185         return hoverColor_;
186     }
187 
GetPressColor()188     const Color& GetPressColor() const
189     {
190         return pressColor_;
191     }
192 
GetStarColorActive()193     const Color& GetStarColorActive() const
194     {
195         return starColorActive_;
196     }
197 
GetStarColorInactive()198     const Color& GetStarColorInactive() const
199     {
200         return starColorInactive_;
201     }
202 
GetHoverAnimationDuration()203     double GetHoverAnimationDuration() const
204     {
205         return hoverAnimationDuration_;
206     }
207 
GetPressAnimationDuration()208     double GetPressAnimationDuration() const
209     {
210         return pressAnimationDuration_;
211     }
212 
213 protected:
214     RatingTheme() = default;
215 
216 private:
217     int32_t starNum_ = 0;
218     Dimension ratingWidth_;
219     Dimension ratingHeight_;
220     Dimension ratingMiniWidth_;
221     Dimension ratingMiniHeight_;
222     Dimension paddingVertical_;
223     double stepSize_ = 0.0;
224     double ratingScore_ = 0.0;
225     double ratingMiniScore_ = 0.0;
226     double designedStarAspectRatio_ = 1.0;
227     double hoverAnimationDuration_ = 0.0;
228     double pressAnimationDuration_ = 0.0;
229     InternalResource::ResourceId foregroundResourceId_ = InternalResource::ResourceId::RATE_STAR_BIG_ON_SVG;
230     InternalResource::ResourceId secondaryResourceId_ = InternalResource::ResourceId::RATE_STAR_BIG_OFF_SVG;
231     InternalResource::ResourceId backgroundResourceId_ = InternalResource::ResourceId::RATE_STAR_BIG_OFF_SVG;
232     InternalResource::ResourceId foregroundMiniResourceId_ = InternalResource::ResourceId::RATE_STAR_SMALL_ON_SVG;
233     InternalResource::ResourceId secondaryMiniResourceId_ = InternalResource::ResourceId::RATE_STAR_SMALL_ON_SVG;
234     InternalResource::ResourceId backgroundMiniResourceId_ = InternalResource::ResourceId::RATE_STAR_SMALL_OFF_SVG;
235 
236     // properties for phone platform
237     Color hoverColor_;
238     Color pressColor_;
239     Color starColorActive_;
240     Color starColorInactive_;
241     Dimension focusBorderWidth_;
242     Dimension borderRadius_;
243 };
244 
245 } // namespace OHOS::Ace
246 
247 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RATING_RATING_THEME_H
248