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_v2/inspector/text_clock_composed_element.h"
17 
18 #include <unordered_map>
19 
20 #include "base/log/dump_log.h"
21 #include "core/components/common/layout/constants.h"
22 #include "core/components/text/text_element.h"
23 #include "core/components_v2/inspector/utils.h"
24 
25 namespace OHOS::Ace::V2 {
26 namespace {
27 
28 const std::unordered_map<std::string, std::function<std::string(const TextClockComposedElement&)>> CREATE_JSON_MAP {
__anon0c44ad4c0202(const TextClockComposedElement& inspector) 29     { "timeZoneOffset", [](const TextClockComposedElement& inspector) { return inspector.GetTimeZoneOffset(); } },
__anon0c44ad4c0302(const TextClockComposedElement& inspector) 30     { "format", [](const TextClockComposedElement& inspector) { return inspector.GeFormat(); } },
__anon0c44ad4c0402(const TextClockComposedElement& inspector) 31     { "fontColor", [](const TextClockComposedElement& inspector) { return inspector.GetTextFontColor(); } },
__anon0c44ad4c0502(const TextClockComposedElement& inspector) 32     { "fontSize", [](const TextClockComposedElement& inspector) { return inspector.GetTextFontSize(); } },
__anon0c44ad4c0602(const TextClockComposedElement& inspector) 33     { "fontStyle", [](const TextClockComposedElement& inspector) { return inspector.GetTextFontStyle(); } },
__anon0c44ad4c0702(const TextClockComposedElement& inspector) 34     { "fontWeight", [](const TextClockComposedElement& inspector) { return inspector.GetTextFontWeight(); } },
__anon0c44ad4c0802(const TextClockComposedElement& inspector) 35     { "fontFamily", [](const TextClockComposedElement& inspector) { return inspector.GetTextFontFamily(); } },
36 };
37 
38 } // namespace
39 
Dump()40 void TextClockComposedElement::Dump()
41 {
42     InspectorComposedElement::Dump();
43     DumpLog::GetInstance().AddDesc(std::string("timeZoneOffset: ").append(GetTimeZoneOffset()));
44     DumpLog::GetInstance().AddDesc(std::string("format: ").append(GeFormat()));
45     DumpLog::GetInstance().AddDesc(std::string("fontColor: ").append(GetTextFontColor()));
46     DumpLog::GetInstance().AddDesc(std::string("fontSize: ").append(GetTextFontSize()));
47     DumpLog::GetInstance().AddDesc(std::string("fontStyle: ").append(GetTextFontStyle()));
48     DumpLog::GetInstance().AddDesc(std::string("fontWeight: ").append(GetTextFontWeight()));
49     DumpLog::GetInstance().AddDesc(std::string("fontFamily: ").append(GetTextFontFamily()));
50 }
51 
ToJsonObject() const52 std::unique_ptr<JsonValue> TextClockComposedElement::ToJsonObject() const
53 {
54     auto resultJson = InspectorComposedElement::ToJsonObject();
55     for (const auto& value : CREATE_JSON_MAP) {
56         resultJson->Put(value.first.c_str(), value.second(*this).c_str());
57     }
58     return resultJson;
59 }
60 
GetTimeZoneOffset() const61 std::string TextClockComposedElement::GetTimeZoneOffset() const
62 {
63     auto render = GetRenderTextClock();
64     if (render) {
65         double timeZoneOffset = render->GetHoursWest();
66         return std::to_string(timeZoneOffset);
67     }
68     return "";
69 }
70 
GeFormat() const71 std::string TextClockComposedElement::GeFormat() const
72 {
73     auto render = GetRenderTextClock();
74     if (render) {
75         return render->GeFormat();
76     }
77     return "";
78 }
79 
GetRenderTextClock() const80 RefPtr<RenderTextClock> TextClockComposedElement::GetRenderTextClock() const
81 {
82     auto node = GetInspectorNode(TextClockElement::TypeId());
83     if (node) {
84         return AceType::DynamicCast<RenderTextClock>(node);
85     }
86     return nullptr;
87 }
88 
GetTextFontColor() const89 std::string TextClockComposedElement::GetTextFontColor() const
90 {
91     auto renderText = GetRenderText();
92     auto fontColor =
93         renderText ? renderText->GetTextStyle().GetTextColor() : Color::BLACK;
94     return fontColor.ColorToString();
95 }
96 
GetTextFontSize() const97 std::string TextClockComposedElement::GetTextFontSize() const
98 {
99     auto renderText = GetRenderText();
100     auto fontSize =
101         renderText ? renderText->GetTextStyle().GetFontSize() : Dimension();
102     return fontSize.ToString();
103 }
104 
GetTextFontStyle() const105 std::string TextClockComposedElement::GetTextFontStyle() const
106 {
107     auto renderText = GetRenderText();
108     auto fontStyle =
109         renderText ? renderText->GetTextStyle().GetFontStyle() : FontStyle::NORMAL;
110     return ConvertWrapFontStyleToStirng(fontStyle);
111 }
112 
GetTextFontWeight() const113 std::string TextClockComposedElement::GetTextFontWeight() const
114 {
115     auto renderText = GetRenderText();
116     auto fontWeight =
117         renderText ? renderText->GetTextStyle().GetFontWeight() : FontWeight::NORMAL;
118     return ConvertWrapFontWeightToStirng(fontWeight);
119 }
120 
GetTextFontFamily() const121 std::string TextClockComposedElement::GetTextFontFamily() const
122 {
123     auto renderText = GetRenderText();
124     auto fontFamily = renderText ? renderText->GetTextStyle().GetFontFamilies() : std::vector<std::string>();
125     return ConvertFontFamily(fontFamily);
126 }
127 
ConvertFontFamily(const std::vector<std::string> & fontFamily) const128 std::string TextClockComposedElement::ConvertFontFamily(const std::vector<std::string>& fontFamily) const
129 {
130     std::string result = "";
131     for (const auto& item : fontFamily) {
132         result += item;
133         result += ",";
134     }
135     if (!result.empty()) {
136         result = result.substr(0, result.size() - 1);
137     }
138     return result;
139 }
140 
GetRenderText() const141 RefPtr<RenderText> TextClockComposedElement::GetRenderText() const
142 {
143     auto renderTextClock = GetRenderTextClock();
144     if (renderTextClock) {
145         return renderTextClock->GetRenderText();
146     }
147     return nullptr;
148 }
149 
150 } // namespace OHOS::Ace::V2