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  * limitations under the License.
14  */
15 
16 #include "core/components_ng/pattern/progress/progress_paint_property.h"
17 
18 #include "base/geometry/dimension.h"
19 #include "core/common/container.h"
20 #include "core/components/progress/progress_theme.h"
21 #include "core/components_ng/base/inspector_filter.h"
22 #include "core/components_ng/pattern/progress/progress_date.h"
23 #include "core/components_v2/inspector/utils.h"
24 #include "core/pipeline/pipeline_base.h"
25 
26 namespace OHOS::Ace::NG {
27 constexpr float PROGRSS_MAX_VALUE = 100.f;
28 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const29 void ProgressPaintProperty::ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
30 {
31     PaintProperty::ToJsonValue(json, filter);
32     /* no fixed attr below, just return */
33     if (filter.IsFastFilter()) {
34         ToJsonValueForCapsule(json, filter);
35         return;
36     }
37     auto pipeline = PipelineBase::GetCurrentContext();
38     CHECK_NULL_VOID(pipeline);
39     auto progressTheme = pipeline->GetTheme<ProgressTheme>();
40     CHECK_NULL_VOID(progressTheme);
41 
42     json->PutExtAttr("constructor", ProgressOptions().c_str(), filter);
43     json->PutExtAttr("total", std::to_string(GetMaxValue().value_or(PROGRSS_MAX_VALUE)).c_str(), filter);
44     json->PutExtAttr("value", std::to_string(GetValue().value_or(0.f)).c_str(), filter);
45     json->PutExtAttr("isSensitive", std::to_string(GetIsSensitive().value_or(false)).c_str(), filter);
46     json->PutExtAttr("scaleCount",
47         std::to_string(GetScaleCount().value_or(progressTheme->GetScaleNumber())).c_str(), filter);
48     json->PutExtAttr("scaleWidth",
49         (GetScaleWidth().value_or(progressTheme->GetScaleWidth()).ToString()).c_str(), filter);
50     Color defaultBackgroundColor = progressTheme->GetTrackBgColor();
51     Color defaultColor = progressTheme->GetTrackSelectedColor();
52     ProgressType progressType = GetProgressType().value_or(ProgressType::LINEAR);
53     if (progressType == ProgressType::CAPSULE) {
54         defaultColor = progressTheme->GetCapsuleSelectColor();
55         defaultBackgroundColor = progressTheme->GetCapsuleBgColor();
56     } else if (progressType == ProgressType::RING) {
57         defaultBackgroundColor = progressTheme->GetRingProgressBgColor();
58     }
59     json->PutExtAttr("color", (GetColor().value_or(defaultColor)).ColorToString().c_str(), filter);
60     json->PutExtAttr("backgroundColor",
61         (GetBackgroundColor().value_or(defaultBackgroundColor)).ColorToString().c_str(), filter);
62     json->PutExtAttr("capsuleBorderColor",
63         (GetBorderColor().value_or(progressTheme->GetBorderColor())).ColorToString().c_str(), filter);
64     ToJsonValueForCapsule(json, filter);
65     json->PutExtAttr("progressGradientColor", ToJsonGradientColor().c_str(), filter);
66 }
67 
ProgressOptions() const68 std::string ProgressPaintProperty::ProgressOptions() const
69 {
70     auto jsonValue = JsonUtil::Create(true);
71     jsonValue->Put("value", std::to_string(GetValue().value_or(0.f)).c_str());
72     jsonValue->Put("total", std::to_string(GetMaxValue().value_or(PROGRSS_MAX_VALUE)).c_str());
73     jsonValue->Put("type",
74         ProgressTypeUtils::ConvertProgressTypeToString(GetProgressType().value_or(ProgressType::LINEAR)).c_str());
75     return jsonValue->ToString();
76 }
77 
ToJsonValueForCapsule(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const78 void ProgressPaintProperty::ToJsonValueForCapsule(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
79 {
80     /* no fixed attr below, just return */
81     if (filter.IsFastFilter()) {
82         return;
83     }
84     auto pipeline = PipelineBase::GetCurrentContext();
85     CHECK_NULL_VOID(pipeline);
86     auto progressTheme = pipeline->GetTheme<ProgressTheme>();
87     CHECK_NULL_VOID(progressTheme);
88     auto capsuleStyle = JsonUtil::Create(true);
89     auto fontStyle = JsonUtil::Create(true);
90     auto font = JsonUtil::Create(true);
91     capsuleStyle->Put("borderWidth", (GetBorderWidth().value_or(progressTheme->GetBorderWidth())).ToString().c_str());
92     capsuleStyle->Put(
93         "borderColor", (GetBorderColor().value_or(progressTheme->GetBorderColor())).ColorToString().c_str());
94     capsuleStyle->Put("fontColor", (GetTextColor().value_or(progressTheme->GetTextColor())).ColorToString().c_str());
95     capsuleStyle->Put("content", (GetText().value_or("")).c_str());
96     capsuleStyle->Put("enableScanEffect", (GetEnableScanEffect().value_or(false)) ? "true" : "false");
97     capsuleStyle->Put("showDefaultPercentage", (GetEnableShowText().value_or(false)) ? "true" : "false");
98     font->Put("size", (GetTextSize().value_or(progressTheme->GetTextSize())).ToString().c_str());
99     font->Put("style", GetItalicFontStyle().value_or(Ace::FontStyle::NORMAL) == Ace::FontStyle::NORMAL
100                            ? "FontStyle.Normal"
101                            : "FontStyle.Italic");
102     font->Put("weight", V2::ConvertWrapFontWeightToStirng(GetFontWeight().value_or(FontWeight::NORMAL)).c_str());
103     std::vector<std::string> defaultFamily = { "Sans" };
104     std::vector<std::string> fontFamilyVector = GetFontFamily().value_or(defaultFamily);
105     if (fontFamilyVector.empty()) {
106         fontFamilyVector = defaultFamily;
107     }
108     std::string fontFamily = fontFamilyVector.at(0);
109     for (uint32_t i = 1; i < fontFamilyVector.size(); ++i) {
110         fontFamily += ',' + fontFamilyVector.at(i);
111     }
112     font->Put("family", fontFamily.c_str());
113     capsuleStyle->Put("font", font);
114     json->PutExtAttr("capsuleStyle", capsuleStyle, filter);
115 }
116 
ToJsonGradientColor() const117 std::string ProgressPaintProperty::ToJsonGradientColor() const
118 {
119     Gradient colors;
120     if (propGradientColor_.has_value()) {
121         colors = propGradientColor_.value();
122     } else {
123         auto pipelineContext = PipelineBase::GetCurrentContext();
124         CHECK_NULL_RETURN(pipelineContext, "");
125         auto theme = pipelineContext->GetTheme<ProgressTheme>();
126         auto endColor = theme->GetRingProgressEndSideColor();
127         auto beginColor = theme->GetRingProgressBeginSideColor();
128         GradientColor gradientColorEnd;
129         gradientColorEnd.SetLinearColor(LinearColor(endColor));
130         gradientColorEnd.SetDimension(Dimension(0.0f));
131         colors.AddColor(gradientColorEnd);
132         GradientColor gradientColorBegin;
133         gradientColorBegin.SetLinearColor(LinearColor(beginColor));
134         gradientColorBegin.SetDimension(Dimension(1.0f));
135         colors.AddColor(gradientColorBegin);
136     }
137 
138     auto jsonArray = JsonUtil::CreateArray(true);
139     for (size_t index = 0; index < colors.GetColors().size(); ++index) {
140         auto gradientColor = colors.GetColors()[index];
141         auto gradientColorJson = JsonUtil::Create(true);
142         gradientColorJson->Put("color", gradientColor.GetLinearColor().ToColor().ColorToString().c_str());
143         gradientColorJson->Put("offset", std::to_string(gradientColor.GetDimension().Value()).c_str());
144         jsonArray->Put(std::to_string(index).c_str(), gradientColorJson);
145     }
146     return jsonArray->ToString();
147 }
148 } // namespace OHOS::Ace::NG
149