• 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_FLEX_FLEX_LAYOUT_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_FLEX_FLEX_LAYOUT_PATTERN_H
18 
19 #include <string>
20 
21 #include "base/log/dump_log.h"
22 #include "core/components_ng/pattern/flex/flex_layout_algorithm.h"
23 #include "core/components_ng/pattern/flex/flex_layout_property.h"
24 #include "core/components_ng/pattern/flex/wrap_layout_algorithm.h"
25 #include "core/components_ng/pattern/pattern.h"
26 
27 namespace OHOS::Ace::NG {
28 class FlexLayoutPattern : public Pattern {
29     DECLARE_ACE_TYPE(FlexLayoutPattern, Pattern);
30 
31 public:
32     FlexLayoutPattern() = default;
FlexLayoutPattern(bool wrap)33     explicit FlexLayoutPattern(bool wrap) : isWrap_(wrap) {};
34     ~FlexLayoutPattern() override = default;
35 
CreateLayoutProperty()36     RefPtr<LayoutProperty> CreateLayoutProperty() override
37     {
38         return MakeRefPtr<FlexLayoutProperty>();
39     }
40 
CreateLayoutAlgorithm()41     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
42     {
43         if (isWrap_) {
44             return MakeRefPtr<WrapLayoutAlgorithm>(isDialogStretch_);
45         }
46         return MakeRefPtr<FlexLayoutAlgorithm>();
47     }
48 
IsAtomicNode()49     bool IsAtomicNode() const override
50     {
51         return false;
52     }
53 
GetFocusPattern()54     FocusPattern GetFocusPattern() const override
55     {
56         return { FocusType::SCOPE, true };
57     }
58 
GetScopeFocusAlgorithm()59     ScopeFocusAlgorithm GetScopeFocusAlgorithm() override
60     {
61         auto property = GetLayoutProperty<FlexLayoutProperty>();
62         if (!property) {
63             return {};
64         }
65         bool isVertical = false;
66         if (property->GetFlexDirection().has_value()) {
67             isVertical = property->GetFlexDirection().value() == FlexDirection::COLUMN ||
68                          property->GetFlexDirection().value() == FlexDirection::COLUMN_REVERSE;
69         }
70         return { isVertical, true, isWrap_ ? ScopeType::PROJECT_AREA : ScopeType::FLEX };
71     }
72 
SetDialogStretch(bool stretch)73     void SetDialogStretch(bool stretch)
74     {
75         isDialogStretch_ = stretch;
76     }
77 
DumpInfo()78     void DumpInfo() override
79     {
80         DumpLog::GetInstance().AddDesc(std::string("Type: ").append(isWrap_ ? "Wrap" : "NoWrap"));
81     }
82 
GetIsWrap()83     bool GetIsWrap() const
84     {
85         return isWrap_;
86     }
87 
SetIsWrap(bool isWrap)88     void SetIsWrap(bool isWrap)
89     {
90         isWrap_ = isWrap;
91     }
92 
ToJsonValue(std::unique_ptr<JsonValue> & json)93     void ToJsonValue(std::unique_ptr<JsonValue>& json) const override
94     {
95         auto property = GetLayoutProperty<FlexLayoutProperty>();
96         CHECK_NULL_VOID(property);
97         auto jsonConstructor = JsonUtil::Create(true);
98         auto direction = property->GetFlexDirection().value_or(FlexDirection::ROW);
99         jsonConstructor->Put("direction", V2::ConvertFlexDirectionToStirng(direction).c_str());
100         if (!isWrap_) {
101             jsonConstructor->Put("wrap", "FlexWrap.NoWrap");
102             jsonConstructor->Put("justifyContent",
103                 V2::ConvertFlexAlignToStirng(property->GetMainAxisAlign().value_or(FlexAlign::FLEX_START)).c_str());
104             jsonConstructor->Put("alignItems",
105                 V2::ConvertItemAlignToStirng(property->GetCrossAxisAlign().value_or(FlexAlign::FLEX_START)).c_str());
106             jsonConstructor->Put("alignContent", "FlexAlign.Start");
107         } else {
108             auto wrapDirection = property->GetWrapDirection().value_or(WrapDirection::HORIZONTAL);
109             if (static_cast<int32_t>(direction) <= 1) {
110                 auto wrap = (static_cast<int32_t>(wrapDirection) - static_cast<int32_t>(direction)) / 2 + 1;
111                 jsonConstructor->Put("wrap", wrap == 1 ? "FlexWrap.Wrap" : "FlexWrap.WrapReverse");
112             } else {
113                 auto wrap = (static_cast<int32_t>(wrapDirection) + static_cast<int32_t>(direction)) / 2 + 1;
114                 jsonConstructor->Put("wrap", wrap == 1 ? "FlexWrap.Wrap" : "FlexWrap.WrapReverse");
115             }
116             jsonConstructor->Put("justifyContent",
117                 V2::ConvertWrapAlignmentToStirng(property->GetMainAlignment().value_or(WrapAlignment::START)).c_str());
118             jsonConstructor->Put("alignItems",
119                 V2::ConvertWrapAlignmentToStirng(property->GetCrossAlignment().value_or(WrapAlignment::START)).c_str());
120             jsonConstructor->Put("alignContent",
121                 V2::ConvertWrapAlignmentToStirng(property->GetAlignment().value_or(WrapAlignment::START)).c_str());
122         }
123         json->Put("constructor", jsonConstructor);
124         json->Put("space", SpaceToString().c_str());
125     }
126 
FromJson(const std::unique_ptr<JsonValue> & json)127     void FromJson(const std::unique_ptr<JsonValue>& json) override
128     {
129         auto property = GetLayoutProperty<FlexLayoutProperty>();
130         CHECK_NULL_VOID(property);
131         if (json->Contains("constructor")) {
132             auto constructor = json->GetValue("constructor");
133             property->UpdateFlexDirection(V2::ConvertStringToFlexDirection(constructor->GetString("direction")));
134             property->UpdateMainAxisAlign(V2::ConvertStringToFlexAlign(constructor->GetString("justifyContent")));
135             property->UpdateCrossAxisAlign(V2::ConvertStringToFlexAlign(constructor->GetString("alignItems")));
136             if (constructor->GetString("wrap") == "FlexWrap.NoWrap") {
137                 SetIsWrap(false);
138                 property->UpdateMainAxisAlign(V2::ConvertStringToFlexAlign(constructor->GetString("justifyContent")));
139                 property->UpdateCrossAxisAlign(V2::ConvertStringToItemAlign(constructor->GetString("alignItems")));
140             }
141         }
142         Pattern::FromJson(json);
143     }
144 
SpaceToString()145     std::string SpaceToString() const
146     {
147         auto host = GetHost();
148         CHECK_NULL_RETURN(host, Dimension().ToString());
149         auto layoutProperty = host->GetLayoutProperty<FlexLayoutProperty>();
150         CHECK_NULL_RETURN(layoutProperty, Dimension().ToString());
151         return layoutProperty->GetSpaceValue(Dimension()).ToString();
152     }
153 
154 private:
155     bool isWrap_ = false;
156     bool isDialogStretch_ = false;
157 
158     ACE_DISALLOW_COPY_AND_MOVE(FlexLayoutPattern);
159 };
160 } // namespace OHOS::Ace::NG
161 
162 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_FLEX_FLEX_LAYOUT_PATTERN_H
163