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 
14  * limitations under the License.
15  */
16 
17 #include "bridge/declarative_frontend/jsview/models/progress_model_impl.h"
18 
19 #include "base/geometry/dimension.h"
20 #include "bridge/declarative_frontend/view_stack_processor.h"
21 #include "core/components/progress/progress_theme.h"
22 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
23 
24 namespace OHOS::Ace::Framework {
25 namespace {
26 const int32_t DEFALUT_PROGRESS_SCALE_NUMBER = 100;
27 const Dimension DEFALUT_PROGRESS_SCALE_WIDTH = Dimension(2);
28 } // namespace
29 
Create(double min,double value,double cachedValue,double max,NG::ProgressType type)30 void ProgressModelImpl::Create(double min, double value, double cachedValue, double max, NG::ProgressType type)
31 {
32     auto progressComponent = AceType::MakeRefPtr<OHOS::Ace::ProgressComponent>(
33         min, value, cachedValue, max, static_cast<ProgressType>(type));
34     ViewStackProcessor::GetInstance()->ClaimElementId(progressComponent);
35     auto theme = JSViewAbstract::GetTheme<ProgressTheme>();
36     if (!theme) {
37         LOGE("progress Theme is null");
38         return;
39     }
40 
41     progressComponent->InitStyle(theme);
42 
43     if (type == NG::ProgressType::SCALE) {
44         progressComponent->SetScaleNumber(DEFALUT_PROGRESS_SCALE_NUMBER);
45         progressComponent->SetScaleWidth(DEFALUT_PROGRESS_SCALE_WIDTH);
46     }
47 
48     ViewStackProcessor::GetInstance()->Push(progressComponent);
49 }
50 
SetValue(double value)51 void ProgressModelImpl::SetValue(double value)
52 {
53     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
54     auto progress = AceType::DynamicCast<ProgressComponent>(component);
55     if (!progress) {
56         LOGI("progress component is null.");
57         return;
58     }
59 
60     auto maxValue_ = progress->GetMaxValue();
61     if (value > maxValue_) {
62         LOGE("value is lager than total , set value euqals total");
63         value = maxValue_;
64     }
65     progress->SetValue(value);
66 }
67 
SetColor(const Color & value)68 void ProgressModelImpl::SetColor(const Color& value)
69 {
70     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
71     auto progress = AceType::DynamicCast<ProgressComponent>(component);
72     if (!progress) {
73         LOGI("progress component is null.");
74         return;
75     }
76     RefPtr<TrackComponent> track = progress->GetTrack();
77 
78     track->SetSelectColor(value);
79 }
80 
SetBackgroundColor(const Color & value)81 void ProgressModelImpl::SetBackgroundColor(const Color& value)
82 {
83     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
84     auto progress = AceType::DynamicCast<ProgressComponent>(component);
85     if (!progress) {
86         LOGE("progress Component is null");
87         return;
88     }
89     auto track = progress->GetTrack();
90     if (!track) {
91         LOGE("track Component is null");
92         return;
93     }
94 
95     track->SetBackgroundColor(value);
96 }
97 
SetStrokeWidth(const Dimension & value)98 void ProgressModelImpl::SetStrokeWidth(const Dimension& value)
99 {
100     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
101     auto progress = AceType::DynamicCast<ProgressComponent>(component);
102     if (!progress) {
103         LOGI("progress component is null.");
104         return;
105     }
106 
107     progress->SetTrackThickness(value);
108 }
109 
SetScaleCount(int32_t value)110 void ProgressModelImpl::SetScaleCount(int32_t value)
111 {
112     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
113     auto progress = AceType::DynamicCast<ProgressComponent>(component);
114     if (!progress) {
115         LOGI("progress component is null.");
116         return;
117     }
118 
119     if (value > 0.0) {
120         progress->SetScaleNumber(value);
121     }
122 }
123 
SetScaleWidth(const Dimension & value)124 void ProgressModelImpl::SetScaleWidth(const Dimension& value)
125 {
126     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
127     auto progress = AceType::DynamicCast<ProgressComponent>(component);
128     if (!progress) {
129         LOGI("progress component is null.");
130         return;
131     }
132 
133     progress->SetScaleWidth(value);
134 }
135 
136 } // namespace OHOS::Ace::Framework
137