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_COMPONENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RATING_RATING_COMPONENT_H
18 
19 #include "base/resource/internal_resource.h"
20 #include "base/utils/utils.h"
21 #include "core/components/image/image_component.h"
22 #include "core/components/rating/rating_theme.h"
23 #include "core/components_v2/common/common_def.h"
24 #include "core/pipeline/base/component_group.h"
25 #include "core/pipeline/base/constants.h"
26 #include "core/pipeline/base/element.h"
27 
28 namespace OHOS::Ace {
29 
30 class ACE_EXPORT RatingComponent : public RenderComponent {
31     DECLARE_ACE_TYPE(RatingComponent, RenderComponent);
32 
33 public:
34     RatingComponent() = default;
35     ~RatingComponent() override = default;
36 
37     RefPtr<RenderNode> CreateRenderNode() override;
38     RefPtr<Element> CreateElement() override;
39 
40     void SetResIdFromTheme(const RefPtr<RatingTheme>& theme);
41     void SetMiniResIdFromTheme(const RefPtr<RatingTheme>& theme);
42     void SetThemeStyle(const RefPtr<RatingTheme>& theme);
43 
GetStarNum()44     int32_t GetStarNum() const
45     {
46         return starNum_;
47     }
48 
SetStarNum(int32_t starNum)49     void SetStarNum(int32_t starNum)
50     {
51         starNum_ = starNum;
52     }
53 
GetIndicator()54     bool GetIndicator() const
55     {
56         return isIndicator_;
57     }
58 
SetIndicator(bool indicator)59     void SetIndicator(bool indicator)
60     {
61         isIndicator_ = indicator;
62     }
63 
GetRatingScore()64     double GetRatingScore() const
65     {
66         return ratingScore_;
67     }
68 
SetRatingScore(double ratingScore)69     void SetRatingScore(double ratingScore)
70     {
71         ratingScore_ = ratingScore;
72     }
73 
GetStepSize()74     double GetStepSize() const
75     {
76         return stepSize_;
77     }
78 
SetStepSize(double stepSize)79     void SetStepSize(double stepSize)
80     {
81         stepSize_ = stepSize;
82     }
83 
GetWidth()84     const Dimension& GetWidth() const
85     {
86         return width_;
87     }
88 
SetWidth(const Dimension & width)89     void SetWidth(const Dimension& width)
90     {
91         width_ = width;
92     }
93 
GetHeight()94     const Dimension& GetHeight() const
95     {
96         return height_;
97     }
98 
SetHeight(const Dimension & height)99     void SetHeight(const Dimension& height)
100     {
101         height_ = height;
102     }
GetDefaultHeight()103     const Dimension& GetDefaultHeight() const
104     {
105         return defaultHeight_;
106     }
107 
SetDefaultHeight(const Dimension & defaultHeight)108     void SetDefaultHeight(const Dimension& defaultHeight)
109     {
110         defaultHeight_ = defaultHeight;
111     }
112 
GetForegroundSrc()113     const std::string& GetForegroundSrc() const
114     {
115         return foregroundSrc_;
116     }
117 
SetForegroundSrc(const std::string & foregroundSrc)118     void SetForegroundSrc(const std::string& foregroundSrc)
119     {
120         foregroundSrc_ = foregroundSrc;
121     }
122 
GetSecondarySrc()123     const std::string& GetSecondarySrc() const
124     {
125         return secondarySrc_;
126     }
127 
SetSecondarySrc(const std::string & secondarySrc)128     void SetSecondarySrc(const std::string& secondarySrc)
129     {
130         secondarySrc_ = secondarySrc;
131     }
132 
GetBackgroundSrc()133     const std::string& GetBackgroundSrc() const
134     {
135         return backgroundSrc_;
136     }
137 
SetBackgroundSrc(const std::string & backgroundSrc)138     void SetBackgroundSrc(const std::string& backgroundSrc)
139     {
140         backgroundSrc_ = backgroundSrc;
141     }
142 
GetForegroundImage()143     RefPtr<ImageComponent> GetForegroundImage() const
144     {
145         return foregroundImage_;
146     }
147 
GetSecondaryImage()148     RefPtr<ImageComponent> GetSecondaryImage() const
149     {
150         return secondaryImage_;
151     }
152 
GetBackgroundImage()153     RefPtr<ImageComponent> GetBackgroundImage() const
154     {
155         return backgroundImage_;
156     }
157 
SetChangeEventId(const EventMarker & changeEventId)158     void SetChangeEventId(const EventMarker& changeEventId)
159     {
160         changeEventId_ = changeEventId;
161     }
162 
GetChangeEventId()163     const EventMarker& GetChangeEventId() const
164     {
165         return changeEventId_;
166     }
167 
SetPaddingHorizontal(const Dimension & paddingHorizontal)168     void SetPaddingHorizontal(const Dimension& paddingHorizontal)
169     {
170         paddingHorizontal_ = paddingHorizontal;
171     }
172 
GetPaddingHorizontal()173     const Dimension& GetPaddingHorizontal() const
174     {
175         return paddingHorizontal_;
176     }
177 
SetPaddingVertical(const Dimension & paddingVertical)178     void SetPaddingVertical(const Dimension& paddingVertical)
179     {
180         paddingVertical_ = paddingVertical;
181     }
182 
GetPaddingVertical()183     const Dimension& GetPaddingVertical() const
184     {
185         return paddingVertical_;
186     }
187 
GetForegroundResourceId()188     const InternalResource::ResourceId& GetForegroundResourceId() const
189     {
190         return foregroundResourceId_;
191     }
192 
GetSecondaryResourceId()193     const InternalResource::ResourceId& GetSecondaryResourceId() const
194     {
195         return secondaryResourceId_;
196     }
197 
GetBackgroundResourceId()198     const InternalResource::ResourceId& GetBackgroundResourceId() const
199     {
200         return backgroundResourceId_;
201     }
202 
SetRtlFlip(bool rtlFlip)203     void SetRtlFlip(bool rtlFlip)
204     {
205         rtlFlip_ = rtlFlip;
206     }
207 
IsRtlFlip()208     bool IsRtlFlip() const
209     {
210         return rtlFlip_;
211     }
212 
SetDesignedStarAspectRatio(double designedStarAspectRatio)213     void SetDesignedStarAspectRatio(double designedStarAspectRatio)
214     {
215         designedStarAspectRatio_ = designedStarAspectRatio;
216     }
217 
GetDesignedStarAspectRatio()218     double GetDesignedStarAspectRatio() const
219     {
220         return designedStarAspectRatio_;
221     }
222 
SetFocusBorderWidth(const Dimension & focusBorderWidth)223     void SetFocusBorderWidth(const Dimension& focusBorderWidth)
224     {
225         focusBorderWidth_ = focusBorderWidth;
226     }
227 
GetFocusBorderWidth()228     const Dimension& GetFocusBorderWidth() const
229     {
230         return focusBorderWidth_;
231     }
232 
SetFocusBorderRadius(const Dimension & focusBorderRadius)233     void SetFocusBorderRadius(const Dimension& focusBorderRadius)
234     {
235         focusBorderRadius_ = focusBorderRadius;
236     }
237 
GetFocusBorderRadius()238     const Dimension& GetFocusBorderRadius() const
239     {
240         return focusBorderRadius_;
241     }
242 
SetHoverColor(const Color & hoverColor)243     void SetHoverColor(const Color& hoverColor)
244     {
245         hoverColor_ = hoverColor;
246     }
247 
GetHoverColor()248     const Color& GetHoverColor() const
249     {
250         return hoverColor_;
251     }
252 
SetStarColorActive(const Color & starColorActive)253     void SetStarColorActive(const Color& starColorActive)
254     {
255         starColorActive_ = starColorActive;
256     }
257 
GetStarColorActive()258     const Color& GetStarColorActive() const
259     {
260         return starColorActive_;
261     }
262 
SetStarColorInactive(const Color & starColorInactive)263     void SetStarColorInactive(const Color& starColorInactive)
264     {
265         starColorInactive_ = starColorInactive;
266     }
267 
GetStarColorInactive()268     const Color& GetStarColorInactive() const
269     {
270         return starColorInactive_;
271     }
272 
273     ACE_DEFINE_COMPONENT_EVENT(OnChange, void(std::string));
274 
GetMouseAnimationType()275     HoverAnimationType GetMouseAnimationType() const
276     {
277         return animationType_;
278     }
SetMouseAnimationType(HoverAnimationType animationType)279     void SetMouseAnimationType(HoverAnimationType animationType)
280     {
281         animationType_ = animationType;
282     }
283 
284 private:
285     int32_t starNum_ = 5;
286     double ratingScore_ = 0.0;
287     double stepSize_ = 0.5;
288     double designedStarAspectRatio_ = 1.0;
289     Dimension width_;
290     Dimension height_;
291     Dimension defaultHeight_;
292     Dimension paddingHorizontal_;
293     Dimension paddingVertical_;
294     std::string foregroundSrc_;
295     std::string secondarySrc_;
296     std::string backgroundSrc_;
297     InternalResource::ResourceId foregroundResourceId_ = InternalResource::ResourceId::RATE_STAR_BIG_ON_SVG;
298     InternalResource::ResourceId secondaryResourceId_ = InternalResource::ResourceId::RATE_STAR_BIG_OFF_SVG;
299     InternalResource::ResourceId backgroundResourceId_ = InternalResource::ResourceId::RATE_STAR_BIG_OFF_SVG;
300     bool isIndicator_ = false;
301     bool rtlFlip_ = true;
302     RefPtr<ImageComponent> foregroundImage_ =
303         AceType::MakeRefPtr<ImageComponent>(InternalResource::ResourceId::RATE_STAR_BIG_ON_SVG);
304     RefPtr<ImageComponent> secondaryImage_ =
305         AceType::MakeRefPtr<ImageComponent>(InternalResource::ResourceId::RATE_STAR_BIG_OFF_SVG);
306     RefPtr<ImageComponent> backgroundImage_ =
307         AceType::MakeRefPtr<ImageComponent>(InternalResource::ResourceId::RATE_STAR_BIG_OFF_SVG);
308     EventMarker changeEventId_;
309     Color starColorActive_;
310     Color starColorInactive_;
311 
312     // properties for phone platform
313     Color hoverColor_;
314     Dimension focusBorderWidth_;
315     Dimension focusBorderRadius_;
316     HoverAnimationType animationType_ = HoverAnimationType::UNKNOWN;
317 };
318 
319 } // namespace OHOS::Ace
320 
321 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RATING_RATING_COMPONENT_H
322