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 StopExpandMark()54 bool StopExpandMark() override 55 { 56 return true; 57 } 58 GetFocusPattern()59 FocusPattern GetFocusPattern() const override 60 { 61 return { FocusType::SCOPE, true }; 62 } 63 GetScopeFocusAlgorithm()64 ScopeFocusAlgorithm GetScopeFocusAlgorithm() override 65 { 66 auto property = GetLayoutProperty<FlexLayoutProperty>(); 67 if (!property) { 68 return {}; 69 } 70 bool isVertical = false; 71 if (property->GetFlexDirection().has_value()) { 72 isVertical = property->GetFlexDirection().value() == FlexDirection::COLUMN || 73 property->GetFlexDirection().value() == FlexDirection::COLUMN_REVERSE; 74 } 75 return { isVertical, true, isWrap_ ? ScopeType::PROJECT_AREA : ScopeType::FLEX }; 76 } 77 SetDialogStretch(bool stretch)78 void SetDialogStretch(bool stretch) 79 { 80 isDialogStretch_ = stretch; 81 } 82 DumpInfo()83 void DumpInfo() override 84 { 85 DumpLog::GetInstance().AddDesc(std::string("Type: ").append(isWrap_ ? "Wrap" : "NoWrap")); 86 } 87 GetIsWrap()88 bool GetIsWrap() const 89 { 90 return isWrap_; 91 } 92 SetIsWrap(bool isWrap)93 void SetIsWrap(bool isWrap) 94 { 95 isWrap_ = isWrap; 96 } 97 ToJsonValue(std::unique_ptr<JsonValue> & json)98 void ToJsonValue(std::unique_ptr<JsonValue>& json) const override 99 { 100 auto property = GetLayoutProperty<FlexLayoutProperty>(); 101 CHECK_NULL_VOID(property); 102 auto jsonConstructor = JsonUtil::Create(true); 103 auto direction = property->GetFlexDirection().value_or(FlexDirection::ROW); 104 jsonConstructor->Put("direction", V2::ConvertFlexDirectionToStirng(direction).c_str()); 105 if (!isWrap_) { 106 jsonConstructor->Put("wrap", "FlexWrap.NoWrap"); 107 jsonConstructor->Put("justifyContent", 108 V2::ConvertFlexAlignToStirng(property->GetMainAxisAlign().value_or(FlexAlign::FLEX_START)).c_str()); 109 jsonConstructor->Put("alignItems", 110 V2::ConvertItemAlignToStirng(property->GetCrossAxisAlign().value_or(FlexAlign::FLEX_START)).c_str()); 111 jsonConstructor->Put("alignContent", "FlexAlign.Start"); 112 } else { 113 auto wrapDirection = property->GetWrapDirection().value_or(WrapDirection::HORIZONTAL); 114 if (static_cast<int32_t>(direction) <= 1) { 115 auto wrap = (static_cast<int32_t>(wrapDirection) - static_cast<int32_t>(direction)) / 2 + 1; 116 jsonConstructor->Put("wrap", wrap == 1 ? "FlexWrap.Wrap" : "FlexWrap.WrapReverse"); 117 } else { 118 auto wrap = (static_cast<int32_t>(wrapDirection) + static_cast<int32_t>(direction)) / 2 + 1; 119 jsonConstructor->Put("wrap", wrap == 1 ? "FlexWrap.Wrap" : "FlexWrap.WrapReverse"); 120 } 121 jsonConstructor->Put("justifyContent", 122 V2::ConvertWrapAlignmentToStirng(property->GetMainAlignment().value_or(WrapAlignment::START)).c_str()); 123 jsonConstructor->Put("alignItems", 124 V2::ConvertWrapAlignmentToStirng(property->GetCrossAlignment().value_or(WrapAlignment::START)).c_str()); 125 jsonConstructor->Put("alignContent", 126 V2::ConvertWrapAlignmentToStirng(property->GetAlignment().value_or(WrapAlignment::START)).c_str()); 127 } 128 json->Put("constructor", jsonConstructor); 129 json->Put("space", SpaceToString().c_str()); 130 } 131 FromJson(const std::unique_ptr<JsonValue> & json)132 void FromJson(const std::unique_ptr<JsonValue>& json) override 133 { 134 auto property = GetLayoutProperty<FlexLayoutProperty>(); 135 CHECK_NULL_VOID(property); 136 if (json->Contains("constructor")) { 137 auto constructor = json->GetValue("constructor"); 138 property->UpdateFlexDirection(V2::ConvertStringToFlexDirection(constructor->GetString("direction"))); 139 property->UpdateMainAxisAlign(V2::ConvertStringToFlexAlign(constructor->GetString("justifyContent"))); 140 property->UpdateCrossAxisAlign(V2::ConvertStringToFlexAlign(constructor->GetString("alignItems"))); 141 if (constructor->GetString("wrap") == "FlexWrap.NoWrap") { 142 SetIsWrap(false); 143 property->UpdateMainAxisAlign(V2::ConvertStringToFlexAlign(constructor->GetString("justifyContent"))); 144 property->UpdateCrossAxisAlign(V2::ConvertStringToItemAlign(constructor->GetString("alignItems"))); 145 } 146 } 147 Pattern::FromJson(json); 148 } 149 SpaceToString()150 std::string SpaceToString() const 151 { 152 auto host = GetHost(); 153 CHECK_NULL_RETURN(host, Dimension().ToString()); 154 auto layoutProperty = host->GetLayoutProperty<FlexLayoutProperty>(); 155 CHECK_NULL_RETURN(layoutProperty, Dimension().ToString()); 156 return layoutProperty->GetSpaceValue(Dimension()).ToString(); 157 } 158 IsNeedInitClickEventRecorder()159 bool IsNeedInitClickEventRecorder() const override 160 { 161 return true; 162 } 163 164 private: 165 bool isWrap_ = false; 166 bool isDialogStretch_ = false; 167 168 ACE_DISALLOW_COPY_AND_MOVE(FlexLayoutPattern); 169 }; 170 } // namespace OHOS::Ace::NG 171 172 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_FLEX_FLEX_LAYOUT_PATTERN_H 173