1 /*
2  * Copyright (c) 2022-2023 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/indexer/indexer_layout_property.h"
17 #include <memory>
18 #include <vector>
19 #include "base/json/json_util.h"
20 #include "core/components/indexer/indexer_theme.h"
21 #include "core/components_ng/base/inspector_filter.h"
22 #include "core/components_v2/inspector/utils.h"
23 #include "core/pipeline_ng/pipeline_context.h"
24 
25 namespace OHOS::Ace::NG {
26 namespace {
27 constexpr float DEFAULT_SIZE = 12.0f;
28 }
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const29 void IndexerLayoutProperty::ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
30 {
31     LayoutProperty::ToJsonValue(json, filter);
32     /* no fixed attr below, just return */
33     if (filter.IsFastFilter()) {
34         return;
35     }
36     json->PutExtAttr("selected", std::to_string(propSelected_.value_or(0)).c_str(), filter);
37     json->PutExtAttr("color", propColor_.value_or(Color::WHITE).ColorToString().c_str(), filter);
38     json->PutExtAttr("selectedColor", propSelectedColor_.value_or(Color::WHITE).ColorToString().c_str(), filter);
39     json->PutExtAttr("popupColor", propPopupColor_.value_or(Color::WHITE).ColorToString().c_str(), filter);
40     json->PutExtAttr("usingPopup", propUsingPopup_.value_or(false) ? "true" : "false", filter);
41     json->PutExtAttr("itemSize",
42         propItemSize_.value_or(Dimension(0, DimensionUnit::VP)).ToString().c_str(), filter);
43     std::string alignStyle = AlignStyleToString(propAlignStyle_.value_or(AlignStyle::END));
44     json->PutExtAttr("alignStyle", alignStyle.c_str(), filter);
45     auto PopupPositionJsonObject = JsonUtil::Create(true);
46     PopupPositionJsonObject->Put("popupPositionX", propPopupPositionX_.value_or(Dimension()).ToString().c_str());
47     PopupPositionJsonObject->Put("popupPositionY", propPopupPositionY_.value_or(Dimension()).ToString().c_str());
48     json->PutExtAttr("popupPosition", PopupPositionJsonObject, filter);
49     auto jsonArrayValue = JsonUtil::CreateArray(true);
50     auto arrayValue = propArrayValue_.value_or(std::vector<std::string>());
51     for (uint32_t i = 0; i < arrayValue.size(); i++) {
52         auto index = std::to_string(i);
53         jsonArrayValue->Put(index.c_str(), arrayValue[i].c_str());
54     }
55     json->PutExtAttr("arrayValue", jsonArrayValue, filter);
56     auto defaultFont = TextStyle();
57     defaultFont.SetFontStyle(FontStyle::NORMAL);
58     defaultFont.SetFontSize(Dimension(DEFAULT_SIZE, DimensionUnit::FP));
59     defaultFont.SetFontWeight(FontWeight::REGULAR);
60     auto fontFamily = std::vector<std::string>();
61     defaultFont.SetFontFamilies(fontFamily);
62     json->PutExtAttr("font", ToJsonObjectValue(propFont_.value_or(defaultFont)), filter);
63     json->PutExtAttr("selectedFont", ToJsonObjectValue(propSelectedFont_.value_or(defaultFont)), filter);
64     json->PutExtAttr("popupFont", ToJsonObjectValue(propPopupFont_.value_or(defaultFont)), filter);
65     auto pipeline = PipelineContext::GetCurrentContext();
66     CHECK_NULL_VOID(pipeline);
67     auto indexerTheme = pipeline->GetTheme<IndexerTheme>();
68     CHECK_NULL_VOID(indexerTheme);
69     auto defaultFontSize = indexerTheme->GetPopupTextStyle().GetFontSize();
70     auto defaultFontWeight = indexerTheme->GetPopupTextStyle().GetFontWeight();
71     json->PutExtAttr("popupItemFontSize", propFontSize_.value_or(defaultFontSize).ToString().c_str(), filter);
72     json->PutExtAttr("popupItemFontWeight",
73         V2::ConvertWrapFontWeightToStirng(propFontWeight_.value_or(defaultFontWeight)).c_str(), filter);
74     json->PutExtAttr("autoCollapse", propAutoCollapse_.value_or(false) ? "true" : "false", filter);
75     json->PutExtAttr("popupHorizontalSpace", propPopupHorizontalSpace_.value_or(
76         Dimension(NG::INDEXER_BUBBLE_INVALID_SPACE, DimensionUnit::VP)).ToString().c_str(), filter);
77     json->PutExtAttr("adaptiveWidth", propAdaptiveWidth_.value_or(false) ? "true" : "false", filter);
78     json->PutExtAttr("enableHapticFeedback", propEnableHapticFeedback_.value_or(true) ? "true" : "false", filter);
79 }
80 
ToJsonObjectValue(const TextStyle & textStyle)81 std::unique_ptr<JsonValue> IndexerLayoutProperty::ToJsonObjectValue(const TextStyle& textStyle)
82 {
83     auto fontJsonObject = JsonUtil::Create(true);
84     fontJsonObject->Put("fontSize", textStyle.GetFontSize().ToString().c_str());
85     fontJsonObject->Put(
86         "fontStyle", textStyle.GetFontStyle() == FontStyle::NORMAL ? "FontStyle::NORMAL" : "FontStyle::ITALIC");
87     fontJsonObject->Put("fontWeight", V2::ConvertWrapFontWeightToStirng(textStyle.GetFontWeight()).c_str());
88     auto fontFamilyVector = textStyle.GetFontFamilies();
89     std::string fontFamily;
90     for (uint32_t i = 0; i < fontFamilyVector.size(); i++) {
91         if (i == (fontFamilyVector.size() - 1)) {
92             fontFamily += fontFamilyVector.at(i);
93             break;
94         }
95         fontFamily += fontFamilyVector.at(i) + ",";
96     }
97     fontJsonObject->Put("fontFamily", fontFamily.c_str());
98     return fontJsonObject;
99 }
100 
AlignStyleToString(const AlignStyle & alignStyle)101 std::string IndexerLayoutProperty::AlignStyleToString(const AlignStyle& alignStyle)
102 {
103     std::string alignStyleStr;
104     switch (alignStyle) {
105         case AlignStyle::LEFT:
106             alignStyleStr = "IndexerAlign.Left";
107             break;
108         case AlignStyle::RIGHT:
109             alignStyleStr = "IndexerAlign.Right";
110             break;
111         case AlignStyle::START:
112             alignStyleStr = "IndexerAlign.Start";
113             break;
114         case AlignStyle::END:
115             alignStyleStr = "IndexerAlign.End";
116             break;
117         default:
118             break;
119     }
120     return alignStyleStr;
121 }
122 
123 
124 } // namespace OHOS::Ace::NG
125