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_GRID_GRID_LAYOUT_GRID_LAYOUT_ALGORITHM_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_GRID_GRID_LAYOUT_GRID_LAYOUT_ALGORITHM_H 18 19 #include <utility> 20 21 #include "core/components_ng/layout/layout_algorithm.h" 22 #include "core/components_ng/layout/layout_wrapper.h" 23 #include "core/components_ng/pattern/grid/grid_layout_base_algorithm.h" 24 #include "core/components_ng/pattern/grid/grid_layout_info.h" 25 #include "core/components_ng/pattern/grid/grid_layout_property.h" 26 #include "core/components_ng/property/layout_constraint.h" 27 28 namespace OHOS::Ace::NG { 29 30 class ACE_EXPORT GridLayoutAlgorithm : public GridLayoutBaseAlgorithm { 31 DECLARE_ACE_TYPE(GridLayoutAlgorithm, GridLayoutBaseAlgorithm); 32 33 public: GridLayoutAlgorithm(GridLayoutInfo gridLayoutInfo,int32_t crossCount,int32_t mainCount)34 GridLayoutAlgorithm(GridLayoutInfo gridLayoutInfo, int32_t crossCount, int32_t mainCount) 35 : GridLayoutBaseAlgorithm(std::move(gridLayoutInfo)), crossCount_(crossCount), mainCount_(mainCount) {}; 36 ~GridLayoutAlgorithm() override = default; 37 38 void Measure(LayoutWrapper* layoutWrapper) override; 39 void Layout(LayoutWrapper* layoutWrapper) override; 40 41 private: 42 void InitGridCeils(LayoutWrapper* layoutWrapper, const SizeF& idealSize); 43 bool CheckGridPlaced(int32_t index, int32_t row, int32_t col, int32_t& rowSpan, int32_t& colSpan); 44 void GetNextGrid(int32_t& curRow, int32_t& curCol) const; 45 OffsetF ComputeItemPosition(LayoutWrapper* layoutWrapper, int32_t row, int32_t col, int32_t& rowSpan, 46 int32_t& colSpan, const RefPtr<LayoutWrapper>& childLayoutWrapper) const; 47 LayoutConstraintF CreateChildConstraint(const SizeF& idealSize, const RefPtr<GridLayoutProperty>& layoutProperty, 48 int32_t row, int32_t col, int32_t& rowSpan, int32_t& colSpan, 49 const RefPtr<LayoutProperty>& childLayoutProperty) const; 50 float GetItemSize(int32_t row, int32_t col, bool height) const; 51 52 int32_t crossCount_ = 0; 53 int32_t mainCount_ = 0; 54 bool isVertical_ = true; 55 bool rightToLeft_ = false; 56 57 float rowsGap_ = 0; 58 float columnsGap_ = 0; 59 60 // Map structure: [rowIndex, [columnIndex - (width, height)]]. 61 std::map<int32_t, std::map<int32_t, SizeF>> gridCells_; 62 63 // Map structure: [index, [positionX, positionY]], store position of each item. 64 std::map<int32_t, OffsetF> itemsPosition_; 65 66 ACE_DISALLOW_COPY_AND_MOVE(GridLayoutAlgorithm); 67 }; 68 69 } // namespace OHOS::Ace::NG 70 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_GRID_GRID_LAYOUT_GRID_LAYOUT_ALGORITHM_H 71