1 /*
2 * Copyright (c) 2021-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_v2/inspector/texttimer_composed_element.h"
17
18 #include "base/log/dump_log.h"
19 #include "core/components/common/layout/constants.h"
20 #include "core/components/text_field/text_field_element.h"
21 #include "core/components_v2/inspector/utils.h"
22
23 namespace OHOS::Ace::V2 {
24
25 namespace {
26
27 const std::unordered_map<std::string, std::function<std::string(const TextTimerComposedElement&)>> CREATE_JSON_MAP {
__anon977ee0c20202(const TextTimerComposedElement& inspector) 28 { "isCountDown", [](const TextTimerComposedElement& inspector) { return inspector.GetIsCountDown(); } },
__anon977ee0c20302(const TextTimerComposedElement& inspector) 29 { "count", [](const TextTimerComposedElement& inspector) { return inspector.GetCount(); } },
__anon977ee0c20402(const TextTimerComposedElement& inspector) 30 { "format", [](const TextTimerComposedElement& inspector) { return inspector.GetFormat(); } },
__anon977ee0c20502(const TextTimerComposedElement& inspector) 31 { "fontColor", [](const TextTimerComposedElement& inspector) { return inspector.GetFontColor(); } },
__anon977ee0c20602(const TextTimerComposedElement& inspector) 32 { "fontSize", [](const TextTimerComposedElement& inspector) { return inspector.GetFontSize(); } },
__anon977ee0c20702(const TextTimerComposedElement& inspector) 33 { "fontWeight", [](const TextTimerComposedElement& inspector) { return inspector.GetFontWeight(); } },
__anon977ee0c20802(const TextTimerComposedElement& inspector) 34 { "fontStyle", [](const TextTimerComposedElement& inspector) { return inspector.GetFontStyle(); } },
__anon977ee0c20902(const TextTimerComposedElement& inspector) 35 { "fontfamily", [](const TextTimerComposedElement& inspector) { return inspector.GetFontFamily(); } },
36 };
37
38 } // namespace
39
Dump()40 void TextTimerComposedElement::Dump()
41 {
42 InspectorComposedElement::Dump();
43 DumpLog::GetInstance().AddDesc(std::string("isCountDown: ").append(GetIsCountDown()));
44 DumpLog::GetInstance().AddDesc(std::string("count: ").append(GetCount()));
45 DumpLog::GetInstance().AddDesc(std::string("format: ").append(GetFormat()));
46 DumpLog::GetInstance().AddDesc(std::string("fontColor : ").append(GetFontColor()));
47 DumpLog::GetInstance().AddDesc(std::string("fontSize : ").append(GetFontSize()));
48 DumpLog::GetInstance().AddDesc(std::string("fontWeight : ").append(GetFontWeight()));
49 DumpLog::GetInstance().AddDesc(std::string("fontStyle : ").append(GetFontSize()));
50 DumpLog::GetInstance().AddDesc(std::string("fontFamily : ").append(GetFontSize()));
51 }
52
ToJsonObject() const53 std::unique_ptr<JsonValue> TextTimerComposedElement::ToJsonObject() const
54 {
55 auto resultJson = InspectorComposedElement::ToJsonObject();
56 for (const auto& value : CREATE_JSON_MAP) {
57 resultJson->Put(value.first.c_str(), value.second(*this).c_str());
58 }
59 return resultJson;
60 }
61
GetIsCountDown() const62 std::string TextTimerComposedElement::GetIsCountDown() const
63 {
64 auto render = GetRenderTextTimer();
65 if (render) {
66 return ConvertBoolToString(render->GetIsCountDown());
67 }
68 return "false";
69 }
70
GetCount() const71 std::string TextTimerComposedElement::GetCount() const
72 {
73 auto render = GetRenderTextTimer();
74 if (render) {
75 return std::to_string(render->GetCount());
76 }
77 return "60000";
78 }
79
GetFormat() const80 std::string TextTimerComposedElement::GetFormat() const
81 {
82 auto render = GetRenderTextTimer();
83 if (render) {
84 return render->GetFormat();
85 }
86 return "HH:mm:ss.SS";
87 }
88
GetFontColor() const89 std::string TextTimerComposedElement::GetFontColor() const
90 {
91 auto render = GetRenderTextTimer();
92 auto fontColor =
93 render ? render->GetTextStyle().GetTextColor() : Color::BLACK;
94 return fontColor.ColorToString();
95 }
GetFontSize() const96 std::string TextTimerComposedElement::GetFontSize() const
97 {
98 auto render = GetRenderTextTimer();
99 auto fontSize =
100 render ? render->GetTextStyle().GetFontSize() : Dimension();
101 return fontSize.ToString();
102 }
103
GetFontWeight() const104 std::string TextTimerComposedElement::GetFontWeight() const
105 {
106 auto render = GetRenderTextTimer();
107 auto fontWeight =
108 render ? render->GetTextStyle().GetFontWeight() : FontWeight::NORMAL;
109 return ConvertWrapFontWeightToStirng(fontWeight);
110 }
111
GetFontStyle() const112 std::string TextTimerComposedElement::GetFontStyle() const
113 {
114 auto render = GetRenderTextTimer();
115 auto fontStyle =
116 render ? render->GetTextStyle().GetFontStyle() : FontStyle::NORMAL;
117 return ConvertWrapFontStyleToStirng(fontStyle);
118 }
119
GetFontFamily() const120 std::string TextTimerComposedElement::GetFontFamily() const
121 {
122 auto render = GetRenderTextTimer();
123 auto fontFamily = render ? render->GetTextStyle().GetFontFamilies() : std::vector<std::string>();
124 return ConvertFontFamily(fontFamily);
125 }
126
ConvertFontFamily(const std::vector<std::string> & fontFamily) const127 std::string TextTimerComposedElement::ConvertFontFamily(const std::vector<std::string>& fontFamily) const
128 {
129 std::string result = "";
130 for (const auto& item : fontFamily) {
131 result += item;
132 result += ",";
133 }
134 if (!result.empty()) {
135 result = result.substr(0, result.size() - 1);
136 }
137 return result;
138 }
139
GetRenderTextTimer() const140 RefPtr<RenderTextTimer> TextTimerComposedElement::GetRenderTextTimer() const
141 {
142 auto node = GetInspectorNode(TextTimerElement::TypeId());
143 if (node) {
144 return AceType::DynamicCast<RenderTextTimer>(node);
145 }
146 return nullptr;
147 }
148 } // namespace OHOS::Ace::V2