1 /*
2  * Copyright (c) 2024 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/cj_frontend/interfaces/cj_ffi/cj_progress_ffi.h"
17 
18 #include "bridge/cj_frontend/interfaces/cj_ffi/utils.h"
19 #include "core/common/container.h"
20 #include "core/components_ng/pattern/progress/progress_model_ng.h"
21 
22 using namespace OHOS::Ace;
23 using namespace OHOS::Ace::NG;
24 using namespace OHOS::Ace::Framework;
25 
26 namespace {
27 const std::vector<ProgressType> PROGRESS_TYPES = { ProgressType::LINEAR, ProgressType::RING, ProgressType::MOON,
28     ProgressType::SCALE, ProgressType::CAPSULE };
29 
30 const std::vector<NG::ProgressType> PROGRESS_TYPES_NG = { NG::ProgressType::LINEAR, NG::ProgressType::RING,
31     NG::ProgressType::MOON, NG::ProgressType::SCALE, NG::ProgressType::CAPSULE };
32 
33 } // namespace
34 
35 extern "C" {
FfiOHOSAceFrameworkProgressCreate(double value,double total,int32_t type)36 void FfiOHOSAceFrameworkProgressCreate(double value, double total, int32_t type)
37 {
38     if (!Utils::CheckParamsValid(type, PROGRESS_TYPES.size())) {
39         LOGE("invalid value for progress type");
40         return;
41     }
42 
43     double realValue = value;
44     if (value > total) {
45         realValue = total;
46     } else if (value < 0) {
47         realValue = 0;
48     }
49 
50     ProgressModel::GetInstance()->Create(0.0, realValue, 0.0, total, PROGRESS_TYPES_NG[type]);
51 }
52 
FfiOHOSAceFrameworkProgressSetValue(double value)53 void FfiOHOSAceFrameworkProgressSetValue(double value)
54 {
55     double realValue = value;
56     if (value < 0) {
57         realValue = 0;
58     }
59 
60     ProgressModel::GetInstance()->SetValue(realValue);
61 }
62 
FfiOHOSAceFrameworkProgressSetColor(uint32_t color)63 void FfiOHOSAceFrameworkProgressSetColor(uint32_t color)
64 {
65     ProgressModel::GetInstance()->SetColor(Color(color));
66 }
67 
FfiOHOSAceFrameworkProgressSetBackgroundColor(uint32_t color)68 void FfiOHOSAceFrameworkProgressSetBackgroundColor(uint32_t color)
69 {
70     ProgressModel::GetInstance()->SetBackgroundColor(Color(color));
71 }
72 
FfiOHOSAceFrameworkProgressSetStyle(double strokeWidth,int32_t strokeWidthUnit,int32_t scaleCount,double scaleWidth,int32_t scaleWidthUnit)73 void FfiOHOSAceFrameworkProgressSetStyle(
74     double strokeWidth, int32_t strokeWidthUnit, int32_t scaleCount, double scaleWidth, int32_t scaleWidthUnit)
75 {
76     Dimension strokeWidthValue(strokeWidth, static_cast<DimensionUnit>(strokeWidthUnit));
77     Dimension scaleWidthValue(scaleWidth, static_cast<DimensionUnit>(scaleWidthUnit));
78 
79     ProgressModel::GetInstance()->SetStrokeWidth(strokeWidthValue);
80     ProgressModel::GetInstance()->SetScaleCount(scaleCount);
81     ProgressModel::GetInstance()->SetScaleWidth(scaleWidthValue);
82 }
83 }
84