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/list/list_layout_property.h"
17 
18 #include "base/utils/string_utils.h"
19 #include "core/components_ng/base/inspector_filter.h"
20 #include "core/components_v2/list/list_properties.h"
21 
22 namespace OHOS::Ace::NG {
23 namespace {
ItemDividerFromJson(const std::unique_ptr<JsonValue> & json)24 V2::ItemDivider ItemDividerFromJson(const std::unique_ptr<JsonValue>& json)
25 {
26     V2::ItemDivider divider;
27     divider.strokeWidth = Dimension::FromString(json->GetString("strokeWidth"));
28     divider.startMargin = Dimension::FromString(json->GetString("startMargin"));
29     divider.endMargin = Dimension::FromString(json->GetString("endMargin"));
30     divider.color = Color::ColorFromString(json->GetString("color"));
31     return divider;
32 }
33 } // namespace
34 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const35 void ListLayoutProperty::ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
36 {
37     LayoutProperty::ToJsonValue(json, filter);
38     /* no fixed attr below, just return */
39     if (filter.IsFastFilter()) {
40         ScrollSnapPropToJsonValue(json, filter);
41         return;
42     }
43     json->PutExtAttr("space", propSpace_.value_or(Dimension(0, DimensionUnit::VP)).ToString().c_str(), filter);
44     json->PutExtAttr("contentStartOffset", std::to_string(propContentStartOffset_.value_or(0)).c_str(), filter);
45     json->PutExtAttr("contentEndOffset", std::to_string(propContentEndOffset_.value_or(0)).c_str(), filter);
46     json->PutExtAttr("initialIndex", std::to_string(propInitialIndex_.value_or(0)).c_str(), filter);
47     json->PutExtAttr("listDirection", propListDirection_.value_or(Axis::VERTICAL) == Axis::VERTICAL
48                                    ? "Axis.Vertical" : "Axis.Horizontal", filter);
49     json->PutExtAttr("editMode", propEditMode_.value_or(false), filter);
50     json->PutExtAttr("chainAnimation", propChainAnimation_.value_or(false), filter);
51     auto divider = JsonUtil::Create(true);
52     if (propDivider_.has_value()) {
53         divider->Put("strokeWidth", propDivider_.value().strokeWidth.ToString().c_str());
54         divider->Put("startMargin", propDivider_.value().startMargin.ToString().c_str());
55         divider->Put("endMargin", propDivider_.value().endMargin.ToString().c_str());
56         divider->Put("color", propDivider_.value().color.ColorToString().c_str());
57     }
58     json->PutExtAttr("divider", divider, filter);
59     json->PutExtAttr("lanes", std::to_string(propLanes_.value_or(0)).c_str(), filter);
60     json->PutExtAttr("laneMinLength",
61         propLaneMinLength_.value_or(Dimension(0, DimensionUnit::VP)).ToString().c_str(), filter);
62     json->PutExtAttr("laneMaxLength",
63         propLaneMaxLength_.value_or(Dimension(0, DimensionUnit::VP)).ToString().c_str(), filter);
64     json->PutExtAttr("laneGutter",
65         propLaneGutter_.value_or(Dimension(0, DimensionUnit::VP)).ToString().c_str(), filter);
66     if (propListItemAlign_.value_or(V2::ListItemAlign::START) == V2::ListItemAlign::START) {
67         json->PutExtAttr("alignListItem", "ListItemAlign.Start", filter);
68     } else if (propListItemAlign_.value_or(V2::ListItemAlign::START) == V2::ListItemAlign::CENTER) {
69         json->PutExtAttr("alignListItem", "ListItemAlign.Center", filter);
70     } else {
71         json->PutExtAttr("alignListItem", "ListItemAlign.End", filter);
72     }
73     json->PutExtAttr("cachedCount", std::to_string(propCachedCount_.value_or(0)).c_str(), filter);
74     auto sticky = propStickyStyle_.value_or(V2::StickyStyle::NONE);
75     if (sticky == V2::StickyStyle::HEADER) {
76         json->PutExtAttr("sticky", "StickyStyle.Header", filter);
77     } else if (sticky == V2::StickyStyle::FOOTER) {
78         json->PutExtAttr("sticky", "StickyStyle.Footer", filter);
79     } else if (sticky == V2::StickyStyle::BOTH) {
80         json->PutExtAttr("sticky", "StickyStyle.Header | StickyStyle.Footer", filter);
81     } else {
82         json->PutExtAttr("sticky", "StickyStyle.None", filter);
83     }
84     ScrollSnapPropToJsonValue(json, filter);
85 }
86 
ScrollSnapPropToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const87 void ListLayoutProperty::ScrollSnapPropToJsonValue(
88     std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
89 {
90     /* no fixed attr below, just return */
91     if (filter.IsFastFilter()) {
92         return;
93     }
94     auto scrollSnapAlign = propScrollSnapAlign_.value_or(V2::ScrollSnapAlign::NONE);
95     if (scrollSnapAlign == V2::ScrollSnapAlign::START) {
96         json->PutExtAttr("scrollSnapAlign", "ScrollSnapAlign.START", filter);
97     } else if (scrollSnapAlign == V2::ScrollSnapAlign::CENTER) {
98         json->PutExtAttr("scrollSnapAlign", "ScrollSnapAlign.CENTER", filter);
99     } else if (scrollSnapAlign == V2::ScrollSnapAlign::END) {
100         json->PutExtAttr("scrollSnapAlign", "ScrollSnapAlign.END", filter);
101     } else {
102         json->PutExtAttr("scrollSnapAlign", "ScrollSnapAlign.NONE", filter);
103     }
104     json->PutExtAttr("enableScrollInteraction", propScrollEnabled_.value_or(true), filter);
105 }
106 
FromJson(const std::unique_ptr<JsonValue> & json)107 void ListLayoutProperty::FromJson(const std::unique_ptr<JsonValue>& json)
108 {
109     UpdateSpace(Dimension::FromString(json->GetString("space")));
110     UpdateInitialIndex(StringUtils::StringToInt(json->GetString("initialIndex")));
111     auto dividerJson = json->GetObject("divider");
112     if (dividerJson->Contains("strokeWidth")) {
113         UpdateDivider(ItemDividerFromJson(dividerJson));
114     }
115     LayoutProperty::FromJson(json);
116 }
117 }
118