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