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/pattern/grid/grid_item_layout_property.h"
22 #include "core/components_ng/pattern/grid/grid_layout_base_algorithm.h"
23 #include "core/components_ng/pattern/grid/grid_layout_info.h"
24 #include "core/components_ng/pattern/grid/grid_layout_options.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     GridItemRect GetItemRect(const RefPtr<GridLayoutProperty>& gridLayoutProperty,
52         const RefPtr<GridItemLayoutProperty>& childLayoutProperty, int32_t index) const;
53     void PrintConflictingPositionLog(
54         int32_t itemIndex, GridItemRect rect, int32_t rowIndex, int32_t colIndex, int32_t rowSpan, int32_t colSpan);
55 
56     int32_t crossCount_ = 0;
57     int32_t mainCount_ = 0;
58     bool isVertical_ = true;
59     bool rightToLeft_ = false;
60 
61     float rowsGap_ = 0;
62     float columnsGap_ = 0;
63 
64     // Map structure: [rowIndex, [columnIndex - (width, height)]].
65     std::map<int32_t, std::map<int32_t, SizeF>> gridCells_;
66 
67     // Map structure: [index, [positionX, positionY]], store position of each item.
68     std::map<int32_t, OffsetF> itemsPosition_;
69 
70     ACE_DISALLOW_COPY_AND_MOVE(GridLayoutAlgorithm);
71 };
72 
73 } // namespace OHOS::Ace::NG
74 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_GRID_GRID_LAYOUT_GRID_LAYOUT_ALGORITHM_H
75