• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 "core/components_ng/pattern/progress/progress_paint_property.h"
17 
18 #include "core/components/progress/progress_theme.h"
19 #include "core/components_v2/inspector/utils.h"
20 #include "core/pipeline/pipeline_base.h"
21 
22 namespace OHOS::Ace::NG {
23 constexpr float PROGRSS_MAX_VALUE = 100.f;
24 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const25 void ProgressPaintProperty::ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
26 {
27     PaintProperty::ToJsonValue(json, filter);
28     auto pipeline = PipelineBase::GetCurrentContext();
29     CHECK_NULL_VOID(pipeline);
30     auto progressTheme = pipeline->GetTheme<ProgressTheme>(GetThemeScopeId());
31     CHECK_NULL_VOID(progressTheme);
32 
33     json->PutExtAttr("constructor", ProgressOptions().c_str(), filter);
34     json->PutExtAttr("total", std::to_string(GetMaxValue().value_or(PROGRSS_MAX_VALUE)).c_str(), filter);
35     json->PutExtAttr("value", std::to_string(GetValue().value_or(0.f)).c_str(), filter);
36     json->PutExtAttr("isSensitive", std::to_string(GetIsSensitive().value_or(false)).c_str(), filter);
37     json->PutExtAttr("scaleCount",
38         std::to_string(GetScaleCount().value_or(progressTheme->GetScaleNumber())).c_str(), filter);
39     json->PutExtAttr("scaleWidth",
40         (GetScaleWidth().value_or(progressTheme->GetScaleWidth()).ToString()).c_str(), filter);
41     Color defaultBackgroundColor = progressTheme->GetTrackBgColor();
42     Color defaultColor = progressTheme->GetTrackSelectedColor();
43     ProgressType progressType = GetProgressType().value_or(ProgressType::LINEAR);
44     if (progressType == ProgressType::CAPSULE) {
45         defaultColor = progressTheme->GetCapsuleSelectColor();
46         defaultBackgroundColor = progressTheme->GetCapsuleBgColor();
47     } else if (progressType == ProgressType::RING) {
48         defaultBackgroundColor = progressTheme->GetRingProgressBgColor();
49     } else if (progressType == ProgressType::SCALE) {
50         defaultColor = progressTheme->GetScaleTrackSelectedColor();
51     }
52     json->PutExtAttr("color", (GetColor().value_or(defaultColor)).ColorToString().c_str(), filter);
53     json->PutExtAttr("backgroundColor",
54         (GetBackgroundColor().value_or(defaultBackgroundColor)).ColorToString().c_str(), filter);
55     json->PutExtAttr("capsuleBorderColor",
56         (GetBorderColor().value_or(progressTheme->GetBorderColor())).ColorToString().c_str(), filter);
57     json->PutExtAttr("progressGradientColor", ToJsonGradientColor().c_str(), filter);
58 }
59 
ProgressOptions() const60 std::string ProgressPaintProperty::ProgressOptions() const
61 {
62     auto jsonValue = JsonUtil::Create(true);
63     jsonValue->Put("value", std::to_string(GetValue().value_or(0.f)).c_str());
64     jsonValue->Put("total", std::to_string(GetMaxValue().value_or(PROGRSS_MAX_VALUE)).c_str());
65     jsonValue->Put("type",
66         ProgressTypeUtils::ConvertProgressTypeToString(GetProgressType().value_or(ProgressType::LINEAR)).c_str());
67     return jsonValue->ToString();
68 }
69 
ToJsonGradientColor() const70 std::string ProgressPaintProperty::ToJsonGradientColor() const
71 {
72     Gradient colors;
73     if (propGradientColor_.has_value()) {
74         colors = propGradientColor_.value();
75     } else {
76         auto pipelineContext = PipelineBase::GetCurrentContext();
77         CHECK_NULL_RETURN(pipelineContext, "");
78         auto theme = pipelineContext->GetTheme<ProgressTheme>(GetThemeScopeId());
79         auto endColor = theme->GetRingProgressEndSideColor();
80         auto beginColor = theme->GetRingProgressBeginSideColor();
81         GradientColor gradientColorEnd;
82         gradientColorEnd.SetLinearColor(LinearColor(endColor));
83         gradientColorEnd.SetDimension(Dimension(0.0f));
84         colors.AddColor(gradientColorEnd);
85         GradientColor gradientColorBegin;
86         gradientColorBegin.SetLinearColor(LinearColor(beginColor));
87         gradientColorBegin.SetDimension(Dimension(1.0f));
88         colors.AddColor(gradientColorBegin);
89     }
90 
91     auto jsonArray = JsonUtil::CreateArray(true);
92     for (size_t index = 0; index < colors.GetColors().size(); ++index) {
93         auto gradientColor = colors.GetColors()[index];
94         auto gradientColorJson = JsonUtil::Create(true);
95         gradientColorJson->Put("color", gradientColor.GetLinearColor().ToColor().ColorToString().c_str());
96         gradientColorJson->Put("offset", std::to_string(gradientColor.GetDimension().Value()).c_str());
97         jsonArray->Put(std::to_string(index).c_str(), gradientColorJson);
98     }
99     return jsonArray->ToString();
100 }
101 
GetThemeScopeId() const102 int32_t ProgressPaintProperty::GetThemeScopeId() const
103 {
104     auto host = GetHost();
105     CHECK_NULL_RETURN(host, 0);
106     return host->GetThemeScopeId();
107 }
108 } // namespace OHOS::Ace::NG
109