1 /*
2 * Copyright (c) 2022 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 #include "bridge/declarative_frontend/jsview/models/rating_model_impl.h"
17
18 #include "core/components/rating/rating_component.h"
19 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
20
21 namespace OHOS::Ace::Framework {
22
Create(double rating,bool indicator)23 void RatingModelImpl::Create(double rating, bool indicator)
24 {
25 auto component = AceType::MakeRefPtr<RatingComponent>();
26 component->SetMouseAnimationType(HoverAnimationType::NONE);
27 component->SetRatingScore(rating);
28 component->SetIndicator(indicator);
29 ViewStackProcessor::GetInstance()->ClaimElementId(component);
30 ViewStackProcessor::GetInstance()->Push(component);
31
32 auto pipelineContext = PipelineContext::GetCurrentContext();
33 CHECK_NULL_VOID(pipelineContext);
34 auto themeManager = pipelineContext->GetThemeManager();
35 CHECK_NULL_VOID(themeManager);
36 auto theme = themeManager->GetTheme<RatingTheme>();
37 CHECK_NULL_VOID(theme);
38
39 auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
40 if (component->GetIndicator()) {
41 if (boxComponent->GetHeightDimension().Value() < 0.0) {
42 boxComponent->SetHeight(theme->GetRatingMiniHeight());
43 }
44 if (boxComponent->GetWidthDimension().Value() < 0.0) {
45 boxComponent->SetWidth(theme->GetRatingMiniWidth());
46 }
47 component->SetPaddingVertical(Dimension());
48 component->SetMiniResIdFromTheme(theme);
49 } else {
50 if (boxComponent->GetHeightDimension().Value() < 0.0) {
51 boxComponent->SetHeight(theme->GetRatingHeight());
52 }
53 if (boxComponent->GetWidthDimension().Value() < 0.0) {
54 boxComponent->SetWidth(theme->GetRatingWidth());
55 }
56 component->SetPaddingVertical(theme->GetPaddingVertical());
57 component->SetResIdFromTheme(theme);
58 }
59 component->SetThemeStyle(theme);
60 }
61
SetRatingScore(double value)62 void RatingModelImpl::SetRatingScore(double value)
63 {
64 auto ratingComponent = AceType::DynamicCast<RatingComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
65 CHECK_NULL_VOID(ratingComponent);
66 ratingComponent->SetRatingScore(value);
67 }
68
SetIndicator(bool value)69 void RatingModelImpl::SetIndicator(bool value)
70 {
71 auto ratingComponent = AceType::DynamicCast<RatingComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
72 CHECK_NULL_VOID(ratingComponent);
73 ratingComponent->SetIndicator(value);
74 }
75
SetStars(int32_t value)76 void RatingModelImpl::SetStars(int32_t value)
77 {
78 auto ratingComponent = AceType::DynamicCast<RatingComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
79 CHECK_NULL_VOID(ratingComponent);
80 ratingComponent->SetStarNum(value);
81 }
82
SetStepSize(double value)83 void RatingModelImpl::SetStepSize(double value)
84 {
85 auto ratingComponent = AceType::DynamicCast<RatingComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
86 CHECK_NULL_VOID(ratingComponent);
87 ratingComponent->SetStepSize(value);
88 }
89
SetForegroundSrc(const std::string & value,bool flag)90 void RatingModelImpl::SetForegroundSrc(const std::string& value, bool flag)
91 {
92 auto ratingComponent = AceType::DynamicCast<RatingComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
93 CHECK_NULL_VOID(ratingComponent);
94 ratingComponent->SetForegroundSrc(value);
95 }
96
SetSecondarySrc(const std::string & value,bool flag)97 void RatingModelImpl::SetSecondarySrc(const std::string& value, bool flag)
98 {
99 auto ratingComponent = AceType::DynamicCast<RatingComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
100 CHECK_NULL_VOID(ratingComponent);
101 ratingComponent->SetSecondarySrc(value);
102 }
103
SetBackgroundSrc(const std::string & value,bool flag)104 void RatingModelImpl::SetBackgroundSrc(const std::string& value, bool flag)
105 {
106 auto ratingComponent = AceType::DynamicCast<RatingComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
107 CHECK_NULL_VOID(ratingComponent);
108 ratingComponent->SetBackgroundSrc(value);
109 }
110
SetOnChange(RatingChangeEvent && onChange)111 void RatingModelImpl::SetOnChange(RatingChangeEvent&& onChange)
112 {
113 auto ratingComponent = AceType::DynamicCast<RatingComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
114 CHECK_NULL_VOID(ratingComponent);
115 ratingComponent->SetOnChange(onChange);
116 }
117
118 } // namespace OHOS::Ace::Framework