1 /* 2 * Copyright (c) 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_WATERFLOW_WATER_FLOW_LAYOUT_PROPERTY_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_WATERFLOW_WATER_FLOW_LAYOUT_PROPERTY_H 18 19 #include "core/components/common/layout/constants.h" 20 #include "core/components_ng/layout/layout_property.h" 21 22 namespace OHOS::Ace::NG { 23 class InspectorFilter; 24 25 class ACE_EXPORT WaterFlowLayoutProperty : public LayoutProperty { 26 DECLARE_ACE_TYPE(WaterFlowLayoutProperty, LayoutProperty); 27 28 public: 29 WaterFlowLayoutProperty() = default; 30 ~WaterFlowLayoutProperty() override = default; 31 32 RefPtr<LayoutProperty> Clone() const override; 33 Reset()34 void Reset() override 35 { 36 LayoutProperty::Reset(); 37 ResetColumnsTemplate(); 38 ResetRowsTemplate(); 39 ResetColumnsGap(); 40 ResetRowsGap(); 41 ResetWaterflowDirection(); 42 ResetScrollEnabled(); 43 itemLayoutConstraint_.reset(); 44 } 45 46 void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override; 47 GetAxis()48 Axis GetAxis() const 49 { 50 auto axis = propWaterflowDirection_.value_or(FlexDirection::COLUMN); 51 return (axis == FlexDirection::COLUMN || axis == FlexDirection::COLUMN_REVERSE) ? Axis::VERTICAL 52 : Axis::HORIZONTAL; 53 } 54 IsReverse()55 bool IsReverse() const 56 { 57 auto axis = propWaterflowDirection_.value_or(FlexDirection::COLUMN); 58 auto isRtl = GetNonAutoLayoutDirection() == TextDirection::RTL; 59 return (!isRtl && (axis == FlexDirection::COLUMN_REVERSE || axis == FlexDirection::ROW_REVERSE)) || 60 (isRtl && (axis == FlexDirection::COLUMN_REVERSE || axis == FlexDirection::ROW)); 61 } 62 IsVerticalReverse()63 bool IsVerticalReverse() const 64 { 65 auto axis = propWaterflowDirection_.value_or(FlexDirection::COLUMN); 66 return axis == FlexDirection::COLUMN_REVERSE; 67 } 68 69 ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(ColumnsTemplate, std::string); OnColumnsTemplateUpdate(const std::string &)70 void OnColumnsTemplateUpdate(const std::string& /* columnsTemplate */) const 71 { 72 ResetWaterflowLayoutInfoAndMeasure(); 73 } 74 75 ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(RowsTemplate, std::string); OnRowsTemplateUpdate(const std::string &)76 void OnRowsTemplateUpdate(const std::string& /* rowsTemplate */) const 77 { 78 ResetWaterflowLayoutInfoAndMeasure(); 79 } 80 81 ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP_AND_USING_CALLBACK(ColumnsGap, Dimension, PROPERTY_UPDATE_MEASURE); 82 void OnColumnsGapUpdate(Dimension /* columnsGap */) const; 83 84 ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP_AND_USING_CALLBACK(RowsGap, Dimension, PROPERTY_UPDATE_MEASURE); 85 void OnRowsGapUpdate(Dimension /* rowsGap */) const; 86 87 ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(CachedCount, int32_t, PROPERTY_UPDATE_MEASURE_SELF); 88 ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(ShowCachedItems, bool, PROPERTY_UPDATE_MEASURE_SELF); 89 90 ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(WaterflowDirection, FlexDirection); OnWaterflowDirectionUpdate(FlexDirection)91 void OnWaterflowDirectionUpdate(FlexDirection /* WaterflowDirection */) const 92 { 93 ResetWaterflowLayoutInfoAndMeasure(); 94 } 95 UpdateItemMinSize(const CalcSize & value)96 void UpdateItemMinSize(const CalcSize& value) 97 { 98 if (!itemLayoutConstraint_) { 99 itemLayoutConstraint_ = std::make_unique<MeasureProperty>(); 100 } 101 if (itemLayoutConstraint_->UpdateMinSizeWithCheck(value)) { 102 propertyChangeFlag_ = propertyChangeFlag_ | PROPERTY_UPDATE_MEASURE; 103 } 104 } 105 UpdateItemMaxSize(const CalcSize & value)106 void UpdateItemMaxSize(const CalcSize& value) 107 { 108 if (!itemLayoutConstraint_) { 109 itemLayoutConstraint_ = std::make_unique<MeasureProperty>(); 110 } 111 if (itemLayoutConstraint_->UpdateMaxSizeWithCheck(value)) { 112 propertyChangeFlag_ = propertyChangeFlag_ | PROPERTY_UPDATE_MEASURE; 113 } 114 } 115 GetItemMinSize()116 std::optional<CalcSize> GetItemMinSize() const 117 { 118 if (itemLayoutConstraint_) { 119 return itemLayoutConstraint_->minSize; 120 } 121 return std::optional<CalcSize>(); 122 } 123 GetItemMaxSize()124 std::optional<CalcSize> GetItemMaxSize() const 125 { 126 if (itemLayoutConstraint_) { 127 return itemLayoutConstraint_->maxSize; 128 } 129 return std::optional<CalcSize>(); 130 } 131 HasItemLayoutConstraint()132 bool HasItemLayoutConstraint() const 133 { 134 return itemLayoutConstraint_ != nullptr; 135 } 136 137 ACE_DEFINE_PROPERTY_ITEM_WITHOUT_GROUP(ScrollEnabled, bool, PROPERTY_UPDATE_MEASURE); 138 139 private: 140 ACE_DISALLOW_COPY_AND_MOVE(WaterFlowLayoutProperty); 141 142 void ResetWaterflowLayoutInfoAndMeasure() const; 143 std::string GetWaterflowDirectionStr() const; 144 std::unique_ptr<MeasureProperty> itemLayoutConstraint_; 145 }; 146 } // namespace OHOS::Ace::NG 147 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_WATERFLOW_WATER_FLOW_LAYOUT_PROPERTY_H 148