• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_LINEAR_LAYOUT_LINEAR_LAYOUT_PROPERTY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_LINEAR_LAYOUT_LINEAR_LAYOUT_PROPERTY_H
18 
19 #include "base/geometry/dimension.h"
20 #include "core/components/common/layout/constants.h"
21 #include "core/components_ng/layout/layout_property.h"
22 #include "core/components_ng/pattern/flex/flex_layout_property.h"
23 #include "core/components_ng/property/flex_property.h"
24 #include "core/components_ng/property/property.h"
25 #include "core/components_v2/inspector/utils.h"
26 
27 namespace OHOS::Ace::NG {
28 class ACE_EXPORT LinearLayoutProperty : public FlexLayoutProperty {
29     DECLARE_ACE_TYPE(LinearLayoutProperty, FlexLayoutProperty);
30 
31 public:
LinearLayoutProperty(bool isVertical)32     explicit LinearLayoutProperty(bool isVertical) : isVertical_(isVertical)
33     {
34         UpdateFlexDirection(isVertical_ ? FlexDirection::COLUMN : FlexDirection::ROW);
35     }
36 
37     ~LinearLayoutProperty() override = default;
38 
Clone()39     RefPtr<LayoutProperty> Clone() const override
40     {
41         auto value = MakeRefPtr<LinearLayoutProperty>(isVertical_);
42         Clone(value);
43         return value;
44     }
45 
Reset()46     void Reset() override
47     {
48         FlexLayoutProperty::Reset();
49         isVertical_ = false;
50     }
51 
SetIsVertical(bool isVertical)52     void SetIsVertical(bool isVertical)
53     {
54         isVertical_ = isVertical;
55     }
56 
IsVertical()57     bool IsVertical() const
58     {
59         return isVertical_;
60     }
61 
ToJsonValue(std::unique_ptr<JsonValue> & json)62     void ToJsonValue(std::unique_ptr<JsonValue>& json) const override
63     {
64         LayoutProperty::ToJsonValue(json);
65         std::string alignItems;
66         auto flexAlignItems = GetCrossAxisAlign().value_or(FlexAlign::CENTER);
67         if (isVertical_) {
68             alignItems = "HorizontalAlign.Center";
69             if (flexAlignItems == FlexAlign::FLEX_START) {
70                 alignItems = "HorizontalAlign.Start";
71             } else if (flexAlignItems == FlexAlign::CENTER) {
72                 alignItems = "HorizontalAlign.Center";
73             } else if (flexAlignItems == FlexAlign::FLEX_END) {
74                 alignItems = "HorizontalAlign.End";
75             }
76         } else {
77             alignItems = "VerticalAlign.Center";
78             if (flexAlignItems == FlexAlign::FLEX_START) {
79                 alignItems = "VerticalAlign.Top";
80             } else if (flexAlignItems == FlexAlign::CENTER) {
81                 alignItems = "VerticalAlign.Center";
82             } else if (flexAlignItems == FlexAlign::FLEX_END) {
83                 alignItems = "VerticalAlign.Bottom";
84             }
85         }
86         json->Put("space", GetSpaceValue(Dimension(0.0f)).ToString().c_str());
87         json->Put("alignItems", alignItems.c_str());
88         auto justifyContent = V2::ConvertFlexAlignToStirng(GetMainAxisAlign().value_or(FlexAlign::FLEX_START));
89         json->Put("justifyContent", justifyContent.c_str());
90     }
91 
FromJson(const std::unique_ptr<JsonValue> & json)92     void FromJson(const std::unique_ptr<JsonValue>& json) override
93     {
94         static const std::unordered_map<std::string, FlexAlign> uMap {
95             { "Start", FlexAlign::FLEX_START },
96             { "Top", FlexAlign::FLEX_START },
97             { "Center", FlexAlign::CENTER },
98             { "End", FlexAlign::FLEX_END },
99             { "Bottom", FlexAlign::FLEX_END },
100         };
101 
102         UpdateSpace(Dimension::FromString(json->GetString("space")));
103         auto alignItems = json->GetString("alignItems");
104         auto pos = alignItems.find('.');
105         LOGD("UITree alignItems=%{public}s", alignItems.c_str());
106         if (pos != std::string::npos) {
107             ++pos;
108             alignItems = alignItems.substr(pos, alignItems.length() - pos);
109             UpdateCrossAxisAlign(uMap.count(alignItems) ? uMap.at(alignItems) : FlexAlign::CENTER);
110         } else {
111             LOGE("UITree |ERROR| invalid %{public}s", alignItems.c_str());
112         }
113         UpdateMainAxisAlign(V2::ConvertStringToFlexAlign(json->GetString("justifyContent")));
114 
115         LayoutProperty::FromJson(json);
116     }
117 
118 protected:
Clone(RefPtr<LayoutProperty> property)119     void Clone(RefPtr<LayoutProperty> property) const override
120     {
121         auto value = DynamicCast<LinearLayoutProperty>(property);
122         FlexLayoutProperty::Clone(value);
123         value->isVertical_ = isVertical_;
124     }
125 
126 private:
127     // This will call after ModifyLayoutConstraint.
128     bool isVertical_ = false;
129 
130     ACE_DISALLOW_COPY_AND_MOVE(LinearLayoutProperty);
131 };
132 } // namespace OHOS::Ace::NG
133 
134 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_LAYOUTS_LAYOUT_PROPERTY_H
135