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_BASE_ALGORITHM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_GRID_GRID_LAYOUT_BASE_ALGORITHM_H
18 
19 #include <utility>
20 
21 #include "core/components_ng/layout/layout_algorithm.h"
22 #include "core/components_ng/pattern/grid/grid_item_layout_property.h"
23 #include "core/components_ng/pattern/grid/grid_item_pattern.h"
24 #include "core/components_ng/pattern/grid/grid_layout_info.h"
25 
26 namespace OHOS::Ace::NG {
27 
28 class ACE_EXPORT GridLayoutBaseAlgorithm : public LayoutAlgorithm {
29     DECLARE_ACE_TYPE(GridLayoutBaseAlgorithm, LayoutAlgorithm);
30 
31 public:
GridLayoutBaseAlgorithm(GridLayoutInfo gridLayoutInfo)32     explicit GridLayoutBaseAlgorithm(GridLayoutInfo gridLayoutInfo) : gridLayoutInfo_(std::move(gridLayoutInfo)) {};
33     ~GridLayoutBaseAlgorithm() override = default;
34 
GetGridLayoutInfo()35     const GridLayoutInfo& GetGridLayoutInfo()
36     {
37         return std::move(gridLayoutInfo_);
38     }
39 
UpdateRealGridItemPositionInfo(const RefPtr<LayoutWrapper> & itemLayoutWrapper,int32_t mainIndex,int32_t crossIndex)40     virtual void UpdateRealGridItemPositionInfo(
41         const RefPtr<LayoutWrapper>& itemLayoutWrapper, int32_t mainIndex, int32_t crossIndex)
42     {
43         auto gridItemLayoutProperty =
44             AceType::DynamicCast<GridItemLayoutProperty>(itemLayoutWrapper->GetLayoutProperty());
45         CHECK_NULL_VOID(gridItemLayoutProperty);
46         bool isItemAtExpectedPosition =
47             gridItemLayoutProperty->CheckWhetherCurrentItemAtExpectedPosition(gridLayoutInfo_.axis_);
48         auto gridItemNode = itemLayoutWrapper->GetHostNode();
49         CHECK_NULL_VOID(gridItemNode);
50         auto gridItemPattern = gridItemNode->GetPattern<GridItemPattern>();
51         CHECK_NULL_VOID(gridItemPattern);
52         if (isItemAtExpectedPosition) {
53             gridItemPattern->ResetGridItemInfo();
54         }
55         if (!isItemAtExpectedPosition && gridLayoutInfo_.hasBigItem_) {
56             GridItemIndexInfo itemInfo;
57             itemInfo.mainIndex = mainIndex;
58             itemInfo.crossIndex = crossIndex;
59             itemInfo.mainSpan = gridItemLayoutProperty->GetRealMainSpan(gridLayoutInfo_.axis_);
60             itemInfo.crossSpan = gridItemLayoutProperty->GetRealCrossSpan(gridLayoutInfo_.axis_);
61             itemInfo.mainStart = mainIndex - itemInfo.mainSpan + 1;
62             itemInfo.mainEnd = mainIndex;
63             itemInfo.crossStart = crossIndex;
64             itemInfo.crossEnd = crossIndex + itemInfo.crossSpan - 1;
65             gridItemPattern->ResetGridItemInfo();
66             gridItemPattern->SetIrregularItemInfo(itemInfo);
67         }
68     }
69 
70 protected:
71     void AdjustChildrenHeight(LayoutWrapper* layoutWrapper);
72 
73     // The default value is set to true to skip the second measure in other layout algorithms.
IsIrregularLine(int32_t lineIndex)74     virtual bool IsIrregularLine(int32_t lineIndex) const
75     {
76         return true;
77     }
78 
79     GridLayoutInfo gridLayoutInfo_;
80 
81     ACE_DISALLOW_COPY_AND_MOVE(GridLayoutBaseAlgorithm);
82 };
83 
84 } // namespace OHOS::Ace::NG
85 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_GRID_GRID_LAYOUT_BASE_ALGORITHM_H
86