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